﻿$(document).ready(function(){
	showCurCat();
	initCat();
	initSubCat()
	
	//$(".accordeon").accordion({header: 'h2'}).ready(function(){
	
	$(".accordeon").ready(function(){
		if(!$.browser.opera){
			$("#catalogs > li").css("display","none");
		}
		else{
			$("#catalogs a[name]").parent().css("display","none");
		}
	});
	
});

function initCat(){
	var loc=new String(document.location);
	var maxHeight="0";
	$("#catalogs > li").each(function(){
		var tmpHeight=0;
		$(this).find("table").each(function(){
			if($(this).height()>tmpHeight)
				tmpHeight=$(this).height();
		});
		$(this).find("h2,p").each(function(){
			tmpHeight+=$(this).height();
		});
		if(tmpHeight>maxHeight){maxHeight=tmpHeight;}
	});
	maxHeight+=8;
	$("#catalogs").css("height",maxHeight);
	
	var bottomNote=$(".note");
	bottomNote.css("position","absolute");
	bottomNote.css("bottom","0");
	bottomNote.css("left","20px");
	
	$("#cat a").click(function(){
		var thisParent=$(this).parent();
		
		/* out if active */
		if(thisParent.hasClass("active"))return true;
		
		/* if inactive — what catalog to show */
		//alert($(this).attr("href"));
		if($(this).attr("href"))
			var to=$(this).attr("href").substring(1,$(this).attr("href").length);
		else{
			alert("There is no such anchor on the page");
			return false;
		}
		
		/* make this el active */
		$("#cat li[class=active]").removeAttr("class");
		//$("#cat li[class=active]").removeClass("active")
		$(this).parent().attr("class","active");
		
		/* make cat active */
		var catToShow=$(".values a[name="+to+"]").parent();
		showOnly(catToShow);
		return true;
	});
	/*
	if(loc.indexOf("#"))
		var anch=loc.substring(loc.indexOf("#")+1,loc.length);
		alert(anch);
	return true;
	*/
}

function showOnly(el){
	$("#catalogs .visible").hide(300,function(){$(this).removeAttr("class");});
	$(el).show(300,function(){$(this).toggleClass("visible")});
	return true;
}

function showCurCat(){
	var loc=new String(document.location);
	if(loc.indexOf("#")!=-1){
		to=loc.substring(loc.indexOf("#")+1,loc.length);
		if($("#cat>li a[href=#"+to+"]").length){		
			var catToShow=$(".values a[name="+to+"]").parent();
			$("#cat>li a[href=#"+to+"]").parent().attr("class","active");
			
			if($("#cat a:first").attr("href")==("#"+to))return true;
			else{$("#cat a:first").parent().removeClass("active");}
			
			showOnly(catToShow);
			return true;
		}
		$("#cat>li:first").attr("class","active");
		return false;
	}
	else $("#cat>li:first").attr("class","active");
}
function initSubCat(){
	var catN=0;
	$(".accordeon").each(function(){
		$(".t-wrp",this).parent().css("display","none");
		var subCat=$("h2",this);
		var length=subCat.length;
		var subCatVAlues=$(".brand-values",this);
		
		for(var i=0;i<length;i++){
			$(subCat[i]).attr("index",catN+"-"+i);
			$(subCatVAlues[i]).attr("index",catN+"-"+i);
		}

		$(subCat[0]).toggleClass("ui-state-active");
		$(">li:nth-child("+ (length+1) +")",this).css("display","block");
		
		subCat.click(function(){
			if(!($(this).hasClass("ui-state-active"))){
				var curHeading=this;
				
				if($(".visible .ui-state-active")){
					$(".visible .ui-state-active").removeClass("ui-state-active");
				}
				$(this).addClass("ui-state-active");
				
				
				var index=$(this).attr("index");
				$(".visible .brand-values:visible").hide(300,function(){
					$(".visible .brand-values[index="+index+"]").show(300);
				});
			}
		});
		catN++;
	});	
}