﻿
//autofill
function autoFill(id, v) {

    if ($(id).val() == "" || $(id).val() == v) {
        $(id).css({ color: "#b2adad" }).attr({ value: v }).focus(function() {
            if ($(this).val() == v) {
                $(this).val("").css({ color: "#333" });
            }
        }).blur(function() {
            if ($(this).val() == "") {
                $(this).css({ color: "#b2adad" }).val(v);
            }

        });
    }

}


//querystring
location.querystring = (function() {

    // The return is a collection of key/value pairs
    var queryStringDictionary = {};

    // Gets the query string, starts with '?'
    var querystring = decodeURI(location.search);

    // document.location.search is empty if no query string
    if (!querystring) {
        return {};
    }

    // Remove the '?' via substring(1)
    querystring = querystring.substring(1);

    // '&' seperates key/value pairs
    var pairs = querystring.split("&");

    // Load the key/values of the return collection
    for (var i = 0; i < pairs.length; i++) {
        var keyValuePair = pairs[i].split("=");
        queryStringDictionary[keyValuePair[0]]
                        = keyValuePair[1];
    }

    // toString() returns the key/value pairs concatenated
    queryStringDictionary.toString = function() {

        if (queryStringDictionary.length == 0) {
            return "";
        }

        var toString = "?";

        for (var key in queryStringDictionary) {
            toString += key + "=" +
                        queryStringDictionary[key];
        }

        return toString;
    };

    // Return the key/value dictionary

    return queryStringDictionary;
})();

//z-index
$.maxZIndex = $.fn.maxZIndex = function(opt) {
    /// <summary>
    /// Returns the max zOrder in the document (no parameter)
    /// Sets max zOrder by passing a non-zero number
    /// which gets added to the highest zOrder.
    /// </summary>    
    /// <param name="opt" type="object">
    /// inc: increment value, 
    /// group: selector for zIndex elements to find max for
    /// </param>
    /// <returns type="jQuery" />
    var def = { inc: 10, group: "*" };
    $.extend(def, opt);
    var zmax = 0;
    $(def.group).each(function() {
        var cur = parseInt($(this).css('z-index'));
        zmax = cur > zmax ? cur : zmax;
    });
    if (!this.jquery)
        return zmax;

    return this.each(function() {
        zmax += def.inc;
        $(this).css("z-index", zmax);
    });
}

//global Jquery
$(document).ready(function() {

    autoFill($("#KeyWord"), "Search");
    autoFill($("#Keyword"), "Key Word");

    

    $('.datepicker').datepicker({ beforeShow: function() { $('#ui-datepicker-div').maxZIndex(); },
        dateFormat: 'm/d/yy'
    });

    $(".zebra > tbody > tr:even").addClass("even");

    $('.confirm').click(function() {
        var answer = confirm('Are you sure you want to delete this ' + $(this).attr('title'));
        return answer;
    });

    $('.confirmMessage').click(function() {
        var answer = confirm($(this).attr('title'));
        return answer;
    });

    // Dialogs			
    $('#dialog').dialog({
        autoOpen: false,
        width: 800,
        buttons: {

            "Close": function() { 
		        $(this).dialog("close"); 
	        } 
        }
    });

    $('#dialog_map').dialog({
        autoOpen: false,
        width: 700,
        buttons: {

            "Close": function() {
                $(this).dialog("close");
            }
        }
    });

    // Dialog Link
    $('#dialog_link').click(function() {
        $('#dialog').dialog('open');
        return false;
    });

    $('#dialog_map_link').click(function() {
        $('#dialog_map').dialog('open');
        return false;
    });

    $('a[title]').qtip({ style: { name: 'light', tip: true} })

});