﻿var Items = {
	Init:function(){		
		
		$$(".item a.expandLink").each(function(link){
			var itemId = link.id.substring(link.id.indexOf("_")+1);
			
			link.addReplacingEvent("click",function(e){
				Items.ViewInfoCategories(itemId);				
			});
			
		});
		
		$$(".catsList a").each(function(tab){			
			tab.addEvent("click",function(e){
				e = new Event(e).stop();				
				Items.InfoCategoryTabClick(tab.id);				
			});			
		});		
	},
	
	InfoCategoryTabClick:function(tabId){
		var itemId = tabId.substring(tabId.indexOf("_")+1, tabId.lastIndexOf("_"));
		var itemInfoCatId = tabId.substring(tabId.lastIndexOf("_")+1);
		Items.ViewInfoCategoryContent(itemId,itemInfoCatId);
		
		//remove the last tab for the same item
		var lastSelected = $(tabId).getParent().getElement("a[class=current]");
		if(lastSelected)lastSelected.removeClass("current");
		
		$(tabId).addClass("current");
	},
			
	ViewInfoCategories:function(itemId){
		// show the tabs
		var tabs = $("CatsList_"+itemId);
		if (!tabs)return;
		
		if (!tabs.visible()){
			tabs.show();
			$("ExpandLink_"+itemId).addClass("expandLinkOpened");
			// select the first tab and click it
			
			var tab = $("CatsList_"+itemId).getElement("a");			
			Items.InfoCategoryTabClick(tab.id);
			
		}else{
			// close the tabs and the contents
			tabs.hide();
			$("ExpandLink_"+itemId).removeClass("expandLinkOpened");
			Items.CloseAllTabs(itemId);
		}
		
	},
	
	CloseAllTabs:function(itemId){
		// content
		$$("#Item"+itemId +" .catContent").each(function(el){						
			el.hide();
		});
		
		//tabs
		$$("#CatsList_"+itemId +" a").each(function(tab){
			if (tab.hasClass("current"))tab.removeClass("current");			
		});
		
	},
	
	ViewInfoCategoryContent:function(itemId, infoCategoryId){
		//close all the items
		//$("ExpandLink_"+itemId).addClass("expandLinkOpened");
		
		Items.CloseAllTabs(itemId);
		
		//open the requested one
		$("CatContent_"+infoCategoryId).show();
	}
}

$DL(Items.Init);

