// Copyright BizAndGo 2006 

var BzCartWidget = Class.create();
BzCartWidget.prototype = {
    initialize: function (){
			//this.selfLocation = '/index.php?action=pan';
    },
    changeDeliveryType : function (srvRowPanIndex) {
			this.showLoader();
			new Ajax.Updater(
				$('cart_contents'),	// Element ID that receives the response text
				'/bzajax',					// URL to send data to with parameters
				{	
					method:'post',
					postBody:'&ajaxaction=cartActions&indice='+srvRowPanIndex+'&changeDeliveryType=true',
					onComplete : function() {
						var flObj = new fx.Flash("cart_row_"+srvRowPanIndex, {color_from:"#ffffff", color_to:"#F0F5F9", count:1, transition:fx.circ, duration:300});
						flObj.toggle();
						if(ie) {
							fnLoadPngs();
							fnLoadImPngs();									
						}								
					}
				}		
			);	
		},
		adjustQuantity: function (srvRowPanIndex,sOpType){
				if(sOpType=="plus") {
					this.showLoader();
					new Ajax.Updater(
						$('cart_contents'),	// Element ID that receives the response text
						'/bzajax',					// URL to send data to with parameters
						{	
							method:'post',
							postBody:'&ajaxaction=cartActions&indice='+srvRowPanIndex+'&chgqte=1&chgqte_type=unit&&nqte=1&op=plus',
							onComplete : function() {
								BzCartWidget.prototype.hideLoader();								
								var flObj = new fx.Flash("cart_row_"+srvRowPanIndex, {color_from:"#ffffff", color_to:"#F0F5F9", count:1, transition:fx.circ, duration:300});
								flObj.toggle();
								if(ie) {
									fnLoadPngs();
									fnLoadImPngs();									
								}										
							}
						}		
					);					
				}
				else if(sOpType == "moins") {
					this.showLoader();
					new Ajax.Updater(
						$('cart_contents'),	// Element ID that receives the response text
						'/bzajax',					// URL to send data to with parameters
						{	
							method:'post',
							postBody:'&ajaxaction=cartActions&indice='+srvRowPanIndex+'&chgqte=1&chgqte_type=unit&&nqte=1&op=moins',
							onComplete : function() {
								BzCartWidget.prototype.hideLoader();	
								var flObj = new fx.Flash("cart_row_"+srvRowPanIndex, {color_from:"#ffffff", color_to:"#F0F5F9", count:1, transition:fx.circ, duration:300});
								flObj.toggle();
								if(ie) {
									fnLoadPngs();
									fnLoadImPngs();									
								}										
							}
						}		
					);
				}
				
		},
		removeCartItem : function (rowIndex,cartContainer) {
			if(confirm("Etes vous sūr de vouloir retirer cet article de votre panier ?")) {
				new Ajax.Updater(
					$(cartContainer),	// Element ID that receives the response text
					'/bzajax',					// URL to send data to with parameters
					{	
						method:'post',
						postBody:'&ajaxaction=cartSummaryRemove&prindex='+rowIndex+'&removeitem=true',
						onComplete : function() {
						
						}
					}		
				);					
			}
		},
		removeItemFromCart : function(rowIndex,confirmAction) {
			if(confirmAction) {
				if(confirm("Etes vous sūr de vouloir retirer cet article de votre panier ?")) {
					this.showLoader();
					new Ajax.Updater(
						$('cart_contents'),	// Element ID that receives the response text
						'/bzajax',					// URL to send data to with parameters
						{	
							method:'post',
							postBody:'&ajaxaction=cartActions&prindex='+rowIndex+'&removeitem=true',
							onComplete : function() {
								BzCartWidget.prototype.hideLoader();	
								var flObj = new fx.Flash('cart_contents', {color_from:"#ffffff", color_to:"#F0F5F9", count:1, transition:fx.circ, duration:300});
								flObj.toggle();
								if(ie) {
									fnLoadPngs();
									fnLoadImPngs();									
								}										
							}
						}		
					);					
				}
			}
			else {
					this.showLoader();
					new Ajax.Updater(
						$('cart_contents'),	// Element ID that receives the response text
						'/bzajax',					// URL to send data to with parameters
						{	
							method:'post',
							postBody:'&ajaxaction=cartActions&prindex='+rowIndex+'&removeitem=true',
							onComplete : function() {
								BzCartWidget.prototype.hideLoader();	
								var flObj = new fx.Flash('cart_contents', {color_from:"#ffffff", color_to:"#F0F5F9", count:1, transition:fx.circ, duration:300});
								flObj.toggle();
								if(ie) {
									fnLoadPngs();
									fnLoadImPngs();									
								}								
							}
						}		
					);	
			}
		},
	displayFull : function(el) {
		new AjaxUpdaterObject(el,'/bzajax','&ajaxaction=getCartContents','post');
	},
	displayCompact : function() {
		var options = {
			// Form method (post, get)
			method : 'post',
			// Parameters to pass to remote url
			parameters : '&ajaxaction=checkCartNotEmpty',
			// Connect mode
			asynchronous:true,
			// Server response on Success
			onSuccess : function(rsp) {
				if(rsp.responseText == "true") {
					new Ajax.Updater(
						'minicart',		
						'/bzajax',
						{	
							method:'post',
							postBody:'&ajaxaction=cartSummary',
							onComplete : function(){						
								new Effect.Appear($('minicart'));	
								if(ie) {
									setTimeout("fnLoadPngs()",200);
									setTimeout("fnLoadImPngs()",200);									
								}
							}
						}		
					);
				}
			},
			// On request error
			onFailure : function(rsp) {
				alert("Une erreur e15 s'est produite, veuillez vous reconnecter ultérieurement");
			}
		};
		// End Options
		// Do request
		new Ajax.Request('/bzajax', options);	
	},
	showLoader : function() {
		$('bzloader').style.left = 0 + "px";
		$('bzloader').style.top  = 0 + "px";
		
		var cElm = $('cartContainer');
		var loaderInitialPosition = Position.cumulativeOffset($(cElm));
		var loaderDimensions	= Element.getDimensions('bzloader');
		$('bzloader').style.display = '';
		$('bzloader').style.position = "absolute";
		$('bzloader').style.display = 'none';
		
		var oContainerElm = Element.getDimensions(cElm);
		var loaderPosLeft = Math.floor((oContainerElm.width-loaderDimensions.width)/2);
		var loaderPosTop  = Math.floor(loaderInitialPosition[1]-(loaderDimensions.height));
		
		// Calculate loader position inside rendering element
		$('bzloader').style.left =  loaderPosLeft + "px";
		$('bzloader').style.top = loaderPosTop + "px";
		$('bzloader').style.display = '';
	},		
	hideLoader : function() {
		Element.hide('bzloader');
	}	
};