﻿/* UI functions */

/* DOM ready */
$(function() {

    stylize();

    // Default dialog in master
    $('#divdialog').dialog({
        autoOpen: false,
        buttons: { "Ok": function() { $(this).dialog("close"); } },
        modal: true,
        width: 550
    });

    $("#aspnetForm").validate({
        errorPlacement: function(error, element) { val_errorPlacement(error, element); },
        success: function(element) { val_success(element); },
        messages: {}
    });

    $("input").validate({
        onfocusout: true
    });

});

function stylize() {

    uiaddclasses();

    uisetinteraction();

    uinavmenu();
}

function uinavmenu() {
    $(".nav li").hover(
        function() { $(this).addClass('ui-state-hover'); },
        function() { $(this).removeClass('ui-state-hover'); }
    );
}

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").prev("td").children("span").removeClass("ui-icon-arrow-1-e");
        element.parent("td").prev("td").children("span").removeClass("ui-icon-check");
        element.parent("td").prev("td").children("span").addClass("ui-icon");
        element.parent("td").prev("td").children("span").addClass("ui-icon-close");
    } else {
        element.parent("td").prev("td").children("span").removeClass("ui-icon-arrow-1-e");
        element.parent("td").prev("td").children("span").removeClass("ui-icon-close");
        element.parent("td").prev("td").children("span").addClass("ui-icon");
        element.parent("td").prev("td").children("span").addClass("ui-icon-check");
    }
}

function val_success(element) {
    element.parent("td").prev("td").children("span").removeClass("ui-icon-arrow-1-e");
    element.parent("td").prev("td").children("span").removeClass("ui-icon-close");
    element.parent("td").prev("td").children("span").addClass("ui-icon");
    element.parent("td").prev("td").children("span").addClass("ui-icon-check");
}

// Make sure all ui elements in a page have the right class
function uiaddclasses() {

    $("input").each(function(i) {
        $(this).addClass("ui-state-default");
        $(this).addClass("ui-corner-all");
        $(this).addClass("fg-input");
    });

    $("textarea").each(function(i) {
        $(this).addClass("ui-state-default");
        $(this).addClass("ui-corner-all");
        $(this).addClass("fg-textarea");
    });

    $("select").each(function(i) {
        //        $(this).selectmenu();
    });

    $("button").each(function(i) {
        $(this).addClass("ui-state-default");
        $(this).addClass("ui-corner-all");
        $(this).addClass("fg-button");
    });

}

// 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"); showhint($(this)); }
	);

    $('.fg-input').blur(
	    function() { $(this).removeClass("ui-state-active"); hidehint($(this)); }
	);

    $('.fg-textarea').focus(
	    function() { $(this).addClass("ui-state-active"); showhint($(this)); }
	);

    $('.fg-textarea').blur(
	    function() { $(this).removeClass("ui-state-active"); hidehint($(this)); }
	);

    $('.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
    });
}

function showhint(ele) {

    var content = $(ele).next("span").html();

    if (content != null && content.length) {
        $('#hintcontent').html(content);
        $('#hint').slideDown('slow');
    }

}

function hidehint(ele) {

    if ($(ele).hasClass('cancelhint')) { return; }

    $('#hint').hide();
    $('#hintcontent').html('');

}