var BYUMenu = byu.Class();
var _pp = BYUMenu.prototype;

_pp.initialize = function(){
	this.nav = null;
	this.sideBar = null;
	this.flatList = null;
	this.level1Container = null;
	this.level2Container = null;
	this.level1Elements = [];
	this.level2Elements = [];
	this.level3Elements = [];
	this.menuRecall = {"level1":null};
	this.delay = 350;//menu load delay
	this.clickEvent = false;
};

_pp.restoreMenuState = function(){
	var menuCookie = byu.cookie.getCookie("menuState");
	if(menuCookie != null)//cookie value trumps currentlyDisplayed
	{
		this.menuRecall.level1 = menuCookie;
	}
	
	var params = byu.url.getPageParams();
	if(params.showRY && params.showRY == "true")//route y url flag trumps everything
	{
		for(var i=0; i<this.level1Elements.length; ++i) {
			if(this.level1Elements[i].innerHTML.match("Route Y")) {
				this.menuRecall.level1 = i;
				this.menuRecall.level2 = 0;
			}		
		}
	}

	/*if(this.menuRecall.level1 == null || this.level1Elements.length < this.menuRecall.level1 + 1)//show the first menu if there is no other menu selected
	{
		this.menuRecall.level1 = 0;
	}*/
	
	if(this.menuRecall.level1 != null && this.level1Elements.length > this.menuRecall.level1) {
		this.level1Elements[this.menuRecall.level1].show();
		if(this.menuRecall.level2 != null && this.menuRecall.level2 != "undefined" &&
				this.level2Elements[this.menuRecall.level2].childNodes.length > 0 && 
				this.level2Elements[this.menuRecall.level2].firstChild.childNodes.length === 2)//open the first drop down by default if level2 var is set
		{
			byu.event.fireEvent(this.level2Elements[this.menuRecall.level2].firstChild.lastChild,"mouseover");
		}
	}
};

_pp.preProcessMenuList = function(ul){
	if(!byu.util.IsDefined("byuConfig.dontAutoSelectCurrentlyDisplayed") || !byuConfig.dontAutoSelectCurrentlyDisplayed) {
		var levelClass = byu.util.getElementsByClassName('infoPage', document, 'BODY');
		if(levelClass.length > 0)//this is an infoPage page
		{
			var currentlyDisplayedElement = byu.util.getElementsByClassName('currentlyDisplayed', ul, 'li');
			if(currentlyDisplayedElement.length === 0)//none selected
			{
				currentlyDisplayedElement = null;
				
				// extract path from url
				var URL = unescape(location.href);
				var hostName = URL.match("http.*//[^/]+/");
				var filePath = URL.substring(hostName[0].length);
				var xend = URL.length;
				if(URL.indexOf("?") >= 0)//remove query string
				{
					xend = URL.indexOf("?");
				}
				else if(URL.indexOf("#") >= 0)//remove anchor references
				{
					xend = URL.indexOf("#");
				}
				var fileName = URL.substring(URL.lastIndexOf("/") + 1, xend);
				
				var aTags = ul.getElementsByTagName("a");
				
				if(byu.util.IsDefined("byuConfig.currentlyDisplayedMenuPath") && byuConfig.currentlyDisplayedMenuPath.length != 0)//check if they have explicitly set a path for the selected menu item
				{
					for(var i=0;i<aTags.length;i++)//first check against user set javascript variable
					{
						if(aTags[i].href.indexOf(byuConfig.currentlyDisplayedMenuPath) >= 0)//found a match
						{
							currentlyDisplayedElement = aTags[i].parentNode;
							break;
						}
					}
				}
				if(!currentlyDisplayedElement && filePath.length > 0) {
					for(var i=0;i<aTags.length;i++)//first check against full path
					{
						if(aTags[i].href.indexOf(filePath) >= 0)//found a match
						{
							currentlyDisplayedElement = aTags[i].parentNode;
							break;
						}
					}
				}
				if(!currentlyDisplayedElement && fileName.length > 0) {
					for(var i=0;i<aTags.length;i++)//then check against file name
					{
						if(aTags[i].href.indexOf(fileName) >= 0)//found a match
						{
							currentlyDisplayedElement = aTags[i].parentNode;
							break;
						}
					}
				}
			}
			else//is manually marked
			{
				currentlyDisplayedElement = currentlyDisplayedElement[0];
			}
			
			if(currentlyDisplayedElement)//mark the parent's as currentlyDisplayed
			{
				var markAsDisplayed = function(element){
					if(element.className != "popupMenus")//stop at top level ul
					{
						byu.css.addClass(element,"currentlyDisplayed");
						markAsDisplayed(element.parentNode);
					}
					return;
				};
				markAsDisplayed(currentlyDisplayedElement);
			}
		}
	}
};

