/**
 * gmaps_common.js
 *
 * Content custom libs to handle google maps
 */



/**
 * Diplay / hide bike tracks
 */
function switchPistesCyclables() {

	var btn = document.getElementById('spanPistesCyclables');
	if(displayPistesCyclables)
		btn.style.fontWeight= "normal";
	else
		btn.style.fontWeight= "bold";
	displayPistesCyclables = !displayPistesCyclables;
	redraw();
}


/**
 * function to sort an array of float numbers
 */
function ArrayFloatSort(arr) {

	var i,j, max_idx, temp;
	for(i = 0; i < arr.length; i++)
	{
		max_idx = i;
		for(j=i+1; j < arr.length; j++)
		{
			if(arr[j] < arr[i])
				max_idx = j;
		}
		temp = arr[i];
		arr[i] = arr[max_idx];
		arr[max_idx] = temp;
	}
	return arr;
}

/**
 * generate the url for the "tip a friend" button
 */
function tipAFriend(x, y , z, id)
{
	var mailLink = document.getElementById("tipafriend");
	if(mailLink) {
		var subj = "?";
		if(tipAFriendSubject != "") {
			subj = "?subject="+escape(tipAFriendSubject)+"&";
		}


		if(x != 0 && y != 0)
		{
			mailLink.href = "mailto:"+subj+"body="+escape(thisPage+"/(x)/"+x+"/(y)/"+y);
		}
		else if(id != "")
		{
			mailLink.href = "mailto:"+subj+"body="+escape(thisPage+"/(id)/"+id);
		}
	}
}

/**
 * search for a station by its number and move the map to it
 */
function searchStation() {

	var number = document.getElementById('searchAddress_stationNumber').value;
	if(number != '')
	{
		var result = stations.getStationByNumber(number);

		if(! result) {
			alert(GmapsTexts.not_found);
		} else {
			map.panTo(new GLatLng(result.lat, result.lng));
			stations.startStation = result;
			stations.init = true;
			stations.computeClosest(new GLatLng(result.lat, result.lng));
			stations.init = false;
			stations.drawAllMarkers();
			result.openInfoWindow();
		}
	}
}

/**
 * search with GoogleMaps Api
 */
function searchGoogleMaps() {

	var search = document.getElementById('searchAddress_stationNumber').value;
	var arrdt = document.getElementById('searchArrdt').value;

	document.getElementById('stationDetails').innerHTML = GmapsTexts.toolbar_default_text;

	if(search != '')
	{
		if(arrdt > 0) {
			if(arrdt == 1)
				var suf = 'er';
			else
				var suf = 'eme';
			search += ", " + arrdt + suf + " arrondissement"
		}

		search += ", " + searchAppendString;

		geo.getLocations(search, function (result)
		{
			if (result.Status.code == G_GEO_SUCCESS) {
				
				var p = result.Placemark[0].Point.coordinates;
				var point = new GLatLng(p[1],p[0]);
				if(arrdt == 0) {
					map.setCenter(point);
				} else {
					//ici on est dans une recherche par arrondissement.
					//si les coords du point trouv� sont dedans, on centre sur l'arrdt
					//sinon, on centre sur le point
					var myArr = arrdts.getByNumber(arrdt);
					if (myArr.contains(point)) {
						map.setCenter(myArr.center, myArr.zoom);
					} else {
						map.setCenter(point);
					}
				}
				
				
				if(home.isEnabled) {
					home.move(p[1], p[0]);
				} else {
					home.create(p[1], p[0]);
				}
			}
			else {
				if (reasons[result.Status.code]) {
					reason = reasons[result.Status.code]
				}
				alert(reason);
			}
		});
	}
}


function is_integer(value)
{
re = /[^ 0-9\-\+]/
return !re.test(value) && !isNaN(value)
} 
/**
 * launch searchStation()  or searchGoogleMaps()
 */
function search() 
{
	var search = document.getElementById('searchAddress_stationNumber').value;
	if(search != '')
	{
		if ( is_integer( search  ) )
			searchStation();
		else
			searchGoogleMaps();
	}
}
/**
 * launch a google maps search
 */
function launchSearch(search) {
	document.getElementById('searchAddress_stationNumber').value = search;
	document.getElementById('searchArrdt').value = 0;
	if(search != '')
	{
		if ( is_integer( search  ) )
			searchStation();
		else
			searchGoogleMaps();
	}
}

