var treeMenu = Class.create();

treeMenu.prototype = {
	initialize: function() {
		this.tabElectro = $("mainMenu").getElementsByTagName("h3")[0];
		this.tabGarden = $("mainMenu").getElementsByTagName("h3")[1];
		this.menuElectro = $("menuElectro");
		this.menuGarden = $("menuGarden");
		this.togglers = $("mainMenu").getElementsByClassName("toggler");
		
		Event.observe(this.tabGarden, 'click', function(){
			this.toggleMenu(this.tabGarden, this.menuGarden, this.tabElectro, this.menuElectro);
		}.bind(this));
		
		Event.observe(this.tabElectro, 'click', function(){
			this.toggleMenu(this.tabElectro, this.menuElectro, this.tabGarden, this.menuGarden);
		}.bind(this));
		
		this.togglers.each (function(item){
			Event.observe(item, 'click', function(){
				this.toggleSubmenu(item);
			}.bind(this));
		}.bind(this));
		
	},
	
	toggleMenu: function(tabShow, menuShow, tabHide, menuHide) {
		this.header = $("header");
		tabShow.addClassName("active");
		menuHide.style.display = "none";
		menuShow.style.display = "block";
		tabHide.removeClassName("active");
		if (tabShow.className.match("garden"))
			this.header.className = this.header.className.replace("electroHeader", "gardenHeader");
		else
			this.header.className = this.header.className.replace("gardenHeader", "electroHeader");
	},
	
	toggleSubmenu: function(toggler) {
		this.list = toggler.parentNode;
		this.listClass = this.list.className;
		if (this.listClass.match("expanded")) {
			this.listClass = this.listClass.replace("expanded", "collapsed");
		}
		else {
			this.listClass = this.listClass.replace("collapsed", "expanded");
		}
		this.list.className = this.listClass;
	}
	
}

Event.observe(window, 'load', function(){
	WtreeMenu = new treeMenu();
});



// hleda prime potomky objektu, narozdil od getElementsByTagName
function objektDeti(objekt,jmeno_tagu)
{
var i,j,pole,deti,dite;
j    = 0;
pole = [];
deti = objekt.childNodes;
if (deti && deti.length>0)
	{
	for (i=0;i<deti.length;i++)
		{
		dite = deti[i];
		if (dite.nodeType==1 && dite.tagName==jmeno_tagu)
			{
			pole[j] = dite;
			j++;
			}
		}
	}
return pole;
}

// zmeni class pro UL a A na zavreno nebo otevreno podle aktualniho stavu
function navZavri(obj_A,obj_UL,podminka)
{
var a,b;
if (obj_A && obj_UL)	// jestlize existuje A a UL
	{
	// zmen class pro A
	a = 'a_otevren';
	b = 'a_zavren';
	obj_A.className  = podminka ? b : a;
	// zmen class pro UL
	a = 'ul_otevren';
	b = 'ul_zavren';
	obj_UL.className = podminka ? b : a;
	}
}

// akce pri onclick, proved rozbaleni nebo zabaleni
function navAkce(obj_A)
{
var a,b,i,o1,o2,o3, podminka;
podminka = (obj_A.className=='a_otevren');
o1 = objektDeti(obj_A.parentNode,'UL');			// z A do rodice, tj. LI a ziskat vsechny UL
if (o1.length>0)
	{
	// zavri vsechny vetve
	o2 = objektDeti(obj_A.parentNode.parentNode,'LI');	// z A do LI a do UL a ziskat vsechny LI uzly
	for (i=0;i<o2.length;i++)
		{
		o3 = objektDeti(o2[i],'UL');
		if (o3.length>0)
			{
			// zmen class pro UL cislo 0
			navZavri(objektDeti(o2[i],'A')[0], o3[0], true);
			}
		}
	// zmen class A i UL
	navZavri(obj_A, o1[0], podminka);
	return false;
	}
return true;
}

// vytvor rozbalovaci menu
function navPridej(o1)
{
var o2, i,j;		// o1, o2 jsou pole objektu
if (o1.length>0)
	{
	for (i=0;i<o1.length;i++)
		{
		// nastav pro vsechny A class a onclick, ktere maji ve stejnem LI aspon 1 UL
		o2 = objektDeti(o1[i],'A');
		for (j=0;j<o2.length;j++)
			{
			if (objektDeti(o1[i],'UL').length>0)
				{
				o2[j].className = 'a_zavren';
				o2[j].onclick   = new Function ('return navAkce(this)');
				}
			}
		// nastav pro vsechny UL class a spust opakovani funkce pro dalsi prime potomky
		o2 = objektDeti(o1[i],'UL');
		if (o2.length>0)
			{
			for (j=0;j<o2.length;j++)
				{
				o2[j].className = 'ul_zavren';
				navPridej(objektDeti(o2[j],'LI'));	// opakuj sam sebe pro vsechy dalsi LI
				}
			}
		}
	}
}

// pomocna funkce pro zjednoduseni vytvoreni
function navVytvor(id)
{
navPridej(objektDeti(objektDeti(document.getElementById(id),'UL')[0],'LI'));
}
