

// check browsers
var ua = navigator.userAgent;
var opera = /opera [56789]|opera\/[56789]/i.test(ua);
var ie = !opera && /MSIE/.test(ua);
var ie50 = ie && /MSIE 5\.[01234]/.test(ua);
var ie6 = ie && /MSIE [6789]/.test(ua);
var ieBox = ie && (document.compatMode == null || document.compatMode != "CSS1Compat");
var moz = !opera && /gecko/i.test(ua);
var nn6 = !opera && /netscape.*6\./i.test(ua);

function alertRect(rect) {
	alert(rect.top+":"+rect.left+":"+rect.width+":"+rect.height);
}

function setPosition(src, relEl, sDir, extraLeft, extraTop) {
	var dir = sDir;
	// find parent item rectangle, piRect
	var piRect;
	if (relEl.left != null && relEl.top != null && relEl.width != null && relEl.height != null) {	// got a rect
		piRect = relEl;
	}
	else
		piRect = getOuterRect(relEl);

	var menuRect = getOuterRect(src);
	var docRect = getDocumentRect();
	var scrollPos = getScrollPos();
	var pMenu = src.parentMenu; //多级菜单的支持??

	if (dir != "horizontal" && dir != 'h') {
		if (piRect.left + menuRect.width - scrollPos.left <= docRect.width)
			src.left = piRect.left;
		else if (docRect.width >= menuRect.width)
			src.left = docRect.width + scrollPos.left - menuRect.width;
		else
			src.left = scrollPos.left;
	
		if (piRect.top + piRect.height + menuRect.height <= docRect.height + scrollPos.top)
			src.top = piRect.top + piRect.height;
		else if (piRect.top - menuRect.height >= scrollPos.top)
			src.top = piRect.top - menuRect.height;
		else if (docRect.height >= menuRect.height)
			src.top = docRect.height + scrollPos.top - menuRect.height;
		else
			src.top = scrollPos.top;
	}
	else {
		if (piRect.top + menuRect.height - getBorderTop(src) - 1/*this.paddingTop*/ <= docRect.height + scrollPos.top)
			src.top = piRect.top - getBorderTop(src) - 1/*this.paddingTop*/;
		else if (piRect.top + piRect.height - menuRect.height + getBorderTop(src) + 1/*this.paddingTop*/ >= 0)
			src.top = piRect.top + piRect.height - menuRect.height + 1/*this.borderBottom*/ + 1/*this.paddingBottom*/ + 1/*this.shadowBottom*/;
		else if (docRect.height >= menuRect.height)
			src.top = docRect.height + scrollPos.top - menuRect.height;
		else
			src.top = scrollPos.top;

		var pMenuPaddingLeft = pMenu ? 1/*pMenu.paddingLeft*/ : 0;
		var pMenuBorderLeft = pMenu ? getBorderLeft(pMenu) : 0;
		var pMenuPaddingRight = pMenu ? 1/*pMenu.paddingRight*/ : 0;
		var pMenuBorderRight = pMenu ? 1/*pMenu.borderRight*/ : 0;
		var defaultShadowRight = ie && !ie50 && /win32/i.test(navigator.platform) ? 4 :0;

		if (piRect.left + piRect.width + menuRect.width + pMenuPaddingRight +
			pMenuBorderRight - getBorderLeft(src) + defaultShadowRight/*this.shadowRight*/ <= docRect.width + scrollPos.left)
			src.left = piRect.left + piRect.width + pMenuPaddingRight + pMenuBorderRight - getBorderLeft(src);
		else if (piRect.left - menuRect.width - pMenuPaddingLeft - pMenuBorderLeft + 1/*this.borderRight*/ + defaultShadowRight/*this.shadowRight*/ >= 0)
			src.left = piRect.left - menuRect.width - pMenuPaddingLeft - pMenuBorderLeft + 1/*this.borderRight*/ + defaultShadowRight/*this.shadowRight*/;
		else if (docRect.width >= menuRect.width)
			src.left = docRect.width  + scrollPos.left - menuRect.width;
		else
			src.left = scrollPos.left;
	}
	if (!extraLeft) extraLeft = 0;
	if (!extraTop) extraTop = 0;
	src.style.left = src.left + extraLeft;
	src.style.top = src.top + extraTop;
}


/* Position functions */

function getInnerLeft(el) {
	if (el == null) return 0;
	if (ieBox && el == document.body || !ieBox && el == document.documentElement) return 0;
	return getLeft(el) + getBorderLeft(el);
}

function getLeft(el) {
	if (el == null) return 0;
	return el.offsetLeft + getInnerLeft(el.offsetParent);
}

function getInnerTop(el) {
	if (el == null) return 0;
	if (ieBox && el == document.body || !ieBox && el == document.documentElement) return 0;
	return getTop(el) + getBorderTop(el);
}

function getTop(el) {
	if (el == null) return 0;
	return el.offsetTop + getInnerTop(el.offsetParent);
}

function getBorderLeft(el) {
	return ie ?
		el.clientLeft :
		parseInt(window.getComputedStyle(el, null).getPropertyValue("border-left-width"));
}

function getBorderTop(el) {
	return ie ?
		el.clientTop :
		parseInt(window.getComputedStyle(el, null).getPropertyValue("border-top-width"));
}

function opera_getLeft(el) {
	if (el == null) return 0;
	return el.offsetLeft + opera_getLeft(el.offsetParent);
}

function opera_getTop(el) {
	if (el == null) return 0;
	return el.offsetTop + opera_getTop(el.offsetParent);
}

function getOuterRect(el) {
	return {
		left:	(opera ? opera_getLeft(el) : getLeft(el)),
		top:	(opera ? opera_getTop(el) : getTop(el)),
		width:	el.offsetWidth,
		height:	el.offsetHeight
	};
}

// mozilla bug! scrollbars not included in innerWidth/height
function getDocumentRect(el) {
	return {
		left:	0,
		top:	0,
		width:	(ie ?
					(ieBox ? document.body.clientWidth : document.documentElement.clientWidth) :
					window.innerWidth
				),
		height:	(ie ?
					(ieBox ? document.body.clientHeight : document.documentElement.clientHeight) :
					window.innerHeight
				)
	};
}

function getScrollPos(el) {
	return {
		left:	(ie ?
					(ieBox ? document.body.scrollLeft : document.documentElement.scrollLeft) :
					window.pageXOffset
				),
		top:	(ie ?
					(ieBox ? document.body.scrollTop : document.documentElement.scrollTop) :
					window.pageYOffset
				)
	};
}

/* end position functions */