/*******************************************************************************************************
	Date Created: February 2007
	Author(s): Alex Talarico
	Description: JavaScript file to request and handle Virtual Earth API functions to display the 
				 TASER Maps and Directions page.

	CHANGE HISTORY
	---------------------------------------------------------------------------------------------------------------
	Date		Author			Description
	---------------------------------------------------------------------------------------------------------------
	02/10/07	Alex Talarico	Completed core logic for this script.
	03/12/07	Alex Talarico	Completed IE and Firefox compatibility testing.
	---			---

********************************************************************************************************/

    var map = null;

	function Map_OnLoad()
	{
		//only load the map on the directions page
		if( document.getElementById( "myVEMap" )) LoadMap();
	}

	function LoadMap()
	{		
		GetMap();
		//AddPin();
	}

	function GetMap()	
	{
		// Compatibility: If the browser is Firefox get version number and call additional drawing libraries
		var ffv = 0;
		var ffn = "Firefox/"
		var ffp = navigator.userAgent.indexOf(ffn);
		if (ffp != -1) ffv = parseFloat(navigator.userAgent.substring(ffp + ffn.length));
		// If we're using Firefox 1.5 or above override the Virtual Earth drawing functions to use SVG
		if (ffv >= 1.5) 
		{
		  	Msn.Drawing.Graphic.CreateGraphic=function(f,b) { return new Msn.Drawing.SVGGraphic(f,b) }
		}

		try 
		{
			map = new VEMap("myVEMap");
			map.LoadMap( new VELatLong(33.648600,-111.897990), 15 ,'r' , false );
		}
		catch ( err ) 
		{ 		
			// Compatibility: If the browser is Safari VE is not supported
			if (navigator.userAgent.indexOf('Safari') != -1) 
			{
				alert( 'Virtual Earth Compatibility note:\n\nSorry, the Safari browser is currently not supported with Virtual Earth Mapping technology.\n\nPlease use Firefox or Internet Explorer to view this page.' );
				return;
			}
			else 
			{
				// an exception occurred in a supported browser
				alert( 'Map Load Excetpion: '+err.message ); 
			}
		}
		
		setTimeout("AddPin()", 2000);
	}

	function AddPin()
	{
		map.DeleteAllPushpins();
	
		var pin = new VEPushpin(
					1,
					new VELatLong(33.648600,-111.897990),
				   '/SiteCollectionImages/Icons/taserMapIcon_small.gif',
				   'TASER Headquarters',
				   '<table><tr><td><img src="/SiteCollectionImages/Icons/taserBuilding.jpg"></td><td>'+
				   '17800 N. 85th St.<br>Scottsdale, AZ 85255</td></tr></table>', 
				   'iconStyle', 'titleSytle', 'detailsStyle' );
		
		map.AddPushpin(pin);
	}

	function RouteDirections( fromAddressParam )
	{
		var fromAddress = fromAddressParam; //'4510 E Glenn, Tucson AZ 85712';
		var toAddress = '17800 N. 85th St, Scottsdale, AZ 85255-9603';

		map.GetRoute(fromAddress, toAddress, null, null, onGotRoute);
	}

	function onGotRoute(route)
    {
        var routeinfo="<h2>Route:</h2>";
        routeinfo+="<font color=navy>Total distance: ";
        routeinfo+=   route.Itinerary.Distance+" ";
        routeinfo+=   route.Itinerary.DistanceUnit+"</font><br>";

        var steps="";
        
        var len = route.Itinerary.Segments.length;
           for(var i = 0; i < len ;i++)
           {
			  steps+= "<b>"+(i+1)+".</b> ";
              steps+=route.Itinerary.Segments[i].Instruction+" -- (";
              steps+=route.Itinerary.Segments[i].Distance+" ";
              steps+=route.Itinerary.DistanceUnit+")<br>";
           }
        routeinfo+="<br><i>Steps:</i><br>"+steps;

		// set the route info to display out on the div
        document.getElementById( "directionsSection" ).innerHTML = routeinfo;
    }

	function getDirections() 
	{
		var address = document.getElementById("address");

	  	var city = document.getElementById("city");
	  	var state = document.getElementById("state");
	  	var zip = document.getElementById("zip");

	    var fullFromAddress = address.value + "%20" + city.value + "%20" + state.value + "%20" + zip.value;

	  	RouteDirections( fullFromAddress );
	}