﻿/*
PingManager support.

Copyright(C) 2007 Active Web Services, LLC.
All Rights Reserved.
*/
Type.registerNamespace('AWS.UI');

AWS.UI.PingManager = function AWS$UI$PingManager(element) {
    AWS.UI.PingManager.initializeBase(this, [element]);
    this._interval = 60000;
    this._internalInterval = 60000;
    this._enabled = true;
    this._pending = false;
    this._raiseTickDelegate = null;
    this._endRequestHandlerDelegate = null;
    this._timer = null;
    this._pageRequestManager = null;
    this._servicePath = null;
    this._serviceMethod = null;
    this._errorPage = null;
}

    function AWS$UI$PingManager$get_enabled() {
        /// <value type="Boolean"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._enabled;
    }
    function AWS$UI$PingManager$set_enabled(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Boolean}]);
        if (e) throw e;

        this._enabled = value;
    }
    function AWS$UI$PingManager$get_interval() {
        /// <value type="Number"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._interval;
    }
    function AWS$UI$PingManager$set_interval(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Number}]);
        if (e) throw e;

        this._interval = value;
    }
    function AWS$UI$PingManager$get_internalInterval() {
        /// <value type="Number"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._internalInterval;
    }
    function AWS$UI$PingManager$set_internalInterval(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Number}]);
        if (e) throw e;

        this._internalInterval = value;
    }    
    function AWS$UI$PingManager$get_servicePath() {
        /// <value type="String"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._servicePath;
    }
    function AWS$UI$PingManager$set_servicePath(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: String}]);
        if (e) throw e;

        this._servicePath = value;
    }
    function AWS$UI$PingManager$get_serviceMethod() {
        /// <value type="String"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._serviceMethod;
    }
    function AWS$UI$PingManager$set_serviceMethod(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: String}]);
        if (e) throw e;

        this._serviceMethod = value;
    }
    function AWS$UI$PingManager$get_errorPage() {
        /// <value type="String"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._errorPage;
    }
    function AWS$UI$PingManager$set_errorPage(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: String}]);
        if (e) throw e;

        this._errorPage = value;
    }
    function AWS$UI$PingManager$dispose(){
       this._stopTimer();
       if(this._pageRequestManager !== null){
           this._pageRequestManager.remove_endRequest(this._endRequestHandlerDelegate);
       }
       AWS.UI.PingManager.callBaseMethod(this,"dispose");
    }
    function AWS$UI$PingManager$_ping(){
        try
        {
            Sys.Net.WebServiceProxy.invoke(this.get_servicePath(), this.get_serviceMethod(), false, {}, Function.createDelegate(this,this._onSucceeded), Function.createDelegate(this,this._onFailed), "User Context", 1000000);
        } 
        catch (e)
        {
        }
    }
    function AWS$UI$PingManager$_onSucceeded(result, eventArgs)
    {
        if (this.get_interval() !== this.get_internalInterval())
        {
            window.clearTimeout(this._timer);
            this.set_internalInterval(this.get_interval());
            this._timer = window.setTimeout(Function.createDelegate(this,this._raiseTick),this.get_internalInterval());
        }
            
        if (result == true)
        {
            window.location = this.get_errorPage();
        }
    }
    function AWS$UI$PingManager$_onFailed(result, eventArgs)
    {
        if (this.get_interval() == this.get_internalInterval())
        {
            window.clearTimeout(this._timer);
            this.set_internalInterval(10000);
            this._timer = window.setTimeout(Function.createDelegate(this,this._raiseTick),this.get_internalInterval());
        }
    }
    function AWS$UI$PingManager$_handleEndRequest(sender, arg){
        if ((this._pending === true) && (this._pageRequestManager !== null) 
            && (this._pageRequestManager.get_isInAsyncPostBack() === false))
        {
    	   	this._pending = false;
            this._ping();
        } 
    }
    function AWS$UI$PingManager$initialize(){
        AWS.UI.PingManager.callBaseMethod(this, 'initialize');
    	this._raiseTickDelegate = Function.createDelegate(this,this._raiseTick);
    	this._endRequestHandlerDelegate = Function.createDelegate(this,this._handleEndRequest);
    	if (Sys.WebForms && Sys.WebForms.PageRequestManager){
           this._pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();  
    	}
    	if (this._pageRequestManager !== null ){
    	    this._pageRequestManager.add_endRequest(this._endRequestHandlerDelegate);
    	}
        if(this.get_enabled())
        {
            this.set_internalInterval(this.get_interval());
            this._startTimer();
        }
    }
    function AWS$UI$PingManager$_raiseTick() {
        this._startTimer();
        if ((this._pageRequestManager === null) || (!this._pageRequestManager.get_isInAsyncPostBack())){
            this._ping();
            this._pending = false;
        } 
        else {
            this._pending = true;
        }
    }
    function AWS$UI$PingManager$_startTimer(){
       this._timer = window.setTimeout(Function.createDelegate(this,this._raiseTick),this.get_internalInterval());
    }
    function AWS$UI$PingManager$_stopTimer(){
	    if (this._timer !== null){
	 	    window.clearTimeout(this._timer);
		    this._timer = null;
       } 	
    }
AWS.UI.PingManager.prototype = {
    get_enabled: AWS$UI$PingManager$get_enabled,
    set_enabled: AWS$UI$PingManager$set_enabled,
    get_interval: AWS$UI$PingManager$get_interval,
    set_interval: AWS$UI$PingManager$set_interval,
    get_internalInterval: AWS$UI$PingManager$get_internalInterval,
    set_internalInterval: AWS$UI$PingManager$set_internalInterval,
    get_servicePath: AWS$UI$PingManager$get_servicePath,
    set_servicePath: AWS$UI$PingManager$set_servicePath,
    get_serviceMethod: AWS$UI$PingManager$get_serviceMethod,
    set_serviceMethod: AWS$UI$PingManager$set_serviceMethod,
    get_errorPage: AWS$UI$PingManager$get_errorPage,
    set_errorPage: AWS$UI$PingManager$set_errorPage,
    dispose: AWS$UI$PingManager$dispose,
    _ping: AWS$UI$PingManager$_ping,
    _onSucceeded: AWS$UI$PingManager$_onSucceeded,
    _onFailed: AWS$UI$PingManager$_onFailed,
    _handleEndRequest: AWS$UI$PingManager$_handleEndRequest,
    initialize: AWS$UI$PingManager$initialize,
    _raiseTick: AWS$UI$PingManager$_raiseTick,
    _startTimer: AWS$UI$PingManager$_startTimer,
    _stopTimer: AWS$UI$PingManager$_stopTimer
}
AWS.UI.PingManager.registerClass('AWS.UI.PingManager', Sys.UI.Control); 

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();