/***HTML TREE compression is required! ***/

JAME.Package('JAME.Components.TreeMenu');
JAME.Components.TreeMenu = function(options) {

	this.startingDepth      = options.startingDepth || 2;
	this.mainMenu          = options.mainMenu;
	this.breadCrumb		   = options.breadCrumb;
	this.mainMenu.isOpened = options.isOpened;
	this.expandedLI        = undefined;
	this.root			   = options.root || parse('ol.tree')[0];
	this.olList			   = this.root.getElementsByTagName('ol');
	this.onClick   	       = this.onClick||function(){};
	this.closedHeight      = options.closedHeight  || 20;
	this.openedHeight	   = options.openedHeight || 150;
	JAME.extend(this,new JAME.Events.EventDispatcher());
	this.ols=[];
	for(var i=0;i<this.olList.length;i++) this.ols.push(this.olList[i]);
	//hide all ols
	this.ols.each(function(elm,i) { elm.style.display='none'; });
	this.getMenuLis();
	this.handleMainMenuClickEvent();
	var self  = this;

};
JAME.Components.TreeMenu.prototype={
	getAncestorTree : function(li) {
		var ancestors = [];
		var ol= li.parentNode;
		var ols = ol.getElementsByTagName('ol');
		if(ols[0]) ancestors.push(ols[0]);
		ancestors.push(ol);		
		do {
			ol = ol.parentNode;
			if(ol && ol.nodeName.toLowerCase()==='ol') ancestors.push(ol);
		}while(ol && ol!==this.root);
		return ancestors;
	},
	hideExpandedTree : function(target) {	
		if(!this.expandedLI) return;
		//same level
		if(this.expandedLI.parentNode===target.parentNode) {
			JAME.CSS.setStyles(JAME.DOM.firstNode(this.expandedLI),{color:this.offColor});
			var ols = this.expandedLI.getElementsByTagName('ol');
			if(ols[0]) ols[0].style.display='none';
			return;
		}
		if(!this.isChild(this.expandedLI,target,this.root)) {

			do {

				if(this.expandedLI.nodeName.toLowerCase()==='li') {
					JAME.CSS.setStyles(JAME.DOM.firstNode(this.expandedLI),{color:this.offColor});
					if(this.expandedLI.getElementsByTagName('ol')[0] && this.expandedLI.parentNode!==target.parentNode) {
						this.expandedLI.getElementsByTagName('ol')[0].style.display='none';
					}
				}
				if(this.expandedLI.nodeName.toLowerCase()==='ol' && target.parentNode!==this.expandedLI) {
					this.expandedLI.style.display='none';
				}
				this.expandedLI=this.expandedLI.parentNode;

			} while(this.expandedLI && (this.expandedLI!==this.root && this.expandedLI!==target.parentNode));
		}

	},
	handleMainMenuClickEvent : function() {

		var self = this;
		JAME.Events.addListener(this.mainMenu,'mouseover', function(e) {

			e.stopPropagation();

			var target = e.target;

			if(target.tagName.toLowerCase()==='a') target=target.parentNode;
			if(target.tagName.toLowerCase()!=='li') return;

			if(self.expandedLI) {
				var hostname   = window.location.protocol+'//'+hostname+'/';
				var clickedURL = JAME.DOM.firstNode(target).href;
				if(clickedURL.indexOf(hostname)==-1)
					clickedURL=hostname+clickedURL;

				var seenURL = hostname+window.location.hash.replace('#/','');


				if(seenURL==clickedURL && self.expandedLI===target && self.mainMenu.isOpened) {
					self.closeMainMenu();
					return true;
				}
			}


			if(target.parentNode===self.root && target.getElementsByTagName('ol')[0]) {
				self.openMainMenu();
			}

			if(self.expandedLI && self.expandedLI.parentNode!==self.root) {

				var ancestors = self.getAncestorTree(self.expandedLI);
				var link=JAME.DOM.firstNode(ancestors[ancestors.length-2].parentNode).href;
				if(link && JAME.DOM.firstNode(target).href===link) {
					return true;
				}
			}
			
			self.hideExpandedTree(target);

			self.expandedLI=target;

			var submenu = target.getElementsByTagName('ol')[0];

			self.onClick(target,submenu);

			if(submenu) {
				var OLLiChildren = self.hideLiElements(submenu);
				//display the submenu
				self.displayOLElement(submenu);
				self.showLiElements(OLLiChildren);
				return true;
			}
			return true;
		});
	},
	getMenuLis : function() {
		var allLis = this.root.getElementsByTagName('li');
		this.headLinks=[];
		for(var i=0,ln=allLis.length;i<ln;i++)
			if(allLis[i].parentNode===this.root) this.headLinks.push(allLis[i]);
	},
	isChild : function(assumeParent,assumeChild,limit) {
		do {
			if(assumeChild===assumeParent) return true;
			assumeChild = assumeChild.parentNode;
		} while(assumeChild && assumeChild!==limit);
		return false;
	},
	hideLiElements : function(OLparent) {

		var lis = OLparent.getElementsByTagName('li')
		var alis=[];
		for(var i=0;i<lis.length;i++) { 

			if(lis[i].parentNode===OLparent){
				 alis.push(lis[i]);
				 JAME.CSS.setStyles(lis[i],{opacity:0});
			}
		}
		return alis;
	},
	showLiElements : function(lis) {
		lis.each(function(elm){
			JAME.CSS.setStyles(elm,{opacity:1});
		});
	},
	displayOLElement : function(ol) {
		if(ol) ol.style.display='block';
	},
	closeMainMenu : function() {
		new JAME.FX({duration:250}).Tween(this.mainMenu,{height:[this.closedHeight]});
		var ols = this.root.getElementsByTagName('ol');
		for(var i=0;i<ols.length;i++) ols[i].style.visibility='hidden';
		this.mainMenu.isOpened = false;	
	},
	openMainMenu : function() {
		new JAME.FX({duration:250}).Tween(this.mainMenu,{height:[this.openedHeight]});
		var ols = this.root.getElementsByTagName('ol');
		for(var i=0;i<ols.length;i++) ols[i].style.visibility='visible';
		this.mainMenu.isOpened = true;
	}
};
