/*
 * Copyright (c) 2003, 2006 TRIEMAX Software. All rights reserved.
 *
 * $Revision: #2 $ $Date: 2004/12/23 $
 */

var DOM = document.getElementById ? true : false;
var OP = window.opera ? true : false;
var IE = !OP && document.all && (navigator.appName == "Microsoft Internet Explorer");
var MOZ = !OP && (navigator.appName == "Netscape");
var SAFARI = navigator.userAgent.indexOf("Safari") > -1;

var SIDEBAR_TOP = 120;

var aSidebar;
var aPageTop;
var aSidebarX;
var aSidebarY
var aTimer;
var aContainer;
var aCurMenu;
var aCurSubMenu;

//~ ----------------------------------------------------------------------------

function initialize()
{
	if(DOM)
	{
		initializeMainMenu("navbar");

		aContainer = document.getElementById("container");
		aSidebar = document.getElementById("sidebar");
		aPageTop = document.getElementById("totop");

		if(aSidebar != null)
		{
			if(IE)
			{
				positionSidebar();

				window.onscroll = triggerPositionSidebar;
				window.onresize = triggerPositionSidebar;

				document.getElementById("content").onmouseover = function (ev)
				{
					hideSubmenu();
				}

				document.getElementById("header").onmouseover = function (ev)
				{
					hideSubmenu();
				}

				document.getElementById("sidebar").onmouseover = function (ev)
				{
					hideSubmenu();
				}
			}
			else if((MOZ || OP) && !SAFARI)
			{
				aPageTop.style.display = "none";
				aPageTop.style.visibility = "hidden";

				initSideBar();
				mozPositionSidebar();

				if(OP)
				{
					window.window.onscroll = mozPositionSidebar;
				}
				else
				{
					// Mozilla does not fire onscroll on wheel mouse scrolling!
					window.setInterval("mozPositionSidebar()", 250);
				}

				window.onresize = initSideBar;
			}
		}
	}

    if (!NiftyCheck()) {
      return;
    }

    Rounded("div.contents", "#FFFFFF", "#E8EAEA", 7, 8);
    Rounded("div.related", "#FFFFFF", "#E8EAEA", 7, 8);
    Rounded("div.manual", "#FFFFFF", "#E8EAEA", 7, 8);
}

function initSideBar()
{
	var containerPosition = getPosition(aContainer);

	aSidebarX = (containerPosition.x + aContainer.offsetWidth - aSidebar.offsetWidth) + "px";
	aSidebarY = aSidebar.offsetTop;

	mozPositionSidebar();
}

//~ ----------------------------------------------------------------------------

function getOperaVersion()
{
	var regex = /.*Opera\s([0-9.]+)*/;

	regex.exec(navigator.userAgent);

	var result = parseFloat(RegExp.$1);

	return ((isNaN(result)) ? -1 : result);
}

//~ ----------------------------------------------------------------------------

function getPageY()
{
	if(IE)
	{
		return document.body.scrollTop;
	}
	else if(MOZ || OP)
	{
		return window.pageYOffset;
	}
	else
	{
		return -1;
	}
}

//~ ----------------------------------------------------------------------------

function getPageHeight()
{
	if(IE)
	{
		return document.body.offsetHeight - 30;
	}
	else if(MOZ ||OP)
	{
		return window.innerHeight - 30;
	}
	else
	{
		return -1;
	}
}

//~ ----------------------------------------------------------------------------

function getHeight(element)
{
	return element.offsetHeight;
}

//~ ----------------------------------------------------------------------------

