/* Category Menu */

var catCurrentOpen = null;

addLoadEvent(initLeftNav);

function initLeftNav() {
	if(document.getElementById('navid')) {
		if(document.getElementById(document.getElementById('navid').value)) {
//			showNextCatLevel(document.getElementById(document.getElementById('navid').value));
		}
	}
}

function showNextCatLevel(elm) {	
	if(catCurrentOpen==elm) {
		change_menu_state(catCurrentOpen, "off");
		catCurrentOpen = null;
		return false;
	}

	if(catCurrentOpen != null) {
		change_menu_state(catCurrentOpen, "off");
	}

	catCurrentOpen = elm;
	return change_menu_state(elm, "on");
}

function change_menu_state(elm, state) {
	if(state=="on") {
		classN = "on";
		display = "block";
	} else if(state=="off") {
		classN = "";
		display = "none";
	} else {
		return false;	
	}
	
	result = true;
	
	navNodes = elm.parentNode.childNodes;
	for(i=0; i<navNodes.length; i++) {
		if(navNodes[i].tagName == "UL") {
			navNodes[i].style.display = display;
			//yes this is the opposite of what you'd expect - return false if there are items under the menu and follow the a link if there are not
			result = false;
		}
	}
	
	elem = elm.parentNode;
	do {
		if(elem.tagName == 'UL') {
			elem.style.display = display;
		}
		if(elem.tagName == 'LI') {
			elem.className = classN;
		}
		if(elem.parentNode.id == "mainNav") {
			break;
		}
		elem = elem.parentNode;
	} while (elem.parentNode);
	
	return result;
}



/* Right Menu */

var currentOpen = null;
var divHeight = 250;
var numItems;
var headingHeight = 30;
var queueItem = null;
var menusReady = new Array();

addLoadEvent(initMenu);

function initMenu() {
	c = 0;
	if(!document.getElementById('rightMenu')){
		return;
	}
	n = document.getElementById('rightMenu').childNodes;
	for(i=0; i<n.length; i++) {
		if(n[i].tagName == 'DIV') {
			c++;
		}
	}
	numItems = c;
	if(document.getElementById('right_menu_open')) {
		startOpen = document.getElementById('right_menu_open').value;
	} else {
		startOpen = 0;
	}

	for(var i=0; i<numItems; i++) {
		if(i <= startOpen) {
			addOn = 0;
		} else {
			addOn = divHeight;
		}
		if(i == startOpen) {
			document.getElementById('rightMenuHead'+i).className = "on";
		}
		document.getElementById('rightMenu'+i).style.height = parseInt(divHeight+headingHeight)+"px";
		document.getElementById('rightMenu'+i).style.top = parseInt((i*headingHeight)+addOn)+"px";
		menusReady[i] = 1;
	}
	document.getElementById('rightMenu').style.height = parseInt(divHeight+(numItems*headingHeight))+"px";
	currentOpen = parseInt(startOpen);
}

function openRightMenu(id) {
	if(id != currentOpen) {
		if(checkMenuReady() == true) {
			
			if(id<currentOpen) {
				for(var i=parseInt(id+1); i<=currentOpen; i++) {
					changeMenu(i, '+');
				}
			} else {
				for(var i=parseInt(id); i>currentOpen; i--) {
					changeMenu(i, '-');
				}
			}
			currentOpen = id;
			
			for(var i=0; i<numItems; i++) {
				if(document.getElementById('rightMenuHead'+i).className == "on") {
					document.getElementById('rightMenuHead'+i).className = "off";
				}
			}
			document.getElementById('rightMenuHead'+id).className = "on";
		} else {
			queueItem = id;
		}
	}
	return false;
}

function changeMenu(menuid, direction) {
	menusReady[menuid] = 0;
	
	elm = document.getElementById('rightMenu'+menuid);
	
	if(direction == "+") {
		
		startPos = parseInt(elm.style.top.replace("px", ""));
		endPos = parseInt((divHeight+(numItems*headingHeight))-((numItems-menuid)*headingHeight));
		
		dist = parseInt(endPos - startPos);
		speed = parseInt(dist/4);
		if (speed == 0) speed = 1;
		
		elm.style.top = eval("(parseInt(elm.style.top.replace('px', ''))"+ direction + speed + ") + 'px'");
		
		if(parseInt(elm.style.top.replace("px", "")) < endPos) {
			setTimeout("changeMenu('"+menuid+"', '"+direction+"')", 20);
		} else {
			menusReady[menuid] = 1;
			if(checkMenuReady() == true && queueItem!=null) {
				openRightMenu(queueItem);
				queueItem = null;
			}
		}
	} else {
		startPos = parseInt(elm.style.top.replace("px", ""));
		endPos = parseInt(menuid*headingHeight);
		
		dist = parseInt(startPos - endPos);
		speed = parseInt(dist/4);
		if (speed == 0) speed = 1;
		
		elm.style.top = eval("(parseInt(elm.style.top.replace('px', ''))"+ direction + speed +") + 'px'");
		
		if(parseInt(elm.style.top.replace("px", "")) > endPos) {
			setTimeout("changeMenu('"+menuid+"', '"+direction+"')", 20);
		} else {
			menusReady[menuid] = 1;
			if(checkMenuReady() == true && queueItem!=null) {
				openRightMenu(queueItem);
				queueItem = null;
			}
		}
	}
}

function checkMenuReady() {
	for(var i=0; i<numItems; i++) {
		if(menusReady[i] == 0) {
			return false;
		}
	}
	return true;
}

function hideCartAdded(){
	if(document.getElementById("cartAdded")){
		document.getElementById("cartAdded").style.display = 'none';
	}
}

