$(document).ready(function(){
    /**
     * Multiple actions functioonality
     */
    $("input#selectAll").click( function() {
        if ($(this).attr("checked")) {
            $("form[name='" + $(this).attr('rel') + "'] input[type='checkbox']").attr('checked', true);
            $("form[name='" + $(this).attr('rel') + "'] input[type='checkbox']").parent().parent().find("td").addClass("colorSelected");
        } else {
            $("form[name='" + $(this).attr('rel') + "'] input[type='checkbox']").each(function(){
                $(this).attr('checked', false);
                var className = $(this).parent().parent().attr("class");
                $(this).parent().parent().find("td").removeClass();
                $(this).parent().parent().find("td").addClass(className);
            });
        }
    });
    $("a#selectAllLink").click( function() {
        $("form[name='" + $(this).attr('rel') + "'] input[type='checkbox']").attr('checked', true);
        return false;
    });
    $("a#deselectAllLink").click( function() {
        $("form[name='" + $(this).attr('rel') + "'] input[type='checkbox']").attr('checked', false);
        return false;
    });

    $("#activateSelectedAccountsDialog").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 300,
        height: 150,
        modal: true,
        draggable: false,
        resizable: false,
        buttons: {
            'No': function() {
                $(this).dialog('close');
            },
            'Yes': function() {
                $("form[name='escortsForm']").submit();
            }
        }
    });
    $("a[href='#activateSelectedAccounts']").click( function() {
        $("input[name='action']").val("activateSelectedAccounts");
        $("#activateSelectedAccountsDialog").dialog('open');
        return false;
    });
    
    $("#deactivateSelectedAccountsDialog").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 300,
        height: 150,
        modal: true,
        draggable: false,
        resizable: false,
        buttons: {
            'No': function() {
                $(this).dialog('close');
            },
            'Yes': function() {
                $("form[name='escortsForm']").submit();
            }
        }
    });
    $("a[href='#deactivateSelectedAccounts']").click( function() {
        $("input[name='action']").val("deactivateSelectedAccounts");
        $("#deactivateSelectedAccountsDialog").dialog('open');
        return false;
    });

    $("#setGalarySelectedAccountsDialog").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 300,
        height: 150,
        modal: true,
        draggable: false,
        resizable: false,
        buttons: {
            'No': function() {
                $(this).dialog('close');
            },
            'Yes': function() {
                $("form[name='escortsForm']").submit();
            }
        }
    });
    $("a[href='#setGalarySelectedAccounts']").click( function() {
        $("input[name='action']").val("setGalarySelectedAccounts");
        $("#setGalarySelectedAccountsDialog").dialog('open');
        return false;
    });

    $("#unsetGalarySelectedAccountsDialog").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 300,
        height: 150,
        modal: true,
        draggable: false,
        resizable: false,
        buttons: {
            'No': function() {
                $(this).dialog('close');
            },
            'Yes': function() {
                $("form[name='escortsForm']").submit();
            }
        }
    });
    $("a[href='#unsetGalarySelectedAccounts']").click( function() {
        $("input[name='action']").val("unsetGalarySelectedAccounts");
        $("#unsetGalarySelectedAccountsDialog").dialog('open');
        return false;
    });

    $("#setPremierSelectedAccountsDialog").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 300,
        height: 150,
        modal: true,
        draggable: false,
        resizable: false,
        buttons: {
            'No': function() {
                $(this).dialog('close');
            },
            'Yes': function() {
                $("form[name='escortsForm']").submit();
            }
        }
    });
    $("a[href='#setPremierSelectedAccounts']").click( function() {
        $("input[name='action']").val("setPremierSelectedAccounts");
        $("#setPremierSelectedAccountsDialog").dialog('open');
        return false;
    });

    $("#unsetPremierSelectedAccountsDialog").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 300,
        height: 150,
        modal: true,
        draggable: false,
        resizable: false,
        buttons: {
            'No': function() {
                $(this).dialog('close');
            },
            'Yes': function() {
                $("form[name='escortsForm']").submit();
            }
        }
    });
    $("a[href='#unsetPremierSelectedAccounts']").click( function() {
        $("input[name='action']").val("unsetPremierSelectedAccounts");
        $("#unsetPremierSelectedAccountsDialog").dialog('open');
        return false;
    });
    
    $("#deleteSelectedAccountsDialog").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 300,
        height: 150,
        modal: true,
        draggable: false,
        resizable: false,
        buttons: {
            'No': function() {
                $(this).dialog('close');
            },
            'Yes': function() {
                $("form[name='escortsForm']").submit();
            }
        }
    });
    $("a[href='#deleteSelectedAccounts']").click( function() {
        $("input[name='action']").val("deleteSelectedAccounts");
        $("#deleteSelectedAccountsDialog").dialog('open');
        return false;
    });

    $("form.groupSelectiveForm table tbody tr").hover(
        function() {
            $(this).find("td").removeClass();
            $(this).find("td").addClass("colorActive")
        }, function() {
            if ($(this).find("td input[type='checkbox']").attr('checked')) {
                $(this).find("td").removeClass();
                $(this).find("td").addClass("colorSelected");
            } else {
                var className = $(this).attr("class");
                $(this).find("td").removeClass();
                $(this).find("td").addClass(className);
            }
        }
    );
    $("form.groupSelectiveForm table tbody tr td input[type='checkbox']").change(function() {
        if ($(this).attr('checked')) {
            $(this).parent().parent().find("td").removeClass();
            $(this).parent().parent().find("td").addClass("colorSelected");
        } else {
            var className = $(this).parent().parent().attr("class");
            $(this).parent().parent().find("td").removeClass();
            $(this).parent().parent().find("td").addClass(className)
        }
    });

    /**
     * After field checkbox
     */
    $("input[class='afterFieldCheckBox']").change(function(){
        if ($(this).attr("checked")) {
            $(this).prev().removeAttr("disabled");
            $(this).prev().css('background-color', "#FFFFFF");
        } else {
            $(this).prev().val("");
            $(this).prev().attr("disabled", true);
            $(this).prev().css('background-color', "#FFCCCC");
        }
    });
    $("input[class='afterFieldCheckBox']").change();

    $("a#forgotLoginDetails").click(function() {
        $("div#loginFormTitle").html("Login details recovering");
        $("div#loginContainer > div:eq(0)").html("E-mail:");
        $("div#passwordContainer").remove();
        $("div#forgotLoginLinkContainer").remove();

        //$("div#accountFormContainer").remove();
        $("div#accountFormContainer> div:eq(0)").html("Account form:");

        $("input#loginSubmitBtn").attr('value', "Send new password");
        $("input[name='login']").attr('name', "email");
        $("form[name='loginForm']").attr('action', "passwordRecover");
        return false;
    });
});