var openLink = function (link) {
	var id = document.getElementById('idLocale').value;
	var checkSum = document.getElementById('checkSum').value;
	if(id != 'undefined') {
		dojo.xhrPost({
			url: "/requests/op_locale.php",
			handleAs: "xml",
			postData: "op=c&id="+id+"&cs="+checkSum,
			handle: function(data,args){
				if(typeof data == "error"){
					console.warn("error!");
					console.log(args);
				}
			}
		});
	}
	window.open(link);
};

function initialize() {
	if (GBrowserIsCompatible()) {
		var mapOptions = {
			googleBarOptions : {
			style : "new"
			}
		}
		map = new GMap2(document.getElementById("map_google"), mapOptions);
		map.setCenter(new GLatLng(latitudine, longitudine), 18);
		var point = new GLatLng(latitudine, longitudine);
		map.addOverlay(new GMarker(point));
		//map.addOverlay(new GMarker(point, markerOptions));
		map.setUIToDefault();
		map.disableScrollWheelZoom();
	}
}

function setDirections(toAddress, fromAddress) {
	map.clearOverlays();
	gdir = new GDirections(map, document.getElementById("route"));
	GEvent.addListener(gdir, "error", handleErrors);
	GEvent.addListener(gdir, "addoverlay", onGDirectionsLoad);
	gdir.load("from: " + fromAddress + " to: " + latitudine + ", " + longitudine);
	showDirection(fromAddress);
	
}

function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("Indirizzo non trovato");
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		alert("Si è verificato un errore nella geocodifica degli indirizzi");

	else alert("Si è verificato un errore");
}

function onGDirectionsLoad(){ 
	var info_route = document.getElementById("info_route_map");
	if (info_route){
		distance = Math.round(gdir.getDistance().meters/100)/10;
		duration = 'circa ' + Math.round(gdir.getDuration().seconds/60);
		info_route.innerHTML = '<div class="distance"><span class="label">Distanza:</span><span class="value">&nbsp;'+distance+' km</span></div><div class="duration"><span class="label">Tempo percorrenza:</span><span class="value">&nbsp;'+duration+' min</span></div><a href="#" onclick="showRoute();return false;"><div class="link"><img src="/images/route_map.png" />mostra percorso</div></a>'
		info_route.style.display = 'block';
	}
}


function showRoute () {
	var div_route = document.getElementById('route');
	var div_info_route = document.getElementById('info_route_map');
	var div_form_get_route = document.getElementById('form_get_route');
	var div_new_route = document.getElementById('calc_new_route');
	if (div_route && div_info_route && div_form_get_route){
		div_route.style.display = 'block';
		div_info_route.style.display = 'none'
		div_form_get_route.style.display = 'none';
		div_new_route.style.display = 'block';
	}
	
}

function destroyAllRoute () {
	var div_route = document.getElementById('route');
	var div_info_route = document.getElementById('info_route_map');
	var div_form_get_route = document.getElementById('form_get_route');
	var div_new_route = document.getElementById('calc_new_route');
	if (div_route && div_info_route){
		div_route.style.display = 'none';
		div_info_route.style.display = 'none';
		div_form_get_route.style.display = 'block';
		div_new_route.style.display = 'none';
		div_route.innerHTML = '';
		div_info_route.innerHTML = '';
	}
}

function enableLink(idDiv){
	var oContDiv = document.getElementById('content_links');
	if (oContDiv){
		var idx = 0;
		while(oDiv = oContDiv.childNodes[idx++]) {
			if (oDiv && oDiv.id && oDiv.id.indexOf('link_') > -1 ){
				if (oDiv.id == idDiv) {
					oDiv.className="selected";
					enableBlock('div_' + idDiv);
				}else {
					oDiv.className="other";
				}
			}
		}
	}
	showLink(idDiv);
	return false;
}

function enableBlock(idDiv) {
	var oContDiv = document.getElementById('content_box_extra');
	if (oContDiv){
		var idx = 0;
		while(oDiv = oContDiv.childNodes[idx++]) {
			if (oDiv && oDiv.id && oDiv.id.indexOf('div_link') > -1 ){
				if (oDiv.id == idDiv) {
					oDiv.className="selected";
				}else {
					oDiv.className="other";
				}
			}
		}
	}
	return false;
}

function showLink(link) {
	var id = document.getElementById('idLocale').value;
	if(id != 'undefined') {
		dojo.xhrPost({
			url: "/requests/tracking_op_detail.php",
			handleAs: "xml",
			postData: "op=showLD&id="+id+"&link="+link,
			handle: function(data,args){
				if(typeof data == "error"){
					console.warn("error!");
					console.log(args);
				}
			}
		});
	}
}

function showDirection(from) {
	var id = document.getElementById('idLocale').value;
	if(id != 'undefined') {
		dojo.xhrPost({
			url: "/requests/tracking_op_detail.php",
			handleAs: "xml",
			postData: "op=calcRoute&id="+id+"&from="+from,
			handle: function(data,args){
				if(typeof data == "error"){
					console.warn("error!");
					console.log(args);
				}
			}
		});
	}
}

