﻿var map;
var Layers = new Array();
var toggleState = 0;

//var icon = new GIcon();
//icon.image = "http://www.google.com/mapfiles/marker.png";
//icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
//icon.iconSize = new GSize(20, 34);
//icon.shadowSize = new GSize(37, 34);
//icon.iconAnchor = new GPoint(10, 34);

var isUserLocated = false;
var userLatLng;
var userMarker;


function initialize() {
    $("#map_locateMeButtonContainer").hide();
    var controlForMap = document.getElementById("map_canvas");
    
    if (controlForMap != null) {
        var latlng = new google.maps.LatLng(54.52350, -1.55311);
        var mapOptions = {
            zoom: 13,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(controlForMap, mapOptions);
        
        LocateUser();
    }
}

function toggleMyKml(index) {
    if (Layers[index].getMap() != null) {
        Layers[index].setMap(null);
    }
    else {
        Layers[index].setMap(map)
    }
}

function usePointFromPostcode(postcode, zoomLevel) {
    localSearch = new GlocalSearch();
    localSearch.setSearchCompleteCallback(null,
		function() {

		    if (localSearch.results[0]) {
		        var resultLat = localSearch.results[0].lat;
		        var resultLng = localSearch.results[0].lng;
		        setCenterToPoint(new google.maps.LatLng(resultLat, resultLng), zoomLevel);
		    } else {
		        alert("Postcode not found!");
		        setCenterToPoint(new google.maps.LatLng(54.52350, -1.55311));
		    }
		});

    localSearch.execute(postcode + ", UK");
}

//function setCenterToPoint(point) {
//    // Set the default zoom level to 13.
//    setCenterToPoint(point, 13);
//}

function setCenterToPoint(point, zoomLevel) {
    if (zoomLevel == null) {
        zoomLevel = 13;
    }
    if (map != null) {
        map.setCenter(point);
        map.setZoom(zoomLevel);

        var marker = new google.maps.Marker({
            position: point
        });
        marker.setMap(map);
    }
}

function LocateUser() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(LocateUserSuccess, LocateUserError);
    }
}

function LocateUserSuccess(position) {

    if (position != null) {
        userLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude)
        userMarker = new google.maps.Marker({
            position: userLatLng,
            title: "You are here."
        });
        userMarker.setMap(map);
        isUserLocated = true;
        $("#map_locateMeButtonContainer").show();
    }
}

function LocateUserError(error) {
    switch (error.code) {
        case error.TIMEOUT:
            alert('Share Location: Timeout');
            break;
        case error.POSITION_UNAVAILABLE:
            alert('Share Location: Position unavailable');
            break;
        case error.PERMISSION_DENIED:
            alert('Share Location: Permission denied');
            break;
        case error.UNKNOWN_ERROR:
            alert('Share Location: Unknown error');
            break;
    }
}

function CenterMapToMyLocation() {
    var isUserWithinDarlingtonArea = false;

    if (userLatLng.lat() >= 54.445809 && userLatLng.lat() <= 54.625085) {
        if (userLatLng.lng() <= -1.414604 && userLatLng.lng() >= -1.674156) {
            isUserWithinDarlingtonArea = true;
        }
    }

    if (isUserWithinDarlingtonArea == false) {
        var confirmed = window.confirm("You appear to be outide Darlington Borough Council's boundaries. Do you want to display your location anyway?");
        if (confirmed) {
            DisplayUserLocation();
        }
    }
    else {
        DisplayUserLocation();
    }
}

function DisplayUserLocation() {
    var controlForMap = document.getElementById("map_canvas");
    var mapOptions = {
        zoom: 13,
        center: userLatLng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(controlForMap, mapOptions);

    userMarker = new google.maps.Marker({
        position: userLatLng,
        title: "You are here."
    });
    userMarker.setMap(map);

    ClearOverlayList();
}

function ClearOverlayList() {

    $('input:checkbox').each(function() {
        if (this.id.indexOf('chkOverlays') > 0) {
            this.checked = false;
        }
    });
}

function DisplayGeoHelp() {
    newwindow = window.open("http://darlington.waterstons.com/Darlington.Net2.Web/CustomPages/GeoLocationHelp.htm", "GeoLocation", "height=350,width=310,scrollbars=yes,resizable=no,toolbars=no,menubar=no,location=no");
}
