﻿/*
* jQuery Timer Plugin
* http://www.evanbot.com/article/jquery-timer-plugin/23
*
* @version      1.0
* @copyright    2009 Evan Byrne (http://www.evanbot.com)
*/
jQuery.timer = function(time, func, callback) {
	var a = { timer: setTimeout(func, time), callback: null }
	if (typeof (callback) == 'function') { a.callback = callback; }
	return a;
};
jQuery.clearTimer = function(a) {
	clearTimeout(a.timer);
	if (typeof (a.callback) == 'function') { a.callback(); };
	return this;
};
/*********************************************************************************/

/*
* Google Maps JavaScript API V2
* Funzioni per l'utilizzo delle mappe di google.
*/
var geocoderV2 = null;
var mapV2 = null;
var srcListenerLoad = null;
var srcListenerError = null;
var imageMarker = '/common/images/{0}-google-marker2.png';
function initialize(idMapEl, coord1, coord2, viewZoom, myMapType) {
	if (coord1 == 0) coord1 = 42.244785;
	if (coord2 == 0) coord2 = 12.458496;
	if (viewZoom == 0) viewZoom = 5;

	if (GBrowserIsCompatible()) {
		geocoderV2 = new GClientGeocoder();
		mapV2 = new GMap2(document.getElementById(idMapEl));
		mapV2.setCenter(new GLatLng(coord1, coord2), viewZoom);
		mapV2.addControl(new GLargeMapControl3D());
		mapV2.addControl(new GMapTypeControl());
		//mapV2.addControl(new GOverviewMapControl());
		//mapV2.addControl(new GNavLabelControl());

		return mapV2;
	}
}

/* ho creato questa funzione per evitare che google non facesse vedere tutte le filiali */
function addFilialeMarkerFromAddrDealay(objMap, markAddress, markTitle, markHtml, delayMilliSec, markerType) {
	var myTimer = $.timer(delayMilliSec,
		function() {
			addFilialeMarkerFromAddr(objMap, markAddress, markTitle, markHtml, markerType);
		},
		function() {
			//Display new message when canceled
		}
	);
}
function addFilialeMarkerFromAddr(objMap, markAddress, markTitle, markHtml, markerType) {
	var imageMarkerFn = imageMarker.replace("{0}", markerType);
	if (geocoderV2) {
		geocoderV2.getLatLng(
			address,
			function(point) {
				if (!point) {
					alert(address + " non trovato");
				} else {
					//mapV2.setCenter(point, 11);
					var cfIcon = new GIcon(G_DEFAULT_ICON);
					cfIcon.image = imageMarkerFn;
					cfIcon.iconSize = new GSize(32, 34); //42, 45
					//cfIcon.iconAnchor = new GPoint(9, 5);
					//cfIcon.infoWindowAnchor = new GPoint(0, 0);
					cfIcon.shadowSize = new GSize(0, 0);
					markerOptions = { icon: cfIcon };
					var marker = new GMarker(point, markerOptions);
					GEvent.addListener(marker, "click", function() {
						marker.openInfoWindowHtml(markHtml);
					});
					objMap.addOverlay(marker);
				}
			}
		);
	}
}

/* ho creato questa funzione per evitare che google non facesse vedere tutte le filiali */
function addFilialeMarkerFromCoordDealay(objMap, coord1, coord2, markTitle, markHtml, delayMilliSec, markerType) {
	var myTimer = $.timer(delayMilliSec,
		function() {
			addFilialeMarkerFromCoord(objMap, coord1, coord2, markTitle, markHtml, markerType);
		},
		function() {
			//Display new message when canceled
		}
	);
}
function addFilialeMarkerFromCoord(objMap, coord1, coord2, markTitle, markHtml, markerType) {
	var imageMarkerFn = imageMarker.replace("{0}", markerType);
	if (coord1 == 0 || coord2 == 0)
		return;

	var latlng = new GLatLng(coord1, coord2);
	var cfIcon = new GIcon(G_DEFAULT_ICON);
	cfIcon.image = imageMarkerFn;
	cfIcon.iconSize = new GSize(32, 34); //42, 45
	//cfIcon.iconAnchor = new GPoint(9, 5);
	//cfIcon.infoWindowAnchor = new GPoint(0, 0);
	cfIcon.shadowSize = new GSize(0, 0);
	markerOptions = { icon: cfIcon };
	var marker = new GMarker(latlng, markerOptions);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(markHtml);
	});
	objMap.addOverlay(marker);
}

