var Nav = Class.create({

	initialize: function(navContainer, openOnMouseOver)
	{
		this.mouseOpen = openOnMouseOver == true;
		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){
		if(lvl == undefined)
			lvl = 0;
		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];


		if(navigator.appName == "Microsoft Internet Explorer" && this._level == 1){
			this.iframe = new Element('iframe', {'style':'display:none; top:0px; left:0px; width:0px; height:0px; position:absolute; z-index:10;'});
			this.itemContainer.appendChild(this.iframe);
		}
		var idParts = navItem.id.split("_");
		this.id = idParts[idParts.length-1];

		var subChildren = this.itemContainer.getElementsByClassName("children");//this.itemContainer.getElementsByTagName("UL");

		if(subChildren.length > 0){
			this.childContainer = this.itemContainer.getElementsByClassName("child_container")[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));

		if(this.root.mouseOpen){
			Event.observe(this.itemContainer, 'mouseover', this.onMouseOverContainer.bindAsEventListener(this));
			Event.observe(this.itemContainer, 'mouseout', this.onMouseOutContainer.bindAsEventListener(this));
			//if(this.childContainer){
				//Event.observe(this.childContainer, 'mouseover', this.onMouseOverChildContainer.bindAsEventListener(this));
			//	Event.observe(this.childContainer, 'mouseout', this.onMouseOutChildContainer.bindAsEventListener(this));
			//}
		}
	},
	open: function (recursive){
		if(this.childContainer){
			if(recursive == true){
				for(var i = 0; i < this.children.length; i++)
					this.children[i].open();
			}
			this.childContainer.style.display = "";
			this.childContainer.addClassName('copen');
			this.item.addClassName('open');

			var lvl = this.level();


			var pos = this.childContainer.getStyle('position');


			if(pos == 'absolute' && this.childContainer.hasClassName('popup')){
				var itl = this.item.getDimensions();
				var cs = this.childContainer.getDimensions();
				//this.childContainer.style.top = - cs.height + 'px';
				this.childContainer.style.bottom = itl.height + 'px';
			}

			if(this.iframe){
				this.iframe.style.display = "";
				if(pos == 'absolute')
					this.childContainer.style.position = 'relative';

				this.iframe.clonePosition(this.childContainer);


				if(pos == 'absolute')
					this.childContainer.style.position = 'absolute';

				this.iframe.clonePosition(this.childContainer);
			}

			this.item.fire("tree:open", this);
			this.root.navContainer.fire("tree:openItem", this);
			document.fire('Document:resize');
		}
	},
	close: function (recursive){
		if(this.childContainer){
			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.childContainer.removeClassName('copen');
				}
				if(this.iframe)
					this.iframe.style.display = "none";
				this.item.removeClassName('open');
				this.item.fire("tree:close", this);
				this.root.navContainer.fire("tree:closeItem", this);
				Event.fire(document, 'resize');

			}
		}
	},
	toggleOpen: function (){
		if(this.childContainer.style.display == "none")
			this.open();
		else
			this.close();
	},
	onMouseClick: function(event){
		//alert('dsdasd');
		this.toggleOpen();
	},
	onMouseOver: function(event){
		if(this.isMouseLeaveOrEnter(event, this.item))
			this.item.addClassName('over');
	},
	onMouseOut: function(event){
		if(this.isMouseLeaveOrEnter(event, this.item))
			this.item.removeClassName('over');
	},
	onMouseOverContainer: function(event){
		if(this.isMouseLeaveOrEnter(event, this.itemContainer))
			this.open();
	},
	onMouseOutContainer: function(event){
		if(this.isMouseLeaveOrEnter(event, this.itemContainer))
			this.close();
	},
	onMouseOverChildContainer: function(event){
		if(this.isMouseLeaveOrEnter(event, this.itemContainer))
			this.mouseIsOverChild = true;
	},
	onMouseOutChildContainer: function(event){
		if(this.isMouseLeaveOrEnter(event, this.itemContainer)){
			this.mouseIsOverChild = false;
		}
	},
	isMouseLeaveOrEnter: function (e, handler) {
		if (e.type != 'mouseout' && e.type != 'mouseover')
			return false;
		var reltg = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement;
		while (reltg && reltg != handler)
			reltg = reltg.parentNode;
		return (reltg != handler);
	},
	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;
	}
});
