/* UI functions */


function sitepostback() {
    /* DOM ready */
    $(function () {

        uiaddclasses();

        mainmenu();

        uisetinteraction();

        // Default dialog in master
        $('#divdialog').dialog({
            autoOpen: false,
            buttons: { "Ok": function () { $(this).dialog("close"); } },
            modal: true,
            width: 550
        });

        $('.tooltip').hover(
					function () { $(this).addClass('ui-state-hover'); },
					function () { $(this).removeClass('ui-state-hover'); }
				);

        $('.tooltip').each(function () {
            $(this).qtip({
                content: $(this).attr('tooltip'),
                style: 'dark'
            });
        });

    });
}

sitepostback();

jQuery.validator.addMethod("phoneUS", function (phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, "");

    $('#' + element.id).val(phone_number.replace(/[\(\)\s+-]/g, ""));

    return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");

function val_errorPlacement(error, element) {
    if (error.html() != '') {
        element.parent("td").next("td").children("span").removeClass("ui-icon-arrow-1-e");
        element.parent("td").next("td").children("span").removeClass("ui-icon-check");
        element.parent("td").next("td").children("span").addClass("ui-icon");
        element.parent("td").next("td").children("span").addClass("ui-icon-close");
    } else {
        element.parent("td").next("td").children("span").removeClass("ui-icon-arrow-1-e");
        element.parent("td").next("td").children("span").removeClass("ui-icon-close");
        element.parent("td").next("td").children("span").addClass("ui-icon");
        element.parent("td").next("td").children("span").addClass("ui-icon-check");
    }
}

function val_success(element) {
    element.parent("td").next("td").children("span").removeClass("ui-icon-arrow-1-e");
    element.parent("td").next("td").children("span").removeClass("ui-icon-close");
    element.parent("td").next("td").children("span").addClass("ui-icon");
    element.parent("td").next("td").children("span").addClass("ui-icon-check");
}

function mainmenu() {
    $(".nav ul").css({ display: "none" }); // Opera Fix
    $(".nav li").hover(function () {
        $(this).find('ul:first').css({ visibility: "visible", display: "none" }).fadeIn(800);
    }, function () {
        $(this).find('ul:first').css({ visibility: "hidden" });
    });
}


// Make sure all ui elements in a page have the right class
function uiaddclasses() {

    $(":input").each(function (i) {
        $(this).addClass("fg-input");
        $(this).addClass("ui-state-default");
        $(this).addClass("ui-corner-all");
    });

    var arr = ["button", "checkbox", "password", "radio", "reset", "submit", "text"];

    jQuery.each(arr, function () {
        var classname = this;
        $(":" + classname).each(function (i) {
            $(this).addClass("fg-" + classname);
            //            alert("adding " + classname + " to " + this);
        });
    });

    $("fieldset").each(function (i) {
        $(this).addClass("ui-widget");
        $(this).addClass("ui-corner-all");
    });

    $("fieldset legend").each(function (i) {
        $(this).addClass("ui-state-default");
    });
}

// default interaction effects across pages
function uisetinteraction() {
    $('.fg-button').hover(
        function () { $(this).addClass('ui-state-hover'); },
        function () { $(this).removeClass('ui-state-hover'); }
	);

    $('.fg-input').focus(
	    function () { $(this).addClass("ui-state-active"); }
	);

    $('.fg-input').blur(
	    function () { $(this).removeClass("ui-state-active"); }
	);

    $('.fg-textarea').focus(
	    function () { $(this).addClass("ui-state-active"); }
	);

    $('.fg-textarea').blur(
	    function () { $(this).removeClass("ui-state-active"); }
	);

    $('.linkbut').hover(
        function () { $(this).addClass('ui-state-hover'); },
        function () { $(this).removeClass('ui-state-hover'); }
    );
}

// default dialog in master
function showresponse(title, msg) {
    if (msg.length) {
        $(function () {
            $('#divdialog').dialog('option', 'title', title);
            $('#divdialog p').text(msg);
            $('#divdialog').dialog('open');
        });
    }
}

// manually call after a gv postback when a pager is used
function stylizepager() {
    $(function () {
        $(".gvPager a").addClass('fg-button');
        $(".gvPager a").addClass('ui-state-default');
        $(".gvPager a").addClass('ui-corner-all');

        $(".gvPager span").addClass('fg-button');
        $(".gvPager span").addClass('ui-state-active');
        $(".gvPager span").addClass('ui-corner-all');

        uisetinteraction();  // from master
    });
}


// Allow a textbox to be focused and cursor set to the end of the value in either browser

function setSelectionRange(input, selectionStart, selectionEnd) {
    if (input.setSelectionRange) {
        input.focus();
        input.setSelectionRange(selectionStart, selectionEnd);
    }
    else if (input.createTextRange) {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
    }
}

function setCaretToEnd(input) {
    setSelectionRange(input, input.value.length, input.value.length);
}

function goShowIt(cla, ele) {
    cla = "." + cla;
    ele = "#" + ele;

    $(cla).fadeOut("fast");
    $(ele).fadeIn("slow");

    return false;
}