﻿var map = null;
var pinLayer = null;
var pinID = 1;
var currentAddress = 0;
var beginShape = null;
var locationSelected = false;
                  
// add the onload event
if (window.addEventListener) //DOM method for binding an event
{
    window.addEventListener("load", GetMap, false)
}
    else if (window.attachEvent) //IE exclusive method for binding an event
{
    window.attachEvent("onload", GetMap)
}
    else if (document.getElementById) //support older modern browsers
{
    window.onload=GetMap
}
                 
                  
function GetMap()      
{         
    map = new VEMap('themap');         
    map.onLoadMap  = onMapLoad;
    try{
        map.LoadMap();
    }catch(e){
        //Browser does not support SGV or VML
        onMapLoad();
    }
}         

function onMapLoad()
{
    pinLayer = new VEShapeLayer();
    map.AttachEvent("onmouseover",ShapeHandler);         
    map.AttachEvent("onmouseout",ShapeHandler);
    FindLoc();
}

function FindLoc()         
{        
    map.Find(null , /* what */
            locations[currentAddress].address1 + ", " + locations[currentAddress].city + ", " + locations[currentAddress].state + " " + locations[currentAddress].zip, /* where */
            null, /* findType - VEFindType */
            null, /* shapeLayer - VEShapeLayer */
            null, /* startIndex */
            null, /*numberOfResults */
            null, /* showResults */
            null, /* createResults */
            null, /* useDefaultDisambiguation */
            false, /* setBestMapView */
            onFoundResults); /* callback */
}         

function onFoundResults(layer, resultsArray, places, hasMore, veErrorMessage)         
{
    if (places != null && places.length > 0)
    {
        shape = new VEShape(VEShapeType.Pushpin, places[0].LatLong);         
        shape.SetTitle(locations[currentAddress].name);
        shape.SetDescription(locations[currentAddress].address1 + "<br />" + locations[currentAddress].city + ", " + locations[currentAddress].state + " " + locations[currentAddress].zip + "<span><br />Phone: " + locations[currentAddress].phone + "<br />Fax: " + locations[currentAddress].fax + "<br /><a href=\"javascript:InitializeDirections();\">Get Directions</a></span>");
        pinLayer.AddShape(shape);
    }
    currentAddress += 1;
    if (currentAddress <= lastAddress)
    {
        FindLoc();
    }
    else
    {
        map.AddShapeLayer(pinLayer);
        map.ShowAllShapeLayers();
        var loading = document.getElementById("mapLoading");
        loading.style.display = "none";
    }
} 

function ShapeHandler(e)
{
    if (e.eventName  == "onmouseover")
    {
        beginShape = map.GetShapeByID(e.elementID);
    }
    else if (e.eventName == "onmouseout")
    {
        // beginShape = null;
    }
}

function InitializeDirections()
{
    var end = document.getElementById("txtEnd");
    var adr = beginShape.GetDescription();
    adr = adr.substring(0, adr.indexOf("<span>"));
    end.innerHTML = adr;
    
    var begin = document.getElementById("txtBegin");
    begin.style.backgroundColor = '#F6D919';
    begin.focus();
    map.HideInfoBox();
    
    locationSelected = true;
}

function GetDirections()
{
    var begin = document.getElementById("txtBegin");
    var end = document.getElementById("txtEnd");
    
    if (!locationSelected)
    {
        alert("Please select a location from the map and click 'Get Directions'");
        return;
    }
    
    if (begin.value == "")
    {
        alert("Please enter an address");
        return;
    }
    
    begin.style.backgroundColor = '#FFFFFF';
    
    var result = document.getElementById("locDirections")
    result.innerHTML = "<p><img src='images/spinner.gif' alt='Loading' align='absmiddle' style='margin-right:5px; vertical-align: middle' />Loading . . . Please Wait.</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>";
    
    var options = new VERouteOptions();
    options.RouteCallback = onGotRoute;            
    map.GetDirections([begin.value, end.innerHTML], options);
    
    var showall = document.getElementById("ShowAllLink");
    showall.style.display = "inline";
}

function onGotRoute(route)         
{    
    pinLayer.Hide();
    
    var turns = "<div>";
    turns += "<span style='float:right;vertical-align:middle;'><a id='printlinktop' href='javascript:PrintMap();'><img src='Images/printer.png' style='vertical-align:middle;margin-right:5px;border-width:0px;' alt='Print' align='absmiddle' />Print</a></span>";
    // Unroll route           
    var legs = route.RouteLegs;           
    turns += "<strong>Total distance: " + route.Distance.toFixed(1) + " mi</strong><br />";
    var numTurns = 0;           
    var leg = null;           
    // Get intermediate legs            
    turns += "<ul>";
    for(var i = 0; i < legs.length; i++)            
    {               
        // Get this leg so we don't have to derefernce multiple times               
        leg = legs[i];  // Leg is a VERouteLeg object                                 
        // Unroll each intermediate leg               
        var turn = null;  // The itinerary leg                                 
        for(var j = 0; j < leg.Itinerary.Items.length; j ++)               
        {                  
            turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object                  
            numTurns++;     
            // + numTurns + ".\t"              
            turns += "<li>" + turn.Text + " (" + turn.Distance.toFixed(1) + " mi)</li>";               
        }            
    }            
    turns += "</ul>";
    turns += "<span style='float:right;vertical-align:middle;'><a id='printlinkbottom' href='javascript:PrintMap();'><img src='Images/printer.png' style='vertical-align:middle;margin-right:5px;border-width:0px;' alt='Print' align='absmiddle' />Print</a></span></div>";
    
    var result = document.getElementById("locDirections")
    result.innerHTML = turns;
}

function ShowLocations()
{
    if (pinLayer != null)
    {
        pinLayer.Show();
        map.ShowAllShapeLayers();
    }
    
    var showall = document.getElementById("ShowAllLink");
    showall.style.display = "none";
}

function PrintMap()
{
    var WinPrint = window.open('','','left=0,top=0,width=1,height=1,t oolbar=0,scrollbars=0,status=0');
    WinPrint.document.open();

    var begin = document.getElementById("txtBegin");
    var end = document.getElementById("txtEnd");

    WinPrint.document.write("<p><strong>Begin:</strong><br />");
    WinPrint.document.write(begin.value.replace(/\n/, '<br>'));
    WinPrint.document.write("</p>");
    
    WinPrint.document.write("<p><strong>End:</strong><br />");
    WinPrint.document.write(end.innerHTML);
    WinPrint.document.write("</p>");

    var prtContent = document.getElementById("locDirections");
    WinPrint.document.write("<p>");
    WinPrint.document.write(prtContent.innerHTML);
    WinPrint.document.write("</p>");
   
    // Hide the print links 
    var printtop = WinPrint.document.getElementById("printlinktop");
    if (printtop != null)
    {
        printtop.style.display = "none";
    }
    var printbottom = WinPrint.document.getElementById("printlinkbottom");
    if (printbottom != null)
    {
        printbottom.style.display = "none";
    }
    
    WinPrint.document.close();
    WinPrint.focus();
    WinPrint.print();
    WinPrint.close();    
}