/*
	Biz&Go Tabs Widget
	Copyright Biz&Go 2006 - All rights reserved
	Author : Christophe Eblé
	
	----------------------------------------------
	@ version : 1.0
*/
var tabStart;
var directUrlAccess = false;

var BzTabsWidget = Class.create();
BzTabsWidget.prototype = {
  initialize: function (tabID){	
		// Get Current Categories
		tabStart = tabID;
		if(arguments[1]) {
			directUrlAccess = true;
		}
		var options = {
			// Form method (post, get)
			method : 'post',
			// Parameters to pass to remote url
			parameters : '&ajaxaction=getActiveCategoriesArray',
			// Connect mode
			asynchronous:true,
			// Server response on Success
			onSuccess : function(rsp) {
				var aCurrentCategories = eval('(' + rsp.responseText + ')');
				// Build Tabs Dynamically...
				BzTabsWidget.prototype.makeTabs($A(aCurrentCategories));
			},
			// On request error
			onFailure : function(rsp) {
				alert("Une erreur e16 s'est produite, veuillez vous reconnecter ultérieurement");
			}
		};
		// End Options
		// Do request
		new Ajax.Request('/bzajax', options);
	},
	makeTabs : function(Ar) {
		// Start iteration
		var tabItr = 0;
		Ar.each( function(category){
			// We got the whole tree
			// Building active tabs
			BzTabsWidget.prototype.addTab(category.cat_id,category.cat_name,category.css_class, category.ajax_service);
			if(category.cat_id == tabStart) {
				BzTabsWidget.prototype.makeActive(category.cat_id,category.css_class, category.ajax_service);
			}
			tabItr++;
		});
	},
	addTab : function(catID,catName,cssClass,ajaxService) {
		// create links
		var oTabElement=document.createElement('li');
		var oTabElementSpan=document.createElement('span');
		var first4 = catName.substring(4,0).toLowerCase();          
        var invisible = (first4 == "inv_");
		if (invisible)
		{
			oTabElementSpan.setAttribute('style',"display:none;visibility:hidden;");
			oTabElement.setAttribute('style',"display:none;visibility:hidden;");
			oTabElementSpan.style.display='none'; // bloody IE doesn't understand setAttribute('style') !!
			oTabElement.style.display='none'; // bloody IE doesn't understand setAttribute('style') !!
		}
		// assign 'href' attribute
		oTabElement.onclick = function () {
				bzTabs.makeActive(catID,cssClass,ajaxService);
		}
		oTabElement.setAttribute('id',catID);
		
		// add link labels
		oTabElementSpan.appendChild (
			document.createTextNode(catName)
		);
		oTabElement.appendChild(oTabElementSpan);
		$('bztab').appendChild(oTabElement);
	},
	makeActive : function(tabID,tabCSS,ajaxService) {	
	 $(tabID).className = 'selected';
	 $('tabcontents').style.display = 'none';
	 $('bztab').className = (tabCSS != "") ? tabCSS : 'bztab';
	 $('product_listing').style.display='none';
	 $('bzloader').style.display='block';
	 
		var loaderInitialPosition = Position.cumulativeOffset($('bodyMainBlock'));
		var loaderDimensions	= Element.getDimensions('bzloader');
		
		var oContainerElm = Element.getDimensions('bodyMainBlock');
		var loaderPosLeft = Math.floor(loaderInitialPosition[0]+((oContainerElm.width-loaderDimensions.width)/2));
		var loaderPosTop  = Math.floor(loaderInitialPosition[1]+((oContainerElm.height-loaderDimensions.height)/2)+200);
		
		// Calculate loader position inside rendering element
		$('bzloader').style.left =  loaderPosLeft + "px";
		$('bzloader').style.top = loaderPosTop + "px";	 

	 this.getContent(tabID,tabCSS,ajaxService);
	 	 
	 for (i=0; i <  $('bztab').childNodes.length; i++) {
			node =  $('bztab').childNodes[i];
			if (node.nodeName.toLowerCase()=="li") {
				// Clear All Selected Tabs
				if(node.id != tabID) {
					node.className = '';
				}
			}
		}
	},
	getContent : function(tabID, tabCSS, ajaxService) {
		// Retrieving content
		var tabCSS = tabCSS;
		new Ajax.Updater(
			'tabcontents',		
			'/bzajax',
			{	
				method:'post',
				postBody:'&ajaxaction=getCategoryContentsByID&category_id='+tabID,
				onComplete : function(){
					new Effect.BlindDown('tabcontents', {
						duration:.6, 
						fps:30
					});
					if(!directUrlAccess) {
						$('product_listing').style.display='none';
						if(ajaxService != "" && ajaxService != null) {
							// Call Ajax Service
							new Ajax.Updater(
									'product_listing',		// Element ID that receives the response text
									'/bzajax',				// URL to send data to with parameters
									{	
										method:'post',
										postBody:'&ajaxaction='+ajaxService,
										onComplete : function() {
											$('product_listing').style.display='block';
											Element.hide('bzloader');
											if (ie) {
												fnLoadPngs();
												fnLoadImPngs();
											}
											if(ajaxService=="getHomePageContents") {
												createBzScroller();
											}											
										}
									}		
							);
						}
						else {
							bzCatalog.getContent(tabID,tabCSS);
						}
					}
					else {
						$('product_listing').style.display='block';
						Element.hide('bzloader');
					}
				}
			}		
    );
	}
};

