﻿/// <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.Widget = function() {
    Infocaster.Web.Ajax.Widget.initializeBase(this);
    var _taskQueue = {};
    var _interval = 0;
    var _widgetManager;
    var _enabled = true;
}

Infocaster.Web.Ajax.Widget.prototype = {
    initialize: function() {
        Infocaster.Web.Ajax.Widget.callBaseMethod(this, 'initialize');
        var sm = this.get_WidgetManager();
        Sys.Debug.assert(sm != null);
        this.get_WidgetManager().registerUpdateWidget(this);
    },
    dispose: function() {        
        Infocaster.Web.Ajax.Widget.callBaseMethod(this, 'dispose');
        this.get_WidgetManager().unregisterUpdateWidget(this);
    },
    //Interval
    get_Interval: function(){
        return this._interval;
    },
    set_Interval: function(val){
        this._interval = val;
    },
    //WidgetManager
    get_WidgetManager: function(){
        return this._widgetManager;
    },
    set_WidgetManager: function(val){
        this._widgetManager = val;
    },
    //Enabled
    get_Enabled: function(){
        return this._enabled;
    },
    set_Enabled: function(val){
        this._enabled = val;
    }
}
Infocaster.Web.Ajax.Widget.registerClass('Infocaster.Web.Ajax.Widget', Sys.Component);

Infocaster.Web.Ajax.PostbackWidget = function() {
    Infocaster.Web.Ajax.PostbackWidget.initializeBase(this);
    var _delayLoad;
    var _updateInvoker;
    var _isLoaded;
    var _useHttpGet;
}

Infocaster.Web.Ajax.PostbackWidget.prototype = {
	initialize: function() {
		Infocaster.Web.Ajax.PostbackWidget.callBaseMethod(this, 'initialize');
		if (this.get_DelayLoad() && !this.get_IsLoaded()) { //We should invoke a postback asap but not without a timeout so we can give the page time to complete loading (hack)
			var pThis = this;
			setTimeout(function() { pThis._updateContent(); }, 100);
		}
	},
	dispose: function() {
		Infocaster.Web.Ajax.PostbackWidget.callBaseMethod(this, 'dispose');
	},
	get_DelayLoad: function() {
		return this._delayLoad;
	},
	set_DelayLoad: function(val) {
		this._delayLoad = val;
	},
	get_UpdateInvoker: function() {
		return this._updateInvoker;
	},
	set_UpdateInvoker: function(val) {
		this._updateInvoker = val;
	},
	get_IsLoaded: function() {
		return this._isLoaded;
	},
	set_IsLoaded: function(val) {
		this._isLoaded = val;
	},
	firePostback: function() {
		__doPostBack(this.get_UpdateInvoker(), '');
	},
	_updateContent: function() {
		//This either queues or executes firePostback immidiatly depending on wether there is a postback already running
		this.get_WidgetManager().queuePostbackItem(this);
	}
}

Infocaster.Web.Ajax.PostbackWidget.registerClass('Infocaster.Web.Ajax.PostbackWidget', Infocaster.Web.Ajax.Widget);

Infocaster.Web.Ajax.WebServiceWidget = function(){
    Infocaster.Web.Ajax.WebServiceWidget.initializeBase(this);
    var _webRequest = null; 
    // Path to the web service, or null if a page method
    var _servicePath = null;
    // Name of the web method
    var _serviceMethod = null;
    var _isLoaded = false;
}

