﻿/// <reference name="MicrosoftAjax.js"/>
Type.registerNamespace("Crown.Web.Ajax.Controls");

// --------------------------------------------------------------------------
// Control Base
// --------------------------------------------------------------------------
Crown.Web.Ajax.Controls.AjaxControlBase = function(element) 
{
    Crown.Web.Ajax.Controls.AjaxControlBase.initializeBase(this, [element]);
    this._callbackHandler = Function.createDelegate(this, this._onServerResponse);
    this._callbackTarget = "";
    this._callbackState = null;
}
Crown.Web.Ajax.Controls.AjaxControlBase.prototype = 
{
    initialize: function() 
    {
        Crown.Web.Ajax.Controls.AjaxControlBase.callBaseMethod(this, 'initialize');
        
        // Load our state from the server
        if (this.get_callbackState() != null)
			this.loadState(this.get_callbackState().value);
    },
    dispose: function() 
    {
		if (this._saveStateEventHandler != null)
			delete this._saveStateEventHandler;
			
		if (this._callbackHandler != null)
			delete this._callbackHandler;
		
        Crown.Web.Ajax.Controls.AjaxControlBase.callBaseMethod(this, 'dispose');
    },
    
    // Gets or the target control id for callbacks
    get_callbackTarget: function () { return this._callbackTarget; },
    set_callbackTarget: function (value) { this._callbackTarget = value; },
    
    // Gets or sets the client state control id
    get_callbackState: function () { return this._callbackState; },
    set_callbackState: function (value) { this._callbackState = value; },
    
    // Loads the state information from the server
    loadState: function (state)
    {
		if (this._callbackState != null)
			this._callbackState.value = state;
    },
    
    // Performs a post back to the server control
    performPostback: function (commandName, messageContent)
    {
        var commandMessage = [];
        
        // Ensure we have a callback target
        if (this.get_callbackTarget().length == 0)
            return;
        
        // Add the two parts (command name and message content) to our callback
        Array.add(commandMessage, commandName == null ? "" : commandName.toString());
        Array.add(commandMessage, messageContent == null ? "" : messageContent.toString());
        
        // Perform the post back
        __doPostBack(this.get_callbackTarget(), Sys.Serialization.JavaScriptSerializer.serialize(commandMessage));
    },
    
    // Performs a call back to the server control
    performCallback: function (commandName, messageContent, callbackHandler)
    {
		var commandMessage = [];
        
        // Ensure we have a callback target
        if (this.get_callbackTarget().length == 0)
            return;
       
        // Add the two parts (command name and message content) to our callback
        Array.add(commandMessage, commandName == null ? "" : commandName.toString());
        Array.add(commandMessage, messageContent == null ? "" : messageContent.toString());
        
        // Perform the callback
        WebForm_DoCallback(this.get_callbackTarget(), Sys.Serialization.JavaScriptSerializer.serialize(commandMessage), this._callbackHandler, callbackHandler);
    },
    
    // Receives the server response after a callback
    _onServerResponse: function(response, context)
    {
		// Deserialize our two strings
        var responseComponents = Sys.Serialization.JavaScriptSerializer.deserialize(response, false);
        
        // Save the client state to the state control
        if (this._callbackState != null)
        	this.loadState(responseComponents[0]);
        
        // If we have a callback function, pass on the server response
        if (context != null)
            context(responseComponents[1]);
    }
}
Crown.Web.Ajax.Controls.AjaxControlBase.registerClass('Crown.Web.Ajax.Controls.AjaxControlBase', Sys.UI.Control);

// --------------------------------------------------------------------------
// Extender Base
// --------------------------------------------------------------------------
Crown.Web.Ajax.Controls.AjaxExtenderBase = function(element) 
{
    Crown.Web.Ajax.Controls.AjaxExtenderBase.initializeBase(this, [element]);
    this._callbackHandler = Function.createDelegate(this, this._onServerResponse);
    this._callbackTarget = "";
}
Crown.Web.Ajax.Controls.AjaxExtenderBase.prototype = 
{
    initialize: function() 
    {
        Crown.Web.Ajax.Controls.AjaxExtenderBase.callBaseMethod(this, 'initialize');
    },
    dispose: function() 
    {        
        Crown.Web.Ajax.Controls.AjaxExtenderBase.callBaseMethod(this, 'dispose');
    },
    
    // Gets or the target control id for callbacks
    get_callbackTarget: function () { return this._callbackTarget; },
    set_callbackTarget: function (value) { this._callbackTarget = value; },
    
    // Performs a post back to the server control
    performCallback: function (commandName, messageContent)
    {
        var commandMessage = [];
        
        // Ensure we have a callback target
        if (this.get_callbackTarget().length == 0)
            return;
        
       // Add the two parts (command name and message content) to our callback
        Array.add(commandMessage, commandName == null ? "" : commandName.toString());
        Array.add(commandMessage, messageContent == null ? "" : messageContent.toString());
        
        // Perform the post back
        __doPostBack(this.get_callbackTarget(), Sys.Serialization.JavaScriptSerializer.serialize(commandMessage));
    },
    
    // Performs a call back to the server control
    performCallback: function (commandName, messageContent, callbackHandler)
    {
        var commandMessage = [];
        
        // Ensure we have a callback target
        if (this.get_callbackTarget().length == 0)
            return;
        
        // Add the two parts (command name and message content) to our callback
        Array.add(commandMessage, commandName == null ? "" : commandName.toString());
        Array.add(commandMessage, messageContent == null ? "" : messageContent.toString());
        
        // Perform the callback
        WebForm_DoCallback(this.get_callbackTarget(), Sys.Serialization.JavaScriptSerializer.serialize(commandMessage), this._callbackHandler, callbackHandler);
    },
    
    // Receives the server response after a callback
    _onServerResponse: function(response, context)
    {
        // If we have a callback function, pass on the server response
        if (context != null)
            context(response);
    }
}
Crown.Web.Ajax.Controls.AjaxExtenderBase.registerClass('Crown.Web.Ajax.Controls.AjaxExtenderBase', Sys.UI.Behavior);

// --------------------------------------------------------------------------
// Command Event Argument
// --------------------------------------------------------------------------
Crown.Web.Ajax.Controls.CommandEventArgs = function(commandName, commandArgument)
{
	this._commandName = commandName;
	this._commandArgument = commandArgument;
}
Crown.Web.Ajax.Controls.CommandEventArgs.prototype =
{
	initialize: function() 
    {
        Crown.Web.Ajax.Controls.CommandEventArgs.callBaseMethod(this, 'initialize');
    },
    dispose: function() 
    {
		Crown.Web.Ajax.Controls.CommandEventArgs.callBaseMethod(this, 'dispose');
    },
    
    // Gets the command name for the event
    get_commandName: function() { return this._commandName; },
    
    // Gets the command argument for the event
    get_commandArgument: function() { return this._commandArgument; }
}
Crown.Web.Ajax.Controls.CommandEventArgs.registerClass('Crown.Web.Ajax.Controls.CommandEventArgs', Sys.EventArgs);
Type.registerNamespace('Crown.Web.Ajax.Controls');Crown.Web.Ajax.Controls.Resource={};
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();