function registerMiniCartEvents(e) {

	if($('minibask2')) {
		var mbChildren = $A($('minibask2').childNodes);

		mbChildren.each( function(e) { 
			if(e.nodeType == 1 && e.nodeName == 'DIV') {	
				
				var addID = $(e.id + '_add');
				addID.onclick = addProduct;
				if (addID.captureEvents) addID.captureEvents(Event.CLICK);
				var subID = $(e.id + '_sub');
				subID.onclick = subProduct;
				if (subID.captureEvents) subID.captureEvents(Event.CLICK);
			}

		});

		var empty_basket = $('empty_basket');
		empty_basket.onclick = emptyBasket;
		if (empty_basket.captureEvents) empty_basket.captureEvents(Event.CLICK);

		

	}

}

function emptyBasket(e) {
	var url = '/basketinterface/c';
	new Ajax.Request(url, {method:'post', postBody:'ajax_action=empty_basket', onFailure: errFunc, on404: errFunc});
	$$('div.cartlines').each(function(el) { new Effect.Fade(el, {duration: 0.5, onComplete:'el.remove();'}); });
	return false;
}

function addProduct(e) {
	
	//Access event object in all browsers & stop propagation
	if (!e) var e = window.event
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();

	//Find target of the event
	var t;
	if (e.target) t = e.target;
	else if (e.srcElement) t = e.srcElement;
	if (t.nodeType == 3) // defeat Safari bug
		t = t.parentNode;
	//var pd = (t.id).match('/product_[0-9]+_[0-9]+_add/');
	var sid = (t.id).replace(/_add/gi, "");
	var sid = sid.replace(/_sub/gi, "");

	if(sid == 'addproduct') {

		var productname = $F('productname');
		var productname = productname.substring(0, 29);
		var productid = $F('productid');
		var productvariation = $F('productvariation');
		var productquantity = $F('productquantity');
	
		//Form.disable($('addproduct_form'));
		
		item_indication = $('product_added_' + productid);
		if(item_indication) {
			clearElementAll(item_indication);
			var item_name = "You have added <strong>" + productname + "...</strong> to your basket!";
			new Insertion.Bottom(item_indication,item_name);
			new Effect.Appear(item_indication, {
				duration: 1.0,
				queue: {position: 'start', scope: 'dexa'}
				}
			);
			setTimeout("closePopup(" + productid + "," + productid + ")",5000);
		}

	} else {
		var productid = $F(sid + '_productid');
		if($(sid + '_productname')) {
			var productname = $F(sid + '_productname');
			var productname = productname.substring(0, 29);
		}

		var productvariation = $F(sid + '_productvariation');
		var productquantity = $F(sid + '_productquantity');

		item_indication = $('product_added_' + productid);
		if(item_indication && productname) {
			clearElementAll(item_indication);
			var item_name = "You have added <strong>" + productname + "...</strong> to your basket!";
			new Insertion.Bottom(item_indication,item_name);
			new Effect.Appear(item_indication, {
				duration: 1.0,
				queue: {position: 'start', scope: 'dexa'}
				}
			);
			setTimeout("closePopup(" + productid + "," + productid + ")",5000);
		}

	}

	var url = '/basketinterface/c';
	new Ajax.Request(url, {method:'post', postBody:'ajax_action=add&id=' + productid + '&var=' + productvariation + '&qty=' + productquantity, onSuccess: updateBasket, onFailure: errFunc, on404: errFunc});

	return false;
}

function subProduct(e) {
	//alert('removing');
	
	//Access event object in all browsers & stop propagation
	if (!e) var e = window.event
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();

	//Find target of the event
	var t;
	if (e.target) t = e.target;
	else if (e.srcElement) t = e.srcElement;
	if (t.nodeType == 3) // defeat Safari bug
		t = t.parentNode;
	var sid = (t.id).replace(/_add/gi, "");
	var sid = sid.replace(/_sub/gi, "");
	//alert('sid is ' + sid);
	if(sid == 'subproduct') {
		
		var productid = $F('productid');
		var productvariation = $F('productvariation');
		var productquantity = $F('productquantity');
		
		
		
	} else {
		
		var productid = $F(sid + '_productid');
		
		var productvariation = $F(sid + '_productvariation');
		var productquantity = $F(sid + '_productquantity');
	
	}
	//console.log($$('#' + sid + ' div.num')[0].innerHTML);
	if(($$('#' + sid + ' div.num')[0].innerHTML.substr(0,2))=='1 ') {
		new Effect.Highlight($(sid), {startcolor: '#C7E4FD', endcolor: '#ffffff', queue: {position: 'start', scope: 'nou'}});
		new Effect.Fade($(sid), {duration: 0.5, onComplete:'$(sid).remove();', queue: {position: 'end', scope: 'nou'}});
	}
	var url = '/basketinterface/c';
	new Ajax.Request(url, {method:'post', postBody:'ajax_action=sub&id=' + productid + '&var=' + productvariation + '&qty=' + productquantity, onSuccess: updateBasket, onFailure: errFunc, on404: errFunc});

	return false;
	
}



