/**
 * Tree menu (ver.: 0.01)
 *
 * Copyright (c) 2007 Hristo Drumev [www.hdrumev.com]
 *
 * use parts from Credox framework [www.credox.org]
 */

function treeMenu( DOMElementId, ruleClass )
{
	if( !document.getElementById )
		return;
	eval( ( this.id = ( DOMElementId + 'menu' ) ) + '=this' );
	this.fade = { interval: 10, step: ( document.all ? 10 : 2 ) };
	this.DOMElement = document.getElementById( DOMElementId );
	this.menuList = [];
	this.ruleClass = ruleClass;

	this.toExpand = null;

	for( var i = this.DOMElement.childNodes.length; i--; )
	{
		var node = this.DOMElement.childNodes[i];
		if( node.tagName && node.tagName.toLowerCase() == 'ul' )
			this.menu = this.menu( node );
	}
	this.read();

	if( this.toExpand )
	{
		var node = this.toExpand;
		this.showMenu( node.parentNode.parentNode );
		node.menu.visible = true;
	}

	return this;
}

treeMenu.prototype.menu = function( ulElement )
{
	var menu = new Object();
	this.menuList[this.menuList.length] = menu;
	menu.item = [];
	menu.ul = ulElement;
	for( var i = menu.ul.getElementsByTagName( 'a' ).length; i--; )
	{
		var node = menu.ul.getElementsByTagName( 'a' )[i];
		if( node.parentNode.parentNode === menu.ul )
		{
			node.li = node.parentNode;
			node.hasChilds = node.li.getElementsByTagName( 'ul' ).length > 0;
			if( node.hasChilds )
			{
				for( var j = node.li.childNodes.length; j--; )
				{
					var subNode = node.li.childNodes[j];
					if( subNode.tagName && subNode.tagName.toLowerCase() == 'ul' )
						node.menu = this.menu( subNode );
				}
				this.hideMenu( node.menu.ul, true );
				this.setWidth( node.menu.ul );
				node.menu.visible = false;
				node.className = this.ruleClass;
				if( window.opera )
					Event.add( node, 'click', this.id + '.onclick(event)' );
				else
					Event.add( node, 'click', this.onclick );
			}
			node.tree = this;

			if( node.className == 'selected' )
			{
				this.toExpand = node;
				this.toExpand.menu = menu;
			}
			menu.item[menu.item.length] = node;
		}
	}
	return menu;
}

treeMenu.prototype.onclick = function( event )
{
	var node = Event.element( event );

	if( node.menu.visible )
		return;

	for( var i = 0; i < node.tree.menuList.length; i++ )
	{
		var menu = node.tree.menuList[i];
		if( menu.visible )
		{
			node.tree.hideMenu( menu.ul );
			menu.visible = false;
			break;
		}
	}
	node.tree.showMenu( node.menu.ul );
	node.menu.visible = true;


/*
	if( node.menu.visible )
	{
		node.tree.hideMenu( node.menu.ul );
		node.menu.visible = false;
	}
	else
	{
		node.tree.showMenu( node.menu.ul );
		node.menu.visible = true;
	}
*/
}

treeMenu.prototype.read = function()
{
	return;
  if( Cookie.get( this.DOMElement.id ) )
		var menusToShow = Cookie.get( this.DOMElement.id ).split( ',' );
	else
		return;
	for( var i = menusToShow.length; i--; )
	if( this.menuList[menusToShow[i]] )
	{
		this.menuList[menusToShow[i]].visible = true;
		this.showMenu( this.menuList[menusToShow[i]].ul, true );
	}
}

treeMenu.prototype.save = function()
{
	return;
  var showedMenus = [];
	for( var i = 0, len = this.menuList.length; i < len; i++ )
		if( this.menuList[i].visible )
			showedMenus[showedMenus.length] = i;
	Cookie.set( this.DOMElement.id, showedMenus.toString() );
}

treeMenu.prototype.showMenu = function( element, fast )
{
	if( !fast )
		fast = true;
	this.fade.element = element;
	if( fast ) //  || document.all
	{
		this.setOpacity( 100 );
		element.style.display = '';
		return;
	}
	this.fade.element = element;
	this.setOpacity( 0 );
	element.style.display = '';
	for( var i = 100/this.fade.step; i--; )
		window.setTimeout( this.id + '.setOpacity(' + ( this.fade.step*i ) + ' )', i*this.fade.interval );
}

treeMenu.prototype.hideMenu = function( element, fast )
{
	if( !fast )
		fast = true;
	element.style.display = 'none';
}

treeMenu.prototype.setOpacity = function( opacity )
{
	opacity = ( opacity >= 100 ) ? 99.9999 : opacity;
	this.fade.element.style.filter = "alpha(opacity:" + opacity + ")";
	this.fade.element.style.MozOpacity = this.fade.element.style.KHTMLOpacity = this.fade.element.style.opacity = opacity / 100;
}

treeMenu.prototype.setWidth = function( element )
{
	element.style.width = '100%';
}

// ------------------------------------------------------------------------------------------------

var tree = null;

Event.add( window, 'load', function(){ tree = new treeMenu( 'categories', 'sub' ) } );
Event.add( window, 'unload', function(){ if( tree ) tree.save() } );
