/*requires pozitron.js*/

function PageLoader( page, params, target, globalref, unloadeventlisteners, callback, caching ){
	this.init( page, params, target, globalref, unloadeventlisteners, callback, caching );
}


PageLoader.prototype = {
	Ok: true,
	default_err_emptyMsg : "",//Kommunikacios hiba. \n Kerem jelentkezzen be <a href='index.jsp'> ujra </a>",	
	warningTimeOuts : 	{
							 "getsummarypage" : 8000							
						},
	
	
	init: function(page, params, target, globalref, unloadeventlisteners, callback, caching){
		
		this.target = target;
		this.page = page;
		this.params = params;
		this.callback = callback;
		this.caching = caching;	
		
		this.isLate = false;
		
		if ( !this.params ) {
			this.params = {};
		}
		this.params[ "fakeparam" ] = new Date().getTime();
		
		this.globalref = globalref;
		//this.timeoutTimer = window.setTimeout( this.onTimeout.bind( this ), 180000 );
		
		//detect slow connections.., 5 sec limit
		this.slowSpeedTimer = window.setTimeout( this.onSpeedTimeout.bind( this ), 5000 );
		
		if(this.params)
			this.request = new Ajax.Request( page,
										{
											method: 'post',
											parameters: this.params,
											onComplete:	this.complete.bind(this),
											onFailure: this.error.bind(this)
										});
		else
			this.request = new Ajax.Request( page,
										{
											method: 'post',
											onComplete:	this.complete.bind(this),
											onFailure: this.error.bind(this)
										});	
	},
	
	complete: function(param)
	{
		//window.clearTimeout( this.timeoutTimer );
		window.clearTimeout( this.slowSpeedTimer );
		
		if ( !this.isLate ) {
			hideSpeedWarning();
		}
		
		if(this.Ok) 
		{
			var res;
			try {
				res = eval("(" + param.responseText + ")" );
			} catch (e) {}
		
			if ( res && res.result_code == "notloggedin" ) {
				if( RenewSession.isLoginInProgress() ){
						RenewSession.addPageLoaderToList( this );
					} else {
						RenewSession.initReLogin();
						RenewSession.addPageLoaderToList( this );
					}
				
				return;
			}

			if(param.responseText == ""){
				Reconnect.addPageLoaderToList( this );
			}else{
			
				if ( res && res.result_code == "error"  ) {
					applyInnerHTML($(this.target), "<span style='color:#ff0000;'>" + res.result_description + "</span>", this.globalref);				
				} else {
					applyInnerHTML($(this.target), param.responseText, this.globalref);
				}
				try{
					if ( Modules && Modules.pageLoad ) {
						Modules.pageLoad.done();
						Modules.pageLoad = null;
					}
					if(this.callback) {
						this.callback();
					}
				} catch(e) {}
			}
		}
	},
	
	error: function(param)
	{
		//window.clearTimeout( this.timeoutTimer );
		window.clearTimeout( this.slowSpeedTimer );
		this.Ok = false;
		this.notify(this.default_err_emptyMsg);
	},
	
	notify: function(text)
	{
		applyInnerHTML($(this.target), text );
	},
	
	onTimeout: function(){
		this.request.transport.abort();
		Reconnect.addPageLoaderToList( this );
	},
	
	onSpeedTimeout: function() {
		this.isLate = true;
		showSpeedWarning();
	},	
	
	msg: function(param)
	{
		//window.alert("Class:\tPageLoader\nRequest:\t" + this.page + "\nParams:\t" + this.req_params + "\nMessage:\t" + param);
	}
}
