
var _selectedFlag = -1;

function colorFlags()
{
	for (var i = 0; i < _typesXMLElt.length; i++) {
		var type = _typesXMLElt[i];
		var myid = type.getAttribute("id");
		var elt = $('type'+myid);
		if( elt!=null)
		{
			elt.src = _typesXMLElt[i].getAttribute('color_flag');
		}
	}
}

function decolorFlags()
{
	for (var i = 0; i < _typesXMLElt.length; i++) {
		var type = _typesXMLElt[i];
		var myid = type.getAttribute("id");
		var elt = $('type'+myid);
		if( elt!=null && myid != _selectedFlag)
		{
			elt.src = _typesXMLElt[i].getAttribute('grey_flag');
		}
	}
}

function getMouseOutFlagImage( id)
{
	if( id == _selectedFlag)
		return _typesArrayElt[id].getAttribute('color_flag');
	return _typesArrayElt[id].getAttribute('grey_flag');
}

function reloadFlags( mapElement, id) {
	var debug = false;
	if (debug) {console.info("[FlagsTools.js] Starting reloadFlags");}
	
	if (getZoom( mapElement) > 4) {
		if (debug) { console.info("[MapTools.js] [reloadMarkers] Searching square []"); }
		
		_selectedFlag  = id;
		
		for (var i = 0; i < _typesXMLElt.length; i++) {
			var type = _typesXMLElt[i];
			var myid = type.getAttribute("id");
			var elt = $('type'+myid);
			if( elt!=null)
			{
				elt.src = _typesXMLElt[i].getAttribute('grey_flag');
				//elt.onmouseout = "this.src='"+_typesXMLElt[i].getAttribute('grey_flag')+"'";
			}
		}
		searchValues ='everything';
		
		if( id>0)
		{
			var elt = $('type'+id);
			elt.src = _typesArrayElt[id].getAttribute('color_flag');
			//elt.onmouseout = "this.src='"+_typesArrayElt[id].getAttribute('color_flag')+"'";		
		}
		
		latLngBounds = getMap(mapElement).getBounds();
		
	    getEventListAjaxWithCoordSwitchingType(
			mapElement,
			searchValues, 
			latLngBounds.getNorthEast().lat(), 
			latLngBounds.getNorthEast().lng(),
			latLngBounds.getSouthWest().lat(), 
			latLngBounds.getSouthWest().lng(),
			id);
	}else{
		getMap(mapElement).clearOverlays();
		
		if( $('liste_resultat'))	
			$('liste_resultat').empty().setStyle('display', 'none');
	
			
		if( $('selectCtl'))	
			$('selectCtl').options.length =0;
	}

	if( debug) {console.info("[FlagsTools.js] End of reloadFlags");}
}

function getEventListAjaxWithCoordSwitchingType( mapElement, mySearchValues, neLat, neLng, swLat, swLng, id){

	mode = 'search';
	
	searchValues = mySearchValues;
	
	if( $('maploading'))
		$('maploading').setStyle('display', 'block');
	
	new Request.JSON(
	{
		url:contextPath+modulePath+'search/'+mode+'/'+mySearchValues+'/'+neLat+'/'+neLng+'/'+swLat+'/'+swLng+'/'+id+'/'+default_sorter+'/0/false/'+user_map_id,
		method: 'get',
		encoding:'ISO-8859-1',
		onComplete:function(jsonObj) {
				processEventList(mapElement, jsonObj);
			}
	}).send();
}