var updateBasket = function(t) {
	eval("var basket = "+t.responseText);
	//console.log(basket);
	var minibasket = $('minibask2');
	var v = 0;
	var octy = 0;
	basket[2].each(function(product) {
		
		qty = basket[3][v];
		octy += qty;
		vari = basket[4][v];
		
		var pp = $('product_' + product.id + '_' + vari);
		var pc = $('product_' + product.id + '_' + vari + '_productcount');
		var pd = $('product_' + product.id + '_' + vari + '_productqtyvis');

		
		//console.log('product_' + product.id + '_' + vari);
		//console.log('product_' + product.id + '_' + vari + '_productcount');
		//console.log('product_' + product.id + '_' + vari + '_productqtyvis');
		//alert($F('product_' + product.id + '_' + vari + '_productcount'));
		
		//console.log ('pp:' + pp + 'pc:' + pc + 'pd:' + pd);
		//alert('product_' + product.id + '_' + vari + '_productcount');
		if(pp != null && pc != null && pd != null) {
			//console.log(qty);
			if(qty < 1) {
			
				new Effect.Fade(pp, {duration: 0.5});
				clearElement(pd); 
				pc.value = qty;
				new Insertion.Top(pd,qty + ' '); 

			} else if(qty >= 1 && !Element.visible(pp)) {
				pc.value = qty;
				clearElement(pd); 
				str = '' + qty + ' ';
				new Insertion.Top(pd,str);
				//new Effect.Appear(pp, {duration: 0.5});
				new Effect.Appear(pp, {duration: 0.5, queue: {position: 'start', scope: 'sxa'}});
				new Effect.Highlight(pp, {queue: {startcolor: '#C7E4FD', endcolor: '#ffffff', position: 'end', scope: 'sxa'}});
				
			} else if(pc.value != qty) {
				
				pc.value = qty;
				str = '' + qty + ' ';
				
				new Effect.Opacity(pp,{ 
					duration: 0.4, 
					transition: Effect.Transitions.linear,
					from: 1.0, 
					to: 0.0,
					afterFinish: function() {  clearElement(pd); new Insertion.Top(pd,pc.value + ' '); },
					queue: 'start'
					}
				);


				new Effect.Opacity(pp,{ 
					duration: 0.4, 
					transition: Effect.Transitions.linear,
					from: 0.0, 
					to: 1.0,
					queue: 'end'	
					}
				);
			}

		} else {
			
			if(qty > 0) {
				
				
				new Insertion.Top(minibasket,'<div class="cartlines" id="product_' + product.id + '_' + vari + '" style="display: none;"><div class="num" id="product_' + product.id + '_' + vari + '_productqtyvis">' + qty + ' <span>x</span></div><div class="item" title="' + product.name + '">' + product.shortname + ' </div><div class="addrem"><form id="product_' + product.id + '_' + vari + '_form" action=""><div><input id="product_' + product.id + '_' + vari + '_productid" value="' + product.id + '" type="hidden" /></div><div><input id="product_' + product.id + '_' + vari + '_productquantity" value="1" type="hidden" /></div><div><input id="product_' + product.id + '_' + vari + '_productcount" value="' + qty + '" type="hidden" /></div><div><input id="product_' + product.id + '_' + vari + '_productvariation" value="' + vari + '" type="hidden" /></div><div><input id="product_' + product.id + '_' + vari + '_add" value="+" type="button" class="plus" /></div><div><input id="product_' + product.id + '_' + vari + '_sub" value="-" type="button" class="minus" /></div></form></div></div>');

				//old - <div id="product_' + product.id + '_' + vari + '" style="display: none;">(' + qty + ') ' + product.name + '<form id="product_' + product.id + '_' + vari + '_form"><input type="hidden" id="product_' + product.id + '_' + vari + '_productid" value="' + product.id + '" /><input type="hidden" id="product_' + product.id + '_' + vari + '_productquantity" value="1" /><input type="hidden" id="product_' + product.id + '_' + vari + '_productcount" value="1" /><input type="hidden" id="product_' + product.id + '_' + vari + '_productvariation" value="' + vari + '" /><input type="button" id="product_' + product.id + '_' + vari + '_add" value="+" /><input type="button" id="product_' + product.id + '_' + vari + '_sub" value="-" /></form></div>

				var addID = $('product_' + product.id + '_' + vari + '_add');
				
				addID.onclick = addProduct;
				
				if (addID.captureEvents) addID.captureEvents(Event.CLICK);
				var subID = $('product_' + product.id + '_' + vari + '_sub');
				subID.onclick = subProduct;
				if (subID.captureEvents) subID.captureEvents(Event.CLICK);

				
				var curproduct = $('product_' + product.id + '_' + vari);
				
				//new Effect.Appear(curproduct, {duration: 0.5});
				new Effect.Appear(curproduct, {duration: 0.5, queue: {position: 'start', scope: 'sxr'}});
				new Effect.Highlight(curproduct, {startcolor: '#C7E4FD', endcolor: '#ffffff', queue: {position: 'end', scope: 'sxr'}});

				
				
			}
		}
		v++;
	});

	if(octy == 0) { Form.disable($('checkout_form')); Form.disable($('empty_form'));} else { Form.enable($('checkout_form')); Form.enable($('empty_form')); }


}


onloads.push(registerMiniCartEvents);
if (window.captureEvents) window.captureEvents(Event.LOAD);