// JS-LINT ran: 07.16.2008
TlcIndigo.Dwr = function(){
	
	var callbackFunc = function(returnValue, dwrFunction){
		// do nothing
	};
	
	var errorHandlerFunc = function(error, guid, dwrFunction){
		TlcIndigo.Modal.Error(error, 'The Call was (' + dwrFunction + ')');
	};
	
	var exceptionHandlerFunc = function(errorString, exception, guid, dwrFunction){
		TlcIndigo.Modal.Error(errorString + ', ' + exception.cause, 'The Call was (' + dwrFunction + ')');
	}; 
	
	var textHtmlHandlerFunc = function(transport, guid, dwrFunction){
		TlcIndigo.Modal.Error('The operation failed because html or text was returned instead of JSON.', 'The Call was (' + dwrFunction + ').  The transport was: ' + transport);
	};
	
	var timeoutHandlerFunc = function(guid, dwrFunction){
		TlcIndigo.Modal.Error('The operation timed out.', 'The Call was (' + dwrFunction + ')');
	};
	
	var finallyHandlerFunc = function(){
		// do nothing
	};

	this.init = function(){

	};
	
	this.call = function(/*Function*/ dwrFunction, /*...*/ variable_args, /*Object*/ options){
		
		var args = [];
		
		for (var i = 1; i < arguments.length - 1; i++){
			args.push(arguments[i]);
		}
		
		var options2 = arguments[arguments.length - 1];
		
		options2 = jQuery.extend({
			callback: callbackFunc,
			errorHandler: errorHandlerFunc,
			exceptionHandler: exceptionHandlerFunc,
			textHtmlHandler: textHtmlHandlerFunc,
			timeoutHandler: timeoutHandlerFunc,
			finallyHandler: finallyHandlerFunc,
			timeout: 30000,
			async: true,
			guid: null
		}, options2);
		
		var calleeThis = arguments.callee;
		
		var dwrOptions = {
			callback: function(returnValue){
				if (options2.callback){
					options2.callback.call(calleeThis, returnValue, options2.guid, dwrFunction);
				}
				if (options2.finallyHandler){
					options2.finallyHandler.call(calleeThis, true, options2.guid);
				}
			},
			errorHandler: function(error){
				if (error == "Timeout"){
//					TlcIndigo.logger.log('Request Timeout : ' + dwrFunction);
					if (options2.timeoutHandler){
						options2.timeoutHandler.call(calleeThis, options2.guid, dwrFunction);
					}
				} else if (options2.errorHandler){
					options2.errorHandler.call(calleeThis, error, options2.guid, dwrFunction);
				}
				if (options2.finallyHandler){
					options2.finallyHandler.call(calleeThis, false, options2.guid);
				}
			},
			exceptionHandler: function(errorString, exception) {
				if (options2.errorHandler){
					options2.exceptionHandler.call(calleeThis, errorString, exception, options2.guid, dwrFunction);
				}
				if (options2.finallyHandler){
					options2.finallyHandler.call(calleeThis, false, options2.guid);
				}
			},
			textHtmlHandler: function(transport){
				// we got here becuase of a page redirect or a yikes
				if (options2.textHtmlHandler){
					options2.textHtmlHandler.call(calleeThis, transport, options2.guid, dwrFunction);
				}
				if (options2.finallyHandler){
					options2.finallyHandler.call(calleeThis, false, options2.guid);
				}
			},
			timeout: options2.timeout,
			async: options2.async
		};
		
		args.push(dwrOptions);
		
		dwrFunction.apply(null, args);
	};
};

TlcIndigo.dwr = new TlcIndigo.Dwr();

TlcIndigo.dwr.init();
