﻿$(document).ready(function() {
    // Will only be called on regular post backs
    InitjQuery(null, null);

    // Make sure we retry after every ajax request
    BindFunctionsOnAjaxRequest(null, InitjQuery);
});

function jQueryBindsGeneral() {
    var formElementsToStyle = "input:text,input:password,select:input,textarea:input";
    $(formElementsToStyle).addClass("text");
    $(formElementsToStyle).hover(
                function() { $(this).addClass("hover"); },
                function() { $(this).removeClass("hover"); });
    $(formElementsToStyle).focus(
                function() { $(this).addClass("active"); });
    $(formElementsToStyle).blur(
                function() { $(this).removeClass("active"); });

    $("textarea").each(function() {
        $(this).css("max-width", $(this).width() + "px")
    });

    $("input:radio,input:checkbox").css("margin-top", "6px");
    $("input:radio,input:checkbox").css("margin-left", "4px");
    $("input:radio,input:checkbox").parent("span").css("display", "block");
    $("input:radio,input:checkbox").parent("span").css("margin-bottom", "6px");
    $("input:radio,input:checkbox").parent("span").children("span").css("display", "inline");
    $("input:radio,input:checkbox").parent("span").children("span").prevAll("label").css("display", "inline");
    $("input:radio+label,input:checkbox+label").css("margin-right", "4px");

    // Hide labels in IE with empty content
    $("#sectionMain span.ErrorLabel:empty,#sectionMain span.SuccessLabel:empty").hide();

    FixMainHeight();
}

function InitjQuery(sender, args) {
    // Call general jquery binds
    jQueryBindsGeneral();

    // Try to call jQueryBinds, if it exists on the child page
    TryCalljQueryBinds(null, null);
}

function TryCalljQueryBinds() {
    if (typeof (jQueryBinds) != "undefined")
        jQueryBinds();
}

function FixMainHeight() {
    var divSectionMain = $("#sectionMain");
    if (divSectionMain != null) {
        divSectionMain.css("height", "auto");
        var iSMHeight = divSectionMain.height(),
        iCHHeight = $("#contentHolder").height();
        if (iSMHeight != null && iCHHeight != null && iSMHeight < iCHHeight)
            divSectionMain.css("height", iCHHeight - 22 - 13 + "px"); // MenuTop, padding 12, border-bottom 1
        else
            divSectionMain.css("height", "auto");
    }
}

function toggleSubmenu(sender) {
    var parent = $(sender).parent(".menuItem");
    var submenu = $(parent).next(".subMenu");
    if (!$(sender).hasClass('activeItem')) {
        if ($(parent).hasClass("Open")) {
            $(submenu).slideUp(100, function() { FixMainHeight(); });
            $(parent).removeClass("Open");
        }
        else {
            $(submenu).slideDown(100, function(){ FixMainHeight(); });
            $(parent).addClass("Open");
        }
    }
    
}

function FormFunction(control, event, nextcontrol, type) {
    if ((event.which && event.which == 13) ||
	        (event.keyCode && event.keyCode == 13)) {
        var targetControl = document.getElementById(nextcontrol);
        if (type == "submit") {
            if (targetControl.href != undefined)
                top.location = targetControl.href;
            else if (targetControl == "[object HTMLInputElement]" ||
                        targetControl.type == "submit") {
                targetControl.click();
            }
        }
        else if (type == "tab")
            targetControl.focus();
        return false;
    }
    else return true;
}