function mozPositionSidebar()
{
	if(aSidebar == null) return;

	var pageHeight = getPageHeight();
	var sidebarHeight = getHeight(aSidebar);

	if((pageHeight > -1) && (sidebarHeight > -1) && (getPageY() > SIDEBAR_TOP))
	{
		aSidebar.style.position = "fixed";
		aSidebar.style.left = aSidebarX;
		aSidebar.style.marginTop = "2px";

		if(pageHeight > sidebarHeight)
		{
			aSidebar.style.top = "2px";
		}
		else
		{
			aSidebar.style.top = (pageHeight - sidebarHeight - 2) + "px";
		}

		aPageTop.style.display = "block";
		aPageTop.style.visibility = "visible";
	}
	else
	{
		aSidebar.style.position = "relative";
		aSidebar.style.top = "0px";
		aSidebar.style.left = "0px";
		aSidebar.style.marginTop = "80px";

		aPageTop.style.visibility = "hidden";
		aPageTop.style.display = "none";
	}
}

//~ ----------------------------------------------------------------------------

function positionSidebar()
{
	if(aSidebar == null) return;

	var pageHeight = getPageHeight();
	var sidebarHeight = getHeight(aSidebar);

	if((pageHeight > -1) && (sidebarHeight > -1))
	{
		var pageY = getPageY();

		if(pageY > SIDEBAR_TOP)
		{
			if(pageHeight > sidebarHeight)
			{
				aSidebar.style.top = (pageY + 2) + "px";
			}
			else
			{
				aSidebar.style.top = pageY /*- (sidebarHeight - pageHeight)*/ + 2;
			}

			aPageTop.style.display = "block";
			aPageTop.style.visibility = "visible";
		}
		else
		{
			aSidebar.style.top = 0;
			aPageTop.style.display = "none";
			aPageTop.style.visibility = "hidden";
		}
	}
	else
	{
		aSidebar.style.top = 0;
		aPageTop.style.display = "none";
		aPageTop.style.visibility = "hidden";
	}
}

//~ ----------------------------------------------------------------------------

function triggerPositionSidebar()
{
	if(aTimer != null)
	{
		window.clearTimeout(aTimer);
	}

	aTimer = window.setTimeout("positionSidebar()", 250);
}

//~ ----------------------------------------------------------------------------

function elementContains(elmOuter, elmInner)
{
	while (elmInner && elmInner != elmOuter)
	{
		elmInner = elmInner.parentNode;
	}

	if(elmInner == elmOuter)
	{
		return true;
	}

	return false;
}

//~ ----------------------------------------------------------------------------

function getPosition(elm)
{
	var position = { x:0, y:0 };

	if(elm != null)
	{
// 		position.y = elm.offsetHeight;

// 		var fixOpera = OP && getOperaVersion() < 7.5;

// 		while (elm)
// 		{
// 			position.x += elm.offsetLeft;

// 			if(fixOpera) {
// 				position.y += elm.offsetTop;
// 			}

// 			elm = elm.offsetParent;
// 		}


		position.x = findPosX(elm);
		position.y = findPosY(elm);
	}
	
	return position;
}

function findPosX(obj)
{
	var curleft = 0;
	
	if(obj.offsetParent)
	{
		while(true) 
		{
			curleft += obj.offsetLeft;

			if(!obj.offsetParent)
			{
				break;
			}

			obj = obj.offsetParent;
		}
	}
	else if(obj.x)
	{
		curleft += obj.x;
	}

	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;

	if(obj.offsetParent)
	{
		while(true)
		{
			curtop += obj.offsetTop;
	
			if(!obj.offsetParent)
			{
				break;
			}
	
			obj = obj.offsetParent;
		}
	}
	else if(obj.y)
	{
		curtop += obj.y;
	}

	return curtop;
}

//~ ----------------------------------------------------------------------------

function getRelativePosition(elm)
{
	var position = { x:0, y:0 };

	position.y = elm.offsetHeight;

	var fixOpera = OP && getOperaVersion() < 7.5;

	while (elm)
	{
		position.x += elm.offsetLeft;

		if(fixOpera) {
			position.y += elm.offsetTop;
		}

		elm = elm.offsetParent;

		if(elm.id == "navbar")
			break;
	}

	return position;
}

//~ ----------------------------------------------------------------------------

function setPageXY(elm, x, y)
{
	var parentXY = {x: 0, y: 0 };

	parentXY = getPosition(elm.offsetParent);

	elm.style.left = (x - parentXY.x) + 'px';
	elm.style.top = (y - parentXY.y) + 'px';
}

