﻿$(document).ready(function() {

    function fader() {

        var initialized = false;
        // init
        this.init = function() {
            if (!initialized) {
                var bodyHeight = $("form:first").height();
                $("form:first").append("<div id='fader'></div>");
                $("#fader").css("height", bodyHeight + "px");
                $("#fader").css("opacity", "0");
                $("#fader").fadeTo(100, 0.8, function() { initialized = true; });
                $("#fader").bind("click", function(e) {
                    bFader.clear();
                    $("#members").hide();
                    $("#login_control").css({ 'top': '-26px' });
                });
                return false;
            }
        };

        // clear
        this.clear = function() {
            $("#fader").fadeTo(100, 0, function() { $("#fader").remove(); initialized = false; });
            return false;
        };

    };

    function loginControl() {
        bFader = new fader();

        this.show = function() {
            bFader.init();
            $("#login_control").css({ 'top': '0' });
            $("#members").slideDown(400, function() { $(".loginInput:first").focus(); });
            $(document).bind("keyup", function(i) {
                if (i.keyCode == 27) {
                    if (bFader) {
                        bFader.clear();
                        $("#members").hide();
                        $("#login_control").css({ 'top': '-26px' });
                        $(document).unbind("keyup");
                    }
                    return false;
                }
            });
            return false;
        };

        this.hide = function() {
            bFader.clear();
            $("#members").slideUp(400);
            $("#login_control").css({ 'top': '-26px' });
            $("#help").css({ 'display': 'none' });
            return false;
        };
    };

    $("#members-switch").bind("click", function(evt) {
        newLoginControl = new loginControl();

        if ($("#members").is(":hidden")) {
            newLoginControl.show();
        } else {
            newLoginControl.hide();
        }
        return false;

    });

    $("#closeAdminBtn").bind("click", function() {
        bFader.clear();
        $("#members").hide();
        return false;
    });



});


