var theLinesInDirections = 0;
var entryDefaultText = "Please enter your starting location...";

document.onmousedown = loader_inv;

var map = null;
function getMap() {
	map = new VEMap('myMap');
	var center = new VELatLong(39.81,-98.00,null,null);
	map.LoadMap(center);
	map.ShowDisambiguationDialog(false);   
	map.AttachEvent("onchangeview",loader_inv);
	map.AttachEvent("onerror",loader_inv);
}   
function loader_inv() {
	document.getElementById("loader").style.visibility = "hidden";
}
function findRoute() {
	var theFromValue = document.getElementById("txtFrom").value;
	map.Find(null, theFromValue, null, null, 0, 5, false, false, true, false, theCallBack);
}

 function theCallBack(layer, resultsArray, places, hasMore, veErrorMessage) { 
 	document.getElementById("loader").style.visibility = "visible";
	document.getElementById("txtFrom").value = places[0].Name;
	var theFromValue = document.getElementById("txtFrom").value;

	var theToValue = new VELatLong(42.67161,-84.53489); //"IDV Solutions, 5913 Executive Dr, Suite 320,  Lansing, MI 48911";
	var options = new VERouteOptions();
	options.DrawRoute      = true;
	options.SetBestMapView = true;
	options.RouteCallback  = showTurns;
	options.DistanceUnit   = VERouteDistanceUnit.Mile;
	options.ShowDisambiguation = false;
	map.GetDirections([theFromValue, theToValue], options);
 }

function showTurns(route) {
	theLinesInDirections = 7;
	var turns = "<span style='font-size: 12px; font-weight: bold; color: #006699'>Directions from " + document.getElementById("txtFrom").value + " to IDV Solutions</span><br /><br/>";
	turns += "<b>Distance:</b> " + route.Distance.toFixed(1) + " miles";
	turns += "<br/><b>Time:</b> " + getTime(route.Time) + "<br/>";
	
	//if (document.forms.dirsForm.showdirs[0].checked) {
		var legs          = route.RouteLegs;
		var leg           = null;
		var turnNum       = 0; 

		for(var i = 0; i < legs.length; i++) {
			leg = legs[i];
			var legNum = i + 1;
			turns += "<br/><b>Distance for leg " + legNum + ":</b> " + leg.Distance.toFixed(1) + " miles" +
			"<br/><b>Time for leg "     + legNum + ":</b> " + getTime(leg.Time) + "<br/><br/>";
			var turn        = null;  
			var legDistance = null;
			
			for(var j = 0; j < leg.Itinerary.Items.length; j ++) {
				turnNum++;
				turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object
				turns += "<b>" + turnNum + "</b>\t" + turn.Text;
				legDistance = turn.Distance;
				if(legDistance > 0) {
					turns += " (" + legDistance.toFixed(1) + " miles";
					if(turn.Time != null) {
						turns += "; " + getTime(turn.Time);
					}
					turns += ")<br/>";
				}
			}
			turns += "<br/>";
		}
		setDirections(turns);
	//}
	theLinesInDirections += legs.length + turnNum;
	var addLines = Math.floor(theLinesInDirections/10);
	theLinesInDirections += addLines;
	checkShowText();
}
 function setDirections(s) {
	var d = document.getElementById("directions");
	d.innerHTML = s;
 }
 
 function getTime(time) {
	if(time == null) { return(""); }
	if(time > 60) {                               
		var seconds = time % 60;
		var minutes = time - seconds;
		minutes     = minutes / 60;
		if(minutes > 60) {                                     
			var minLeft = minutes % 60; 
			var hours   = minutes - minLeft;
			hours       = hours / 60;
			return(hours + " hour(s), " + minLeft + " minute(s), " + seconds + " second(s)");
		} else {
			return(minutes + " minutes, " + seconds + " seconds");
		}
	} else {
		return(time + " seconds");
	}
}

function checkFocus() {
	var theFromValue = document.getElementById('txtFrom').value;
	if (theFromValue == entryDefaultText) { 
		setFocus(); 
	}
}

function setFocus() {
	var theInput = document.getElementById('txtFrom');
	theInput.value = "";
	theInput.focus();
}

function setDefaultInputMessage() {
	var theInput = document.getElementById('txtFrom');
	theInput.value = entryDefaultText;
}

function checkShowText() {
	var contentTop 		= getPixelsFromTop(document.getElementById("directions"));
	var contentBottom 	= getPixelsFromTop(document.getElementById("preFooter"));
	var heightOfCell 	= (contentBottom - contentTop);
	var theBaseMB = 830;

	var theChecked = document.getElementById("showTextDirections").checked;
	if (theChecked)  {
		theBaseMB += heightOfCell;
	} else {
		var d = document.getElementById("directions");
		d.innerHTML = "";
	}
	document.getElementById("mainBody").style.height = String(theBaseMB) + "px";
}

function newDirectionsSearch() {
	map.Clear();
	findRoute();
}
function getPixelsFromTop(obj){
	var objFromTop = obj.offsetTop;
	while(obj.offsetParent!=null) {
		var objParent = obj.offsetParent;
		objFromTop += objParent.offsetTop;
		obj = objParent;
	}
	return objFromTop;
}
function checkKeydown(e) {
	var keynum;
	var keychar;
	var numcheck;
	if(isIE)	{ 	
		keynum = e.keyCode;
	} else	{	
		keynum = e.which;
	}
	if (keynum==13) newDirectionsSearch();
}