//sublist is the UL that contains all of the second level links in it
_pp.appendMenuItem = function(level1Name, subList){
	var anchor = byu.createElement("a",{"href":"javascript:byu.none();"});
	byu.setInnerText(anchor, level1Name);
	
	var level2Element = this.createLevel2(subList);
	anchor.childMenu = level2Element;
	anchor.elementParent = this;
	level2Element.parentAnchor = anchor;
	
	this.level2Elements.push(level2Element);
	this.level2Container.appendChild(level2Element);
	
	this.initLevel1Element(anchor);
	
	anchor.index = this.level1Elements.length;
	this.level1Elements.push(anchor);
	this.level1Container.appendChild(anchor);
};

_pp.addLevel1DirectLink = function(anchor){
	this.level1Elements.push(anchor);
	this.level1Container.appendChild(anchor);
};

_pp.initLevel1Element = function(anchor){
	anchor.show = function(){
		$("Level2").style.visibility = "visible";
		$("Level2").style.display = "block";
		this.elementParent.hideLevel3Elements();
		this.elementParent.hideLevel2Elements();
		this.elementParent.setSelected(this);
		this.childMenu.style.display = "block";
		var expire = new Date();
		expire.setMinutes(expire.getMinutes() + 5);
		byu.cookie.setCookie("menuState", this.index, "/", expire);
	};
	
	byu.event.observe(anchor,"mouseover",byu.makeBound(anchor,function(){
		var element = this;//put this in scope that inner function can see
		this.timeOut = setTimeout(function(){
			element.show();
		},this.elementParent.delay);
	}));
	
	byu.event.observe(anchor,"mouseout",byu.makeBound(anchor, function(){
		if(this.timeOut) {
			clearTimeout(this.timeOut);
		}
		this.timeOut = null;
	}));
	
	byu.event.observe(anchor,"click",byu.makeBound(anchor,function(){
		this.elementParent.clickEvent = true;
		this.show();
	}));
};

_pp.populateMenus = function(){
	this.nav = $("Navigation");
	this.sideBar = $("SideBar");
	
	var list = byu.util.getElementsByClassName('popupMenus', document, 'UL');
	
	if(list.length > 0) {
		this.flatList = list[0];
		
		this.preProcessMenuList(this.flatList);
		
		this.level1Container = document.getElementById("Level1");
		this.level2Container = document.getElementById("Level2");

		//populate the level 1 links
		var linkList = byu.util.getImmediateChildrenByTagName(this.flatList, 'LI');//Get all the level1 LI
		
		for(var i=0; i<linkList.length; ++i) {
			var anchor = byu.createElement("a",{"href":"javascript:byu.none();"});
			var isDisplayed = false;
			if(i===0)
			{
				byu.css.addClass(anchor,"highlight");
			}
			
			var subList = byu.util.getImmediateChildrenByTagName(linkList[i], 'UL');
			if(subList.length > 0)//has children
			{
				byu.setInnerText(anchor,byu.util.getTextContent(linkList[i]));
				isDisplayed = this.currentlyDisplayedCheck(linkList[i],anchor);
				
				var level2Element = this.createLevel2(subList[0]);
				anchor.childMenu = level2Element;
				anchor.elementParent = this;
				level2Element.parentAnchor = anchor;
				
				this.level2Elements.push(level2Element);
				this.level2Container.appendChild(level2Element);
				
				this.initLevel1Element(anchor);
			}
			else//no children so may be an external link
			{
				var directLink = byu.util.getImmediateChildrenByTagName(linkList[i], 'a');
				if(directLink.length > 0)//there is a link
				{
					anchor = directLink[0];
					
					byu.css.addClass(anchor,"external");
					var isDisplayed = this.currentlyDisplayedCheck(linkList[i],anchor);
					if(isDisplayed) {
						anchor.href = "javascript:byu.none();";
					}
					anchor.title = "Click to go to page.";
				}
				else {
					anchor = null;
				}
			}
			if(anchor) {
				if(isDisplayed) {
					this.menuRecall.level1 = this.level1Elements.length;
				}
				anchor.index = this.level1Elements.length;
				this.level1Elements.push(anchor);
				this.level1Container.appendChild(anchor);
			}
		}
		
		this.nav.appendChild(this.level1Container);
		this.nav.appendChild(this.level2Container);
		
		
		if(this.sideBar && this.sideBar.innerHTML == "")//put in filler
		{
			var spacerUL = byu.createElement('ul',{"className":"emptyLevel3"});
			this.sideBar.appendChild(spacerUL);
		}
	}
	
	var pageLinks = $("PageLinks");
	if(pageLinks)
	{
		this.sideBar.appendChild(pageLinks);
	}
	
	this.includeNavBarSearch();
	this.restoreMenuState();
};

_pp.includeNavBarSearch = function(){
	var navigationBarSearchLayer = $("NavigationBarSearch");
	if(navigationBarSearchLayer) {
		this.level1Container.appendChild(navigationBarSearchLayer);
		navigationBarSearchLayer.style.display = "block";
	}
};

