// JavaScript Document
// This will go through and find all div.secondaryNavigation elements, and turn them into javascript run navigation blocks.


var SecondaryNavigations = {
	Init : function (){
		//Parse document, get all div.secondaryNavigations and set them up to expand/collapse via javascript.
		var secondaryNavigations = new Array();
		elementGetElementsByClass(document, "div", "secondaryNavigation", secondaryNavigations);

		for (var i=0;i<secondaryNavigations.length;i++){
			var sNavEl = secondaryNavigations[i];
			var newSNav = new SecondaryNavigation(sNavEl, this.collection.length);
			this.collection.push(newSNav);
		};	
	},
	
	GetNavByID : function(id){
		var nav = this.collection[id];
		return nav;	
	}
};
SecondaryNavigations.collection = new Array();

SecondaryNavigation.prototype.constructor = SecondaryNavigation();
function SecondaryNavigation(navigationElement, indexPoint){
	if (navigationElement != undefined){
		//Initialize secondary navigation.
		this.element = navigationElement;
		this.subLinks = new Array();
		this.navigationID = indexPoint;
		
		var ulEl = this.element.firstChild;
		while (ulEl.nodeName.toLowerCase() != "ul" && ulEl.nextSibling != undefined){
			ulEl = ulEl.nextSibling;
		};
		
		if (ulEl.nodeName.toLowerCase() != "ul"){
			this.element.innerHTML = this.element.innerHTML + "<p/>This navigation does not have a UL element.";
			return;
		};
				
		var links = new Array();
		links = ulEl.childNodes;
		
		//parse through, find each root link. Set all links to closed unless they are selected or their children are selected.
		for (var i=0;i<links.length;i++){
			var cNode = links[i];
			if (cNode.nodeName.toLowerCase() == "li"){
				var nLink = new Link(cNode, this.navigationID, this.subLinks.length, null);
				this.subLinks.push(nLink);
			};
		};
	};
};

SecondaryNavigation.prototype.getRootLinkByID = function(id){
	return this.subLinks[id];
};

Link.prototype.constructor = Link();
function Link(linkElement, navigationID, indexPoint, parentLinkID){
	if (linkElement != undefined){
		this.element = linkElement;
		this.openstate = null;
		this.navigationID = navigationID;
		this.linkID = indexPoint;
		
		var linkElement_A_tag = this.element.firstChild;
		while (linkElement_A_tag.nodeName.toLowerCase() != "a" && linkElement_A_tag.nextSibling != undefined){
			linkElement_A_tag = linkElement_A_tag.nextSibling;
		};
		
		if (linkElement_A_tag.nodeName.toLowerCase() != "a"){
			linkElement.innerHTML = "This link does not have an A tag." + linkElement.innerHTML;
			return;
		};
				
		this.linkElement = linkElement_A_tag;
		this.linkElement.onmousedown = function(e){killBubble(e);};
		this.linkvalue = linkElement_A_tag.getAttribute("href").toLowerCase();
		
		disableTextSelect(this.element);		
		
		this.parentlink = null;
		//if (parentLinkID != null){ this.parentLink = SecondaryNavigations.GetNavByID(navigationID).getRootLinkByID(parentLinkID);

		this.hasChildren = false;		
		//Check for any LI inside this element, if there are, set hasChildren to true.
		this.sublinkelements = new Array(); // list elements that are children of the current Link.
		this.subLinks = new Array(); //Link objects that are children of the current Link object.
		elementGetElementsByClass(this.element, "li", "link", this.sublinkelements);
		
		if (this.sublinkelements.length > 0){
			this.hasChildren = true;	
		
			//Create sub link Objects.
			for (var i=0;i<this.sublinkelements.length;i++){
				var sublinkEl = this.sublinkelements[i];
				var nSubLink = new Link(sublinkEl, navigationID, this.subLinks.length, this.linkID);
				this.subLinks.push(nSubLink);
			};
		};
		
		if (this.hasChildren){
			var thisLink = this;
			this.element.onmousedown = function(){thisLink.Open()};		
		} else {
			addClass(this.element, "nochildren");
		};
		
		//Check if this link should be open. FULL URL must be submitted as the HREF of the A tag in the menu item.
		
		if (this.linkvalue == unescape(String(window.location).toLowerCase()) || checkIfChildSelected(this.subLinks)){
			addClass(this.linkElement, "selected");
			this.Open();
		};
	};
};

Link.prototype.Open = function(){
	/*if (e != null) {
		//Firefox
		el = e.target;
	} else {
		//IE group.
		e = event;	
		el = event.srcElement;		
	};*/
	
	//Open link node.
	addClass(this.element, "open");
	var thisLink = this;
	this.element.onmousedown = function(){thisLink.Close()};
};

Link.prototype.Close = function(){
	//Close link node.
	removeClass(this.element, "open");
	var thisLink = this;
	this.element.onmousedown = function(){thisLink.Open()};
};


function checkIfChildSelected(subLinkArray){
	//Check if window.location equals any of the sub links.
	var selected = false;
	
	for (var i=0;i<subLinkArray.length;i++){
		var sublink = subLinkArray[i];
		if (sublink.linkvalue == unescape(String(window.location).toLowerCase())){
			addClass(sublink.linkElement, "selected");
			selected = true;
			break;
		};
	};
	
	return selected;
};