
var map = null;
var BaseShapeLayer = null;
var mapCenter = new VELatLong(53.808760650677456, -1.510620117187513);
var mapZoom = 6;
var PostcodeLocations = new Array();
var LongLatLocations = new Array();
var CurrentLocation = 0;
var CurrentShape = null;
var PinCenter = null;
var Timer = null;
var innName = '';

function LoadMap()
{
    map = new VEMap('MapWrapper');
    map.LoadMap(mapCenter, mapZoom);
    BaseShapeLayer = new VEShapeLayer();
    map.AddShapeLayer(BaseShapeLayer);
}

function AddLocationFromPostcode(index, title, description, id, postcode, isInn)
{    
    if(postcode != null && postcode != undefined)
    {
        PostcodeLocations[index] = new Array(title, description, id, postcode, isInn);
    }
}

function AddLocationFromLongLat(index, title, description, id, longitude, latitude, isInn)
{
    LongLatLocations[index] = new Array(title, description, id, longitude, latitude, isInn);
}

function LoadLocation()
{
    if(PostcodeLocations.length > 0)
    {
        results = map.Find('',
            PostcodeLocations[CurrentLocation][3],
            null,
            null,
            null,
            null,
            true,
            true,
            true,
            true,
            AddPostcodePin);
    }
}

function LoadLongLatLocations()
{
    var newMapCentre;
    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 = false;
        if (LongLatLocations[i][5] == true)
        {
            icon = 'Inn';
            if (i == 0)
            {
                newMapCentre = LL;
            }
        }
        else
        {
            icon = 'POI';
            changeMapCentre = true;
        }
        
        pin.SetCustomIcon('<img src="/SiteImages/MapIcon_' + icon + '.gif" />');
        pin.SetTitle('<h3 class="mapTitle">'+LongLatLocations[i][0]+'</h3>');
        pin.SetDescription('<div class="mapInfoBox">'+LongLatLocations[i][1]+'</div>');
        BaseShapeLayer.AddShape(pin);
    }
    
    if (changeMapCentre == true)
    {
        var points = [newMapCentre];
        map.SetMapView(points);
        mapZoom = 9;
    }
}


function AddPostcodePin(layer, resultsArray, places, hasMore, veErrorMessage)
{
    if(places != null)
    {
        var pin = new VEShape(VEShapeType.Pushpin, places[0].LatLong);
        var icon = (PostcodeLocations[CurrentLocation][4] == true ? 'Inn' : 'POI');
        pin.SetCustomIcon('<img src="/SiteImages/MapIcon_' + icon + '.gif" />');
        pin.SetTitle('<h3 class="mapTitle">'+PostcodeLocations[CurrentLocation][0]+'</h3>');
        pin.SetDescription('<div class="mapInfoBox">'+PostcodeLocations[CurrentLocation][1]+'</div>');
        BaseShapeLayer.AddShape(pin);

        var path = '/InteractiveMapHandler.ashx?Longitude='+places[0].LatLong.Longitude+'&Latitude='+places[0].LatLong.Latitude+'&'+ (PostcodeLocations[CurrentLocation][4] == true ? 'InnId' : 'PoiId')+'='+PostcodeLocations[CurrentLocation][2];;
        var AjaxLoader = new Ajax(null, path);

        CurrentLocation ++;
    }

    if(CurrentLocation < PostcodeLocations.length)
    {
        LoadLocation();
    }
}    

function HighlightMapLocation(markerId)
{
    CurrentShape = BaseShapeLayer.GetShapeByIndex(markerId);
    map.ShowInfoBox(CurrentShape);    
}

function HideMapLocation(markerId)
{
    CurrentShape = BaseShapeLayer.GetShapeByIndex(markerId);
    map.HideInfoBox(CurrentShape);
}

function GetRoute(toId, fromId, name)
{
    var fromPostcode = document.getElementById(fromId).value;
    var toSelect = document.getElementById(toId);
    var toPostcode = toSelect.options[toSelect.selectedIndex].value;
    var errorMessage = '';
    innName = toSelect.options[toSelect.selectedIndex].text
    
    if (toPostcode.trim() == '')
    {
        errorMessage += 'Please select an inn\n';
    } 
    
    if (fromPostcode.trim() == '')
    {
        errorMessage += 'Please enter your postcode\n';
    }
    else if (TestPostcode(fromPostcode.trim()) == 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 " + innName + " 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 " + innName + ".</strong></p>";
        document.getElementById('Directions').innerHTML = turns;
    }
}

function TestPostcode(postcode)
{
    var split = postcode.split(" ");
    if (split.length == 2)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function MapInit()
{
	LoadMap();
	LoadLocations();
    map.SetZoomLevel(mapZoom); 
}

window.load = function()
{
	LoadMap();
	LoadLocations();
    map.SetZoomLevel(mapZoom); 
}
/* END - CALLED DIRECTLY FROM MAP APP */
