/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	ADxMenu.js - AD xMenu v2.50
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Copyright (c) Aleksandar Vacic, aleck@sezampro.yu, www.aplus.co.yu
	## This work is licensed under the Creative Commons Attribution-ShareAlike License.
	## To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/1.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Credits: Mike Foster for x functions (cross-browser.com)
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	optional usability script: checks for off-viewport positioning and windowed controls
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	central object: keeps track of viewport size, initializes menus
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
var ADXM_Constructor = function() {
	//	add the menu to the array - dummy for not-supporting browsers
	this.Add = function() {};
	//	deny all non-DOM browsers
	if (!document.getElementById && !document.documentElement) return;
	
	var self = this;

	//	deny non-supporting browsers
	var _ua = navigator.userAgent.toLowerCase();
	var _nv = (navigator.vendor) ? navigator.vendor.toLowerCase() : "";
	//	deny Opera before 7.5, PocketPC, gecko before rv:1.5, safari and omniweb before webkit 100 (should be Safari 1.1)
	//	typeof(XMLHttpRequest) != "undefined" ==> Safari 1.2.
	//	parseFloat(_ua.substring(_ua.indexOf("applewebkit/") + 12)) < 100 ==> Safari 1.1.
	if (
		_ua.indexOf("windows ce") != -1 ||
		( window.opera && parseFloat(_ua.substring(_ua.indexOf("opera ") + 6)) < 7.5 ) ||		
		( _ua.indexOf("gecko") != -1 && _ua.indexOf("khtml") == -1 && parseFloat(_ua.substring(_ua.indexOf("rv:") + 3)) < 1.5 ) ||
		( _nv.indexOf("apple") != -1 && typeof(XMLHttpRequest) != "undefined" )
	) return;

	var _bIE = (document.all && document.getElementById && !window.opera);

	//	keeps the menu IDs (of the main UL)
	var _aMenuids = new Array();
	//	keeps menu layouts
	var _aLayouts = new Array();

	//	add the menu to the array
	this.Add = function(sMenuID, sLayout, sDirection) {
		if ( typeof(sMenuID) == "undefined") return;
		_aMenuids[_aMenuids.length] = sMenuID;
		_aLayouts[_aLayouts.length] = (sLayout = _CastLayout(sLayout));
	};

	//	cast layout - can be H or V. if not sent, then it will be H
	function _CastLayout(sLayout) {
		var sRet = "H";
		if (typeof(sLayout) != "undefined") {
			sLayout = sLayout.toUpperCase();
			if (sLayout == "H" || sLayout == "V")
				sRet = sLayout;
		}
		return sRet;
	};

	//	sets properties, handlers and other stuff for menus
	function _Processmenu(oMenu, sLayout, oMainMenu) {
		var aMenuLIs = _GetChildsByTagName(oMenu, "LI");
		var oMenuLI, oUL, aUL;
		for (var i=0;i<aMenuLIs.length;i++) {
			oMenuLI = aMenuLIs[i];
			aUL = _GetChildsByTagName(oMenuLI, "UL");	//	UL elements in sub(main) item
			if (aUL.length)	{	//	has submenus
				//	save properties for positioning
				oMenuLI.submenu = aUL[0];
				oMenuLI.bIsH = (sLayout == "H");

				//	IE needs WCH and special preparation for it
				if ( _bIE ) {
					oMenuLI.parentMenu = oMenu;
					oMenuLI.mainmenu = oMainMenu;

					oMenuLI.onmouseover = function() {
						//	reposition menu, only in IE5.5+
						if (typeof(document.body.contentEditable) != "undefined") ADXM.Repos(this);
						//	apply WCH
						WCH.Apply(this.submenu, this, true);
						this.parentMenu.shownMenu = this.submenu;
					};

					oMenuLI.onmouseout = function() {
						//	discard WCH
						ADXM.HideWCH(this.mainmenu);
					};

				//	CSS2-enabled browsers are fine
				} else {
					oMenuLI.onmouseover = function() {
						//	reposition menu
						ADXM.Repos(this);
					};
				}

				//	process the submenu in the same manner. all submenus have vertical layout
				_Processmenu( aUL[0], "V", oMainMenu );
			} else if ( _bIE ) {
				oMenuLI.onmouseover = function() {
				};

				oMenuLI.onmouseout = function() {
				};
			}

		}// for aMenuLIs
	};

	//	process all menus
	function _Process() {
		var nMenus = _aMenuids.length, oMenu;
		for (var i=0;i<nMenus;i++) {
			oMenu = document.getElementById(_aMenuids[i]);
			if (oMenu) {
				_Processmenu( oMenu, _aLayouts[i], oMenu );
			}
		}
	};

	//	reposition menu to fit in the viewport
	this.Repos = function(oItem) {
		var nTmp;
		//	get the submenu pointer
		var oMenu = oItem.submenu;
		var bIsH = oItem.bIsH;

		//	menu position relative to parent?
		var nLeft = xLeft(oMenu);
		var nTop = xTop(oMenu);

		//	where is menu on the page?
		var nPageX = xPageX(oMenu);
		var nPageY = xPageY(oMenu);

		//	submenu size
		var nW = xWidth(oMenu);
		var nH = xHeight(oMenu);

		//	* now, check if submenu will go outside the visible window area
		//	get available client dims (with scrolling included)
		var nClientW = ADXM.nCW;
		var nClientH = ADXM.nCH;

		//	if we know client browser dims, re-position layer
		if ( nClientW != 0 && nClientH != 0 ) {
			//	Horizontal placement
			var nDiff = 0;
			if (nPageX < 0) {
				nDiff += -nPageX;
				if (!bIsH) nDiff += xWidth(oItem);
			} else {
				nTmp = nClientW - (nPageX + nW);
				if (nTmp < 0) {
					nDiff = nTmp;
					if (!bIsH) nDiff = -nW;
				}
			}
			if ( nDiff != 0 ) {
				xLeft(oMenu, nLeft + nDiff);
			}
			//	Vertical placement
			var nDiff = 0;
			if (nPageY < 0) {
				nDiff += -nPageY;
				if (bIsH) nDiff += xHeight(oItem);
			} else {
				nTmp = nClientH - (nPageY + nH);
				if (nTmp < 0) {
					nDiff = nTmp;
					if (bIsH) nDiff = -nH;
				}
			}
			if ( nDiff != 0 ) {
				xTop(oMenu, nTop + nDiff);
			}
		}
	};	

	//	fetch client width, including scrolled part
	this.nCW = 0;
	function _FetchCW() {
		var nTmp = xClientWidth();
		if (nTmp > 0) nTmp += xScrollLeft();
		self.nCW = nTmp;
	};

	//	fetch client height, including scrolled part
	this.nCH = 0;
	function _FetchCH() {
		var nTmp = xClientHeight();
		if (nTmp > 0) nTmp += xScrollTop();
		self.nCH = nTmp;
	};

	//	return array of 1st level child elements with specified tag name. someone pls tell me I missed this one in the DOM2 spec
	function _GetChildsByTagName(oNode, sNodeName) {
		var a = new Array();
		if (oNode && oNode.childNodes && oNode.childNodes.length) {
			for (var i=0;i<oNode.childNodes.length;i++) {
				if (oNode.childNodes[i].nodeName == sNodeName) {
					a[a.length] = oNode.childNodes[i];
				}
			}
		}
		return a;
	};

	//	IE5+/Win: main hiding functions, that calls the actual WCH single-menu hide function.
	this.HideWCH = function(oMenu) {
		var curmenu = oMenu.shownMenu;
		var prevMenu;
		while ( curmenu ) {
			//	show windowed controls for current child menu
			WCH.Discard(curmenu);
			//	save the pointer to current child menu
			prevMenu = curmenu;
			//	prepare the next child menu
			curmenu = curmenu.shownMenu;
			//	kill the shownMenu info for current child menu
			prevMenu.shownMenu = null;
		}
	};

	//	call this one for window.onresize
	this.Viewport = function() {
		_FetchCH();
		_FetchCW();
	};

	//	call this one for window.onload
	this.Init = function() {
		_FetchCH();
		_FetchCW();
		_Process();
	};
};
var ADXM = new ADXM_Constructor();

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	set event handlers
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
if (window.addEventListener) {
	window.addEventListener("load", ADXM.Init, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", ADXM.Init);
}
if (window.addEventListener) {
	window.addEventListener("resize", ADXM.Viewport, false);
} else if (window.attachEvent) {
	window.attachEvent("onresize", ADXM.Viewport);
}

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	these functions are taken from Mike Foster's X library (part of CBE), and simplified where possible.
	delete all this if you already use (include) the CBE or X files in your project
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function xClientWidth() {
	var w = 0;
	if (!window.opera && document.documentElement && document.documentElement.clientWidth)
		w = document.documentElement.clientWidth;
	else if (document.body && document.body.clientWidth)
		w = document.body.clientWidth;
	else if ( xDef(window.innerWidth, window.innerHeight, document.height) ) {
		w = window.innerWidth;
		if (document.height > window.innerHeight) w -= 16;
	}
	return w;
}

function xClientHeight() {
	var h = 0;
	if (!window.opera && document.documentElement && document.documentElement.clientHeight)
		h = document.documentElement.clientHeight;
	else if (document.body && document.body.clientHeight)
		h = document.body.clientHeight;
	else if ( xDef(window.innerHeight, window.innerWidth, document.width) ) {
		h = window.innerHeight;
		if (document.width > window.innerWidth) h -= 16;
	}
	return h;
}

function xScrollLeft() {
	var offset = 0;
	if ( xDef(window.pageXOffset) )
		offset = window.pageXOffset;
	else if ( document.documentElement && document.documentElement.scrollLeft )
		offset = document.documentElement.scrollLeft;
	else if ( document.body && xDef(document.body.scrollLeft ) )
		offset = document.body.scrollLeft;
	return offset;
}

function xScrollTop() {
	var offset = 0;
	if ( xDef(window.pageYOffset) )
		offset = window.pageYOffset;
	else if ( document.documentElement && document.documentElement.scrollTop )
		offset = document.documentElement.scrollTop;
	else if ( document.body && xDef(document.body.scrollTop ) )
		offset = document.body.scrollTop;
	return offset;
}

function xWidth(e, uW) {
	if ( !e ) return 0;
	uW = e.offsetWidth;
	return uW;
}

function xHeight(e, uH) {
	if ( !e ) return 0;
	uH=e.offsetHeight;
	return uH;
}

function xMoveTo(e, iX, iY) {
	xLeft(e,iX);
	xTop(e,iY);
}

function xLeft(e, iX) {
	if ( !(e = xGetElementById(e)) ) return 0;
    if ( xDef(iX) )
		e.style.left = iX + "px";
	else {
    	if ( xDef(e.offsetLeft) )
			iX = e.offsetLeft;
		else
			iX = parseInt(e.style.left);
    	if (isNaN(iX))
			iX = 0;
	}
	return iX;
}

function xTop(e, iY) {
	if ( !(e = xGetElementById(e)) ) return 0;
    if ( xDef(iY) )
		e.style.top = iY + "px";
	else {
		if ( xDef(e.offsetTop) )
			iY = e.offsetTop;
		else
			iY = parseInt(e.style.top);
		if (isNaN(iY))
			iY = 0;
	}
	return iY;
}

function xPageX(e) {
	if ( !(e = xGetElementById(e)) ) return 0;
	var x = 0;
	while (e) {
		if ( xDef(e.offsetLeft) ) x += e.offsetLeft;
		else break;
		e = e.offsetParent;
	}
	return x;
}

function xPageY(e) {
	if ( !(e = xGetElementById(e)) ) return 0;
	var y = 0;
	while (e) {
		if ( xDef(e.offsetTop) ) y += e.offsetTop;
		else break;
		e = e.offsetParent;
	}
	return y;
}

function xDef() {
	for (var i=0; i<arguments.length; ++i) {
		if ( typeof(arguments[i]) == "undefined" )
			return false;
	}
	return true;
}

function xGetElementById(e) {
	if (typeof(e) != "string") return e;
	if (document.getElementById)
		e = document.getElementById(e);
	else
		e = null;
	return e;
}
