var Nav = Class.create({

	initialize: function(navContainer)
	{
		this.navContainer = $(navContainer);
		this.itemList = new Object();
		if(this.navContainer.tagName.toUpperCase() == "UL")
			this.parseChildren();
		else if(this.navContainer.tagName.toUpperCase() == "XML")
			this.createNavFromXML();
	},

	parseChildren: function(node, pid, parent, lvl){
		lvl = Number(lvl);
		
		if(node == undefined)
			node = this.navContainer;
		var children = node.childElements();
		var childItems = new Array();
		for(var i = 0; i < children.length; i++){
			var child = children[i];
			var idParts = child.id.split("_");
			var id = null;
			if(idParts[idParts.length-2] == "item"){
				//var subChildren = child.getElementsByTagName("UL");
				id = idParts[idParts.length-1];
				var c = new NavItem(this, child, pid, parent, lvl, i);
				childItems.push(c);
				this.itemList[id] = c;
				//this.itemList[id] = new NavItem(this, child, pid);
				//if(subChildren.length > 0)
				//	this.initNav($(subChildren[0]), id);
			}
		}
		return childItems;
	},
	createNavFromXML: function(){

	},
	getItemById: function(id){
		return this.itemList[id];
	}
});

var NavItem = Class.create({
	initialize: function(root, navItem, parentId, parent, level, idx){
		this.root = root;
		this.itemContainer = navItem;
		this.parentId = parentId;
		this._parent = parent;
		this._level = level;
		this._index = idx;
		this.item = this.itemContainer.getElementsByTagName("DIV")[0];
		var subChildren = this.itemContainer.getElementsByTagName("UL");

		var idParts = navItem.id.split("_");
		this.id = idParts[idParts.length-1];

		var subChildren = this.itemContainer.getElementsByTagName("UL");

		if(subChildren.length > 0){
			this.childContainer = $(subChildren[0]);
			Event.observe(this.item, 'click', this.onMouseClick.bindAsEventListener(this));
			this.children = this.root.parseChildren(subChildren[0], this.id, this, level+1);
		}
		Event.observe(this.item, 'mouseover', this.onMouseOver.bindAsEventListener(this));
		Event.observe(this.item, 'mouseout', this.onMouseOut.bindAsEventListener(this));
	},
	open: function (recursive){
		
		if(recursive == true){
			for(var i = 0; i < this.children.length; i++)
				this.children[i].open();
		}
		this.childContainer.style.display = "";
		this.item.addClassName('open');
		this.item.fire("tree:open", this);
		this.root.navContainer.fire("tree:openItem", this);
	},
	close: function (recursive){
		if(!this.itemContainer.hasClassName('lockopen')){
			if(recursive == true && this.children){
				for(var i = 0; i < this.children.length; i++)
					this.children[i].close();
			}
			
			if(this.childContainer)
				this.childContainer.style.display = "none";
			this.item.removeClassName('open');
			this.item.fire("tree:close", this);
			this.root.navContainer.fire("tree:closeItem", this);
		}
	},
	toggleOpen: function (){
		if(this.childContainer.style.display == "none")
			this.open();
		else
			this.close();
	},
	onMouseClick: function(event){
		//alert('dsdasd');
		this.toggleOpen();
	},
	onMouseOver: function(event){
		//this.open();
		if(!this.item.hasClassName('over'))
			this.item.toggleClassName('over');
		//alert(this.item.className);

	},
	onMouseOut: function(event){
		if(this.item.hasClassName('over'))
			this.item.toggleClassName('over');

	},
	parent: function(){
		return this._parent;
	},
	level: function(){
		return this._level;
	},
	index: function(){
		return this._index;
	},
	firstChild: function(){
		return this.children[0] ? this.children[0] : null;
	},
	nextSibling:function(){
		if(this._parent && this._parent.children.length > this._index)
			return this._parent.children[this._index + 1] ;
		return null;
	},
	prevSibling:function(){
		if(this._parent && this._parent.children.length > 1 && this._index != 0)
			return this._parent.children[this._index - 1] ;
		return null;
	}
});