var currentMenu = null;

function addEvent(func) { 

    var agt=navigator.userAgent.toLowerCase();
    var is_konq = false;
    var kqPos = agt.indexOf('konqueror');
    if (kqPos !=-1) {                 
	       is_konq  = true;
    }

    if( window.addEventListener && !is_konq) { 
	window.addEventListener("load", func, false);
	return true;
    } 
    else if( document.addEventListener && !is_konq) {
	document.addEventListener('load',func,false);
	return true;
    }
    else if( window.attachEvent ) { 
	var r = window.attachEvent("onload", func);
	return r; 
    } else { 
	if( (typeof window.onload) != 'function' ) 
	{ window.onload = func; } 
	else 
	{
	    var oldonload = window.onload;
	    window.onload = function() { oldonload(); func(); }; 
	} 
    } 
}

function initMenu(menu, starter) {
    var placer = document.getElementById("menu");

    if (menu == null || starter == null) return;

    // oh this positioning got ugly

    if(placer.offsetTop > 110 ) //This would be the preferred way
    {
	menu.style.left = placer.offsetLeft + "px";
	menu.style.top = placer.offsetTop + 224 +  1 + "px"; 
    }
    else // For IE and its ilk's weird offsetTop
    {
	pos = findPos(placer);
	if(pos[0] > 0 && pos[1] > 0 )
	{
	    menu.style.left = pos[0] + "px";
	    menu.style.top = pos[1] + 224 + 1 + "px";
	}
	else // For IE Mac and it's complete brokeness
	{
	    menu.style.left = "10px";
	    menu.style.top = "352px";
	}
    }

    if(menu.className == 'active')
    { 
	menu.style.visibility = 'visible'; 
	currentMenu = menu; 
    }

    starter.onmousedown = function() {
	if (currentMenu) {
	    currentMenu.style.visibility = "hidden";
	}
	this.showMenu();
    }

    starter.showMenu = function() {
	menu.style.visibility = "visible";
	currentMenu = menu;
    }
    
    starter.onclick = function() { return false; };
}

function findPos(obj)
{
    var curleft = 0;
    var curtop = 0;
    if (obj.offsetParent)
    {
	while (obj.offsetParent)
	{
	    obj = obj.offsetParent;
	    curleft += obj.offsetLeft;
	    curtop += obj.offsetTop;
	}
    }
    return [ curleft, curtop ];
}

function doColors() {
    
    if(document.getElementById("menu"))
    {
	var menu = document.getElementById("menu");
   	for (var x=0;x<menu.childNodes.length;x++) {
	    if(menu.childNodes[x].nodeName.toUpperCase() == "UL")
	    {
		list = menu.childNodes[x];
		var colors = new Array();
		var count = 0;
		colors[0] = '#94af66';
		colors[1] = '#e5cc80';
		colors[2] = '#b8704d';
		colors[3] = '#b3c2d1';
		for (var x=0;x<list.childNodes.length;x++) {
		    if(list.childNodes[x].nodeName.toUpperCase() == "LI")
		    {
			setupMouses(list.childNodes[x].childNodes[0],colors[count]);
			count++;
			if (list.childNodes[x].getElementsByTagName("ul").length>0) {
				menuStarter = list.childNodes[x].getElementsByTagName("a").item(0);
				menuItem = list.childNodes[x].getElementsByTagName("ul").item(0);
				initMenu(menuItem, menuStarter);
			}
		    }
		}
	    }
	}
    }
}

function fixHeight() {
    if(document.getElementById("content"))
    {
	var content = document.getElementById("content");
	content.style.height = ""; 
	if(content.offsetHeight < 500 )
	{
	    content.style.height = "500px";
	} 
    }
}

function setupMouses(target, color)
{
    target.onmouseover = function() {
	this.style.color = color;
    }
    target.onmouseout = function() {
	this.style.color = '#142a3d';
    }
}

addEvent(doColors);
addEvent(fixHeight);
