offer.ServiceDelegate = function(){

}
offer.ServiceDelegate.prototype = {
	curCallCNT:0,
	init: function(){
		this.globalConnectEvents = {
			beforeSend: function(x){
				offerInstance.serviceDelegate.curCallCNT++
				if(offerInstance.serviceDelegate.curCallCNT==1){
					offerInstance.progressView.start("pobieranie danych")
				}
			},
			complete: function(){
				offerInstance.serviceDelegate.curCallCNT--
				if(offerInstance.serviceDelegate.curCallCNT==0){
					offerInstance.progressView.stop()
				}
			},
			ajaxError: function(type, args){
				offerInstance.serviceDelegate.curCallCNT--
				if(offerInstance.serviceDelegate.curCallCNT==0){
					offerInstance.progressView.stop()
				}
			},
			abort: function(type, args){
				offerInstance.serviceDelegate.curCallCNT--
				if(offerInstance.serviceDelegate.curCallCNT==0){
					offerInstance.progressView.stop()
				}
				offerInstance.evError(arg)
			}
		}
		//$.ajax(this.globalConnectEvents)
	},
	/**
	 * @private
	 * @param {Array} argsA
	 */
	_callService: function(service, method, callback, argsA){
		//callbacksO.failure = this.evServiceError
		var postBody = ""
		if ($.isArray(argsA)) {
			var tmpAr = []
			for (var i = 0; i < argsA.length; i++) {
				if (typeof argsA[i] == "string") {
					tmpAr.push("k" + i + "=" + argsA[i])
				}
				else {
					tmpAr.push("k" + i + "=" + JSON.stringify(argsA[i]))
				}
			}
			postBody = tmpAr.join("&")
		}
		var p=this.globalConnectEvents
		if(callback!=null){
			p.success=callback
		}
		p.error=function(xr, textStatus, errorThrown){
			var data=eval('('+xr.responseText+')')
			offerInstance.evError(data.faultString)
		}
		p.url="/modules/amfphp2/json.php?service=cms2." + service + "." + method
		p.url+='&lang='+lang
		p.url+='&id_shop='+id_shop
		p.type='POST'
		p.data=postBody
		p.dataType='json'
		//p.global=false
		this.init()
		$.ajax(p)
		//$.post(p.url,p.data,callback,'json')
	},
	evServiceError: function(arg){
		offerInstance.evError(arg)
	},
	//remote methods
	/**
	 * @public
	 */
	initSession: function(lang){
		this._callService("OfferPresentationService", "initSession", this.evInitSessionResponse, [lang])
	},
	evInitSessionResponse: function(resp){
		offerInstance.config=offerInstance.serviceDelegate.parseResponse(resp)
	},
	readShopClientAccount: function(){
		var callback = this.evReadShopClientAccountResponse
		this._callService("OfferPresentationService", "readShopClientAccount", callback)
	},
	evReadShopClientAccountResponse: function(resp){
		offerInstance.authenticateView.evReadShopClientAccountResponse(offerInstance.serviceDelegate.parseResponse(resp))
	},
	filterProducts: function(filter){
		
		var callback = this.evFilterProductsResponse
		this._callService("OfferPresentationService", "filterProducts", callback, [filter])
	},
	getMainGroups:function(){
		
		var callback = this.evGetMainGroupsResponse
		this._callService("OfferPresentationService", "getMainGroups", callback)
	},
	evGetMainGroupsResponse: function(resp){
		offerInstance.productsView.createProductsSet(offerInstance.serviceDelegate.parseResponse(resp))
	},
	evFilterProductsResponse: function(resp){
		if (offerInstance.productFilter.name != "") {
			offerInstance.searchView.createProductsSetFromSearch(offerInstance.serviceDelegate.parseResponse(resp))
		}
		else {
			offerInstance.productsView.createProductsSet(offerInstance.serviceDelegate.parseResponse(resp))
		}
	},
	
	getProduct: function(product){
		;
		var callback = this.evGetProductResponse;
		this._callService("OfferPresentationService", "getProduct", callback, [product])
	},
	evGetProductResponse: function(resp){
		offerInstance.productsView.showProduct(offerInstance.serviceDelegate.parseResponse(resp))
	},
	readShoppingCart: function(){
		
		var callback = this.evReadShoppingResponse
		this._callService("OfferPresentationService", "readShoppingCart", callback)
	},
	evReadShoppingResponse: function(resp){
		offerInstance.cartView.updateCart(offerInstance.serviceDelegate.parseResponse(resp))
	},
	evShoppingCartItemManipulation: function(res){
		var res = offerInstance.serviceDelegate.parseResponse(res)
		offerInstance.serviceDelegate.readShoppingCart()
	},
	addShoppingCartItem: function(shoppingCartVO){
		
		var callback = this.evShoppingCartItemManipulation
		this._callService("OfferPresentationService", "addShoppingCartItem", callback, [shoppingCartVO])
	},
	changeQuantity: function(shoppingCartVO){
		
		var callback = this.evShoppingCartItemManipulation
		this._callService("OfferPresentationService", "changeQuantity", callback, [shoppingCartVO])
	},
	removeShoppingCartItem: function(shoppingCartVO){
		
		var callback = this.evShoppingCartItemManipulation
		this._callService("OfferPresentationService", "removeShoppingCartItem", callback, [shoppingCartVO])
	},
	login: function(user, psw){
		
		var callback = this.loginResult
		this._callService("OfferPresentationService", "login", callback, [user, psw])
	},
	loginResult: function(res){
		var res = offerInstance.serviceDelegate.parseResponse(res)
		offerInstance.authenticateView.evLoginResult(res)
	},
	registerNewUser: function(person){
		
		var callback = this.registerNewUserResult
		this._callService("OfferPresentationService", "registerNewUser", callback, [person])
	},
	registerNewUserResult: function(res){
		var res = offerInstance.serviceDelegate.parseResponse(res)
		offerInstance.authenticateView.evRegisterResult(res)
	},
	registerNewFirm: function(person){
		
		var callback = this.registerNewFirmResult
		this._callService("OfferPresentationService", "registerNewUser", callback, [person])
	},
	registerNewFirmResult: function(res){
		var res = offerInstance.serviceDelegate.parseResponse(res)
		offerInstance.authenticateView.evRegisterFirmResult(res)
	},
	restorePassword: function(person){
		
		var callback = this.restorePasswordResult
		this._callService("OfferPresentationService", "restorePassword", callback, [person])
	},
	restorePasswordResult: function(res){
		var res = offerInstance.serviceDelegate.parseResponse(res)
		offerInstance.authenticateView.evRestoreResult(res)
	},
	getDeliveryTypes: function(){
		
		var callback = this.getDeliveryTypesResult
		this._callService("OfferPresentationService", "getDeliveryTypes", callback)
	},
	getDeliveryTypesResult: function(res){
		var res = offerInstance.serviceDelegate.parseResponse(res)
		offerInstance.cartView.showDeliveryTypePanel(res)
	},
	saveDeliveryType: function(deliveryVO){
		
		var callback = this.saveDeliveryTypeResult
		this._callService("OfferPresentationService", "saveDeliveryType", callback,[deliveryVO])
	},
	saveDeliveryTypeResult: function(res){
		
		offerInstance.cartView.evDeliveryTypeSaved(offerInstance.serviceDelegate.parseResponse(res))
	},
	orderConfirm: function(text){
		this._callService("OfferPresentationService", "orderConfirm", this.orderConfirmResult,[text])
	},
	orderConfirmResult: function(res){
		offerInstance.cartView.evOrderConfirmResult(offerInstance.serviceDelegate.parseResponse(res))
	},
	logout: function(){
		
		var callback = this.logoutResult
		this._callService("OfferPresentationService", "logout", callback)
	},
	logoutResult: function(res){
		offerInstance.authenticateView.evLogoutResult(offerInstance.serviceDelegate.parseResponse(res))
	},
	readFullAccountData: function(){
		var callback = this.evReadFullAccountDataResult
		this._callService("OfferPresentationService", "readAddress", callback)
	},
	evReadFullAccountDataResult: function(res){
		offerInstance.cartView.accountDataAvailable(offerInstance.serviceDelegate.parseResponse(res))
	},
	readRegistrationData: function(){
		var callback = this.evReadRegistrationDataResult
		this._callService("OfferPresentationService", "readAddress", callback)
	},
	evReadRegistrationDataResult: function(res){
		offerInstance.cartView.showOrderSummary(offerInstance.serviceDelegate.parseResponse(res))
	},
	saveFullAccountData: function(person2VO){
		
		var callback = this.evSaveFullAccountDataResult
		this._callService("OfferPresentationService", "saveAccountData", callback,[person2VO])
	},
	evSaveFullAccountDataResult: function(v){
		offerInstance.cartView.evChangeAccountDataSaved()
		
	},
	changePassword: function(oldP,newP,newPRepeat){
		
		var callback = this.evChangePasswordResult
		this._callService("OfferPresentationService", "changePassword", callback,[oldP,newP,newPRepeat])
	},
	evChangePasswordResult: function(res){
		offerInstance.cartView.evChangePasswordResult(offerInstance.serviceDelegate.parseResponse(res))
	},
	getStaticContent: function(pageRequest){
		var callback = this.evGetStaticContentResult
		pageRequest.serialize_pure_html=true
		this._callService("SiteService", "publicGetPage", callback,[pageRequest])
	},
	evGetStaticContentResult: function(pageRequest){
		offerInstance.staticContentView.evContentAvailable(offerInstance.serviceDelegate.parseResponse(pageRequest))
	},
	findProductPath: function(id){
		var callback = this.findProductPathResult
		this._callService("OfferPresentationService", "findProductPath", callback,[id])
	},
	findProductPathResult: function(arr){
		var arr=offerInstance.serviceDelegate.parseResponse(arr)
		$(document).trigger('evFindProductPathResult',[arr])
	},
	parseResponse: function(ret){
		//var ret = YAHOO.lang.JSON.parse(response);
		if (ret.faultCode) {
			offerInstance.evError(ret.faultString)
			throw new Error(ret.faultString)
		}
		return ret;
	}
}

