﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Shopmobbing.Controls");

Shopmobbing.Controls.AuthenticationStatus = function(element) {

    var tagName = element.tagName.toUpperCase();
    if (tagName !== "DIV")
        throw Error.argument("element", "AuthenticationStatus control must be a DIV DOM element.");

    Shopmobbing.Controls.AuthenticationStatus.initializeBase(this, [element]);

    this._css = null;
    this._loginForm = null;
    this._logoutForm = null;

    //refactor as parameter to loadLoginFormUserControl function
    this._showLoginFormOnLoad = false;

    //delegates
    this._loadLoginFormUserControlDelegate = null;
    this._loadLogoutFormUserControlDelegate = null;
    this._onLoggingInDelegate = null;
    this._onLoggedInDelegate = null;
    this._onLoggedOutDelegate = null;

    this._userName = null;
}

Shopmobbing.Controls.AuthenticationStatus.prototype = {

    onLoggingIn: function(sender, args) {
    },

    onLoggedIn: function(sender, args) {
        var me = this;

        //        $(this._loginForm).hide('fast', function() {
        //            if (me._logoutForm === null) {
        //                me.loadLogoutFormUserControl(function() {
        //                    $(me._logoutForm).show('fast');
        //                });
        //            }
        //            else {
        //                $(me._logoutForm).show('fast');
        //            }
        //        });

        $("#menu").load("/UserControls/RightMenu.aspx", null, function() { });

        Sys.UI.DomElement.setVisible(me._loginForm.get_element(), false);
        if (me._logoutForm == null) {
            me.loadLogoutFormUserControl(function() {
                Sys.UI.DomElement.setVisible(me._logoutForm.get_element(), true);
                me._logoutForm.set_userName(me._loginForm.get_userName());
            });
        }
        else {
            Sys.UI.DomElement.setVisible(me._logoutForm.get_element(), true);
            me._logoutForm.set_userName(me._loginForm.get_userName());
        }
    },

    onLoggedOut: function(sender, args) {
        var me = this;

        //        $(this._logoutForm).hide('fast', function() {
        //            if (me._loginForm === null) {
        //                me.loadLoginFormUserControl(function() {
        //                    $(me._loginForm).show('fast');
        //                });
        //            }
        //        });

        Sys.UI.DomElement.setVisible(me._logoutForm.get_element(), false);
        if (me._loginForm == null) {
            me.loadLogoutFormUserControl(function() {
                Sys.UI.DomElement.setVisible(me._loginForm.get_element(), true);
            });
        }
        else {
            Sys.UI.DomElement.setVisible(me._loginForm.get_element(), true);
        }
    },

    loadLoginFormUserControl: function(showAfterLoading) {
        var me = this;
        $(this.get_element()).load(userControlsPath + "/htm/LoginForm.htm", null, function(responseText, textStatus, xhr) {
            me._loginForm = $create(Shopmobbing.Behaviors.Login,
                { id: "bhLogin" },
                { loggedIn: me._onLoggedInDelegate },
                {},
                $get("loginForm"));

            Sys.UI.DomElement.setVisibilityMode(me._loginForm.get_element(), Sys.UI.VisibilityMode.collapse);

            if (me._showLoginFormOnLoad) {
                me._loginForm.show();
            }
            me._showLoginFormOnLoad = false;
        });
    },

    loadLogoutFormUserControl: function(callback) {
        var me = this;

        var username = "";
        if (this._loginForm)
            username = this._loginForm.get_userName();
        else if (username === "" && global_UserName !== "")
            username = global_UserName;
        else if ($get('ctl00_hfUserName') !== null)
            username = $get('ctl00_hfUserName').value;

        if (this._logoutForm) {
            this._logoutForm.set_userName(username);
            return;
        }

        $(this.get_element()).load(userControlsPath + "/htm/LogoutForm.htm", null, function(responseText, textStatus, xhr) {
            me._logoutForm = $create(Shopmobbing.Behaviors.Logout,
                { userName: username },
                { loggedOut: me._onLoggedOutDelegate },
                {},
                $get("logoutForm", me.get_element()));

            Sys.UI.DomElement.setVisibilityMode(me._logoutForm.get_element(), Sys.UI.VisibilityMode.collapse);

            //callback();
        });
    },

    initialize: function() {
        Shopmobbing.Controls.AuthenticationStatus.callBaseMethod(this, 'initialize');

        if (this._loadLoginFormUserControlDelegate === null) {
            this._loadLoginFormUserControlDelegate = Function.createDelegate(this, this.loadLoginFormUserControl);
        }
        if (this._loadLogoutFormUserControlDelegate === null) {
            this._loadLogoutFormUserControlDelegate = Function.createDelegate(this, this.loadLogoutFormUserControl);
        }
        if (this._onLoggingInDelegate === null) {
            this._onLoggingInDelegate = Function.createDelegate(this, this.onLoggingIn);
        }
        if (this._onLoggedInDelegate === null) {
            this._onLoggedInDelegate = Function.createDelegate(this, this.onLoggedIn);
        }
        if (this._onLoggedOutDelegate === null) {
            this._onLoggedOutDelegate = Function.createDelegate(this, this.onLoggedOut);
        }

        if (!Sys.Services.AuthenticationService.get_isLoggedIn()) {
            this._showLoginFormOnLoad = true;
            this._loadLoginFormUserControlDelegate.call();
        }
        else {
            this._loadLogoutFormUserControlDelegate.call();
        }

        $("#menu").load("/UserControls/RightMenu.aspx", null, function () { });
    },

    dispose: function() {
        //Add custom dispose actions here
        Shopmobbing.Controls.AuthenticationStatus.callBaseMethod(this, 'dispose');
    }
}
Shopmobbing.Controls.AuthenticationStatus.registerClass('Shopmobbing.Controls.AuthenticationStatus', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
