// google maps -------------------------------------------------------------------------------------
function loadGoogleMap(ifreameId, lati, longi, zoom, mapType, units, pinheader, pindesc) {
//document.getElementById("debugger").innerHTML = 'loadGoogleMap, ' + ifreameId + ', ' + lati + ', ' + longi + ', ' + zoom + ', ' + mapType + ', ' + pinheader + ', ' + pindesc;
	if (GBrowserIsCompatible()) {
		var z = Number(zoom);
		var map = new GMap2(document.getElementById(ifreameId));
		map.setCenter(new GLatLng(lati, longi), z);
		map.addControl(new GSmallZoomControl());
		map.addControl(new GScaleControl());
		if (mapType == 'o') {
			map.setMapType(G_SATELLITE_MAP);
		}
		else if (mapType == 'h') {
			map.setMapType(G_HYBRID_MAP);
		}
		else { // type = r
			map.setMapType(G_NORMAL_MAP);
		}
	}
}

// virtual earth -----------------------------------------------------------------------------------
var map = null;
var pinid = 0;
function GetVEMapOnly(ifreameId, lati, longi, zoom, mapType, units, pinheader, pindesc) {
//document.getElementById("debugger").innerHTML = 'GetVEMapOnly, ' + ifreameId + ', ' + lati + ', ' + longi + ', ' + zoom + ', ' + mapType + ', ' + pinheader + ', ' + pindesc;
	map = new VEMap(ifreameId);
	map.LoadMap(new VELatLong(lati, longi), zoom ,'r' ,false);
	if (pinheader) {
		AddVEPushpin(pinheader, pindesc);
	}
}

function AddVEPushpin(pinheader, pindesc) {
	var shape = new VEShape(VEShapeType.Pushpin, map.GetCenter());
	shape.SetTitle(pinheader);
	shape.SetDescription(pindesc);
	pinid++;
	map.AddShape(shape);
}

function GetVERouteOnly(ifreameId, startLati, startLongi, endLati, endLongi, startPoint, endPoint, zoom, mapType, units) {
//document.getElementById("debugger").innerHTML = 'GetVERouteOnly, ' + ifreameId + ', ' + startLati + ', ' + startLongi + ', ' + endLati + ', ' + endLongi + ', ' + mapType + ', ' + units;
	map = new VEMap(ifreameId);
	map.LoadMap();
            
	var options = new VERouteOptions();
	if (units == 'km') {
		options.DistanceUnit = VERouteDistanceUnit.Kilometer;
	}
	if ((typeof startLati != 'undefined') && (typeof startLongi != 'undefined') && (typeof endLati != 'undefined') && (typeof endLongi != 'undefined')) {
		map.GetDirections([new VELatLong(startLati, startLongi), new VELatLong(endLati, endLongi)], options);
	}
	else if ((typeof startLati != 'undefined') && (typeof startLongi != 'undefined') && (typeof endPoint != 'undefined')) {
		map.GetDirections([new VELatLong(startLati, startLongi), endPoint], options);
	}
	else if ((typeof endLati != 'undefined') && (typeof endLongi != 'undefined') && (typeof startPoint != 'undefined')) {
		map.GetDirections([startPoint, new VELatLong(endLati, endLongi)], options);
	}
	else if ((typeof startPoint != 'undefined') && (typeof endPoint != 'undefined')) {
		map.GetDirections([startPoint, endPoint], options);
	}
	map.SetZoomLevel(zoom);
}

function GetVEDirections(ifreameId, startLati, startLongi, endLati, endLongi, startPoint, endPoint, zoom, mapType, units) {
	map = new VEMap(ifreameId);
	map.LoadMap();
         
	var options = new VERouteOptions();
	options.RouteCallback = onGotVERoute;
	if (units == 'km') {
		options.DistanceUnit = VERouteDistanceUnit.Kilometer;
	}
	if ((typeof startLati != 'undefined') && (typeof startLongi != 'undefined') && (typeof endLati != 'undefined') && (typeof endLongi != 'undefined')) {
		map.GetDirections([new VELatLong(startLati, startLongi), new VELatLong(endLati, endLongi)], options);
	}
	else if ((typeof startLati != 'undefined') && (typeof startLongi != 'undefined') && (typeof endPoint != 'undefined')) {
		map.GetDirections([new VELatLong(startLati, startLongi), endPoint], options);
	}
	else if ((typeof endLati != 'undefined') && (typeof endLongi != 'undefined') && (typeof startPoint != 'undefined')) {
		map.GetDirections([startPoint, new VELatLong(endLati, endLongi)], options);
	}
	else if ((typeof startPoint != 'undefined') && (typeof endPoint != 'undefined')) {
		map.GetDirections([startPoint, endPoint], options);
	}
}

function onGotVERoute(route) {
	// Unroll route
	var legs = route.RouteLegs;
	if (units) {
		var turns    = "Total distance: " + route.Distance.toFixed(1) + " km";
	}
	else {
		var turns    = "Total distance: " + route.Distance.toFixed(1) + " mi";
	}
	var numTurns = 0;
	var leg      = null;

	// Get intermediate legs
	for(var i = 0; i < legs.length; i++) {
		// Get this leg so we don't have to derefernce multiple times
		leg = legs[i];  // Leg is a VERouteLeg object
                  
		// Unroll each intermediate leg
		var turn = null;  // The itinerary leg
                  
		for(var j = 0; j < leg.Itinerary.Items.length; j ++) {
			turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object
			numTurns++;
			turns += numTurns + ".\t" + turn.Text + " (" + turn.Distance.toFixed(1) + " mi)";
		}
	}
	document.getElementById("debugger").innerHTML = turns;
	//alert(turns);
}   