﻿var localSearch = new GlocalSearch();

function getDeviceCurrentPosition() {
    if (navigator.geolocation != null) {
        navigator.geolocation.getCurrentPosition(getDeviceCurrentPositionFoundCallback, null);
    }
    else {
        var loki = new LokiAPI();
        initialiseLoki(loki);
        loki.requestLocation(true, loki.NO_STREET_ADDRESS_LOOKUP); 
    }
}

function getDeviceCurrentPositionFoundCallback(position) {
    getStreetAddressForLatLng(position.coords.latitude, position.coords.longitude, setStartLocationAndCreateGoogleMap);
}

function getDeviceCurrentPositionFoundLokiCallback(location) {
    getStreetAddressForLatLng(location.latitude, location.longitude, setStartLocationAndCreateGoogleMap);
}

function setStartLocationAndCreateGoogleMap(street, city, region, country) {
        $('input#fromAddress').val(street + ', ' + city + ', ' + region + ', ' + country);
        $('input#directionsButton').attr('disabled', '');
        createGoogleMap();
};

function getLatLngForPostcode(postcode, callback) {
    localSearch.setSearchCompleteCallback(null,
        function() {
            var firstResult = localSearch.results[0];

            if (firstResult) {
                callback(new GLatLng(firstResult.lat, firstResult.lng));
            }
        });

    localSearch.execute(postcode + ", UK");
}

function getStreetAddressForLatLng(lat, lng, callback) {
    localSearch.setSearchCompleteCallback(null,
        function() {
            var firstResult = localSearch.results[0];

            if (firstResult) {
                callback(firstResult.streetAddress, firstResult.city, firstResult.region, firstResult.country);
            }
        });

    localSearch.execute(new GLatLng(lat, lng));
}

function createGoogleMap() {
    if ($('div#map').length == 0 || !$('div#map').googlemap)
        return;

    $('div#directions').empty().hide();

    $('input#printDirections').hide();
        
    $('div#map').googlemap({
        controls: true,
        labels: true,
        latitude: parseFloat(destinationLatitude),
        longitude: parseFloat(destinationLongitude),
        zoom: 16,
        markerAtCenter: true,
        directions: $('div#directions')[0],
        from: $('input#fromAddress').val(),
        width: 635,
        height: 400
    });

    $("div#map").removeData('googlemap');

    if ($('input#fromAddress').val().length > 0) {
        $('div#directions').show();
        $('input#printDirections').show();
        $.scrollTo( $('div#directions'), 800 );
    }
    
    $("div#map").hide().show();
}

function initialiseLoki(loki) {
    loki.onSuccess = getDeviceCurrentPositionFoundLokiCallback;
    loki.setKey('118.com');
}

function initialiseGoogleMap() {
    if ((destinationLatitude != 0) && (destinationLongitude != 0)) {
        createGoogleMap();
    }
    else {
        if ((destinationPostcode != null) && (destinationPostcode.length > 0)) {
            getLatLngForPostcode(destinationPostcode, function(point) {
                destinationLatitude = point.lat();
                destinationLongitude = point.lng();
                createGoogleMap();
            });
        }
    }
}

function printDirections() {
    $('div#directions').printElement();   
}

$(document).ready(function() {
    if ($('input#fromAddress').val().length == 0) {
        $('input#directionsButton')
            .attr('disabled', 'disabled');
    }

    $('input#fromAddress')
        .bind('keyup', function() {
            if ($(this).val().length > 0) {
                $('input#directionsButton').attr('disabled', '');
            }
            else {
                $('input#directionsButton').attr('disabled', 'disabled');
            }
        });

    $('input#directionsButton')
        .bind('click', function() {
            createGoogleMap();
        }
    );

    $('input#fromAddress').length > 0
    {
        initialiseGoogleMap();
    }
});
