﻿/// <reference path="jquery-1.3.2-vsdoc.js" />
var geocoder;
var searchReady;

$(function () {
    //all hover and click logic for buttons
    $("a.fg-button:not(.ui-state-disabled)")
        .live("mouseover", function () {
            $(this).addClass("ui-state-hover");
        })
        .live("mouseout", function () {
            $(this).removeClass("ui-state-hover");
        })
		.live("mousedown", function () {
		    $(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
		    if ($(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active')) { $(this).removeClass("ui-state-active"); }
		    else { $(this).addClass("ui-state-active"); }
		})
		.live("mouseup", function () {
		    if (!$(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button, .fg-buttonset-multi .fg-button')) {
		        $(this).removeClass("ui-state-active");
		    }
		});

    searchReady = false;
    geocoder = new google.maps.Geocoder();
    $(".searchForm").submit(searchFormSubmit);
    $(".searchButton").click(function () {
        $(".searchForm").submit();
        return false;
    });

    $("input[type='submit'],input[type='reset'],button").button();
});

function searchFormSubmit() {
    if (searchReady) {
        searchReady = false;
        return true;
    }
    $.blockUI(); 
    var address = ""; // $.trim($("#searchForm input[name='address']").val())
    var city = $.trim($(".searchForm input[name='cityStateZip']").val());
    if (address.length > 0 && city.length > 0) {
        address = address + ", " + city;
    } else if (city.length > 0) {
        address = city;
    }
    geocoder.geocode({ 'address': address }, getGeocodeCallback);
    return false;
}

function getGeocodeCallback(results, status) {
    var location = undefined;
    if (status == google.maps.GeocoderStatus.OK
            //&& results.length == 1
            //&& (results[0].geometry.location_type == google.maps.GeocoderLocationType.APPROXIMATE
            //    || results[0].geometry.location_type == google.maps.GeocoderLocationType.ROOFTOP)
            ) 
    {
        location = results[0].geometry.location;
    }
    if (location != undefined) {
        $(".searchForm input[name='latitude']").val(location.lat());
        $(".searchForm input[name='longitude']").val(location.lng());
    } else {
        $(".searchForm input[name='latitude']").val("");
        $(".searchForm input[name='longitude']").val("");
    }
    searchReady = true;
    $(".searchForm").submit();
}

(function($) {
    $.fn.styleTable = function() {
        this.each(function() {
            $("thead th", this).addClass("ui-state-default").first().addClass("ui-corner-tl");
            $("thead th", this).last().addClass("ui-corner-tr");
            $("thead th.sortable", this)
                .hover(
			        function () {
			            $(this).addClass("ui-state-hover");
			        },
			        function () {
			            $(this).removeClass("ui-state-hover");
			        })
                .mousedown(function(){
			        $(this).addClass("ui-state-active");
                })
                .mouseup(function(){
			        $(this).removeClass("ui-state-active");
                })                
                .click(
                    function(){
                        $.blockUI();
                        document.location.href = $("a", this).attr("href");
                    }
                );
            $(this).addClass("ui-widget-content ui-corner-all");            
        });

        return this;
    };
    $.fn.removeButton = function() {
        this.die().css("cursor", "default").click(function(){
            $(this).addClass("ui-state-active");
            return false;
        }).dblclick(function(){
            $(this).addClass("ui-state-active");
            return false;
        });
        return this;
    };
    $.fn.ajaxDialog = function(url, options) {
        var defaults = {
            open: function(event, ui) { 
                $(this).load(url);
            }
        };
        var opts = $.extend(defaults, options);
        this.dialog(opts);
        return this;
    }
})(jQuery);

String.prototype.startsWith = function (str) {
    return (this.match("^" + str) == str)
}