// JavaScript Document
var map = null;
var intZoomFactor;
var strPropName;
var strToAddress, strFromAddress, strAddress;
// Initial map view

intZoomFactor = 14;


function GetRouteMap() {
    var locations;    

    strToAddress = document.getElementById("Address").value;
    strFromAddress = document.getElementById("FromAddress").value;

    if (strFromAddress == "") {
        alert(" Please entr you address, try again!");
        document.getElementById("FromAddress").focus();
        return false;
    }
     
    locations =  new Array(strFromAddress, strToAddress);

    var options = new VERouteOptions;

    // Otherwise what's the point?
    options.DrawRoute = true;

    // So the map doesn't change:
    options.SetBestMapView = false;

    // Call this function when map route is determined:
    options.RouteCallback = ShowTurns;

    // Show as miles
    options.DistanceUnit = VERouteDistanceUnit.Mile;

    // Show the disambiguation dialog
    options.ShowDisambiguation = true;

    // map.GetDirections(locations, options);
    map.GetDirections(locations, options);
}

function ShowTurns(route) {

    var turns = "<h5>Driving Directions</h5> <span class='DriveInstruction'> <b> From: </b>  " + strFromAddress + " <br />  <b> To: </b>  " + strToAddress + " <br /> ";

    turns += "<b>Distance:</b> " + route.Distance.toFixed(1) + " miles";

    turns += "<br/><b>Time:</b> " + GetTime(route.Time) + " <br /> <br /> ";

    //    if (dirsForm.dirsType[0].checked) {
    // Unroll route and populate DIV
    var legs = route.RouteLegs;
    var leg = null;
    var turnNum = 0;  // The turn #

    // Get intermediate legs
    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

        var legNum = i + 1;

        // Unroll each intermediate leg
        var turn = null;  // The itinerary leg
        var legDistance = null;  // The distance for this leg

        turns = turns ;

        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;

            // So we don't show 0.0 for the arrival
            if (legDistance > 0) {
                // Round distances to 1/10ths
                turns += " (" + legDistance.toFixed(1) + " miles";

                // Append time if found
                if (turn.Time != null) {
                    turns += "; " + GetTime(turn.Time);
                }

                turns += ")<br/> ";
            }
        }
        turns += "<br/>  </span> ";
     }

    // Populate DIV with directions
    SetDirections(turns);
    //       }

}

function SetDirections(s) {
    var d = document.getElementById("directions");
    d.innerHTML = s;
}

// time is an integer representing seconds
// returns a formatted string
function GetTime(time) {
    if (time == null) {
        return ("");
    }
    if (time > 60) {                                 // if time == 100
        var seconds = time % 60;       // seconds == 40
        var minutes = time - seconds;  // minutes == 60
        minutes = minutes / 60;    // minutes == 1


        if (minutes > 60) {                                     // if minutes == 100
            var minLeft = minutes % 60;        // minLeft    == 40
            var hours = minutes - minLeft;   // hours      == 60
            hours = hours / 60;          // hours      == 1

            return (hours + " hour(s), " + minLeft + " minute(s), " + seconds + " second(s)");
        }
        else {
            return (minutes + " minutes, " + seconds + " seconds");
        }
    }
    else {
        return (time + " seconds");
    }
}

function ClearAll() {
    map.DeleteRoute();
    SetDirections("");
    map.LoadMap(PropLocation, intZoomFactor);
}

// Dashboard hide & show calls
function HideDashboard() {
    map.HideDashboard();
}

/* ----------- Test */



function fnSingleLatiLong(findResults) {
    strLatiLong = findResults[0].LatLong;  
  //  alert(" Lati = " + strLatiLong);
}

// This fuction serch for a single location. 
function fnLocatePlaceOnMap(argLati, argLong, strMapPlaceTag, intZoomFactor) 
{ // alert(argLati + "    " + argLong)    

    map = new VEMap(strMapPlaceTag);
	map.LoadMap(new VELatLong(Number(argLati), Number(argLong)), intZoomFactor, 'r', false);
	map.HideDashboard();    
    var layer = new VEShapeLayer();   
    var pin = new VEShape(VEShapeType.Pushpin, map.GetCenter());
    pin.SetTitle(strShortSiteName);
    pin.SetDescription(strCustomNoteWindow);    
    var icon = "<img src='images/GreenArrow.gif'> ";
    pin.SetCustomIcon(icon);
    map.AddShape(pin);
}

// This fuction serch for Multi locations. Get info from XML Files
function fnLocateMultiPlaceOnMap(strMapPlaceTag, strXMLURL, intZoomFactor) {
    map = new VEMap(strMapPlaceTag);
    map.LoadMap();
    map.HideDashboard(); 
    var layer = new VEShapeLayer();
    var veLayerSpec = new VEShapeSourceSpecification(VEDataType.GeoRSS, strXMLURL, layer);
    map.ImportShapeLayerData(veLayerSpec, function() {
        var numShapes = layer.GetShapeCount();
        var s, n, icon;

        for (var i = 0; i < numShapes; ++i) {
            s = layer.GetShapeByIndex(i);

            n = i + 1;
            icon = "<img src='images/orange_pushpin.png'><span class='pinText'>" + " " + n + "</span>";
            s.SetCustomIcon(icon);
        }
    }, true);
}


// This fuction serch for multiple locations based on a keyword  
function fnLocatePlaceOnMapByKeyword(argKeyword, argLati, argLong, strMapPlaceTag, intZoomFactor) {
    var objPropLoction = new VELatLong(Number(argLati), Number(argLong))
 
    map = new VEMap(strMapPlaceTag);
	
    map.LoadMap(objPropLoction, intZoomFactor, 'r', false);

   // map.LoadMap();

   // var strAddress = document.getElementById("PropHiddenAddress").value;

    map.HideDashboard();
    
      
    var pin = new VEShape(VEShapeType.Pushpin, map.GetCenter());
    pin.SetTitle(strShortSiteName);
    pin.SetDescription(strCustomNoteWindow);    
    var icon = "<img src='images/GreenArrow.gif'> ";
    
    pin.SetCustomIcon(icon);
    map.AddShape(pin);
    
    try {
        map.Find(argKeyword, objPropLoction);
    }
    catch (e) {
        alert(e.message);
    }    
}

function fnSearchMap(id) 
{

    var strKeyword = document.getElementById("txtSearchMapKey").value;

    strPropLati = document.getElementById("PropHiddenLati").value;
    strPropLong = document.getElementById("PropHiddenLong").value;
    fnLocatePlaceOnMapByKeyword(strKeyword, strPropLati, strPropLong, 'MapArea', intZoomFactor);    
}

// This function is for test purpose only - not in use anywhere
function JSTest() {
    alert(" Test");
}

function FindAddress() {    
    strAddress =  document.getElementById("FromAddress").value;   
    map.Find('', strAddress, '1', fnSingleLatiLong);
   

}
