
var map = null;      
var smallmap = null;      
var allShape = [];
var first = true;

function VE_refresh(){
	map.SetCenterAndZoom(new VELatLong(46.2, 11.9), 10);
	smallmap.SetCenterAndZoom(new VELatLong(46.1763, 11.834),16);

}



function VE_removeBing(m){
	var logo = $(m+' .MSVE_PoweredByLogo');
	if (logo.length){
		logo.remove();
	}
	else{
		setTimeout(function(){VE_removeBing(m)}, 500);
	}
}

	function VE_synchronize_pushpin(){
		//alert('asd');
//		$('#smallMap img[src$=pushpin.png]').css({position:relative})
	}


function GetMap()
{
	map = new VEMap('myMap');
	map.LoadMap(new VELatLong(46.2, 11.9), 10 , VEMapStyle.Shaded ,false);
	map.SetMapStyle(VEMapStyle.Shaded);
	map.ShowMiniMap(440,240);
	//map.AttachEvent("onchangeview",function(){VE_synchronize(map)});

	var shape = VE_addPoint({
		lat: 46.1739349894237,
		lng: 11.82688951492311,
		icon: 'img/pink-pushpin.png',
		iconTop:-50,
		inonLeft:0,
		title: 'Hotel Luis',
		description: 'Fiera di Primiero - Trentino, ITALY',
		showGetRoute: true
	}, map);
	map.ShowInfoBox(shape); 

	smallmap = new VEMap('smallMap');
	smallmap.LoadMap(new VELatLong(46.1763, 11.834), 16 ,'h' ,false);
	smallmap.HideDashboard();
	smallmap.HideScalebar();
//	smallmap.AttachEvent("ondoubleclick", function(){alert(smallmap.GetCenter());}); // get Center on doubleclick
	
	VE_removeBing('#smallMap');


//smallmap.AttachEvent("onchangeview", function(){VE_synchronize(smallmap)});
/*
	smallmap.AttachEvent("onchangeview", function(){VE_synchronize_pushpin()});
	VE_addPoint({
		lat: 46.1739349894237,
		lng: 11.82688951492311,
		icon: 'img/pink-pushpin.png',
		iconTop:-100,
		inonLeft:0,
		title: '',
		description: '',
		showGetRoute: false
	}, smallmap);
	*/
	

}

var startSynchronize = false;

function VE_synchronize(othermap){
	var pos = othermap.GetCenter();
	var zoom = othermap.GetZoomLevel();
	map.SetCenterAndZoom(pos,zoom);
	smallmap.SetCenterAndZoom(pos,zoom);
}


function VE_addPoint(options, map){
	if (options.lat==null || options.lng==null || options.icon==null || options.icon==null || options.iconTop==null || options.inonLeft==null || options.title==null || options.description==null || options.showGetRoute==null){
		alert('Parametri non sufficenti!');
		return;
	}
	var id = (options.lat+'_'+options.lng).replace(/\./g,'');
	
	if (options.showGetRoute){
		options.description +=
			'<form onsubmit="return VE_getRoute(\''+$.trim(options.title)+'\',\''+id+'\', '+options.lat+','+options.lng+')"><p>'+
			'Come arrivare qui da: '+
			'<input type="text" id="VE_from_'+id+'" value="">'+
			'<input type="hidden" id="VE_to_'+id+'" value="'+options.title+'">'+
			'<input type="submit" value="Cerca">'+
			'</p></form>';
	}
	
	var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(options.lat,options.lng));
	shape.SetTitle(options.title);
	shape.SetDescription(options.description);
	shape.SetCustomIcon('<img src="'+options.icon+'" style="margin-top:'+options.iconTop+'px;margin-left:'+options.iconLeft+'"/>');
	map.AddShape(shape);
	allShape[id] = shape;
	return shape;
}			


function VE_getRoute(title, id, toLat, toLng){
	if (first){
		$('#default_route').prependTo('#route_container').append('<br />');
	}
	currentStart = new String($('#VE_from_'+id).attr('value'));
	currentStart = currentStart.substr(0,1).toUpperCase() + currentStart.substr(1,currentStart.length);
	currentDest = $('#VE_to_'+id).attr('value');
	$('#VE_route').html('<p style="text-align:center;margin-top:10px"><img src="img/pink-loader.gif"><br/>Attendere, caricamento delle indicazioni in corso...</p>');
	var options = new VERouteOptions();
	options.RouteCallback = VE_onGetRoute;
	options.DistanceUnit = VERouteDistanceUnit.Kilometer;
	options.RouteOptimize = VERouteOptimize.MinimizeTime;
	map.GetDirections([currentStart, new VELatLong(toLat,toLng)], options);
	//smallmap.GetDirections([currentStart, new VELatLong(toLat,toLng)], options);
	first = false;
	return false;
}

function VE_onGetRoute(route){
	// Unroll route
	var legs     = route.RouteLegs;
	var turns    =  
		"<h3>Da "+currentStart+" all'"+currentDest+"</h3>"+
		"<p><strong>Distanza totale: " + route.Distance.toFixed(1) + " km</strong><br/>"+
		"Le indicazioni sono generate automaticamente e potrebbero essere soggette ad errori.<br /> &nbsp;</p>"+
		"";
	
	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 += 
				'<a href="#VE_map" onclick="VE_showPoint('+turn.LatLong.Latitude+','+turn.LatLong.Longitude+', 17)">'+
				numTurns +
				". " +
				turn.Text + " (" + turn.Distance.toFixed(1) + " km)"+
				 "</a> ";
			numTurns++;
		}
	}
	
	
	$('#VE_route').slideUp('normal', function(){
		$(this).html(turns).slideDown('normal');
	});
}

function VE_showPoint(lat, lng, zoom){
	var id = (lat+'_'+lng).replace(/\./g,'');
	map.SetCenterAndZoom (new VELatLong(lat,lng),zoom == undefined ? zoomMax : zoom);
	setTimeout('map.ShowInfoBox(allShape["'+id+'"]);',1000);
}