/* Copyright 2007 SCube NewMedia. */

/** ============ FOGLI DI STILE ============= */
document.write('<link href="' + PATH_TEMPLATE + '/style/dettaglio.css" rel="stylesheet" type="text/css" />');


/** ============ VARIABILI COMUNI ============= */
/** Indirizzo per indentificare il punto vendita sul grafo */
var daddrLatLng = null;
var daddrAddress = "";

/** ============ EVENTI ============= */
//gestione evento "onload"
if (window.addEventListener) {
	window.addEventListener("load", loadTemplate, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", loadTemplate);
};

/**
* Gestione evento "load".
*/
function loadTemplate() {
	/** Overlap variabili comuni */
	// frontend di impaginazione dei dati
	FRONTEND = "pdv";

	// creazione mappa e gestione parti comuni
	initialize();
	
	// settaggio posizione mappa
	GMap.setCenter(LATITUDINE, LONGITUDINE, ZOOM);
	
	// Creates a collapsible overview map in the corner of the screen.
	// GMap.addOverviewMapControl();
	// enables double click to zoom in and out (disabled by default).
	GMap.enableDoubleClickZoom();
	// Installs keyboard event handler for the map passed as argument.
	GMap.enableKeyboardHandler();
	// Creates a control with buttons to switch between map types.
	// GMap.addMapTypeControl();
	GMap.addHierarchicalMapTypeControl();
	// Creates a control with buttons to pan in four directions, and zoom in and zoom out.
	GMap.addSmallMapControl();
	// Enables wheel zoom
	// GMap.enableScrollWheelZoom();
	// Enables continuous smooth zooming for select browsers (disabled by default).
	GMap.enableContinuousZoom();

	// definizione controllo per la velina di attesa
	function LoadingControl(){
	}
	LoadingControl.prototype = new GControl();
	LoadingControl.prototype.initialize = function(map) {
		var overlay = document.getElementById("overlay");
		var loading = document.getElementById("loading");
		overlay.removeChild(loading);
		map.getContainer().appendChild(loading);
		return loading;
	}
	LoadingControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(170, 125) );
	}
	// velina di attesa
	GMap.addControl(new LoadingControl());

	// caricamento layer porgetto per definizione stili grafici 
	GMap.getListLayerAsync(function(){
		// caricamento contenuti
		load();
	});
}


/** ============ FUNZIONI ============= */



/**
 * Inizializza lo stato della applicazione
 */
function load() {
	// lettura parametri da URL
	var storeCodeByURL = ParamByURL["storeCode"];
	if (storeCodeByURL != null && storeCodeByURL.length > 0) {
		var query = "[ABBREV]=[" + storeCodeByURL + "]";


		// esecuzione ricerca
		GMap.showLayerAsync(/*layer*/LAYERS.PUNTI_VENTIDA, /*query*/query, /*focus*/true, /*frontend*/null, function (idLayer, listObject) {
			if (listObject.length == 0) {
				alert("Spiacenti.\nLa ricerca non ha prodotto risultati.");
			} else {
				// lettura infomrazioni
				var object = listObject[0];
				daddrLatLng = object.getUserdata("POI");
				daddrAddress = object.getUserdata("ADDRESS") + "," + object.getUserdata("CITY");

				// salvataggio stato stampa
				savePrintData({PRINT_DATA_LAYER:idLayer,PRINT_DATA_QUERY:query})

			}
		});		
	}
}


/**
 * Effettua il calcolo del percorso partendo dai valori inseriti.
 */
function calcolaPercorso() {
	// lettura valori form
	var comune = document.getElementById("comune").value;
	var indirizzo = document.getElementById("indirizzo").value;
	var numerocivico = document.getElementById("numerocivico").value;

	// costruzione indirizzo di partenza
	var saddr = comune;
	if (comune.replace(/[^A-Za-z0-9]/g,"") != "" && indirizzo.replace(/[^A-Za-z0-9]/g,"") != "") {saddr += ","}
	saddr += indirizzo;
	if (indirizzo.replace(/[^A-Za-z0-9]/g,"") != "" && numerocivico.replace(/[^A-Za-z0-9]/g,"") != "") {saddr += "," + numerocivico;}

	
	// verifichiamo che l'informazione sia valida
	if (saddr.replace(/[^A-Za-z0-9]/g,"") == "") {		
		// gestione errore
		alert("Indirizzo di partenza non specificato");
		return false;
	}
	
	// verifichiamo che l'informazione sia valida
	if (daddrLatLng == null || daddrLatLng.replace(/[^A-Za-z0-9]/g,"") == "") {		
		// gestione errore
		alert("Indirizzo di arrivo non specificato");
		return false;
	}

	// esecuzione calcolo percorso
	var option = {"getSteps":"false","locale":"IT"};
	GMap.getDirections([saddr, daddrLatLng], null, option, function(statusCode) {

		if (statusCode != SGEO_SUCCESS) {
			// costruzione messaggio di errore
			var message = ERROR_PATH;
			message = message.replace("ERROR_PATH_FROM", saddr);
			message = message.replace("ERROR_PATH_TO", daddrAddress);
			
			// gestione errore
			alert(message);
		} else {
			// eliminiamo i contenuti caricati su mappa
			GMap.clear();
		
			// settaggio informazioni stampa
			savePrintDirection(saddr, daddrLatLng);
			
			// eliminiamo le informazioni caricate	
			GMap.clear(true, false);
		}
	});
}

/**
 * Ripristina lo stato iniziale della mappa.
 */
function reset() {
	try {
		// eliminiamo tutti contenuti caricati
		GMap.clearAll();

		// ripristino stato template
		load();

	} catch(e) {
		toException(e);
	}
}

