﻿var MapElementId = "map_canvas";
var DefaultLatLng = new google.maps.LatLng(39.5, -98.35);
var possibleMatches;
var locationSet = false;

function initialize() {
    SetDefaultMap();

    var popup = $("div#Directions");
    $('#IAgree').bind('click', function () { $.cookie('agreed', true); $("div#Directions").hide(); });

    var doc = $(document);
    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 SetDefaultMap() {
    var lat = $.cookie('Lat');
    var lng = $.cookie('Lng');
    var zoom = $.cookie('Zoom');
    
    if(lat != null & lng != null) {
        SetMap(new google.maps.LatLng(lat, lng), zoom, GetCurrentServices, MapElementId);
        var services = $.cookie('Services');
        if(services != null) {
            services = JSON.parse(services);
            $('input[name="ServiceFilter"]:checked').removeAttr('checked');
            $(services).each(function() {
                $('input#' + this).attr('checked','checked');
            });

            var checkedCount =  $('input[name="ServiceFilter"]:checked').length;
            var count = $('input[name="ServiceFilter"]').length;
            if(checkedCount < count) {
                $('input#CheckAllServices').removeAttr('checked');    
            }
        }
        locationSet = true;
    } else {
        SetMap(DefaultLatLng, 8, GetCurrentServices, MapElementId);    
    }
    
}

function Getlocation() {
    var where = $("#SearchLocation").val();
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ address: where, latLng: null, bounds: null, region: null }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            //Remove results that are not RangeInterpolated or Rooftop.
            if (results.length < 1) {
                $("#ChooseWhich").hide();
                $("#FoundMatch").hide();
                $("#NoMatch").show();
            } else {
                if (results.length > 1) {
                    //If results greater than one display options.
                    $("#ChooseWhich").show();
                    $("#FoundMatch").hide();
                    $("#NoMatch").hide();
                    $("#ChooseResults").empty();
                    $.each(results, function (index) {
                        //Show dups
                        $("#ChooseResults").append("<div><input type=\"radio\" name=\"Choice\" id=\"Choice\" onclick=\"SelectResult(" + index + ")\">" + this.formatted_address + "</div>");
                    });
                    possibleMatches = results;
                } else {
                    $("#ChooseWhich").hide();
                    $("#FoundMatch").show();
                    $("#NoMatch").hide();

                    //Otherwise fill in fields with the data and enable submission.
                    SelectResult(0, results);
                }
            }
        } else {
            $("#ChooseWhich").hide();
            $("#FoundMatch").hide();
            $("#NoMatch").show();
        }
    });
}

function SelectResult(index, results) {
    if(results == null) {
        results = possibleMatches;
    }
    $("#ChooseWhich").hide();
    locationSet = true;
    SetMap(results[index].geometry.location, 10, GetCurrentServices, MapElementId);
    GetLocations(GetCurrentServices);
}

function GetCurrentServices() {
    var services = [];
    $('input[name="ServiceFilter"]:checked').each(function () {
        services.push(this.id);
    });
    return services;
}

$().ready(function () {
    $('#Search').bind('click', function () { Getlocation(); });
    $('input#CheckAllServices').bind('change', function () {
        var checked = $('input#CheckAllServices').attr('checked');
        if (checked != null) {
            $('input[name="ServiceFilter"]').attr('checked', 'checked');
        } else {
            $('input[name="ServiceFilter"]:checked').removeAttr('checked');
        }
        locationSet = true;
        GetLocations(GetCurrentServices);
    });
    $('input[name="ServiceFilter"]').bind('change', function () {
        if (locationSet) {
            locationSet = true;
            GetLocations(GetCurrentServices);
        }
    });
    initialize();
});
