﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Shopmobbing.Behaviors");

Shopmobbing.Behaviors.Logout = function(element) {

    var tagName = element.tagName.toUpperCase();
    if (tagName !== "DIV") {
        throw Error.argument("element", "A Logout behavior must be attached to a DIV DOM element");
    }

    Shopmobbing.Behaviors.Logout.initializeBase(this, [element]);

    this._css = null;
    this._loggingOutDelegate = null;
    this._loggedOutDelegate = null;
    this._errorDelegate = null;
    this._logOutDelegate = null;

    this._userName = null;
}

Shopmobbing.Behaviors.Logout.prototype = {

    get_css: function () {
        return this._css;
    },
    set_css: function (value) {
        if (value !== null) {
            Sys.UI.DomElement.addCssClass(this.get_element(), value);
        }
    },
    get_userName: function () {
        if (this._userName == null || this._userName === "")
            return $get('ctl00_hfUserName').value;
        else
            return this._userName;
    },
    set_userName: function (value) {
        this._userName = value;
        //document.getElementById('welcomeName').innerHTML = value;

        $.getJSON("/services/usersservice/getProfile", {}, function (data, textStatus) {
            if (data.FirstName != null) {
                document.getElementById('welcomeName').innerHTML = ', ' + data.FirstName + ' ' + data.LastName;
            } else {

            }
        });

        this.raisePropertyChanged('userName');
    },

    add_loggingOut: function (handler) {
        this.get_events().addHandler('loggingOut', handler);
    },
    remove_loggingOut: function (handler) {
        this.get_events().removeHandler('loggingOut', handler);
    },
    add_loggedOut: function (handler) {
        this.get_events().addHandler('loggedOut', handler);
    },
    remove_loggedOut: function (handler) {
        this.get_events().removeHandler('loggedOut', handler);
    },
    add_error: function (handler) {
        this.get_events().addHandler('error', handler);
    },
    remove_error: function (handler) {
        this.get_events().removeHandler('error', handler);
    },

    logOut: function () {
        var h = this.get_events().getHandler('loggingOut');
        if (h) h(this, Sys.EventArgs.Empty);

        var me = this;

        $(document.body).css('cursor', 'wait');
        Sys.Services.AuthenticationService.logout((null),
        function (result, textStatus, userContext) {
            me._loggedOutDelegate.call();
            $(document.body).css('cursor', 'default');
        },
        this._errorDelegate, "User context");
    },

    initialize: function () {
        Shopmobbing.Behaviors.Logout.callBaseMethod(this, 'initialize');

        if (this._loggingOutDelegate === null) {
            this._loggingOutDelegate = Function.createDelegate(this, this._loggingOutHandler);
            //this.get_events().addHandler('loggingOut', this._loggingOutDelegate);
        }

        if (this._loggedOutDelegate === null) {
            this._loggedOutDelegate = Function.createDelegate(this, this._loggedOutHandler);
        }

        if (this._errorDelegate === null) {
            this._errorDelegate = Function.createDelegate(this, this._errorHandler);
        }

        if (this._logOutDelegate === null) {
            this._logOutDelegate = Function.createDelegate(this, this.logOut);
            Sys.UI.DomEvent.addHandler($get("lnkLogout", this.get_element()), 'click', this._logOutDelegate);
        }


    },

    dispose: function () {

        if (this._loggingOutDelegate) {
            delete this._loggingOutDelegate;
        }
        if (this._loggedOutDelegate) {
            delete this._loggedOutDelegate;
        }
        if (this._errorDelegate) {
            delete this._errorDelegate;
        }
        if (this._logOutDelegate) {
            Sys.UI.DomEvent.removeHandler($get("lnkLogout", this.get_element()), 'click', this._logOutDelegate);
            delete this._logoutDelegate;
        }

        Shopmobbing.Behaviors.Logout.callBaseMethod(this, 'dispose');
    },



    _loggingOutHandler: function (event) {
        var handler = this.get_events().getHandler('loggingOut');
        if (handler)
            handler(this, Sys.EventArgs.Empty); //onloggingOut(sender, args);
    },
    _loggedOutHandler: function (event) {
        var handler = this.get_events().getHandler('loggedOut');
        if (handler) {
            handler(this, Sys.EventArgs.Empty); //onloggedOut(sender, args);
        }
    },
    _errorHandler: function (event) {
        var handler = this.get_events().getHandler('error');
        if (handler) {
            handler(this, Sys.EventArgs.Empty); //onError(sender, args);
        }
    }
}
Shopmobbing.Behaviors.Logout.registerClass('Shopmobbing.Behaviors.Logout', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
