// JavaScript Document

var map = null;
var birdseye=false;
var saved_glat;
var saved_glong;

	function vemap_load(glat, glong) {
		
		map = new VEMap('virtual_earth_map_div');
		map.SetDashboardSize(VEDashboardSize.Tiny);
	    map.HideScalebar();
        map.LoadMap(new VELatLong(glat , glong ), 17 ,VEMapStyle.Road); 
		map.AttachEvent("onclick", getDirections);
		map.AttachEvent("onobliqueenter", OnObliqueEnterHandler);
		map.AttachEvent("onmousedown", function(){
												//document.getElementById("flyover").style.display="none";
												});
		
		 var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(glat ,glong ));
		 saved_glat=glat;
		 saved_glong=glong;
		 
	     shape.SetCustomIcon('http://www.elocallink.tv/vp6/videotest4/icon_loc.png');
         map.AddShape(shape);
		 
	}
	
	/////////////////
	//direction stuff
	/////////////////
	var mapexpanded=false;
	
	function myRouteHandler(route) {
		var minVar = Math.floor(route.Time/60); 
		var secVar = route.Time % 60;              
		var rminutes = minVar + ':' + secVar +' minutes away';
		var rmiles = route.Distance.toFixed(2) +' miles away';
		$("dirinfo").innerHTML = 'Travel Info:<br>&nbsp;&nbsp;'+rminutes+'<br>&nbsp;&nbsp;'+rmiles;
		$("dirinfo").style.display="block";
	}
	function canceldirections() {
		$("dirbox").style.display="none";
	}
	function placedirections()  {
		var enddir=document.directionform.elements['dir_addr'].value;
		document.getElementById("dirbox").style.display="none";
		if (enddir) {
			if (!mapexpanded) expand();
			if (birdseye) mapToggle(2);
			var myOptions = new VERouteOptions();
			myOptions.SetBestMapView = true; // Don't change map view
			myOptions.DrawRoute      = true;
			myOptions.ShowDisambiguation  =false;
			myOptions.DistanceUnit   = VERouteDistanceUnit.Mile;
			myOptions.RouteCallback = myRouteHandler;  // Gets VERoute
			var locll = new VELatLong(saved_glat, saved_glong);
			map.GetDirections([enddir, new VELatLong(saved_glat, saved_glong, 0, VEAltitudeMode.RelativeToGround)], myOptions);
		}
	}
	function getDirections(e)   {
		if(e.elementID != null)
		{
		   document.getElementById("dirbox").style.display="block";
		}
		else
		{
		   return false;
		}
     }
	/////////////////
	//end direction stuff
	/////////////////
	
	function OnObliqueEnterHandler()
         {
            if(map.IsBirdseyeAvailable())
            {
				document.getElementById("mapswitch").innerHTML="(Switch to Aerial View)";
				document.getElementById("mapswitch").href="javascript:mapToggle(1)";
				
				document.getElementById("mapswitch2").innerHTML="(Switch to Aerial View)";
				document.getElementById("mapswitch2").href="javascript:mapToggle(1)";
				//document.getElementById("flyover").style.display="block";
				//setTimeout(function(){document.getElementById("flyover").style.display="none";}, 8000);
            }
         }
		 
	function rotate(num){
		bs = map.GetBirdseyeScene();
		
		if(bs){
			cdir=bs.GetOrientation();
		
			tdir=VEOrientation.North;
			if(num==1){
				if(cdir=="North") tdir=VEOrientation.East;
				else if(cdir=="East") tdir=VEOrientation.South;
				else if(cdir=="South") tdir=VEOrientation.West;
				else if(cdir=="West") tdir=VEOrientation.North;
			}
			else if(num==2){
				if(cdir=="North") tdir=VEOrientation.West;
				else if(cdir=="East") tdir=VEOrientation.North;
				else if(cdir=="South") tdir=VEOrientation.East;
				else if(cdir=="West") tdir=VEOrientation.South;
			}
			map.SetBirdseyeOrientation(tdir);
		}
		
	}
		 
	function mapToggle(num){
		
		if(num==1){
			if(map.IsBirdseyeAvailable()==true){
				//document.getElementById("flyover").style.display="none";
				document.getElementById("rotate1").style.display="block";
				document.getElementById("rotate2").style.display="block";
				document.getElementById("mapswitch").innerHTML="(Switch to Road View)";
				document.getElementById("mapswitch").href="javascript:mapToggle(2)";		
				document.getElementById("mapswitch2").innerHTML="(Switch to Road View)";
				document.getElementById("mapswitch2").href="javascript:mapToggle(2)";
				map.SetBirdseyeScene(map.GetCenter());
				map.SetBirdseyeScene(map.GetCenter());
				birdseye=true;
				
			}
			else{
				alert("Sorry, aerial view is not available at this location and zoom.");
			}
		}
		else if(num==2){
			document.getElementById("mapswitch").innerHTML="(Switch to Aerial View)";
			document.getElementById("mapswitch").href="javascript:mapToggle(1)";
			document.getElementById("mapswitch2").innerHTML="(Switch to Aerial View)";
			document.getElementById("mapswitch2").href="javascript:mapToggle(1)";
			document.getElementById("rotate1").style.display="none";
			document.getElementById("rotate2").style.display="none";
			map.LoadMap(map.GetCenter(), 17 ,VEMapStyle.Road);
			
			birdseye=false;
			var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(glat , glong ));
        	//shape.SetCustomIcon('icon_educ.png');
         	map.AddShape(shape);
		}
	}
	
	function reloadMap(){
		document.getElementById("virtual_earth_map_div").style.display="block";
		document.getElementById("vemap").style.top="0px";
		document.getElementById("vemap").style.width=expand_width+"px";
		document.getElementById("vemap").style.height=expand_height+"px";
		//document.getElementById("rotate1").style.top="-162px"
		//document.getElementById("rotate2").style.top="-141px";
		map.Resize(expand_width, expand_height);
	
		if(birdseye==false){
			//map.LoadMap(map.GetCenter(), 17, VEMapStyle.Road);
		}else{
			document.getElementById("rotate1").style.display="block";
			document.getElementById("rotate2").style.display="block";
			//map.LoadMap(map.GetCenter(), 17, VEMapStyle.Birdseye);
		}
		
		//var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(<?= $glat ?>, <?= $glong ?>));
        //map.AddShape(shape);
		
		
		document.getElementById("contract").style.display="block";
		document.getElementById("contract").style.left=(expand_width-60)+"px";
		document.getElementById("large_mapswitch2_container").style.left="0px"
		document.getElementById("large_mapswitch2_container").style.top=expand_height+"px"
		document.getElementById("large_mapswitch2_container").style.width=expand_width+"px"
		document.getElementById("large_mapswitch2_container").style.display="block";
	}
	
	function reloadMapAfterContract(){
		
		
		document.getElementById("vemap").style.top="0px";
		document.getElementById("vemap").style.width=original_width+"px";
		document.getElementById("vemap").style.height=original_height+"px";
		document.getElementById("rotate1").style.top="56px"
		document.getElementById("rotate2").style.top="77px";
	
		if(birdseye==false){
			//map.LoadMap(map.GetCenter(), 17, VEMapStyle.Road);
		}else{
			document.getElementById("rotate1").style.display="block";
			document.getElementById("rotate2").style.display="block";
			//map.LoadMap(map.GetCenter(), 17, VEMapStyle.Birdseye);
		}
		
		//var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(<?= $glat ?>, <?= $glong ?>));
        //map.AddShape(shape);
		
		//document.getElementById("locdiv").style.display="block";
		document.getElementById("expand").style.display="block";
		
		document.getElementById("virtual_earth_map_div").style.display="block";
		
	}
	
	function expand(){
		
		rate=12;
	
		document.getElementById("virtual_earth_map_div").style.display="none";
		//document.getElementById("flyover").style.display="none";
		document.getElementById("expand").style.display="none";

		document.getElementById("rotate1").style.display="none";
		document.getElementById("rotate2").style.display="none";
	
		
		w_func = setInterval(function(){
				
				w = document.getElementById("vemap").style.width.substr(0, String(document.getElementById("vemap").style.width).length-2);
				w=Number(w)+Number(rate);
				
				w_sofar=w-original_width;
				h=((w_sofar*diff_height)/diff_width)+original_height;
				//o=6-(h-original_height)
				
				if(w>=expand_width) { w=expand_width; clearInterval(w_func);reloadMap();return; }
				
				document.getElementById("vemap").style.width=w+"px";
				document.getElementById("vemap").style.height=h+"px";
				//document.getElementById("vemap").style.top=o+"px";
				
				}, 10);
	}

	
	function contract(){
		map.Resize(original_width, original_height);
		document.getElementById("virtual_earth_map_div").style.display="none";
		document.getElementById("dirinfo").style.display="none";
	
		document.getElementById("contract").style.display="none";
		//document.getElementById("locdiv").style.display="none";
		document.getElementById("rotate1").style.display="none";
		document.getElementById("rotate2").style.display="none";
		//document.getElementById("locdiv").style.width="250px";
		document.getElementById("large_mapswitch2_container").style.display="none";
	
		w_func = setInterval(function(){
				
				w = document.getElementById("vemap").style.width.substr(0, String(document.getElementById("vemap").style.width).length-2);
				w=Number(w)-Number(rate);
				w_sofar=expand_width-w;
				h=expand_height-((w_sofar*diff_height)/diff_width);
				o=6-(h-178)
				
				
				if(w<=original_width) { 
					w=original_width; 
					clearInterval(w_func);
					reloadMapAfterContract();
					return;
				}
				
				document.getElementById("vemap").style.width=w+"px";
				document.getElementById("vemap").style.height=h+"px";
				//document.getElementById("vemap").style.top=o+"px";
				
				}, 10);
		
	}
	
var VirtualEarthMapPreScriptLoad = window.onload;
window.onload = function() {
	if (typeof (VirtualEarthMapPreScriptLoad) == 'function') {
		VirtualEarthMapPreScriptLoad();
	}
	vemap_load(glat,glong);
};