//~ ----------------------------------------------------------------------------

function getEventSource(e)
{
	if(e)
	{
		var relatedTarget = e.relatedTarget;
		// work around Gecko Linux only bug where related target is null
		// when clicking on menu links or when right clicking and moving
		// into a context menu.
		if(navigator.product == "Gecko" && navigator.platform.indexOf("Linux") != -1 && !relatedTarget)
		{
			relatedTarget = e.originalTarget;
		}

		return relatedTarget;
	}
	else if(window.event)
	{
		return window.event.fromElement;
	}
}

//~ ----------------------------------------------------------------------------

function hideSubmenu()
{
	if(aCurSubMenu != null)
	{
		aCurSubMenu.style.display = "none";
		aCurSubMenu = null;
	}
}

//~ ----------------------------------------------------------------------------

function isNavBarElement(elm)
{
	return elm.nodeName && (elm.nodeName == "LI" || elm.nodeName=="UL" || elm.nodeName=="A");
}

//~ ----------------------------------------------------------------------------

function getEventTarget(ev)
{
	if(ev)
	{
		return ev.target;
	}
	else if(window.event)
	{
		return window.event.toElement;
	}
}

//~ ----------------------------------------------------------------------------

function getMousePosition(ev)
{
	var position = { x:0, y:0 };

	if(!ev) var ev = window.event;

	if(ev.pageX || ev.pageY)
	{
		position.x = ev.pageX;
		position.y = ev.pageY;
	}
	else if(ev.clientX || ev.clientY)
	{
		position.x = ev.clientX + document.body.scrollLeft;
		position.y = ev.clientY + document.body.scrollTop;
	}

	return position;
}

//~ ----------------------------------------------------------------------------

function initializeMainMenu(rootId)
{
	var navBar = document.getElementById(rootId);

	navBar.onmouseover = function (ev)
	{
		var elm = getEventTarget(ev);

		if(elm.parentNode.parentNode == this)
		{
			var submenu = elm.nextSibling;

			if(submenu != null && submenu.style)
			{
				if(aCurSubMenu != submenu)
				{
					hideSubmenu();
				}
				else
				{
					return;
				}

				var position = getRelativePosition(elm);

				submenu.style.top = position.y + 1 + "px";
				submenu.style.left = position.x + "px";
				submenu.style.display = "inline";

				aCurSubMenu = submenu;

				if(IE)
				{
					submenu.style.left = (position.x-submenu.firstChild.offsetLeft) + "px";

					if((position.x + submenu.firstChild.offsetWidth) >= aContainer.offsetWidth)
					{
						submenu.style.left = aContainer.offsetWidth - submenu.offsetWidth + "px";
					}
				}
				else if((position.x + submenu.firstChild.offsetWidth) >= aContainer.offsetWidth)
				{
					//submenu.style.left = aContainer.offsetWidth - submenu.firstChild.offsetWidth + "px";
				}
			}
			else if(aCurSubMenu != null)
			{
				var sourceElem = getEventSource(ev);

				if((sourceElem.nodeName != "DIV" && elm.nodeName != "UL")
					|| (sourceElem.nodeName == "DIV" && elm.nodeName == "A"))
				{
					hideSubmenu();
				}
			}
		}
	}

	navBar.onmouseout = function (ev)
	{
		if(aCurSubMenu != null)
		{
			var mousePos = getMousePosition(ev);

			if(mousePos.y <= this.offsetTop)
			{
				hideSubmenu();
			}
			else if(mousePos.y > (this.offsetTop + this.offsetHeight))
			{
				var sourceElem = getEventSource(ev);

				if(IE)
				{
					var targetElem = getEventTarget(ev);

					if(!elementContains(this, targetElem) && targetElem.offsetTop < (this.offsetTop + this.offsetHeight))
					{
						hideSubmenu();
					}
				}
				else if(!elementContains(this, sourceElem))
				{
					hideSubmenu();
				}
			}
		}
	}
}