﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit" />
/// <reference name="AjaxControlToolkit.Common.Common.js" assembly="AjaxControlToolkit" />

Type.registerNamespace("Infocaster.Web.Ajax");

Infocaster.Web.Ajax.WidgetManager = function() {
    Infocaster.Web.Ajax.WidgetManager.initializeBase(this, null);
    this._taskQueue = new Array();
    this._postBackQueue = new Array();
    this._enabled = true;
}

Infocaster.Web.Ajax.WidgetManager.prototype = {
    initialize: function() {
        Infocaster.Web.Ajax.WidgetManager.callBaseMethod(this, 'initialize');
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        var pThis = this;
        prm.add_endRequest(function(){pThis._pageRequestEnded();});
    },
    dispose: function() {        
        Infocaster.Web.Ajax.WidgetManager.callBaseMethod(this, 'dispose');
    },
    //Enabled
    get_Enabled: function(){
        return this._enabled;
    },
    set_Enabled: function(val){
        this._enabled = val;
    },
    //Registers a Widget to be competing in the update cycle
    registerUpdateWidget : function(Widget){
        //Validate parameter
        /*if (Object.getType(Widget) !== Infocaster.Web.Ajax.Widget) 
            throw Error.argumentType("Widget", Object.getType(Widget), Infocaster.Web.Ajax.Widget);*/
        if (Array.contains(this._taskQueue, Widget))
            throw Error.argument("Widget", "Widget already exists"); 
            
        Array.add(this._taskQueue, Widget);
    },
    //Unregisters a widget
    unregisterUpdateWidget: function(widget){
        Array.remove(this._taskQueue, widget); //Just remove
    },
    //Queus a new item for postback if not queud already
    queuePostbackItem: function(item){
        //It doesnt seem to work this way (the 2 lines below), TODO: find a solution
        /*if (!Type.inheritsFrom(Infocaster.Web.Ajax.Widget))
            throw Error.argumentType("item", Object.getType(item), Infocaster.Web.Ajax.Widget, "Item is from the wrong type");*/
            
        var prm = Sys.WebForms.PageRequestManager.getInstance(); 
        //If were not having a postback right now
        if (!prm.get_isInAsyncPostBack() && item.get_Enabled()){ 
            item.firePostback();
        }
        else if (!Array.contains(this._postBackQueue, item)){ //If the item isnt scheduled yet
            //Lets add it to the list for later execution    
            Array.enqueue(this._postBackQueue, item);
        }
    },
    //Used to check if there are pending requests
    _pageRequestEnded: function(sender, args){
        if (this._postBackQueue.length > 0){ //There are pending requests, execute the first
            //as current executing item
            _executingElement = Array.dequeue(this._postBackQueue);
            //Fire the postback
            if (_executingElement.get_Enabled())
                _executingElement.firePostback();
        }
    }
}

Infocaster.Web.Ajax.WidgetManager.registerClass('Infocaster.Web.Ajax.WidgetManager', Sys.Component);

//if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();