//<!--
var container = null;

function InitialiseMap()
{
    container = document.getElementById("MapHolder");
    if (container != null)
    {
        MakeContainer();
        GetMap();
    }
}

function MakeContainer()
{
    container.innerHTML += '\n<div id="MapWrap">';
    container.innerHTML += '\n\t<div id="LocationMap" style="width:478px; height:300px;">&nbsp;</div><br />';
    container.innerHTML += '\n\t<div id="Route">';
    container.innerHTML += '\n\t\t<label for="StartPCode">Please enter your starting point here (postcodes must be have the space in the middle)</label><br />';
    container.innerHTML += '\n\t\t<input type="text" id="StartPCode" name="StartPCode" class="textInput" value="enter your postcode or town here" onfocus="this.value=\'\';" onkeydown="javascript:return DisableSubmit(event);" />';
    container.innerHTML += '\n\t\t<input id="GetDirections" type="submit" value="Get directions" onclick="GetRoute(); return false;" class="submitButton" />';
    container.innerHTML += '\n\t</div>';
    container.innerHTML += '\n\t<div id="Directions"></div>';
    container.innerHTML += '\n</div>';
}

function GetMap()
{
    locationMap = new VEMap('LocationMap');
    locationMap.LoadMap();
    locationMap.HideDashboard();
    locationMap.ClearInfoBoxStyles();
    FindLoc();
}   

function FindLoc()
{
    locationLayer = new VEShapeLayer();
    locationMap.AddShapeLayer(locationLayer);
    locationMap.ShowMessageBox = false;

    locationMap.Find(null, mapPostcode + " uk", null, locationLayer, null, null, null, null, false, true, AddPostcodePin);
}

function AddPostcodePin(layer, resultsArray, places, hasMore, veErrorMessage)
{
    var pin = new VEShape(VEShapeType.Pushpin, places[0].LatLong);
    pin.SetCustomIcon(icon);
    pin.SetTitle("<div class='boxTitle'>" + officeName + "</div>");
    pin.SetDescription(infoBox);

    locationMap.SetCenter(places[0].LatLong);
    locationMap.SetZoomLevel(mapZoom);
    locationLayer.AddShape(pin);
    
    document.getElementById('MapWrap').style.display = "block";
}

function GetRoute()
{
    var from = document.getElementById('StartPCode').value;
    if (from == 'enter your postcode or town here' || from.trim() == '')
    {
        alert('Please enter your postcode');
        return false;
    } 
    else
    {
        var locations = new Array(from, mapPostcode);
        var options = new VERouteOptions();

        options.DrawRoute = true;
        options.RouteCallback = onGotRoute;
        options.RouteColor = new VEColor(0, 92, 169, 0.8) //rgb,transparency
        locationMap.GetDirections(locations,options);
    }
}

function onGotRoute(route)
{
    // Unroll route
    if (route == null)
    {
        alert("Sorry no route can be found for your postcode");
    }
    else
    {
        var legs = route.RouteLegs;
        var turns = "<br />\n<p><strong>Your route to " + officeName + " is " + route.Distance.toFixed(1) + " miles</strong></p>";
        turns += "\n<ol id=\"DirectionList\">";
        var numTurns = 0;
        var leg = null;

        // 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

            // 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++;
                turns += "\n\t<li>" + turn.Text + " (" + turn.Distance.toFixed(1) + " miles)</li>";
            }
        }
    
        turns += "\n</ol><br />";
        turns += "\n<p>You have arrived at " + officeName + ".</p>";
        document.getElementById('Directions').innerHTML = turns;
    }
}

function DisableSubmit(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if(charCode == "13")
    {
        return false;
    }
}
//-->