_pp.createLevel2 = function(ul){
	var levelContainer = byu.createElement("div",{"className":"menuContent"});
	
	var LIs = byu.util.getImmediateChildrenByTagName(ul,"LI");
	
	if(LIs.length > 0)//has list elements
	{
		for(var i=0; i<LIs.length; ++i) {
			var div = byu.createElement("div",{"className":"level2"});
			
			var anchor = byu.createElement("a",{"href":"javascript:byu.none();"});
			var isDisplayed = this.currentlyDisplayedCheck(LIs[i],anchor);
			
			var subUL = byu.util.getImmediateChildrenByTagName(LIs[i], 'UL');
			if(subUL.length > 0)//has children
			{
				byu.setInnerText(anchor,byu.util.getTextContent(LIs[i]));
				
				if(!isDisplayed) {
					var linkDiv = byu.createElement("div",{"className":"level3"});
					linkDiv.appendChild(subUL[0]);
					
					if(LIs[i].className.indexOf("longMenu")>=0)
					{
						byu.css.addClass(linkDiv,"longMenu");
					}
					
					anchor.childMenu = linkDiv;
					anchor.elementParent = this;
					linkDiv.parentAnchor = anchor;
					this.level3Elements.push(linkDiv);
					div.appendChild(linkDiv);
					
					byu.event.observe(anchor,"mouseover",byu.makeBound(anchor,function(evt){
						var element = this;//put this in scope that inner function can see
						this.timeOut = setTimeout(function(){
							element.elementParent.hideLevel3Elements();
							element.elementParent.setSelected(element);
							element.childMenu.style.display = "block";
						},this.elementParent.delay);
					}));
					
					byu.event.observe(anchor,"mouseout",byu.makeBound(anchor, function(){
						if(this.timeOut) {
							clearTimeout(this.timeOut);
						}
						this.timeOut = null;
					}));
					
					byu.event.observe(anchor,"click",byu.makeBound(anchor,function(){
						this.elementParent.hideLevel3Elements();
						this.elementParent.clickEvent = true;
						this.elementParent.setSelected(this);
						this.childMenu.style.display = "block";
						if(this.timeOut) {
							clearTimeout(this.timeOut);
							this.timeOut = null;
						}
					}));
				}
				else//copy submenu to left sidebar if level3 page
				{
					if(this.sideBar) {
						if(subUL[0] && subUL[0].childNodes.length > 0) {
							this.sideBar.appendChild(subUL[0]);
						}
					}
				}
				
				div.appendChild(anchor);
			}
			else//no children so may be an external link
			{
				var directLink = byu.util.getImmediateChildrenByTagName(LIs[i], 'a');
				if(directLink.length > 0)//there is a link
				{
					anchor = directLink[0];
					
					byu.css.addClass(anchor,"external");
					var isDisplayed = this.currentlyDisplayedCheck(LIs[i],anchor);
					if(isDisplayed) {
						anchor.href = "javascript:byu.none();";
					}
					anchor.title = "Click to go to page.";
					
					anchor.elementParent = this;
					byu.event.observe(anchor,"mouseover",byu.makeBound(anchor,function(evt){
						var element = this;//put this in scope that inner function can see
						this.timeOut = setTimeout(function(){
							element.elementParent.hideLevel3Elements();
						},this.elementParent.delay);
					}));
					byu.event.observe(anchor,"mouseout",byu.makeBound(anchor, function(){
						if(this.timeOut) {
							clearTimeout(this.timeOut);
						}
						this.timeOut = null;
					}));
				}
				else {
					div = null;
				}
				div.appendChild(anchor);
			}
			
			if(div) {
				levelContainer.appendChild(div);
			}
		}
	}
	return levelContainer;
};

_pp.currentlyDisplayedCheck = function(parent,target){
	if(parent.className.strip() === "currentlyDisplayed") {
		if(target.className.strip() === "external") {
			byu.css.addClass(target,"currentlyDisplayedExternal");	
		}
		else {
			byu.css.addClass(target,"currentlyDisplayed");
		}
		return true;
	}
	return false;
};

_pp.setSelected = function(target){
	var className = target.className.strip();
	
	if(className === "currentlyDisplayed"){
		byu.css.addClass(target,"currentlyDisplayedSelected");
		return;
	}
	if(className === "currentlyDisplayedSelected") {
		return;
	}
	byu.css.addClass(target,"selected");
};

_pp.closeMenu = function(){
	if(this.clickEvent === true){
		this.clickEvent = false;
		return;
	}
	this.hideLevel3Elements();
};

_pp.hideLevel2Elements = function(){
	byu.map(this.level2Elements, function(element){
		byu.css.removeClass(element.parentAnchor,"selected");
		byu.css.removeClass(element.parentAnchor,"currentlyDisplayedSelected");
		element.style.display = "none";
	});
};

_pp.hideLevel3Elements = function(){
	byu.map(this.level3Elements, function(element){
		element.style.display = "none";
		byu.css.removeClass(element.parentAnchor,"selected");
	});
};

byu.menu = new BYUMenu();
byu.event.observe(window, 'load', byu.menu.populateMenus);