var hoverColor = "#c0c0c0";

// Extend Behaviour 
Behaviour.addUnLoadEvent = function(func){
	var oldonunload = window.onunload;
		
	if (typeof window.onunload != 'function') {
		window.onunload = func;
	} else {
		window.onunload = function() {
			oldonunload();
			func();
		}
	}
};

var defaultRules = {
	'a' : function(element){
		element.onfocus = function(){
			this.blur();
		};
	},
	'#main_navi td .menuitem' : function(element){
		element.menuColor = RicoUtil.getElementsComputedStyle( element, "backgroundColor", "background-color");

		element.onmouseover = function(){
			// hover color
			this.menuColor = RicoUtil.getElementsComputedStyle( this, "backgroundColor", "background-color");
			this.style["backgroundColor"] = hoverColor;

/*
if(document.all){
	$("debug").innerHTML += "<pre>";
	$("debug").innerHTML += HTMLentities(this.parentNode.parentNode.parentNode.innerHTML);
	$("debug").innerHTML += "</pre>";
}
*/

			var SPANs = this.lastChild.getElementsByTagName("b"); //this.lastChild.getElementsByTagName("span");
			for(var i=0;i<SPANs.length;i++){
				SPANs[i].style["backgroundColor"] = hoverColor;

			}

			// show tooltip
			var e = document.getElementsByClassName("tooltip", this);
			if(e.length>0){
				// display tooltip
				e[0].style.display="block";
			}

		};

		element.onmouseout = function(){
			// hover color restore
			this.style["backgroundColor"] = this.menuColor;
			var SPANs = this.lastChild.getElementsByTagName("b"); // this.lastChild.getElementsByTagName("span");
			for(var i=0;i<SPANs.length;i++){
				SPANs[i].style["backgroundColor"] = this.menuColor;
			}

			// hide tooltip
			var e = document.getElementsByClassName("tooltip", this);
			if(e.length>0){
				// display tooltip
				e[0].style.display="none";
			}
		};
	}
}
Behaviour.register(defaultRules);
Behaviour.addLoadEvent(function(){
	var s = "";
	// round corners for the top elements of the left and right column
//	Rico.Corner.round('subnavigation', {corners:"top", compact:true});
//	Rico.Corner.round('search_container', {corners:"top", compact:true});

	/*
	 * reverse stacking order of main navi elements 
	 * because otherwise the tooltips might overlap
	 * and round corners 
	 */
	var TDs = $("main_navi").getElementsByTagName("td");
	for(var i = 0;i<TDs.length; i++){
		// reverse stacking
		TDs[i].style.zIndex = 1000-i;

		// position the tooltips
		// prevent them from being clipped on the right side
		var e = document.getElementsByClassName("tooltip", TDs[i]);
		if(e.length>0){
			e[0].style["position"]="absolute";
			e[0].style["visibility"]="hidden";
			e[0].style["display"]="block";
s += "parent w: "+e[0].offsetParent.offsetWidth+", w: "+e[0].offsetWidth+", x: "+e[0].offsetLeft+"\n";
s += "parent parent w: "+e[0].offsetParent.offsetParent.offsetWidth+"\n";
			// compare the width of the table with the width of the tooltip
			if(e[0].offsetParent.offsetWidth<e[0].offsetWidth) e[0].style["width"] = (e[0].offsetParent.offsetWidth-14)+"px";
			if(e[0].offsetParent.offsetWidth<e[0].offsetWidth+e[0].offsetLeft){
				e[0].style["left"] = (e[0].offsetParent.offsetWidth - e[0].offsetWidth)+"px";
			}
			e[0].style["visibility"]="";
			e[0].style["display"]="none";
		}

		// round corners
//		Rico.Corner.round(TDs[i].firstChild, {corners:"bottom", compact:true} );


	}

	// display logout button if user is logged in
	if(Cookies.getCookieValue("loggedin"))$("logout_button").style["display"]="";
/*
if(document.all){
	$("debug").innerHTML += "<pre>";
	$("debug").innerHTML += HTMLentities(s);
	$("debug").innerHTML += "</pre>";
} else console.log("debug: %s" ,s );
*/
});

function HTMLentities(t){
	 return t.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g, "<br>\n");
}