function setMapCenterAddr(objMap, markAddress) {
	if (geocoderV2) {
		geocoderV2.getLatLng(
			address,
			function(point) {
				if (!point) {
					alert(markAddress + " not found");
				} else {
					//objMap.setCenter(point, 11);
					objMap.setCenter(point);
				}
			}
		)
	}
}
function setMapCenterCoord(objMap, coord1, coord2) {
	if (geocoderV2) {
		var latlng = new GLatLng(coord1, coord2);
		objMap.setCenter(latlng);
	}
}

var directions = null;
function calculateDirections() {
	var sFrom = $.trim($("input.filialiFrom").val());
	var sTo = $.trim($("select.filialiTo").val());

	if (sFrom == "" || sTo == "") {
		alert("Inserire il percorso da ricercare");
		return false;
	}

	if (GBrowserIsCompatible()) {
		directionsPanel = document.getElementById("gmap_route");
		if (directions == null) {
			directions = new GDirections(mapV2, directionsPanel);
		}
		else {
			try {
				GEvent.removeListener(srcListenerLoad);
			}
			catch (e) {}
			try {
				GEvent.removeListener(srcListenerError);
			}
			catch (e) { }
			directions.clear();
		}
		srcListenerLoad = GEvent.addListener(directions, "load", onGDirectionsLoad);
		srcListenerError = GEvent.addListener(directions, "error", handleErrors);
		directions.load("from: " + sFrom + " to: " + sTo, { "locale": "it_IT" });
		showRouteMap();
	}
	return false;
}
function calculatePrintDirections(sFrom, sTo, dirPanelId) {
	if (sFrom == "" || sTo == "") {
		alert("Impossibile calcorare il percorso.");
		return false;
	}

	if (GBrowserIsCompatible()) {
		directionsPanel = document.getElementById(dirPanelId);
		if (directions == null) {
			directions = new GDirections(mapV2, directionsPanel);
		}
		else {
			try {
				GEvent.removeListener(srcListenerLoad);
			}
			catch (e) { }
			try {
				GEvent.removeListener(srcListenerError);
			}
			catch (e) { }
			directions.clear();
		}
		srcListenerLoad = GEvent.addListener(directions, "load", onGDirectionsLoad);
		srcListenerError = GEvent.addListener(directions, "error", handleErrors);
		directions.load("from: " + sFrom + " to: " + sTo, { "locale": "it_IT" });
	}
	return false;
}

function handleErrors() {
	if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
		alert("Impossibile determinare le coordinate per l'indirizzo inserito. Le cause potrebbero essere che l'indirizzo è relativamente nuovo o che l'indirizzo inserito è errato.\nCodice di errore: " + directions.getStatus().code);
		hideRouteMap();
	}
	else if (directions.getStatus().code == G_GEO_SERVER_ERROR) {
		alert("Le coordinare o l'indirizzo inserito non possono essere processati, la corretta causa per l'errore non può essere determinata.\nCodice di errore: " + directions.getStatus().code);
		hideRouteMap();
	}
	else if (directions.getStatus().code == G_GEO_MISSING_QUERY) {
		alert("Codice errore: " + directions.getStatus().code);
		hideRouteMap();
	}
	else if (directions.getStatus().code == G_GEO_BAD_KEY) {
		alert("Codice errore: " + directions.getStatus().code);
		hideRouteMap();
	}
	else if (directions.getStatus().code == G_GEO_BAD_REQUEST) {
		alert("Codice errore.\n Error code: " + directions.getStatus().code);
		hideRouteMap();
	}
	else {
		alert("Si è verificato un errore sconosciuto.");
		hideRouteMap();
	}
}
function onGDirectionsLoad() {
	// Use this function to access information about the latest load()
	// results.
	// e.g.
	// document.getElementById("getStatus").innerHTML = directions.getStatus().code;
	// and yada yada yada...
}

function showRouteMap() {
	$("div#gmap_route").show();
	if ($.browser.msie && $.browser.version < 7) {
		$("div#filialiSrc div.ddlRicerca select").each(function() {
			$(this).css("visibility", "hidden");
		});
		$("select.filialiTo").css("visibility", "hidden");
	}
}
function hideRouteMap() {
	$("div#gmap_route").hide(500);
	if ($.browser.msie && $.browser.version < 7) {
		$("div#filialiSrc div.ddlRicerca select").each(function() {
			$(this).css("visibility", "visible");
		});
		$("select.filialiTo").css("visibility", "visible");
	}
	return false;
}

function printRoute() {
	var sFrom = $.trim($("input.filialiFrom").val());
	var sTo = $.trim($("select.filialiTo").val());
	window.open("/percorsoPrint.htm?from=" + encodeURIComponent(sFrom) + "&to=" + encodeURIComponent(sTo));
	hideRouteMap();
}
