/*
Verticle menu v6 by Ed Liang
This is combining javascript DOM and css to control the visibility of navigation.
The struction of nav is inheritance style.
( Note: need to use firefox_dom_fix.js )
*/
// Variables
var activeItem = "";
// Load function after the page is loaded
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
		func();
		}
	}
}

// Initial navigation
function initNav() {
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;	
	navRoot = document.getElementById("nav");
	// Firefox DOM white space fix for firstChild
	navRoot = node_after(first_child(navRoot));
	setChoice("hide");
	for (i=0; i<navRoot.childNodes.length; i++) {
		node = navRoot.childNodes[i];
		setNav(node);
	}
}

// Set navigation
function setNav(node) {
	// Set actions
	setNavAction(node);
	// If there is text node, return false
	node2 = first_child(node);
	if(!node2)return false;
	// Check if it is active then expands
	if (node.getAttribute("id") == getNavActive() && node_after(node2)) {
		// Set defalut condition for expanded item
		node2.pressed = true;
		setNavDown(node2);
		setNavVisible(node2, "show");
		return false;
	}else{
		// Defalut hide sub-menu items
		setNavVisible(node2, "hide");
		// Set actions here if u want to disable the defalut expand item
		//setNavAction(node);
	}
	
}

// Set navigation actions
function setNavAction(node) {
	node2 = first_child(node);
	// If there is text node, return false
	if(!node2)return false;
	// Set menu items if it has sub menu items
	if (node.nodeType == 1 && node.nodeName=="LI" && node_after(node2)) {
		node2.onclick = function() {
			// Check if it is clicked again
			if(!this.pressed){
				closeSub();
				this.pressed = true;
				setNavDown(this);
				setNavVisible(this, "show");
				setChoice("show");
				closeMain(this.parentNode,"hide");
			}else{	
				this.pressed = false;
				setNavUp(this);
				setNavVisible(this, "hide");	
				setChoice("hide");
				closeMain(this.parentNode,"show");
			}
		}
	}
}

// setChoice
function setChoice(myClass) {
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;	
	navChoice = document.getElementById("myChoice");
	navChoice.className = myClass;
}

// close all sub-nav
function closeSub() {
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;	
	navRoot = document.getElementById("nav");
	// Firefox DOM white space fix for firstChild
	navRoot = node_after(first_child(navRoot));
	for (i=0; i<navRoot.childNodes.length; i++) {
		node = navRoot.childNodes[i];		
		node2 = first_child(node);
		setNavVisible(node2, "hide", true);
		setNavUp(node2);
	}
}

// close main nav
function closeMain(curNode, myClass) {
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;	
	//navMain = document.getElementById("Australia");
	//navMain.className = myClass;
	navRoot = document.getElementById("nav");
	// Firefox DOM white space fix for firstChild
	navRoot = node_after(first_child(navRoot));
	for (i=0; i<navRoot.childNodes.length; i++) {
		node = navRoot.childNodes[i];		
		if(node.id != curNode.id)node.className = myClass;
	}
}


// Set active visible
function setNavVisible(node, myClass, myPress) {
	if(!node)return false;
	if(myPress)	node.pressed = false;
	node_after(node).className = myClass;
}

// Set menu item image down
function setNavDown(node) {
	if(!node)return false;
	var myID = node.getAttribute("id");
	node.style.background = "url(/images/homepage/arrow-back.gif) left 4px no-repeat";
	node.style.padding = "0px 0px 0px 10px";
	node.parentNode.style.height = "100%";
}

// Set menu item image up
function setNavUp(node) {
	if(!node)return false;
	var myID = node.getAttribute("id");
	node.style.background = "url(/images/homepage/arrow-up.gif) right 4px no-repeat";
	node.style.padding = "0px 15px 0px 0px";
	node.parentNode.style.height = "20px";
}

// Set active menu
function setNavActive(title) {
	activeItem = title;
}
// Get active  menu
function getNavActive() {
	return activeItem;
}

// Call function
addLoadEvent(initNav);