    var map = null;
    var BaseShapeLayer = null;
    var mapCenter = new VELatLong(53.808760650677456, -1.510620117187513);
    var mapZoom = 6;
    var Locations = new Array();
    var PostcodeLocations = new Array();
    var LongLatLocations = new Array();
    var CurrentLocation = 0;
    var CurrentShape = null;
    var PinCenter = null;
    var Timer = null;
    var CurrentLocationAttempts = 0;
    
    function LoadMap()
    {
        map = new VEMap('FindTArmstrongMap');
        map.LoadMap(mapCenter, mapZoom);        
        BaseShapeLayer = new VEShapeLayer();
        map.AddShapeLayer(BaseShapeLayer);
        map.AttachEvent("onstartpan", dontMove);

    }
    
    function AddLocationFromPostcode(index, title, description, postcode, id)
    {
        if(postcode != null && postcode != undefined)
        {
            PostcodeLocations[index] = new Array(title, description, postcode, id);
        }
    }

    function AddLocationFromLongLat(index, title, description, id, longitude, latitude)
    {
        LongLatLocations[index] = new Array(title, description, id, longitude, latitude);
    }
    
    function dontMove(e)
    {
        map.EndContinuousPan();
    }
    
    function AddLocation(index, title, description, postcode, id)
    {
        if(postcode != null && postcode != undefined)
        {
            PostcodeLocations[index] = new Array(title, description, postcode, id);
        }
    }
    
    function LoadLocation(postcode)
    {
        
        if(PostcodeLocations.length > 0 && postcode == null)
        {
        CurrentLocationAttempts = 1
        results = map.Find('',
            PostcodeLocations[CurrentLocation][2],
            null,
            null,
            null,
            null,
            true,
            true,
            true,
            true,
            AddPostcodePin);
        }
        else if(PostcodeLocations.length > 0)
        {
            results = map.Find('',
            postcode,
            null,
            null,
            null,
            null,
            true,
            true,
            true,
            true,
            AddPostcodePin);
        }
    }
    
    function LoadLongLatLocations()
    {
        for(var i = 0; i < LongLatLocations.length; i ++)
        {
            var tmpLong = parseFloat(LongLatLocations[i][2]);
            var tmpLat = parseFloat(LongLatLocations[i][3]);
            var LL = new VELatLong(tmpLat,tmpLong);
            var pin = new VEShape(VEShapeType.Pushpin, LL);
            
            var icon = new VECustomIconSpecification();

            icon.Image = '/images/presentation/armstrong-marker.gif';
            pin.SetCustomIcon(icon);
            pin.SetTitle('<h4 class="mapTitle">'+LongLatLocations[i][0]+'</h4>');
            pin.SetDescription('<div class="mapInfoBox">'+LongLatLocations[i][1]+'<div class="clear">&nbsp;</div></div>');
            BaseShapeLayer.AddShape(pin);
            
        }
    }
    
     function AddPostcodePin(layer, resultsArray, places, hasMore, veErrorMessage)
     {
        shortenedPostcode = null;
         CurrentLocationAttempts++;
         if(places != null)
         {
            var pin = new VEShape(VEShapeType.Pushpin, places[0].LatLong);
            pin.SetCustomIcon('<img src="/images/presentation/armstrong-marker.gif" />');
            pin.SetTitle('<h4 class="mapTitle">'+PostcodeLocations[CurrentLocation][0]+'</h4>');
            pin.SetDescription('<div class="mapInfoBox">'+PostcodeLocations[CurrentLocation][1]+'<div class="clear">&nbsp;</div></div>');
            BaseShapeLayer.AddShape(pin);           
            var path = '/plugins/LocationMap/LocationsAjax.php?savelocation=true&dsid=7&id='+PostcodeLocations[CurrentLocation][3]+'&long='+places[0].LatLong.Longitude+'&lat='+places[0].LatLong.Latitude;
            var AjaxLoader = new Ajax(null, path);
            PostcodeLocations[CurrentLocation] = pin.GetID();
            CurrentLocation ++;
        }
        else if(resultsArray == null)
        {   
            if(CurrentLocationAttempts > 2)
            {
                var path = '/plugins/LocationMap/LocationsAjax.php?savelocation=true&dsid=7&id='+PostcodeLocations[CurrentLocation][3]+'&long=unknown&lat=unknown';
                var AjaxLoader = new Ajax(null, path);
            
             //if 2nd try doesnt work, set current array item to null

                PostcodeLocations[CurrentLocation] = null;
            }
            else
            {
                fullPostcode = PostcodeLocations[CurrentLocation][2];
                postcodeLength = fullPostcode.length;
                shortenedPostcode = fullPostcode.substring(0, postcodeLength-2);
            }
            
        }
        if(CurrentLocation < PostcodeLocations.length)
        {

            LoadLocation(shortenedPostcode);
        }
     } 
    
    function HighlightPostcodeMapLocation(markerId)
    {
        var id = PostcodeLocations[markerId];
        if(id != null)
        {
            CurrentShape = BaseShapeLayer.GetShapeByIndex(markerId);
            map.ShowInfoBox(CurrentShape);
       } 
    }
    
    function HighlightMapLocation(markerId){
        //Update pushpin
        CurrentShape = BaseShapeLayer.GetShapeByIndex(markerId);
        PinCenter = new VELatLong(CurrentShape.Latitude, CurrentShape.Longitude);
        map.AttachEvent("onendpan", ShowInfo);
        map.SetCenter(PinCenter);
        map.ShowInfoBox(CurrentShape);    
    }

    function HidePostcodeMapLocation(markerId)
    {
        var id = PostcodeLocations[markerId];
        if(id != null)
        {
            CurrentShape = BaseShapeLayer.GetShapeByIndex(markerId);
            map.HideInfoBox(CurrentShape);
        }
    }
    
    function HighlightLongLatMapLocation(markerId)
    {
        var id = LongLatLocations[markerId];
        if(id != null)
        {
            CurrentShape = BaseShapeLayer.GetShapeByIndex(markerId);
            map.ShowInfoBox(CurrentShape);   
        }
    }

    function HideLongLatMapLocation(markerId){
        var id = LongLatLocations[markerId];
        if(id != null)
        {
            CurrentShape = BaseShapeLayer.GetShapeByIndex(markerId);
            map.HideInfoBox(CurrentShape);
        }
    }
    
    function HideMapLocation(markerId){
        //Update pushpin
        CurrentShape = BaseShapeLayer.GetShapeByIndex(markerId);
        map.HideInfoBox(CurrentShape);
    }
    
    function ShowInfo()
    {
        if(map.GetCenter == PinCenter)
        {
            map.ShowInfoBox(CurrentShape);  
            clearInterval(Timer);
        }
        else
        {
            Timer = setInterval(ShowInfo, 500);
        }
    }
    
    function ShowDetails(contentId, markerId)
    {
            CurrentShape = BaseShapeLayer.GetShapeByIndex(markerId);
            PinCenter = new VELatLong(CurrentShape.Latitude, CurrentShape.Longitude);
            map.SetCenter(PinCenter);  
            map.SetZoomLevel(9);
            var path = '/plugins/LocationMap/LocationsAjax.php?showdetails=' + contentId;
            var AjaxLoader = new Ajax('LocationDetails', path);
    }
   
    function SetStartLocation(startId)
    {
        for(var i = 0; i < LongLatLocations.length; i ++)
        {
        
            if(LongLatLocations[i][4] == startId)
            {
                ShowDetails(LongLatLocations[i][4],i);
                break;
            }
        }
        
        for(var i = 0; i < PostcodeLocations.length; i ++)
        {
            if(PostcodeLocations[i][3] == startId)
            {
                ShowDetails(PostcodeLocations[i][3],i);
                break;
            }
        }
    }
    
    function setZoom()
     {
        map.SetZoomLevel(6);
     }

    
    $(document).ready(function(){
				LoadMap();
				LoadLocations();		
    });



