﻿/// <reference name="MicrosoftAjax.js" />
/// <reference name="dnn.js" assembly="DotNetNuke.WebUtility" />
/// <reference name="dnn.xmlhttp.js" assembly="DotNetNuke.WebUtility" />

// Register Namespace
Type.registerNamespace('Bluestar.Common.Controls');

//**********************************************************************
// PlayerBalance Class
//**********************************************************************
// PlayerBalance - Constructor
Bluestar.Common.Controls.PlayerBalance = function () {
    // Call Base Method
    Bluestar.Common.Controls.PlayerBalance.initializeBase(this);
    // Member Variables
    this._msgs = {};
    this._toggleButton = null;
    this._refreshButton = null;
    this._responseLabel = null;
    this._templatePath = '';

    // Associate delegates to single member variable dictionary to make it easier to dispose.
    this._delegates = {
        _refreshSuccessDelegate: Function.createDelegate(this, this._refreshSuccess),
        _refreshFailDelegate: Function.createDelegate(this, this._refreshFail),
        _onLoadDelegate: Function.createDelegate(this, this._onLoad),
        _onPropChangedDelegate: Function.createDelegate(this, this._onPropChanged)
    };
    // Event Hookup:
    Sys.Application.add_load(this._delegates._onLoadDelegate);
};
// PlayerBalance - Prototype
Bluestar.Common.Controls.PlayerBalance.prototype = {
    // Properties
    get_ns: function () { return this.get_id() + '_'; },
    get_msgs: function () { return this._msgs; },
    set_msgs: function (value) { this._msgs = value; },
    get_templatePath: function () { return this._templatePath; },
    set_templatePath: function (value) { this._templatePath = value; },
    // ASP.NET AXAX Events
    initialize: function () {
        // Call Base Method
        Bluestar.Common.Controls.PlayerBalance.callBaseMethod(this, 'initialize');

        // Create UI
        //this._refreshButton = $get(this.get_ns() + 'cmdRefresh');
        //this._refreshButton.innerHTML = this.getMessage('cmdRefresh');
        //this._refreshButton.title = this.getMessage('cmdRefresh');
        //this._responseLabel = this._createChildControl('lblResponse', 'span', null);
        //$get('bsPlayerBalancePanel').appendChild(this._responseLabel);

        // Hookup Event Handlers
        //$addHandlers(this._refreshButton, { "click": this._onRefresh }, this);
    },
    dispose: function () {
        $clearHandlers(this._refreshButton);
        //this._refreshButton = null;
        this._responseLabel = null;
        this._delegates = null;
        Bluestar.Common.Controls.PlayerBalance.callBaseMethod(this, 'dispose');
    },
    // Public Methods
    getMessage: function (key, def) {
        if (this._msgs[key]) {
            return this._msgs[key];
        } else {
            return def;
        }
    },
    showMessage: function (msg) {
        $get(this.get_ns() + 'lblResponse').innerHTML = msg;
    },
    // Events
    _onLoad: function (src, arg) {
        // The page has completely loaded, it is now safe to access any element or component.
        // Scan ajax components.
        var components = arg.get_components();
        for (var i = 0; i < components.length; i++) {
            if (Object.getTypeName(components[i]) == Object.getTypeName(this) && components[i].get_id() != this.get_id())
                this.add_propertyChanged(components[i]._delegates._onPropChangedDelegate);
        }

        if (jQuery('.bsPlayerBalance').length > 0) {
            // Assign button click event to toggle login panel.
            jQuery('.bsPlayerBalance').click(function (e) {
                // Prevent server side event from triggering.
                e.preventDefault();

                // Toggle the login panel & update the toggle button state.
                jQuery('fieldset#bsPlayerBalancePanel').toggle();
                jQuery('.bsPlayerBalance').toggleClass("menu-open");
            });

            // Assign document events to automatically hide login panel.
            jQuery('fieldset#bsPlayerBalancePanel').mouseup(function () {
                return false;
            });
            jQuery(document).mouseup(function (e) {
                if (jQuery(e.target).parent('a.bsPlayerBalance').length == 0) {
                    jQuery('.bsPlayerBalance').removeClass('menu-open');
                    jQuery('fieldset#bsPlayerBalancePanel').hide();
                }
            });
        }
    },
    _onPropChanged: function (src, args) {
        this.showMessage(String.format('You {0} to {1} but not to me?', args.get_propertyName(), src.get_name()));
    },
    // Private Methods
    _onRefresh: function (src, arg) {
        this._displayWait(true);
        dnn.xmlhttp.callControlMethod('Bluestar.Common.Controls.PlayerBalance.' + this.get_id(),
            'GetFeed',
            {Count: 5},
            this._delegates._refreshSuccessDelegate,
            this._delegates._refreshFailDelegate);

        this.raisePropertyChanged('BalanceRefreshed');
    },
    _refreshSuccess: function (payload, ctx, req) {
        this._applyTemplate(payload);
        this._displayWait(false);
    },
    _refreshFail: function (payload, ctx, req) {
        this._displayWait(false);
    },
    _createChildControl: function (id, tag, type) {
        var ctl = document.createElement(tag);
        ctl.id = this.get_ns() + id;
        if (type)
            ctl.type = type;
        return ctl;
    },
    _displayWait: function (show) {
        $get(this.get_ns() + 'cmdRefresh').innerHTML = (show ? this.getMessage('cmdRefreshing') : this.getMessage('cmdRefresh'));
    },
    _applyTemplate: function (jsonData) {
        // Load the HTML template and prep.
        $("#bsPlayerBalanceContent").setTemplateURL(this.get_templatePath() + 'Test.htm');
        $("#bsPlayerBalanceContent").processTemplate(jsonData);
    }
};
// Register class and inherit from Sys.Component
Bluestar.Common.Controls.PlayerBalance.registerClass("Bluestar.Common.Controls.PlayerBalance", Sys.Component);

// Notify Script is loaded.
if (Sys && Sys.Application) {
    Sys.Application.notifyScriptLoaded();
}

function ShowBalanceConent(mode) {
    if (mode == 'show') {
        $('#login.loggedin li.main ul').show();
        $("#login.loggedin li.main ul").hover(function () {
            $("#login.loggedin li.main ul").show();
            },
            function () {
                $("#login.loggedin li.main ul").hide();
       });
    }
}
