/**
* Function called when a checkbox from "Project type" or " Project categories" is checked.
* It sends an ajax request to site.php to retrieve all projects' coordinates to show on the map.
*
* @return void
*/
function markerUpdater(sFieldPrefix, iFieldID, bFieldCheckStatus, sURL)
{
	var oAjax = new Ajax.Request(sURL,
	{
		method: 'post',
		
		parameters: {
			prefix	: sFieldPrefix,
			ID		:iFieldID,
			status	: bFieldCheckStatus
		},
		onSuccess: function( transport )
		{
			oMap.clearOverlays();
			oMap.bounds = new GLatLngBounds();
			oMap.setCenter(new GLatLng(51.8050000 ,5.075300), 11);
			
			aPoints = new Array();
			
			var json = transport.responseText.evalJSON();
			
			json.each(function(element) 
			{
				oIcon = new GIcon(G_DEFAULT_ICON);
				oIcon.image = element.icon;
				oIcon.iconSize = new GSize(20, 20);
				oIcon.shadowSize = new GSize(30, 30);
				
				addGEvent(oMap, element.longitude, element.latitude, oIcon, element.html, element.sURI);
				aPoints.push(new GLatLng(element.longitude, element.latitude));
			});
			
			if(aPoints.length > 0)
			{
				fitToMap(oMap, aPoints);
			}
			
		}
	});
	return false;
}

/**
* Function to execute the call to markerUpdater but from an onClick event.
* We introduced this extra-method because of a delay between onClick and onChange in IE
* and to avoid a dubble execution of the same call. FF is not affected by this bug. Thus, 
* we make a request with onChange in FF (and not IE browsers) and with onClick in IE.
*
* @param object oElement	 Input checkbox form's element clicked or changed of value;
* @param string sPrefix		 Prefix of the checkbox type ('projcateg' or 'projtype')
* @param integer iCategoryID ID of the project category to analize
* @param string sUrl		 URL of the request handler
* @return void;
*/
function onClickMarkerUpdater(oElement, sPrefix, iCategoryID, sUrl)
{
	markerUpdater(sPrefix, iCategoryID, oElement.checked, sUrl);
	return false;
}

/**
* When a GoogleMap is loaded and we have markers to show, let's add an even listener
* on a marker to enable the showing of tooltip informations.
*
* @param object oMap Googlemap object
* @param float sLong Longitude point
* @param float sLat Latitude point
* @param string sString The html code to view in the tooltip
* @param string sIcon Path of the icon to associate to the point
* @param string sURL URL where redirect with onClick event
* @return void
*/
function addGEvent(oMap, sLong, sLat, oIcon, sString, sURL)
{
	setBasicsForMarker(oMap, sLong, sLat, oIcon, sString);
	GEvent.addListener(oMarker, "mouseover", function(){openInfo(oMarker,sString); });
	GEvent.addListener(oMarker, "click", function(){ redirect(sURL) });
}

/**
* Redirection to a project details.
*
*@param integer iID Project ID
* @return void
*/
function redirect(sURL)
{
	location.href=sURL;
}

/**
* Called from the tab Locations, it sets the basics for a marker with a specified even onmouseover
*
* @param object oMap Googlemap object
* @param float sLong Longitude point
* @param float sLat Latitude point
* @param string sString The html code to view in the tooltip
* @param string sIcon Path of the icon to associate to the point
* @return void
*/
function setBasicForLocations(oMap, sLong, sLat, oIcon, sString)
{
	setBasicsForMarker(oMap, sLong, sLat, oIcon, sString);
	GEvent.addListener(oMarker, "mouseover", function(){openInfoWindowHtml(oMarker,sString); });
}

/**
* Setting the basic values for a marker.
*
* @param object oMap Googlemap object
* @param float sLong Longitude point
* @param float sLat Latitude point
* @param string sString The html code to view in the tooltip
* @param string sIcon Path of the icon to associate to the point
* @return void
*/
function setBasicsForMarker(oMap, sLong, sLat, oIcon, sString)
{
	sString = String(sString);
	oPoint = new GLatLng(sLong, sLat);
	oMarker = new GMarker(oPoint, {icon:oIcon});
	oMap.addOverlay(oMarker);
	oMap.bounds.extend(oPoint);	
}

/**
* Automatich fitting the GoogleMap zoom level based on the quantity of present markers.
*
* @param object oMap	Googlemap object
* @param array  aPoints All the points shown on the map.
* @return void
*/
function fitToMap(oMap, aPoints)
{
	var sZoomLevel = oMap.getBoundsZoomLevel(oMap.bounds);
	
	// Let's fix a maximum zoom level of 14.
	if(sZoomLevel >= 17)
	{
		sZoomLevel = 17;
	}
	
	oMap.setCenter(oMap.bounds.getCenter());
	oMap.setZoom(sZoomLevel);
}

/**
* Call to openInfoWindowHtml function on a marker.
* 
* @param object oMarker marker to show on the map
* @param string sString The html code to view in the tooltip
* @return void
*/
function openInfo(oMarker,sString)
{
	$('toolTip_box').innerHTML = sString;
	$('toolTip_box_header').style.display = 'block';
	$('toolTip_box').style.display = 'block';
	$('toolTip_box_footer').style.display = 'block';
}

var MinLng, ManLng,
	MinLtd, MaxLtd;

/**
*	Initializing the googlemap bounds.
*/
function InitializeMapBounds(fLng, fLtd)
{
	MinLng = fLng;
	MaxLng = fLng;
	MinLtde = fLtd;
	MaxLtd = fLtd;
}

/**
*	Calculate the actual new centerpoint of the map.
*/
function CalCenterMap(fLng, fLtd)
{
	if(fLng < MinLng)
	{
		MinLng = fLng;
	}
	else if(fLng > MaxLng)
	{
		MaxLng = fLng;
	}
	if(fLtd < MinLtd)
	{
		MinLtd = fLng;
	}
	else if(fLtd > MaxLtd)
	{
		MaxLtd = fLtd;
	}
}
