function getstyle(id)
{
	if(isDom2 && (o = document.getElementById(id))) {
		return o.style;
	}
	if(isNS && (o = document.layers[id])) {
		return o;
	}
	if(isIE && (o = document.all(id))) {
		return o.style;
	}
	return null;
}

function getelem(id)
{
	if(isDom2 && (o = document.getElementById(id))) {
		return o;
	}
	if(isNS && (o = document.layers[id])) {
		return o;
	}
	if(isIE && (o = document.all(id))) {
		return o;
	}
	return null;
}

function swpbutton(id, bool)
{
	var type = id.substring(0, id.indexOf('_'));
	var a = getstyle('a_'+id);
	var button = getstyle(id);
        if(bool) {
                button.backgroundImage = 'url('+aImg[type]+')';
                a.color = aColor[type];
        } else {
                button.backgroundImage = 'url('+nImg[type]+')';
                a.color = nColor[type];
        }
}

var timeout = 50;
var showtime = new Array();
var shown = false;

function submenu(id, bool)
{
	var smenu = getstyle('smpos_'+id);
	var now = new Date();
	
	// clear other submenus if new one is requested
	if(shown !== false && shown != id) {
		clearsub();
	}		
	
	if(bool) {
		// reset showtime if we are still within timeout and told to show again
		if(showtime[id] && ((showtime[id] + timeout) > Date.parse(now))) {
			showtime[id] = Date.parse(now);
			return;
		}
		smenu.visibility = 'visible';
		swpbutton('menu_'+id, true);
		showtime[id] = Date.parse(now);
		shown = id;
	} else {
		// do nothing if we are still within timeout and told to hide
		if(showtime[id] && ((showtime[id] + timeout) > Date.parse(now))) {
			return;
		}
		smenu.visibility = 'hidden';
		swpbutton('menu_'+id, false);
		showtime[id] = false;
	}
}

function clearsub()
{
	if(shown !== false) {
		var smenu = getstyle('smpos_'+shown);
	
		swpbutton('menu_'+shown, false);
		smenu.visibility = 'hidden';
		showtime[shown] = false;
		shown = false;
	}
}

function swpimg(id, src)
{
	img = getelem(id);
	newimg = new Image;
	newimg.src = src;
	img.src = newimg.src;
}

function menu_init()
{
	var i = 0;
	var j = 0;
	var m = false;
	var ma = false;
	var s = false;
	var sa = false;
	for(i = 0; (m = getstyle('menu_'+i)); i++) {
		m.backgroundImage = 'url('+nImg['menu']+');';
		a = getstyle('a_menu_'+i);
		a.color = nColor['menu'];
		if(getstyle('smenu_'+i+'_0')) {
			for(j = 0; (sm = getstyle('smenu_'+i+'_'+j)); j++) {
				sm.backgroundImage = 'url('+nImg['smenu']+');';
				sa = getstyle('a_smenu_'+i+'_'+j);
				sa.color = nColor['smenu'];
			}
		}
	}
}
