var expanded = null; //id of the last expanded menu item
var locked = false;

var expandcount = 0;
var expandobj = null;

var collapsecount = 0;
var collapseobj = null;

var DEFAULT_DELAY = 50;

var MENU_open = "";

function MENU_getElementsByClassName(container, tagname, classname) {
	if(typeof(classname) == 'undefined') classname = '*';
	var result = new Array();
	var subs = container.getElementsByTagName(tagname);
	for(var i=0;i<subs.length;i++) {
		if((classname == '*') || (subs[i].className == classname)) result.push(subs[i]);
	}
	return result;
}

function isSameTagName(t1, t2) {
	if(t1 == null || typeof(t1) == 'undefined') return false;
	if(t2 == null || typeof(t2) == 'undefined') return false;
	return t1.toLowerCase() == t2.toLowerCase();
}

function MENU_getChildrenByClassName(container, tagname, classname) {
	if(typeof(classname) == 'undefined') classname = '*';
	var result = new Array();
	var subs = container.childNodes
	for(var i=0;i<subs.length;i++) {
		if(isSameTagName(subs[i].tagName, tagname)) {
			if((classname == '*') || (subs[i].className == classname)) result.push(subs[i]);
		}
	}
	return result;
}

function MENU_getParent(obj, tagname) {
	while(!isSameTagName(obj.tagName,tagname)) obj = obj.parentNode;
	return obj;
}

function focusMenu(e) {
	//if this menu is currently opened, displays a "-", other displays a "+"
	var obj = MENU_getParent(this,'div');
	if(expanded == obj.id) this.className = "aless";
	else this.className = "amore";
	
}

function blurMenu(e) {
	//disables any background +- sign for this menu
	this.className = "anormal";
}

function clickMenu(e) {
	//gets rid of accessibility focus
	//if(window.focus) window.focus();
	if(locked) return;
	locked = true;
	expandcount = collapsecount = -1;
	expandobj = collapseobj = null;
	var obj = MENU_getParent(this,'div');
	
	if(!e) e = window.event;
	e.cancelBubble = true;
	if(e.stopPropagation) e.stopPropagation();
	if(expanded == obj.id) { //clicked the currently expanded menu; collapses it
		collapseobj = obj;
		collapseMenu();
	}
	else if(expanded != null) { //expand menu, but first we collapse the expanded one
		collapseobj = document.getElementById(expanded);
		expandobj = obj;
		collapseMenu();
	}
	else { //expand new menu
		expandobj = obj;
		expandMenu();
	}
}

function expandMenu(delay) {
	if(typeof(delay) == 'undefined') delay = DEFAULT_DELAY;
	var subs = MENU_getElementsByClassName(expandobj,'div','submenuitem');
	if(expandcount == -1) expandcount = 0;
	if(expandcount == subs.length) {
		expanded = expandobj.id;
		locked = false;
	}
	else {
		subs[expandcount].style.display="block";
		expandcount++;
		if(delay > 0) setTimeout("expandMenu()", delay);
		else expandMenu(0);
	}
}

function collapseMenu(delay) {
	if(typeof(delay) == 'undefined') delay = DEFAULT_DELAY;
	var subs = MENU_getElementsByClassName(collapseobj,'div','submenuitem');
	if(collapsecount == -1) collapsecount = subs.length;
	if(collapsecount == 0) { //we're done
		expanded = null;
		if(expandobj != null) expandMenu(delay);
		else locked = false;
	}
	else {
		collapsecount--;
		subs[collapsecount].style.display="none";
		if(delay > 0) setTimeout("collapseMenu()", delay);
		else collapseMenu(0);
	}
}

function isOpera() {
	return navigator.userAgent.indexOf('Opera') != -1;
}

function isMSIE() {
	return navigator.userAgent.indexOf('MSIE') != -1;
}

function initHandlers() {
	var menus = MENU_getElementsByClassName(document.body,'div','menuitem');
	for(var i=0;i<menus.length;i++) {
		var children = MENU_getChildrenByClassName(menus[i],'a');
		if(menus[i].id == null) menus[i].id = "MENU"+i.toString(10); //default ids
		children[0].className = "anormal";
		if(MENU_getChildrenByClassName(menus[i],'div','submenuitem').length > 0) {
			//add events to "activable" menu
			children[0].onclick = clickMenu;
			children[0].onmouseover = focusMenu;
			children[0].onmouseout = blurMenu;
		}
	}
	//FIX icon flicker in IE6 -- but it could wreck Opera, so we validate this!
	try {
	  if(!isOpera()) document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
	//if necessary, trigger opening of menu according to context
	if(MENU_open != '') {
		expandobj = document.getElementById(MENU_open);
		expandcount = -1;
		expandMenu(0);
	}
}

