var map = null;
var BaseShapeLayer = null;
var mapCenter = new VELatLong(53.983562849183826, -1.4992421865463204);
var mapZoom = 15;
var PostcodeLocations = new Array();
var LongLatLocations = new Array();
var CurrentLocation = 0;
var CurrentShape = null;
var PinCenter = null;
var Timer = null;

function StartBingMap()
{
	LoadMap();
	var description = '<div class="address"><strong>' + PinAddress + ', <span class="postcode">' + (PinPostcode != PostcodeCentre ? PinPostcode : PostcodeCentre) + '</span></strong></div>';
	
	if (PinTelephone != null && PinTelephone != '')
	{
	    description += '<div class="telephone"><strong>Telephone. </strong> <span class="detail">' + PinTelephone + '</span></div>';
	}
	
	if (PinFax != null && PinFax != '')
	{
	    description += '<div class="fax"><strong>Fax </strong> <span class="detail">' + PinFax + '</span></div>';
	}

	if (PinPostcode != null && PinPostcode != PostcodeCentre)
	{
	    description += '<div class="satNav"><strong>Sat Nav. </strong> <span class="detail">' + PostcodeCentre + '</span></div>'
	}
	
    AddLocationFromLongLat(0, PinTitle, description, 0, Longitude, Latitude);
    LoadLongLatLocations();
}

function LoadMap()
{
    map = new VEMap('MapWrapper');
    map.LoadMap(mapCenter, mapZoom);
    BaseShapeLayer = new VEShapeLayer();
    map.AddShapeLayer(BaseShapeLayer);
}


function AddLocationFromLongLat(index, title, description, id, longitude, latitude)
{
    LongLatLocations[index] = new Array(title, description, id, longitude, latitude);
}

function LoadLongLatLocations()
{
    for(var i = 0; i < LongLatLocations.length; i ++)
    {
        var tmpLong = parseFloat(LongLatLocations[i][3]);
        var tmpLat = parseFloat(LongLatLocations[i][4]);
        var LL = new VELatLong(tmpLat,tmpLong);
        var pin = new VEShape(VEShapeType.Pushpin, LL);

        var icon = '';
        var changeMapCentre = true;
        
        pin.SetCustomIcon('<img src="/images/MapIcon.gif" />');
        pin.SetTitle('<h3 class="mapTitle">'+LongLatLocations[i][0]+'</h3>');
        pin.SetDescription('<div class="mapInfoBox">'+LongLatLocations[i][1]+'</div>');
        BaseShapeLayer.AddShape(pin);
    }
}  

function HighlightMapLocation(markerId)
{
    CurrentShape = BaseShapeLayer.GetShapeByIndex(markerId);
    map.ShowInfoBox(CurrentShape);    
}

function HideMapLocation(markerId)
{
    CurrentShape = BaseShapeLayer.GetShapeByIndex(markerId);
    map.HideInfoBox(CurrentShape);
}

function GetRoute()
{
    var fromInput = document.getElementById("FromPostcode");
    var fromPostcode = '';
    if (fromInput != null)
    {
        fromPostcode = fromInput.value;
    }
    
    var toPostcode = PostcodeCentre;
    var errorMessage = '';
    
    if (fromPostcode == '')
    {
        errorMessage += 'Please enter your postcode\n';
    }
    else if (TestPostcode(fromPostcode) == false)
    {
        errorMessage += 'Please enter your postcode with the space in the middle - eg HG3 3AY\n';
    }
    
    if (errorMessage != '')
    {
        alert(errorMessage);
    }
    else
    {
        var locations = new Array(fromPostcode, toPostcode);
        var options = new VERouteOptions();
        options.DrawRoute = true;
        options.RouteCallback = onGotRoute;
        options.RouteColor = new VEColor(0, 92, 169, 0.8) //rgb,transparency
        map.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 " + PinTitle + " 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><strong>You have arrived at " + PinTitle + ".</strong></p>";
        document.getElementById('Directions').innerHTML = turns;
    }
}

function TestPostcode(postcode)
{
    var split = postcode.split(" ");
    if (split.length == 2)
    {
        return true;
    }
    else
    {
        return false;
    }
}

/* END - CALLED DIRECTLY FROM MAP APP */

