/*requires pozitron.js*/
function Connector( jsonservice, params, func, async ){
	this.init( jsonservice, params, func, async );
}

Connector.prototype = {
	warningTimeOuts : 	{
							 "getsummarypage" : 8000							
						},
	STR : Str.connector,
	readyFunc: null,
	errFunc: null,
	Ok: true,
	default_err_emptyMsg : Str.connector.defaultErr,
	default_err_unknownMsg : Str.connector.unknownErr,
	
	init: function(jsonservice, params, func, async ){ //service neve, paramok, mit hivjon meg ha kesz
		//this.timeoutTimer = window.setTimeout( this.onTimeout.bind( this ), 180000 );	
		
		
		//detect slow connections..
		if ( params && params.indexOf( "req=" ) > -1 ) {
			var req = params.substring( params.indexOf( "req=" ) + 4, ( params.indexOf( "&" ) > -1 ? params.indexOf( "&" ) : params.length ) );
			var slowThreshold = this.warningTimeOuts[ req ]
			
			if ( !slowThreshold ) {
				slowThreshold = 5000;
			}
			
			this.slowSpeedTimer = window.setTimeout( this.onSpeedTimeout.bind( this ), slowThreshold );
		}		
		
		this.isLate = false;
		
		this.async = async;
		if ( this.async == false ) {					
		} else {
			this.async = true;
		}
		this.readyFunc = func;
		this.req_service = jsonservice;
		this.req_params = params;	
		this.request = new Ajax.Request( jsonservice,
									{
										method: 'post',
										parameters: ( (params == "" || params == null)? "fakeparam=" + new Date().getTime(): params + "&fakeparam=" + new Date().getTime() ),
										onComplete:	this.complete.bind(this),
										onFailure: this.error.bind(this),
										encoding:'UTF-8',
										asynchronous: this.async
									});
	},
	
	complete: function(param)
	{
		//window.clearTimeout( this.timeoutTimer );
		window.clearTimeout( this.slowSpeedTimer );
		
		if ( !this.isLate ) {
			hideSpeedWarning();
		}
		
		if(this.Ok) {
			if(param.responseText == ""){
				Reconnect.addConnectorToList( this );
			} else {
				var res;
				try{
					res = eval("(" + param.responseText + ")" );
				} catch (e) {}
				
				if ( res && res.result_code == "notloggedin" ) {
					if( RenewSession.isLoginInProgress() ){
						RenewSession.addConnectorToList( this );
					} else {
						RenewSession.initReLogin();
						RenewSession.addConnectorToList( this );
					}
					return;
				}
				
				if( res && res.result_code && res.result_code.toUpperCase() != "OK" && !this.readyFunc ) {//ha HIBA VAN: van result code es nem OK
					this.Ok = false;
					if( res.result_description ) {
						this.notify(res.result_description, res.result_params, res.result_code );
					} else {
						this.notify(this.default_err_unknownMsg, null, "UNKNOWN_ERROR");
					}
								
				}
				else {				
					if(res && res.result_description ) {//ha van leiras csak akkor jelezzuk
						this.notify(res.result_description, res.result_params, res.result_code );
					}				
					this.readyFunc(param.responseText);
				}
			}
		}
	},
	
	error: function(param)
	{
		//window.clearTimeout( this.timeoutTimer );	
		window.clearTimeout( this.slowSpeedTimer );		
		//alert( "Connector error: " + param.status );
		this.Ok = false;
		
	
	},
	
	notify: function(text, obj, result_code)
	{
		if(!this.Ok)
		{
			this.msg("default Connector.notify(text,obj) \n Uzenet:" + text); 
		}
	},
	
	onTimeout: function(){
		this.request.transport.abort();
		Reconnect.addConnectorToList( this );
	},
	
	onSpeedTimeout: function() {
		showSpeedWarning();
	},
	
	msg: function(param)
	{
		//window.alert("Class:\tConnector\nRequest:\t" + this.req_service + "\nParams:\t" + this.req_params + "\nMessage:\t" + param);
	}
}


