function TreeMenu()
{
	this.inherit = EventDispatcher;
	this.inherit();
	this.ON_CLICK = "onClick";
	this.url = "";
	
	var level = this;
	var selected;
	
	var animate = function(n){
		var l =  ($('#treeMenu :animated').length >n);
		return l;
	}
	
	var hasElement = function( parent, child )
	{
		return parent.find('[@href="'+child.attr('href')+'"]').length;
	}
	
	var redirect = function(n){
		if(n==undefined) n=0;
		if(!animate(n)){
			level.url = selected.attr('href');
			level.dispatchEvent(new Event(level.ON_CLICK));
		}
	}
	var refreshTree = function()
	{
		var refresh = false;
		$('#treeMenu .expandable').each( function(i){
			if( !hasElement( $(this).next(), selected ) && !refresh && (selected.attr('href') != $(this).attr('href') )){
				if( $(this).next().is(':visible')){
					var level = $(this).next().attr('class');
					if(level!="wrapper2"){
						$(this).next().slideUp('slow',function(){
							redirect(1);
							
						});
						refresh = true;
					}
				}
			}
		});
		return refresh;

	}
	var find = function(id)
	{
		return $("#treeMenu ").find("*[id='"+id+"']");
	}


	this.start = function()
	{
		$('#treeMenu .expandable').click( function(){
			selected = $(this);
			if($(this).next().is(":hidden")){
				$(this).next().slideDown("slow",function(){
						redirect(1);
				});
				refreshTree();
			}else{
				redirect();
			}
			return false;
		});
		$('#treeMenu .item').click( function(){
			selected = $(this);
			if(! refreshTree()){
				redirect();
			}
			return false;
		});
	}

}

