﻿// JScript File
/*******************  ActiveBets.WebApp  ***********************/
Type.registerNamespace('ActiveBets');

ActiveBets.WebApp = function() {

    ActiveBets.WebApp.initializeBase(this);
    this._history = null;
    this._startDelegate = null;
    this._endDelegate = null;
    this._pageRequestManager = null;
    this._UserType = ActiveBets.UserType.Account;
    this._User = null;
    this._AppVirtualPath = String.localeFormat("{0}//{1}/", window.location.protokol, window.location.host);
    this._maxStakesCount = 8;
    this._maxFactor = null;
    this._maxPayment = null;
    this._cultureInfo = null;
    this._appServer = null;
    this._oddFormat = null;

    this.ExtApp = null;

    ActiveBets.App = this;
}

ActiveBets.WebApp.prototype = {

    initialize: function() {
        ActiveBets.WebApp.callBaseMethod(this, 'initialize');

        if (Sys.WebForms && Sys.WebForms.PageRequestManager)
            this._pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();

        Sys.Application.add_load(Function.createDelegate(this, this._app_onload));
    },

    dispose: function() {
        ActiveBets.WebApp.callBaseMethod(this, 'dispose');
        if (!this._pageRequestManager)
            this._pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();

        if (this._pageRequestManager && this._endDelegate) {
            this._pageRequestManager.remove_endRequest(this._endDelegate);
            this._endDelegate = null;

            this._pageRequestManager = null;
        }
    },

    _app_onload: function() {
        this._update_user();

        if (this.ExtApp == null)
            switch (this._UserType) {
            case ActiveBets.UserType.Admin:
                this.ExtApp = new ActiveBets.AdminApp();
                break;
            case ActiveBets.UserType.Account:
                this.ExtApp = new ActiveBets.AccountApp();
                break;
            case ActiveBets.UserType.Dealer:
                this.ExtApp = $create(ActiveBets.DealerApp, null, null, null, null);
                break;
            case ActiveBets.UserType.Partner:
                this.ExtApp = new ActiveBets.PartnerApp();
                break;
        }
        Sys.CultureInfo.CurrentCulture = Sys.CultureInfo._parse(this._cultureInfo);
        this.ExtApp._app_onload();
    },

    get_AppVirtualPath: function() {
        return this._AppVirtualPath;
    },
    get_oddFormat: function() {
        return this._oddFormat;
    },

    // region [ User ]

    _update_user: function() {
        var request = new Sys.Net.WebRequest();
        request.set_url(String.localeFormat("{0}WebApp.axd?mode=u&tmp={1}", this.get_AppVirtualPath(), new Date()));
        request.set_httpVerb("GET");
        request.add_completed(Function.createDelegate(this, this._update_user_callback));
        request.invoke();
    },

    _update_user_callback: function(executor, eventArgs) {
        this.get_user().init(Sys.Serialization.JavaScriptSerializer.deserialize(executor.get_responseData()));
        this.ExtApp.update_ui();
    },

    get_user: function() {
        if (this._User == null) {
            switch (this._UserType) {
                case ActiveBets.UserType.Admin:
                    this._User = new ActiveBets.Admin();
                    break;
                case ActiveBets.UserType.Account:
                    this._User = new ActiveBets.Account();
                    break;
                case ActiveBets.UserType.Dealer:
                    this._User = new ActiveBets.Dealer();
                    break;
                case ActiveBets.UserType.Partner:
                    this._User = new ActiveBets.Partner();
                    break;
            }
        }
        return this._User;
    },

    // endregion

    get_maxStakesCount: function() {
        return this._maxStakesCount;
    },

    get_maxFactor: function() {
        return this._maxFactor;
    },

    get_maxPayment: function() {
        return this._maxPayment;
    },

    get_appServer: function() {
        return this._appServer;
    },

    DoPostBack: function(/*control id*/control_id) {
        if (!this._endDelegate)
            this.initialize();

        __doPostBack(control_id);
    },

    DoPostBackWithOptions: function(options) {
        if (!this._endDelegate)
            this.initialize();

        __doPostBack(options.eventTarget, options.eventArgument);
    }
}

//	Start app
ActiveBets.WebApp.registerClass('ActiveBets.WebApp', Sys.Component, Sys.IContainer);

if (typeof (Sys) !== "undefined")
    Sys.Application.notifyScriptLoaded();