var gobjVEMap = null;
var gblnMapIsBusy = false;
var garrPushpins = new Array();
var gintRouteTargetLat;
var gintRouteTargetLong; 
var gstrNearestCityState;

/////////////////////////////////////////////////////////////////////////////////////////////////
//Create Pushpins////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
function RPAddPushpin(objVEMap, dblLat, dblLong, strIcon, strTitle, strDescription, strLinkTarget) {
    var objVEShape = new VEShape(VEShapeType.Pushpin, new VELatLong(dblLat,dblLong));
    objVEShape.SetCustomIcon(strIcon);
    objVEShape.SetTitle(strTitle);
    objVEShape.SetDescription(strDescription);
    if (strLinkTarget.length > 0) {
			objVEShape.SetMoreInfoURL(strLinkTarget);
    }
    objVEMap.AddShape(objVEShape);
    garrPushpins.push(objVEShape);
    return objVEShape;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
//Create Maps////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
function RPCreateRATrailMap(strContainerID, dblCenterLat, dblCenterLong, intZoomLevel, chrMapType) {
	var objVEMap = new VEMap(strContainerID);
	var objCenterCoord = new VELatLong(dblCenterLat,dblCenterLong);
	objVEMap.LoadMap(objCenterCoord,intZoomLevel,chrMapType,false,VEMapMode.Mode2D,false,2);
	objVEMap.HideDashboard();
	objVEMap.AttachEvent('onmodenotavailable', RPEventNo3DClientInstalled);
	objVEMap.AttachEvent("onclick",RPEventPushpinClicked);
	return objVEMap;
}

function RPCreateRATrailMapFixed(strContainerID, dblCenterLat, dblCenterLong, intZoomLevel, chrMapType) {
	var objVEMap = new VEMap(strContainerID);
	var objCenterCoord = new VELatLong(dblCenterLat,dblCenterLong);
	objVEMap.LoadMap(objCenterCoord,intZoomLevel,chrMapType,true,VEMapMode.Mode2D,false,2);
	objVEMap.HideDashboard();
	return objVEMap;
}

function RPCreateRAGlobalMap(strContainerID, dblCenterLat, dblCenterLong, intZoomLevel, chrMapType) {
	var objVEMap = new VEMap(strContainerID);
	var objCenterCoord = new VELatLong(dblCenterLat,dblCenterLong);
	objVEMap.LoadMap(objCenterCoord,intZoomLevel,chrMapType,false,VEMapMode.Mode2D,false,2);
	objVEMap.HideDashboard();
	objVEMap.AttachEvent('onmodenotavailable', RPEventNo3DClientInstalled);
	objVEMap.AttachEvent("onclick",RPEventPushpinClicked);
	return objVEMap;
}

function RPCreateRAWorkMap(strContainerID, dblCenterLat, dblCenterLong, intZoomLevel, chrMapType) {
	var objVEMap = new VEMap(strContainerID);
	var objCenterCoord = new VELatLong(dblCenterLat,dblCenterLong);
	objVEMap.LoadMap(objCenterCoord,intZoomLevel,chrMapType,false,VEMapMode.Mode2D,false,2);
	objVEMap.HideDashboard();
	objVEMap.ShowMiniMap(-20,-10,VEMiniMapSize.Small);
	objVEMap.AttachEvent("onclick",RPEventWorkMapMouseClick);
	objVEMap.AttachEvent("onmousemove",RPEventWorkMapMouseMove);
	objVEMap.AttachEvent("onendpan",RPEventWorkMapEndPan);
	objVEMap.AttachEvent("onendzoom",RPEventWorkMapEndZoom);
	var objLongLat = objVEMap.GetCenter();
  var strMessage = "Map Center: " + objLongLat;
  document.getElementById("txtMapCenterLocation").setAttribute("value",strMessage);
  var intCurrZoomLevel = objVEMap.GetZoomLevel();
  strMessage = "Zoom: " + intCurrZoomLevel;
  document.getElementById("txtMapZoomLevel").setAttribute("value",strMessage);
	return objVEMap;
}

function RPCreateRATestMap(strContainerID, dblCenterLat, dblCenterLong, intZoomLevel, chrMapType) {
	var objVEMap = new VEMap(strContainerID);
	var objCenterCoord = new VELatLong(dblCenterLat,dblCenterLong);
	objVEMap.LoadMap(objCenterCoord,intZoomLevel,chrMapType,false,VEMapMode.Mode2D,false,2);
	objVEMap.HideDashboard();
	objVEMap.AttachEvent('onmodenotavailable', RPEventNo3DClientInstalled);
	objVEMap.AttachEvent("onclick",RPGoToUserLocation);
	return objVEMap;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
//Move and Change Modes//////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
function RPPanToLatLong(objVEMap, dblLat, dblLong) {
	objVEMap.PanToLatLong(new VELatLong(dblLat,dblLong));
}

function RPSwitchTo3DMode(objVEMap) {
	objVEMap.SetMapMode(VEMapMode.Mode3D);
    var style = objVEMap.GetMapStyle();
    if (style == VEMapStyle.Road) {
			objVEMap.SetPitch(-25);
			objVEMap.SetZoomLevel(12);
    }
    else {
			objVEMap.SetPitch(-10);
			objVEMap.SetZoomLevel(16);
    }
}

function RPSwitchTo2DMode(objVEMap) {
	objVEMap.SetMapMode(VEMapMode.Mode2D);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
//Get Map Info///////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
function RPGetMapLeft(objVEMap) {
	return gobjVEMap.GetLeft();
}

function RPGetMapTop(objVEMap) {
	return gobjVEMap.GetTop();
}

function RPGetMapRight(objVEMap) {
	var intMapLeft = objVEMap.GetLeft();
	var intMapCenter = objVEMap.LatLongToPixel(objVEMap.GetCenter()).x;
	var intMapWidth = (intMapCenter - intMapLeft) * 2;
	var intMapRight = intMapLeft + intMapWidth;
	return Math.round(intMapRight);
}

function RPGetMapBottom(objVEMap) {
	var intMapTop = objVEMap.GetTop();
	var intMapCenter = objVEMap.LatLongToPixel(objVEMap.GetCenter()).y;
	var intMapHeight = (intMapCenter - intMapTop) * 2;
	var intMapBottom = intMapTop + intMapHeight;
	return Math.round(intMapRight);
}

function RPGetPosition(objVEMap) {
	prompt('The center of the map is at (lat/long):',objVEMap.GetCenter());
}

function RPMapRouteFromUserLocation(intTargetLat, intTargetLong, strNearestCityState)  { 
	gintRouteTargetLat = intTargetLat;
	gintRouteTargetLong = intTargetLong;
	gstrNearestCityState = strNearestCityState;
	
	//Dynamically create a <script> tag and set the 'src' attribute to the URL of the Virtual Earth WiFi service           
	var objScriptTag = document.createElement("script"); 
	var strWiFiLocatorUrl = "  http://virtualearth.msn.com/WiFiIPService/locate.ashx  "
	objScriptTag.setAttribute("src", strWiFiLocatorUrl); 
	
	//Append the <script> tag to the document header to force the execution of the script (thus invoking the wifi service)       
	objDocumentHead = document.getElementsByTagName("head").item(0);          
	objDocumentHead.appendChild(objScriptTag);
	//When the user is located one of two callbacks will be called by the service:
	//SetAutoLocateViewport() on success, ShowMessage() on failure   
} 

/////////////////////////////////////////////////////////////////////////////////////////////////
//Event Handlers/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
function RPEventNo3DClientInstalled () {
    alert('This feature of the RiderPlanet USA maps requires the Microsoft Virtual Earth 3D client software.\n' +
      '\nThe sofware is distributed FREE by Microsoft and is safe to download.  It contains no malware or spyware.' +
      '\nBefore downloading, be sure your computer meets the minimum system requirements shown on the download page.' +
      '\nOnce installed, you will be able to navigate and fly through a 3D virtual world to explore the terrain and mountains.' +
      '\n\nYou will now be directed to the Microsoft Download Center where you can download this great new feature!');
    //Go to the download page  
	window.location.assign("http://www.microsoft.com/downloads/details.aspx?FamilyID=e9298080-50c4-4f2e-9fc4-4009074996ba&DisplayLang=en");
	//Start the download
	//window.location.assign('http://go.microsoft.com/fwlink/?LinkId=72623');
}

function RPEventPushpinClicked(e) {
    if (e.elementID != null) {
			var strElementID =  e.elementID;
			if (strElementID.length > 19) {
				strElementID = strElementID.substring(0,18);
			}
			for (i=0; i<garrPushpins.length; i++) {
				if (garrPushpins[i].GetID() == strElementID) {
					var strLinkTarget = garrPushpins[i].GetMoreInfoURL();
					if (strLinkTarget.length > 0) {
						window.location.assign(strLinkTarget);
					}
					break;
				}
			}
    }
}
function RPEventWorkMapMouseClick(e) {
    var x = e.mapX;
    var y = e.mapY;
    var objPixel = new VEPixel(x, y);
    var objLongLat = gobjVEMap.PixelToLatLong(objPixel);
    var strMessage = "Last Click: " + objLongLat;
    document.getElementById("txtLastClickLocation").setAttribute("value",strMessage);
    if (e.elementID != null) {
		var strElementID = e.elementID;
		if (strElementID.length > 19) {
			strElementID = strElementID.substring(0,18);
		}
		for (i=0; i<garrPushpins.length; i++) {
			if (garrPushpins[i].GetID() == strElementID) {
				strMessage = garrPushpins[i].GetPoints()[0];
				document.getElementById("txtSelectedElementLocation").setAttribute("value",strMessage);
				break;
			}
		}
		
    }
    else {
        document.getElementById("txtSelectedElementLocation").setAttribute("value","");
    }
}
function RPEventWorkMapMouseMove(e) {
    var x = e.mapX;
    var y = e.mapY;
    var objPixel = new VEPixel(x, y);
    var objLongLat = gobjVEMap.PixelToLatLong(objPixel);
    var strMessage = "Mouse At: " + objLongLat;
    document.getElementById("txtMouseLocation").setAttribute("value",strMessage);
}
function RPEventWorkMapEndPan(e) {
    var objLongLat = gobjVEMap.GetCenter();
    var strMessage = "Map Center: " + objLongLat;
    document.getElementById("txtMapCenterLocation").setAttribute("value",strMessage);
}
function RPEventWorkMapEndZoom(e) {
    var intZoomLevel = gobjVEMap.GetZoomLevel();
    var strMessage = "Zoom: " + intZoomLevel;
    document.getElementById("txtMapZoomLevel").setAttribute("value",strMessage);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
//WiFi Service Callbacks/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
function SetAutoLocateViewport(latitude, longitude, lvl, bl, msg)  { 
	//NOTE: This function is invoked by the Virtual Earth WiFi service when the user has been located             
	var objLatLongBegin = new VELatLong(latitude, longitude);
	
	//If the citystate was specified... use it
	if (gstrNearestCityState != "") {
		gobjVEMap.GetDirections([objLatLongBegin,gstrNearestCityState]);
	}
	else {
		var objLatLongEnd =  new VELatLong(gintRouteTargetLat, gintRouteTargetLong);      
		gobjVEMap.GetDirections([objLatLongBegin,objLatLongEnd]);
  }
} 

function ShowMessage(msg)  { 
	//NOTE: This function is called by Virtual Earth WiFi service if it can't find the user location          
	//alert("Unable to determine user location:\n\n" + msg);  
}      
