/*
	Biz&Go Catalog Widget
	Copyright Biz&Go 2006 - All rights reserved
	Author : Christophe Eblé
	
	----------------------------------------------
	@ version : 1.0
*/

Effect.OpenUp = function(element) {
  element = $(element);
  new Effect.BlindDown(element, arguments[1] || {});
}

Effect.CloseDown = function(element) {
  element = $(element);
  new Effect.BlindUp(element, arguments[1] || {});
}

Effect.Combo = function(element) {
  element = $(element);
  if(element.style.display == 'none') { new Effect.OpenUp(element, arguments[1] || {}); }
  else { new Effect.CloseDown(element, arguments[1] || {}); }
}

var BzCatalogWidget = Class.create();
BzCatalogWidget.prototype = {
  initialize: function (){	
	},
	getContent : function(tabID,tabCSS) {
		// Retrieving content	
		this.tabID = tabID;
		this.tabCSS = tabCSS;
	
		new Ajax.Updater(
			'product_listing',		
			'/bzajax',
			{	
				method:'post',
				postBody:'&ajaxaction=getProductListing&category_id='+tabID,
				onComplete : function(){
					$('bzloader').style.display='none';
					new Effect.BlindDown('product_listing', {
						duration:.3, 
						fps:20
					});						
				}
			}		
    );
	},
	getProductDetail : function(prodID){
	  var tabID = this.tabID;
		var tabCSS = this.tabCSS;
		$('product_listing').style.display='none';
		$('bzloader').style.display='block';	
		new Ajax.Updater(
			'product_listing',		
			'/bzajax',
			{	
				method:'post',
				postBody:'&ajaxaction=getProductDetail&product_id='+prodID+'&category_id='+tabID,
				onComplete : function(){
					$('bzloader').style.display='none';
					//$('product_listing').innerHTML =  $('product_listing').innerHTML;
					// Enable Back Button event
					/*
					$('containerBackButton').onclick = function () {
						bzTabs.makeActive(tabID,''+tabCSS+'','');
					}
					*/
					new Effect.BlindUp('tabcontents', {
						duration:.3, 
						fps:30
					});						
					$('product_listing').style.display='block';
					initTabs();
				}
			}		
    );
	},
	getProductExpressBuyDetail : function(prodID){
	  var tabID = this.tabID;
		var tabCSS = this.tabCSS;
		$('product_listing').style.display='none';
		$('bzloader').style.display='block';	
		new Ajax.Updater(
			'product_listing',		
			'/bzajax',
			{	
				method:'post',
				postBody:'&ajaxaction=getProductExpressBuyDetail&product_id='+prodID+'&category_id='+tabID,
				onComplete : function(){
					$('bzloader').style.display='none';
					$('product_listing').className ='detailProduit';
					var buttons = document.getElementsByClassName('button');
					buttons.each( 
						function(btn){ 
							Element.show(btn); 
						}
					);
					new Effect.BlindUp('tabcontents', {
						duration:.6, 
						fps:30
					});					
					$('product_listing').style.display='block';
				}
			}		
    );
	},
	getCleanProductName : function(prodID) {
		var options = {
			// Form method (post, get)
			method : 'post',
			// Parameters to pass to remote url
			parameters : '&ajaxaction=getCleanProductName&product_id='+prodID,
			// Connect mode
			asynchronous:true,
			// Server response on Success
			onSuccess : function(rsp) {
				dhtmlHistory.add(rsp.responseText, prodID);
			},
			// On request error
			onFailure : function(rsp) {
				alert("Une erreur e12 s'est produite, veuillez vous reconnecter ultérieurement");
			}
		};
		// End Options
		// Do request
		new Ajax.Request('/bzajax', options);
	},
	addProductAndGoToCart : function(oForm) {
		// Form.serialize(oForm)
		var options = {
			// Form method (post, get)
			method : 'post',
			// Parameters to pass to remote url
			parameters : '&ajaxaction=addToCart&'+Form.serialize(oForm),
			// Connect mode
			asynchronous:true,
			// Server response on Success
			onSuccess : function(rsp) {
				document.location.href="/panier";
			},
			// On request error
			onFailure : function(rsp) {
				alert("Une erreur e13 s'est produite, veuillez vous reconnecter ultérieurement");
			}
		};
		new Ajax.Request('/bzajax', options);
	},
	addProductToCart : function (oForm) {
		// Form.serialize(oForm)
		var options = {
			// Form method (post, get)
			method : 'post',
			// Parameters to pass to remote url
			parameters : '&ajaxaction=addToCart&'+Form.serialize(oForm),
			// Connect mode
			asynchronous:true,
			// Server response on Success
			onSuccess : function(rsp) {
				new Ajax.Updater(
					'minicart',		
					'/bzajax',
					{	
						method:'post',
						postBody:'&ajaxaction=cartSummary',
						onComplete : function(){
							if (ie) {
								setTimeout("fnLoadPngs()",50);
								setTimeout("fnLoadImPngs()",50);
							}
							Element.show('minicart');
						}
					}		
				);
			},
			// On request error
			onFailure : function(rsp) {
				alert("Une erreur e14 s'est produite, veuillez vous reconnecter ultérieurement");
			}
		};
		// End Options
		// Do request
		new Ajax.Request('/bzajax', options);		
	}
};

