/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	ADxMenu script
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	(c) 2004 - Aleksandar Vacic, www.aplus.co.yu
	Some rights reserved, http://creativecommons.org/licenses/by-sa/2.0/
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Using:
	X v3.15.2, Cross-Browser.com DHTML Library
	Copyright (c) 2004 Michael Foster, Licensed LGPL (gnu.org)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	central object: keeps track of viewport size, initializes menus
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

var ADXM_Constructor = function() {
	//	deny all non-DOM browsers and all IE
	if ((document.all && !window.opera) || !document.getElementById && !document.documentElement) return;

	var self = this;

	//	deny non-supporting browsers
	var _ua = navigator.userAgent.toLowerCase();
	var _nv = (navigator.vendor) ? navigator.vendor.toLowerCase() : "";
	var _np = (navigator.product) ? navigator.product.toLowerCase() : "";

	/*
		allow:
		- Gecko rv:1.5+
		watch for khtml (webkit) and Opera, for future updates
	*/

	if (
		( window.opera )
		|| ( _np == "gecko" && parseFloat(_ua.indexOf("rv:") + 3) < 1.5 )
		|| ( _ua.indexOf("webkit") != -1 )
	) return;

	//	keeps the menu IDs (of the main UL)
	var _aMenuIDs = new Array();
	//	keeps menu layouts
	var _aLayouts = new Array();

	//	return array of 1st level child elements with specified tag name
	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;
	};

	//	find menus on the page - look for class named "adxm"
	function _FindMenus() {
		var aTmp = xGetElementsByClassName("adxm", document, "ul", function(oM) {
			var sLayout = "H";
			if ( oM.className.indexOf("adxmV") != -1 )
				sLayout = "V";
			_Add(oM.id, sLayout);
		} );
	};

	//	add the menu to the array
	function _Add(sMenuID, sLayout) {
		if ( typeof(sMenuID) == "undefined") return;
		_aMenuIDs[_aMenuIDs.length] = sMenuID;
		_aLayouts[_aLayouts.length] = sLayout;
	};

	//	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 );
			}
		}
	};

	//	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 = oMenuLI.getElementsByTagName("UL");	//	UL elements in sub(main) item
			if (aUL.length)	{	//	has submenus
				//	save properties for positioning
				oMenuLI.submenu = aUL[0];
				oMenuLI.bIsH = (sLayout == "H");

				oMenuLI.onmouseover = function() {
					ADXM.Repos(this);
				};

				//	process the submenu in the same manner. all submenus have vertical layout
				_Processmenu( aUL[0], "V", oMainMenu );
			}

		}// for aMenuLIs
	};

	//	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;

		//	full submenu size
		var nW = oMenu.offsetWidth;
		var nH = oMenu.offsetHeight;

		//	full parent item size, width + padding + border
		var nIW = oItem.offsetWidth;
		var nIH = oItem.offsetHeight;

		//	inner submenu size, width + padding, no border
		if (nTmp = xGetComputedStyle(oItem, "width")) nIW = nTmp;
		if (nTmp = xGetComputedStyle(oItem, "height")) nIH = nTmp;

		//	where should menu be (top-left point) in parent item coordinates?
		var nLeft = (bIsH) ? 0 : nIW;
		var nTop = (bIsH) ? nIH : 0;

		//	where is that top-left point menu in page coordinates?
		var nPageX = xPageX(oItem) + nLeft;
		var nPageY = xPageY(oItem) + nTop;

		//	get available client dims (with scrolling included)
		_Viewport();
		var nClientW = _nCW;
		var nClientH = _nCH;

		if ( nClientW != 0 && nClientH != 0 ) {
			//	Horizontal placement
			var nDiff = 0;
			if (nPageX < 0) {
				nDiff += -nPageX;
				if (!bIsH) nDiff += nIW;
			} else {
				nTmp = nClientW - (nPageX + nW);
				if (nTmp < 0) {
					nDiff = nTmp;
					if (!bIsH) nDiff = -nW-nIW;
				}
			}
			if ( nDiff != 0 ) {
				oMenu.style.right = "auto";
				nLeft += nDiff;
				xLeft(oMenu, nLeft);
			}

			//	Vertical placement
			var nDiff = 0;
			if (nPageY < 0) {
				nDiff += -nPageY;
				if (bIsH) nDiff += nIH;
			} else {
				nTmp = nClientH - (nPageY + nH);
				if (nTmp < 0) {
					nDiff = nTmp;
					if (bIsH) nDiff = -nH-nIH;
				}
			}
			if ( nDiff != 0 ) {
				oMenu.style.bottom = "auto";
				nTop += nDiff;
				xTop(oMenu, nTop);
			}
		}
	};

	//	fetch client width, including scrolled part
	var _nCW = 0;
	function _FetchCW() {
		var nTmp = xClientWidth();
		if (nTmp > 0) nTmp += xScrollLeft();
		_nCW = nTmp;
	};

	//	fetch client height, including scrolled part
	var _nCH = 0;
	function _FetchCH() {
		var nTmp = xClientHeight();
		if (nTmp > 0) nTmp += xScrollTop();
		_nCH = nTmp;
	};

	//	call this one for window.onresize
	function _Viewport() {
		_FetchCH();
		_FetchCW();
	};

	//	call this one for window.onload
	this.Init = function() {
		_Viewport();
		_FindMenus();
		_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); }

addLoadEvent(ADXM_Constructor);
