var CatalogNavigation = Class.create();
CatalogNavigation.prototype = {
	initialize:function(elm){
		this.elm = $(elm);
		this.openedElm = this.elm.down('.endpoint');
		if (this.openedElm){
			this.openedElm = this.openedElm.up(1);
		}
		var aList = this.elm.select('a');
		for (var i=0;i<aList.length;i++){
			aList[i].observe('click', this.toggleNavNode.bindAsEventListener(this));
		}
	},
	isNodeInPath:function(node, pathEl){
		var buf = pathEl;
		while (buf && (buf!=this.elm)){
			if (node==buf){return true;}
			buf = buf.up(1);
		}
	},
	findAbyUL:function(ulNode){
		return ulNode.up().down('a');
	},
	clearClassInUL:function(ulNode){
		var x = this.findAbyUL(ulNode);
		if (x){x.removeClassName('active');}
	},
	setClassInUL:function(ulNode){
		var x = this.findAbyUL(ulNode);
		if (x){x.addClassName('active');}
	},
	toggleNavNode:function(event){
		if (this.openedElm){
			var x = this.openedElm.down('a.active');
			if (x){x.removeClassName('active');}
		}
		var li = Element.up(Event.element(event));
		var ulList = li.select('ul');
		if (ulList.length>0){
			var ul = ulList[0];
			if (this.openedElm){
				while (this.openedElm && (this.openedElm!=this.elm)){
					if (!this.isNodeInPath(this.openedElm, ul)){
						this.openedElm.hide();
						this.clearClassInUL(this.openedElm);
						this.openedElm = this.openedElm.up(1);
					} else {
						break;
					}
				}
			}
			if (ul.visible()){
				ul.hide();
				this.clearClassInUL(ul);
			} else {
				ul.show();
				this.setClassInUL(ul);
			}
			this.openedElm = ul;
		}
	}
}

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,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;
	},
	showx:function(){
		this.box.show();
	},
	show:function(){
		this.showx();
		this.refresh();
	},
	hide:function(){
		this.box.hide();
	},
	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;
				}.bind(this)
			})
		}
	},
	refresh:function(){
		this.initRequest(this.urls.refresh, 'get', '');
	},
	addProduct:function(data){
		this.initRequest(this.urls.add, 'post', data);
	},
	removeProduct:function(id){
		this.initRequest(this.urls.remove, 'post', {'id':id});
	},
	setQty:function(id,qty){
		this.initRequest(this.urls.qty, 'post', {'id':id,'qty':qty});
	},
	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.slice(0,1)=='0'){
				this.respWithError = false;
			}
		}
		this.content.innerHTML = ln;
		setTimeout("decorateTable($$('.mini-cart-list')[0])", 10);
	},
	returnProcButton:function(){
		if (this.btnProcess && this.btnProcessElm){
			if (elm = this.btnProcessElm.down('.button-process')){
				var button = elm.down('.btn-addtocart');
				button.show();
				elm.processText.hide();
			}
			elm = this.btnProcessElm.down('p.add-label');
			if (!this.respWithError && elm) {
				elm.innerHTML = this.html.successText;
			}
		}
	},
	addProductFromList:function(button, id){
	    if (this.busy){return;}
		this.btnProcessElm = Element.up(button, 3);
		var btnKeep = this.btnProcessElm.down('.button-process');
		if(!btnKeep.processText) 
		{
			btnKeep.processText = $(document.createElement('span'));
			btnKeep.processText.appendChild(document.createTextNode(this.html.buttonText));
			btnKeep.processText.hide();
			btnKeep.appendChild(btnKeep.processText);
		}
		var button = btnKeep.down('.btn-addtocart');
		btnKeep.processText.show();
		button.hide();
		this.btnProcess = true;
		this.addProduct({'product':id,'qty':this.btnProcessElm.down('select.qty-select').value});
	},
	addProductFromView:function(form, varienForm, button){
	    if (this.busy){return;}
	    if (!form || !varienForm){return;}
	    var f = $(form);
	    if (varienForm.validator.validate()){
	       this.addProduct(f.serialize(true));
	       //-----
			this.btnProcessElm = Element.up(button, 3);
			var btnKeep = this.btnProcessElm.down('.button-process');
			if(!btnKeep.processText) 
			{
				btnKeep.processText = $(document.createElement('span'));
				btnKeep.processText.appendChild(document.createTextNode(this.html.buttonText));
				btnKeep.processText.hide();
				btnKeep.appendChild(btnKeep.processText);
			}
			var button = btnKeep.down('.btn-addtocart');
			btnKeep.processText.show();
			button.hide();
			this.btnProcess = true;
	       //-----
	    }
	},
	cleanNode:function(elm) {
		while(elm.firstChild) {
			elm.removeChild(elm.firstChild);
		}
	}
}


/* by Neox */
var ProductHighlight = Class.create({
	initialize: function(cssRule) {
		if(!cssRule) {
			cssRule = ".listing-type-grid li.item";
		}
		this.items = $$(cssRule);
		this.borderClass = 'light-border';
		this.updateGridRowHeight();
		this.initHighlight();
	},
	updateGridRowHeight: function(cssRule) {
		if(!cssRule) {
			cssRule = ".listing-type-grid ol.grid-row";
		}
		var olItems = $$(cssRule);
		var len = olItems.length;
		var ol;
		for(var i=0 ; i<len ; i++)
		{
			ol = $(olItems[i]);
			ol.style.height = ol.getHeight() + 'px';
		}
	},
	initHighlight: function() {
		var len = this.items.length;
		var elt;
		for(var i=0 ; i<len ; i++)
		{
			elt = $(this.items[i]);
			elt.observe('mouseover', this.itemMouseOver.bind(this,elt));
			elt.observe('mouseout', this.itemMouseOut.bind(this,elt));
		}
	},
	itemMouseOver: function(elt, e) {
		elt.style.height = elt.getHeight() - 7 + 'px';
		elt.addClassName(this.borderClass);
	},
	itemMouseOut: function(elt, e) {
		elt.removeClassName(this.borderClass);
		elt.style.height = "";
	}
});
