
is = new browserDetect();
document.onmousedown = mouseDown;


function mouseDown(e) {
	if ((is.ns && e.which!=1) || (is.ie && event.button!=1)) return true;
	x = (is.ns)? e.pageX : event.x+document.body.scrollLeft;
	y = (is.ns)? e.pageY : event.y+document.body.scrollTop;
	
	if (is.ns4) route = routeEvent(e);
	
	if (!inside(cSubMenu,x,y)) menuDeselect();
	
	return true;
}

var sMainMenu;
var cMainMenu;
var cSubmenu;

sMainMenu = false;
cMainMenu = false;
cSubMenu = false;

function menuSelect(o,subItem) {
	
	menuDeselect();
	
	o.className = "on";
	cMainMenu = o;
	
	if (subItem) {
		cSubMenu = document.getElementById(subItem);
		oPage = document.getElementById("pageContent");
		
		cSubMenu.style.display = "block";
		maxLeft = getAbsLeft(oPage) + oPage.offsetWidth - cSubMenu.offsetWidth +10;
		cSubMenu.style.left = (getAbsLeft(o)<=maxLeft)? getAbsLeft(o) : maxLeft;
		//luft mellem menu og scroll down
		cSubMenu.style.top = getAbsTop(o) + o.offsetHeight - 5;
	}
}

function menuDeselect() {
	if (cMainMenu) {
		if (cMainMenu!=sMainMenu) {
			cMainMenu.className = "off";
			cMainMenu = false;
		}
	}
	if (cSubMenu) {
		cSubMenu.style.display = 'none';
		cSubMenu = false;
	}
}

function findSelected() {
	for (j=0; j<arguments.length - 1; j++) {
		o = document.getElementById(arguments[j]);
		for(i=0; i<o.childNodes.length - 1; i++) {
			if (o.childNodes.item(i).className=='on') {
				sMainMenu = o.childNodes.item(i);
			}
		}
	}
}
	
function getAbsTop(o) {
	y = 0;
	while (o.offsetParent!=null) {
		y += o.offsetTop;
		o = o.offsetParent;
	}
	return y;
}
function getAbsLeft(o) {
	x = 0;
	while (o.offsetParent!=null) {
		x += o.offsetLeft;
		o = o.offsetParent;
	}
	return x;
}

function inside(o,x,y) {
	l = getAbsLeft(o);
	r = l + o.offsetWidth;
	t = getAbsTop(o);
	b = t + o.offsetHeight;
	
	if (x>l && x<r && y>t && y<b) return true;
	else return false;
}