var map = null;
var pinid = 0;
var countId = 0;
var currentDest = '';
var currentStart = '';
 

function GetMap()
{
	map = new VEMap('veMap');
 	
	//map.AttachEvent("onclick",function(){alert(map.GetCenter())});

	map.LoadMap();
	map.SetCenterAndZoom(new VELatLong(46.1753,11.8292),16);
	AddPushpin(46.1754,11.8290, 'Hotel Relais Orsingher','Via Guadagnini, 14<br>Fiera di Primiero (Trento)<br>Tel. +39 0439.62816<br>Fax. +39 0439.64841', '/inverno/hotel-fiera-primiero/hotel-orsingher.html', '/img/orsingher.jpg', '/img/icona.orsingher.gif');
	map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
	map.AttachEvent("onclick", function(e){if (e.elementID){map.ShowInfoBox(map.GetShapeByID(e.elementID));}});
	map.AttachEvent("onmouseover", function(e){if (e.elementID){return true;}});


//Exception... "'[object Object]' when calling method: [nsIDOMEventListener::handleEvent]"  nsresult: "0x8057001c (NS_ERROR_XPC_JS_THREW_JS_OBJECT)"  location: "<unknown>"  data: no]

	var newWidth = $('#MSVE_navAction_AerialMapStyle').width()+$('#MSVE_navAction_topBackground').width();
	//$('#MSVE_navAction_toggleGlyphWrapper').hide();
	$('#MSVE_navAction_topBackground').width(newWidth);
	$('#MSVE_navAction_topBar').width(newWidth+200);
	$('#MSVE_navAction_toggleGlyphWrapper').hide();

	var newControl = $('#MSVE_navAction_AerialMapStyle').clone().insertAfter($('#MSVE_navAction_AerialMapStyle'));
	newControl
		.html('Hotel')
		.click(function(){showPoint(46.1753,11.8292);})
		.css({color:'#ff5555', fontWeight:'bold'})
		.attr('title','Mostra la posizione degli hotel');
	
	
}

function getRoute(title, id, toLat, toLng){

	currentDest = title;
	currentStart = $('#from_'+id).attr('value');
	
	var from = currentStart;
	
	$('#veMapInfo').html('<p style="text-align:center;margin-top:10px"><img src="/img/loader.gif"><br/>Attendere, caricamento delle indicazioni in corso...</p>');
	
	
	
	

	var options = new VERouteOptions();
	options.RouteCallback = onGetRoute;
	options.DistanceUnit = VERouteDistanceUnit.Kilometer;
	options.RouteOptimize = VERouteOptimize.MinimizeTime;
	map.GetDirections([from, new VELatLong(toLat,toLng)], options);
	return false;
}

 function onGetRoute(route)
 {
	 // Unroll route
	 var legs     = route.RouteLegs;
	 var turns    = "<h2>Da "+currentStart+" al "+currentDest+"</h2>"+
	 								"<p><strong>Distanza totale: " + route.Distance.toFixed(1) + " km</strong><br/>"+
									"Le indicazioni sono generate automaticamente e potrebbero essere soggette ad errori</p>"+
									"<ul>";
		var numTurns = 0;
		var leg      = null;

		for(var i = 0; i < legs.length; i++){
			 leg = legs[i];  
			 var turn = null;
					
			 for(var j = 0; j < leg.Itinerary.Items.length; j ++){
					turn = leg.Itinerary.Items[j]; 
					turns += '<li>'+
						'<a href="#veMapOver" onclick="showPoint('+turn.LatLong.Latitude+','+turn.LatLong.Longitude+', 17)">'+
							numTurns + 
						".</a> " + 
						turn.Text + " (" + turn.Distance.toFixed(1) + " km)"+
					"</li>";
					numTurns++;
			 }
		}
		turns+="</ul>";
		$('#veMapInfo').hide(1000, function(){
			$(this).html(turns).show(1000);
		})
 }

function AddPushpin(lat, lng, title, description, href, image, icon)
{
//		map.ClearInfoBoxStyles();
		var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(lat,lng));
		shape.SetTitle('<h3>'+title+'</h3>');

		if (icon!='')
			shape.SetCustomIcon(icon);
		
		if (countId>0)
			shape.SetMinZoomLevel(16);

		
		shape.SetDescription(''+
			'<div style="width:460px; heigh:200px">'+
				'<img src="'+image+'" style="float:right; padding-left:10px">'+
					description +
					'<a href="'+href+'" class="link">» Vai alla descrizione</a>'+
					'<form onsubmit="return getRoute(\''+title+'\','+countId+', '+lat+','+lng+')"><p>'+
					'Come arrivare qui da: '+
					'<input id="from_'+countId+'" value="" style="border:1px solid #999;" size="30">'+
					'</p></form>'+
			'</div>');
		map.AddShape(shape);
		countId++;
}


function showPoint(Lat,Lan, Zoom){
	map.SetCenterAndZoom(new VELatLong(Lat,Lan),Zoom);
	//map.Pan(180,0);
	//setTimeout('map.ShowInfoBox(allShape["'+Lat+','+Lan+'"]);',1000);
}




function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	}
	else{
		window.onload = function(){
			oldonload();
			func();
		}
	}
}
addLoadEvent(GetMap);

// firefox3 virtualearth bug
(function(){var mouseEvt;if (typeof document.createEvent !== 'undefined'){mouseEvt = document.createEvent('MouseEvents');}if (mouseEvt && mouseEvt.__proto__ && mouseEvt.__proto__.__defineGetter__){mouseEvt.__proto__.__defineGetter__('pageX', function(){return this.clientX + window.pageXOffset;});mouseEvt.__proto__.__defineGetter__('pageY', function(){return this.clientY + window.pageYOffset;});}})();

