﻿var MapElementId = "map_canvas";
var services = [];
services.push(1);

function initialize() {
    var doc = $(document);
    $("div#map_canvas").height(doc.height()-36);
    Getlocation();
    var popup = $("div#Directions");
    $('#IAgree').bind('click', function () { $.cookie('agreed', true); $("div#Directions").hide(); });

    var agreed = $.cookie('agreed');
    if (agreed != 'true') {
        popup.css('position', 'absolute')
            .css('top', (doc.height() / 2) - (popup.height() / 2))
            .css('left', (doc.width() / 2) - (popup.width() / 2))
            .css('background-color', '#ffffff')
            .css('padding', '10px')
            .show();
    }
}

function GetCurrentServices() {
    return services;
}

function Getlocation() {
    var lat = $.cookie('Lat');
    var lng = $.cookie('Lng');
    if (lat != null & lng != null) {
        SetMap(new google.maps.LatLng(lat, lng), 8, GetCurrentServices, MapElementId);
        locationSet = true;
    } else {
        var geocoder = new google.maps.Geocoder();
        var address = $("#AdminAreaName").val();
        if(address == "Washington") {
            address += " state";
        }
        geocoder.geocode({ address: address, latLng: null, bounds: null, region: null }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                SelectResult(0, results);
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });
    }
}

function SelectResult(index, results) {
    //Clear markers set a marker in tiny map and set maps center to it.
    SetMap(results[index].geometry.location, 8, GetCurrentServices, MapElementId);
}

$().ready(function () {
    initialize();
});
