// Initialise global variables
var map;
var depotLatLng;

$("document").ready(function() {
    geocode_x = $.trim(geocode.substring(0, geocode.indexOf(',')));
    geocode_y = $.trim(geocode.substring(geocode.indexOf(',') +1));

    // Initialise Map as a Google Maps object when we are sure we have the div on the page
    map = new GMap2(document.getElementById("google-map"));

    depotLatLng = new GLatLng(geocode_x, geocode_y);

    // Setup default map properties
    map.setCenter(depotLatLng, 13);
    map.addOverlay(new GMarker(depotLatLng));
    map.setUIToDefault();

    /**
     * Enable Traffic Overlay
     *  var trafficOptions = {incidents:true};
     *  trafficInfo = new GTrafficOverlay(trafficOptions);
     *  map.addOverlay(trafficInfo);
     */


    // We hide the directions box by default in-case JS is disabled. This re-enables is on load
    $('#get-directions').show();

    $("#postcode_button").parents('form').submit(function ()
    {
        var postcode = $('#postcode_directions').val();
        CITYLINK.OurLocations.Detail.getDirections(postcode);
        return false;
    });

    $('#loc-feature').mouseenter(function(event) {
        $('#loc-detail-toggle').fadeIn();
        event.stopImmediatePropagation();
    });

    $('#loc-feature').mouseleave(function(event) {
        $('#loc-detail-toggle').fadeOut();
        event.stopImmediatePropagation();
    });

    $('#loc-detail-toggle').click(function() {

        $('#loc-feature-detail').slideToggle(750, function() {
            if ($('#loc-feature-detail').is(':visible'))
            {
                $('#loc-detail-toggle').html('Show Full Image');
            }
            else
            {
                $('#loc-detail-toggle').html('Show Details');
            }
        });
    });

});

$('document').unload(function() {
    GUnload();
});

// Set namespace for locations
CITYLINK.OurLocations = {};

// Functions relating to detail page
CITYLINK.OurLocations.Detail = {

    // Converts a postcode into a geocode point
    postcodeToGeocode : function (postcode)
    {

        var localSearch = new google.search.LocalSearch();

        localSearch.setSearchCompleteCallback(null, function() {
            if (localSearch.results[0])
            {
                var resultLat = localSearch.results[0].lat;
                var resultLng = localSearch.results[0].lng;
                var point = resultLat + resultLng;
                var panel = document.getElementById("google-directions");
                panel.innerHTML = '';

                var directions = new GDirections(map, panel);
                directions.load("from: " + postcode + " to: " + geocode);
                
                var directionsTitle = '<h2>Driving directions from ' + postcode.toUpperCase() + ' to ' + locName + '</h2>';
                $('#google-directions').prepend(directionsTitle);
            }
            else
            {
                $("#google-directions").html('');
                $('#google-directions-error').html('Please enter a valid postcode.');
            }
        });

        localSearch.execute(postcode + ", UK");
    },

    getDirections : function(postcode)
    {
        $('#google-directions-error').html('');
        if (CITYLINK.Common.validatePostcode(postcode) == false) {
            $("#google-directions").html('');
            $('#google-directions-error').html('Please enter a valid postcode.');
            return false;
        }

        CITYLINK.OurLocations.Detail.postcodeToGeocode(postcode);

        return true;
    }
}


