/**
 * @author edwardgough
 */

 function GetMap(){
 	if(document.getElementById('map_lat')) {
	 	if (MSLat.value != '0' && MSLon.value != '0' && MSZoom.value != '0' && MSLat.value != '' && MSLon.value != '' && MSZoom.value != '') {
	 		MSMap = new VEMap('MSMap');
	 		MSMap.SetDashboardSize(VEDashboardSize.Normal);
	 		MSMap.LoadMap(new VELatLong(parseFloat(MSLat.value), parseFloat(MSLon.value)), parseInt(MSZoom.value));
	 		MSPin();
	 		MSMap.AttachEvent("onkeydown", MSMapKD);
	 	}
	}
}
	
function MSMapKD(e) {
	if(e.keyCode == 66 || e.keyCode == 79) { return true; }
}

function MSPin() {
	MSMap.DeleteAllShapes();
	var newShape = new VEShape(VEShapeType.Pushpin, new VELatLong(parseFloat(MSLat.value), parseFloat(MSLon.value)));
	newShape.SetDescription(lineBreak(MSDesc.value));
	newShape.SetTitle(MSName.value);
	MSMap.AddShape(newShape);
}

function lineBreak(obj) {
	obj = obj.replace(/\r\n/gi, "<br />");
	obj = obj.replace(/\r/gi, "<br />");
	obj = obj.replace(/\n/gi, "<br />");
	return obj;
}
function GetRouteMap(origin) {
	var options = new VERouteOptions;
	options.DrawRoute      = true;
	options.SetBestMapView = true;
	options.RouteCallback  = ShowTurns;
	options.DistanceUnit   = VERouteDistanceUnit.Mile;
	options.ShowDisambiguation = true;
	MSMap.DeleteAllShapes();
	MSMap.GetDirections(new Array(origin, new VELatLong(parseFloat(MSLat.value), parseFloat(MSLon.value))), options);
}

function ShowTurns(route) {
	var turns = "<table><tr><th>&nbsp;</th><th>Directions:</th><th>Distance:</th><th>Time:</th></tr>";
	var legs = route.RouteLegs;
	var leg = legs[0];
	var turnNum = 0;
	var turn = null;
	var legDistance = null;
	for(var j = 0; j < leg.Itinerary.Items.length; j ++) {
		turnNum++;
		turn = leg.Itinerary.Items[j];
		turns += "<tr><th>" + turnNum + "</th><td>" + turn.Text + "</td><td>";
		legDistance = turn.Distance;
		if(legDistance > 0) {
			turns += legDistance.toFixed(1) + " miles</td>";
			turns += "<td>" + GetTime(turn.Time) + "</td>";
		} else {
			turns += "&nbsp;</td><td>&nbsp;</td>";
		}
		turns += "</tr>";
	}
	turns += "<tr><td colspan='2'>&nbsp;</td><td>";
	turns += route.Distance.toFixed(1) + " miles</td><td>";
	turns += GetTime(route.Time) + "</td></tr></table>";
	document.getElementById("map_directions").innerHTML = turns;
}

function GetTime(time) {
	if(time == null || time == 0) { time = 1; }
	var minutes = time / 60;
	minutes = Math.round(time / 60);
	var hours = "0hrs " ;
	if(minutes >= 60) {
		var time = minutes;
		minutes = minutes % 60;        // minLeft    == 40
		hours = (time - minutes) / 60;
		if(hours == 1){
			hours += 'hr ';
		} else {
			hours += 'hrs ';
		}
	}
	if(minutes == 1){
		minutes += 'min';
	} else {
		minutes += 'mins';
	}
	return(hours + minutes);
}
    
function ClearRoute() {
	MSMap.DeleteRoute();
	document.getElementById("map_directions").innerHTML = "";
	MSMap.LoadMap(new VELatLong(parseFloat(MSLat.value), parseFloat(MSLon.value)), parseInt(MSZoom.value));
	MSPin();
}

function hitReturn(e, obj){
	var keynum;
	if (window.event) {
		keynum = e.keyCode;
	} else if (e.which) {
		keynum = e.which;
	}
	if (keynum == 13){
		GetRouteMap(obj.value);
	}
	return true;
}