Infocaster.Web.Ajax.WebServiceWidget.prototype = {
    initialize: function() {
        Infocaster.Web.Ajax.WebServiceWidget.callBaseMethod(this, 'initialize');
        if (!this._isLoaded){
            this._isLoaded = true;
            this._update();
        }
    },
    dispose: function() {        
        this._updateCompletedHandler = null;
        Infocaster.Web.Ajax.WebServiceWidget.callBaseMethod(this, 'dispose');
    },
    get_ServiceMethod: function() {
        return this._serviceMethod;
    },
    set_ServiceMethod: function(value) {
        if (this._serviceMethod != value) {
            this._serviceMethod = value;
            this.raisePropertyChanged('serviceMethod');
        }
    },
    
    get_ServicePath: function() {
        return this._servicePath;
    },
    set_ServicePath: function(value) {
        if (this._servicePath != value) {
            this._servicePath = value;
            this.raisePropertyChanged('servicePath');
        }
    },
    get_UserContext: function(){    
        return this._userContext;
    },
    set_UserContext: function(val){
        this._userContext = val;
    },
    get_UseHttpGet: function(){
        return this._useHttpGet;
    },
    set_UseHttpGet: function(val){
        this._useHttpGet  = val;
    },
    get_IsLoaded: function(){
        return this._isLoaded;
    },
    set_IsLoaded: function(val){
        this._isLoaded = val;
    },
    add_UpdateCompleted : function(handler) {
        this.get_events().addHandler('UpdateCompleted', handler);
    },
    remove_UpdateCompleted : function(handler) {
        this.get_events().removeHandler('UpdateCompleted', handler);
    },
    raiseUpdateCompleted : function(eventArgs) {
        var handler = this.get_events().getHandler('UpdateCompleted');
        if (handler) {
            handler(this, eventArgs);
        }
    },
    _onMethodComplete: function(result, headers) {
        //Handler invoked when the request completed     
        this._webRequest = null; // clear out our saved WebRequest object    
        this._updateCompleted(result, headers);
    },
    
    _onMethodFailed: function(err, response) {
        //Handler invoked when the webservice call fails, currently a noop
        //TODO: Handle
        Sys.Debug.trace(err);
        this._webRequest = null; 
    },
    _update: function(){
        //Get the data from the server
        this._webRequest = Sys.Net.WebServiceProxy.invoke(this.get_ServicePath(), this.get_ServiceMethod(), this.get_UseHttpGet(), {"context":this.get_UserContext()},
                                Function.createDelegate(this, this._onMethodComplete),
                                Function.createDelegate(this, this._onMethodFailed),
                                null);  
    },
    _updateCompleted: function(result){
        this.raiseUpdateCompleted(new Infocaster.Web.Ajax.WebServiceWidgetEventArgs(result)); 
    }
}
Infocaster.Web.Ajax.WebServiceWidget.registerClass('Infocaster.Web.Ajax.WebServiceWidget', Infocaster.Web.Ajax.Widget);

Infocaster.Web.Ajax.WebServiceWidgetEventArgs = function(result, headers){
    Infocaster.Web.Ajax.WebServiceWidgetEventArgs.initializeBase(this);
    this._result = result;
    this._headers = headers;
}

Infocaster.Web.Ajax.WebServiceWidgetEventArgs.prototype = {
    get_Result: function(){
        return this._result;
    }, 
    get_Headers: function(){
        return this._headers;
    }
}

Infocaster.Web.Ajax.WebServiceWidgetEventArgs.registerClass('Infocaster.Web.Ajax.WebServiceWidgetEventArgs', Sys.EventArgs);

Infocaster.Web.Ajax.AutoInjectWidget = function(){
    Infocaster.Web.Ajax.AutoInjectWidget.initializeBase(this);
    
    var _targetControl;
}

Infocaster.Web.Ajax.AutoInjectWidget.prototype = {
    initialize: function() {
        Sys.Debug.assert(this._targetControl != null, "TargetControl cant be null");
        this.add_UpdateCompleted(Function.createDelegate(this, this._updateCompletedHandler));
        Infocaster.Web.Ajax.AutoInjectWidget.callBaseMethod(this, 'initialize');
    },
    dispose: function() {        
        Infocaster.Web.Ajax.AutoInjectWidget.callBaseMethod(this, 'dispose');
    },
    get_TargetControl: function(){
        return this._targetControl;
    },
    set_TargetControl: function(val){
        this._targetControl = val;
    },
    _updateCompletedHandler: function(sender, args){
        this.get_TargetControl().innerHTML = args.get_Result();   
    }
}

Infocaster.Web.Ajax.AutoInjectWidget.registerClass('Infocaster.Web.Ajax.AutoInjectWidget', Infocaster.Web.Ajax.WebServiceWidget);

//if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();