if(typeof Tati == "undefined"){Tati = {};}

if(typeof Tati.core == "undefined"){
	Tati.core = {};
}


Tati.core.ServiceProxy = function ( name ){
	this._sUrl = SGL_JS_WEBROOT + '/' + SGL_JS_FRONT_CONTROLLER +'/tati/';
	Tati.core.ServiceProxy._sUrl = this._sUrl;
	this._name = name;
	var self = this;
 	$.ajaxSetup( {
		url: this._sUrl,
   		global: false,
   		type: "POST",
		dataType:"json",
		success:function(response){
			self.onSuccess(this.fMethod, response);
		},
		error:function(request, settings, exception){
			self.onError(request, settings, exception);
		}
 	} );
 	
    this.formSubmitOptions = {
		url:this._sUrl,
		type:"POST",
		clearForm:false,
		success:function(data){
		    Tati.fc.onFormSubmited();
		}
	} 
}

Tati.core.ServiceProxy.isUpdating = false;

Tati.core.ServiceProxy.getService = function(){
	if (Tati.core.ServiceProxy.oProxy == undefined ) {
		Tati.core.ServiceProxy.oProxy = new Tati.core.ServiceProxy();
	}
	return Tati.core.ServiceProxy.oProxy;
}

Tati.core.ServiceProxy.prototype.onError = function( request, settings, exception ){
	var r = eval('(' + request.responseText + ')');
//	this.broadcastMessage( "raiseError", r["errorType"] );
//	$("#sub_nav #ajax_loader").hide();
}

Tati.core.ServiceProxy.prototype.onSuccess = function(fMethod, response){
	this.broadcastMessage(fMethod, response);
	$("#sub_nav #ajax_loader").hide();
}

Tati.core.ServiceProxy.prototype.formSubmit = function(oForm, opt, skip){
    if (Tati.core.ServiceProxy.isUpdating==true && !skip) return;
	$("#sub_nav #ajax_loader").show();
    Tati.core.ServiceProxy.isUpdating=true;
	var options = jQuery.extend(this.formSubmitOptions, opt);
	oForm.ajaxSubmit(options);
    setTimeout(function() {Tati.core.ServiceProxy.isUpdating=false}, 1000);
}

Tati.core.ServiceProxy.prototype.call = function( f, action, oData ){
	var data = "action="+action;
	$("#sub_nav #ajax_loader").show();
	if (oData != undefined ) {
		$.each(oData, function(i, n){ data=data+"&"+i+"="+n; })
	};	
	$.ajax({ data: data, fMethod:f });
}
