var AjaxMiniCart = Class.create();
AjaxMiniCart.prototype = {
	initialize:function(boxId,contentId,counters,urls,html){
		this.box = $(boxId);
		this.content = $(contentId);
		this.counters = counters;
		this.urls = urls;//{refresh,add,remove,qty}
		this.html = html;//{loading,error,currencySimbol,buttonText,successText}
		this.busy = false;
		
/*
		this.box.select('a.close-link').each(function(elm){
			elm.observe('click', function(){this.hide();}.bind(this));
		}.bind(this));
*/
		/*this.box.select('a.top-link-cart').each(function(elm){
			elm.observe('click', function(){this.show();}.bind(this));
		}.bind(this));*/
		this.btnProcess = false;
		this.respWithError = false;
		
		this.showDlg = this.show.bind(this);
		this.hideDlg = this.hide.bind(this);


		this.btnToogleBasket = $('btn_toogle_basket');
		if (this.btnToogleBasket)
			this.btnToogleBasket.observe('click', this.showDlg);
	},
	showx:function(){
		this.box.show();
	},
	show:function(e){
		if(e) e.stop();
		this.showx();
		$('btn_toogle_basket').innerHTML="<span>Hide my basket</span>";
		/*$('btn_toogle_basket').removeChild($('btn_toogle_basket').childElements()[0]);
		spEl=document.createElement("span");
		spEl.appendChild(document.createTextNode('Hide my basket'));
		$('btn_toogle_basket').appendChild(spEl);*/
		this.refresh();
		//$('ajaxminicart').tag = this;
		/*if(!this.btnToogleBasket) return;*/
		if(this.btnToogleBasket)
		{
			this.btnToogleBasket.stopObserving('click', this.showDlg);
			this.btnToogleBasket.observe('click', this.hideDlg);
		}
	},
	hide:function(e){
		if(e) e.stop();
		//alert('!');
		$('ajaxminicart').tag = this;
		$('btn_toogle_basket').innerHTML="<span>Show my basket</span>";
		setTimeout("$('btn_toogle_basket').observe('click', function(){$('ajaxminicart').tag.show();});", 100);
		this.box.hide();
		if (this.btnToogleBasket)
		{
			this.btnToogleBasket.stopObserving('click', this.hideDlg);
			this.btnToogleBasket.observe('click', this.showDlg);
		}
	},
	toggle:function(){
		if (this.box.visible()){
			this.hide();
		} else {
			this.show();
		}
	},
	initRequest:function(url,meth,params){
		if (!this.busy){
			this.busy = true;
			this.content.innerHTML = this.html.loading;
			this.showx();
			this.respWithError = true;
			new Ajax.Request(url, {
				method:meth,
				parameters:params,
				onSuccess:function(transport){
					if (transport.responseText){
						this.parseResponse(transport.responseText);
					} else {
						this.content.innerHTML = this.html.error;
					}
				}.bind(this),
				onFailure:function(){
					this.content.innerHTML = this.html.error;
				}.bind(this),
				onComplete:function(){
					//this.returnProcButton();
					this.btnProcess = false;
					this.busy = false;
					this.refreshCartBar(this.urls.refcartbar);
				}.bind(this)
			})
		}
	},
	refresh:function(){
		this.initRequest(this.urls.refresh, 'get', '');
		//this.refreshCartBar(this.urls.refcartbar);
	},
	addProduct:function(data){
		this.initRequest(this.urls.add, 'post', data);
		$('btn_toogle_basket').innerHTML="<span>Hide my basket</span>";
		this.btnToogleBasket.observe('click', this.hideDlg);
		//this.refreshCartBar(this.urls.refcartbar);
	},
	removeProduct:function(id){
		this.initRequest(this.urls.remove, 'post', {'id':id});
		//this.refreshCartBar(this.urls.refcartbar);
	},
	setQty:function(id,qty){
		this.initRequest(this.urls.qty, 'post', {'id':id,'qty':qty});
		//this.refreshCartBar(this.urls.refcartbar);
	},
	parseResponse:function(t){
		var i = t.indexOf('<gsom>');
		var ln = t;
		if (i>=0){
			ln = t.slice(i+6);
			var x = t.slice(1,i);
			var elm;
			for (var j=0;j<this.counters.length;j++){
				elm = $(this.counters[j]);
				if (elm){
					elm.innerHTML = x;
				}
			}
			if (t[0]=='0'){
				this.respWithError = false;
			}
		}
		this.content.innerHTML = ln;
	},
	returnProcButton:function(){
		if (this.btnProcess && this.btnProcessElm){
			if (elm = this.btnProcessElm.down('.button-process')){
				elm.innerHTML = this.btnProcessHtml;
			}
			elm = this.btnProcessElm.down('p.add-label')
			if (!this.respWithError && elm){
				elm.innerHTML = this.html.successText;
			}
		}
	},
	addProductFromList:function(button, id){
		this.btnProcessElm = Element.up(button, 1);
		
		//var btnKeep = this.btnProcessElm.down('.button-process');
		//this.btnProcessHtml = btnKeep.innerHTML;
		//btnKeep.innerHTML = this.html.buttonText;
		//this.btnProcess = true;
		this.addProduct({'product':id,'qty':$('qty').value});
	},
	refreshCartBar:function(url){
		var c = this.html.currency;
		new Ajax.Request(url, {
			method:'post',
			onSuccess:function(transport){
				if (transport.responseText){
					data = transport.responseText.evalJSON();
					
							var str = data.qty + ' item';
								if(data.qty!='1') { str+=s; }
					$$('.block-basket span.label a')[0].innerHTML =  str;
					$$('.block-basket span.subtotal span.price')[0].innerHTML =   data.subtotal; 				
					$('total').innerHTML = '<span class="price">'+ data.total+'</span>';
					$('shipping').innerHTML = '<span class="price">'+ data.shipping+'</span>';
					
				}
			}
		})
	}
}
