var _browser = new Browser();
var _activeButton = null;

function Browser() {
	this.isIE = false;
	this.isNS = false;
	this.version = null;
	this.platform = navigator.platform.substr(0, 3);
	var browserName;
	browserName = "MSIE";
	var index = navigator.userAgent.indexOf(browserName);
	if (index >= 0) {
		this.isIE = true;
		this.version = parseFloat(navigator.userAgent.substr(index + browserName.length));
		return;
	}
	browserName = "Netscape6/";
	index = navigator.userAgent.indexOf(browserName)
	if (index >= 0) {
		this.isNS = true;
		this.version = parseFloat(navigator.userAgent.substr(index + browserName.length));
		return;
	}
	browserName = "Gecko";
	index = navigator.userAgent.indexOf(browserName)
	if (index >= 0) {
		this.isNS = true;
		this.version = 6.1;
		return;
	}
}
function buttonClick(event, menuId) {
	var button;
	if (_browser.isIE) {
		button = window.event.srcElement;
	} else {
		button = event.currentTarget;
	}
	button.blur();
	if (button.menu == null) {
		button.menu = document.getElementById(menuId);
		if (button.menu.isInitialized == null) {
			menuInit(button.menu);
		}
	}
	if (button.onmouseout == null) {
		button.onmouseout = menuMouseout;
	}
	if (button == _activeButton) {
		return false;
	}
	if (_activeButton != null) {
		resetButton(_activeButton);
	}
	if (button != _activeButton) {
		depressButton(button);
		_activeButton = button;
	} else {
		_activeButton = null;
	}
	return false;
}
function buttonMouseover(event, menuId) {
	if (_activeButton == null) {
		buttonClick(event, menuId);
		return;
	}
	var button = null;
	if (_browser.isIE) {
		button = window.event.srcElement;
	} else {
		button = event.currentTarget;
	}
	if (_activeButton != null && _activeButton != button) {
		buttonClick(event, menuId);
	}
}
function depressButton(button) {
	if (button.onmouseout == null) {
		button.onmouseout = menuMouseout;
	}
	if (button.menu.onmouseout == null) {
		button.menu.onmouseout = menuMouseout;
	}
	var pos = $(button).positionedOffset();
	var x = $(button).positionedOffset().left;
	var y = $(button).getOffsetParent().positionedOffset().top + button.offsetHeight - 2; 
	if (_browser.isIE) {
		x += button.offsetParent.clientLeft;
		y += button.offsetParent.clientTop;
	}
	button.menu.style.left = x + "px";
	button.menu.style.top  = y + "px";
	var buttonMenuAnchors = button.menu.getElementsByTagName("A");
	if (buttonMenuAnchors.length > 0) {
		button.menu.style.visibility = "visible";
	}
	if (button.menu.iframeEl != null)
	{
	  var menuObj = button.menu;
	  var topMargin = getStyle(menuObj, 'margin-top');
	  var rightMargin = getStyle(menuObj, 'margin-right');	
	  var bottomMargin = getStyle(menuObj, 'margin-bottom');
	  var leftMargin = getStyle(menuObj, 'margin-left');
	  button.menu.iframeEl.style.left = button.menu.style.left;
	  button.menu.iframeEl.style.top  = button.menu.style.top;
	  if (topMargin != null || topMargin != "" )
	  {
		  button.menu.iframeEl.style.marginLeft = topMargin;
	  }
	  if (rightMargin != null || rightMargin != "")
	  {
		  button.menu.iframeEl.style.marginLeft = rightMargin;
	  }
	  if (bottomMargin != null || bottomMargin != "")
	  {
		  button.menu.iframeEl.style.marginLeft = bottomMargin;
	  }
	  if (leftMargin != null || leftMargin != "")
	  {
		  button.menu.iframeEl.style.marginLeft = leftMargin;
	  }
	  button.menu.iframeEl.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
	  button.menu.iframeEl.style.MozOpacity = 0;
	  button.menu.iframeEl.style.width  = button.menu.offsetWidth + "px";
	  button.menu.iframeEl.style.height = button.menu.offsetHeight + "px";
	  button.menu.iframeEl.style.display = "";
	}
}
function resetButton(button) {
	if (button.menu != null) {
		closeSubMenu(button.menu);
		button.menu.style.visibility = "hidden";
	}
	if (button.menu.iframeEl != null)
		button.menu.iframeEl.style.display = "none";

}
function menuMouseover(event) {
	var menu;
	if (_browser.isIE) {
		menu = getContainer(window.event.srcElement, "DIV", "menu");
	} else {
		menu = event.currentTarget;
	}
	if (menu.activeItem != null) {
		closeSubMenu(menu);
	}
}
function menuItemMouseover(event, menuId) {
	var item;
	if (_browser.isIE) {
		item = getContainer(window.event.srcElement, "A", "menuitem");
	} else {
		item = event.currentTarget;
	}
	var menu = getContainer(item, "DIV", "menu");
	if (menu.activeItem != null) {
		closeSubMenu(menu);
	}
	
	menu.activeItem = item;
	if (item.subMenu == null) {
		item.subMenu = document.getElementById(menuId);
		if (item.subMenu.isInitialized == null) {
			menuInit(item.subMenu);
		}
	}
	if (item.subMenu.onmouseout == null) {
		item.subMenu.onmouseout = menuMouseout;
	}
	var pos = $(item).positionedOffset();
	pos.left = pos.left + $(item).getOffsetParent().positionedOffset().left;
	pos.top = pos.top + $(item).getOffsetParent().positionedOffset().top;
	var x = pos.left + item.offsetWidth;
	var y = pos.top;
	var maxX = 0;
	var maxY = 0;
	if (_browser.isNS) {
		maxX = window.scrollX + window.innerWidth - $(item).getOffsetParent().getOffsetParent().getOffsetParent().positionedOffset().left;
		maxY = window.scrollY + window.innerHeight - $(item).getOffsetParent().positionedOffset().top;
	}
	if (_browser.isIE) {
		if (_browser.platform != "Mac") {
			maxX = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) + (document.documentElement.clientWidth != 0 ? document.documentElement.clientWidth : document.body.clientWidth);
			maxY = Math.max(document.documentElement.scrollTop, document.body.scrollTop) + (document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : document.body.clientHeight);
		} else {
			maxX = document.body.scrollLeft + document.body.clientWidth;
			maxY = document.body.scrollTop + document.body.clientHeight;
		}
	}
	maxX -= item.subMenu.offsetWidth;
	maxY -= item.subMenu.offsetHeight;
	if (_browser.isIE && _browser.platform == "Mac") {
		x -= 11;
	}
	if (x > maxX) {
		x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth + (menu.offsetWidth - item.offsetWidth));
	}
	y = Math.max(0, Math.min(y, maxY));
	item.subMenu.style.left = x + "px";
	item.subMenu.style.top  = y + "px";
	item.subMenu.style.visibility = "visible";
	if (item.subMenu.iframeEl != null)
	{
		var menuObj = item.subMenu;
		var topMargin = getStyle(menuObj, 'margin-top');
		var rightMargin = getStyle(menuObj, 'margin-right');	
		var bottomMargin = getStyle(menuObj, 'margin-bottom');
		var leftMargin = getStyle(menuObj, 'margin-left');
		item.subMenu.iframeEl.style.left    = item.subMenu.style.left;
		item.subMenu.iframeEl.style.top     = item.subMenu.style.top;
		if (topMargin != null || topMargin != "" )
		{
			item.subMenu.iframeEl.style.marginLeft = topMargin;
		}
		if (rightMargin != null || rightMargin != "")
		{
			item.subMenu.iframeEl.style.marginLeft = rightMargin;
		}
		if (bottomMargin != null || bottomMargin != "")
		{
			item.subMenu.iframeEl.style.marginLeft = bottomMargin;
		}
		if (leftMargin != null || leftMargin != "")
		{
			item.subMenu.iframeEl.style.marginLeft = leftMargin;
		}
		item.subMenu.iframeEl.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
		item.subMenu.iframeEl.style.MozOpacity = 0;
		item.subMenu.iframeEl.style.width   = item.subMenu.offsetWidth + "px";
		item.subMenu.iframeEl.style.height  = item.subMenu.offsetHeight + "px";
		item.subMenu.iframeEl.style.display = "";
	}
	if (_browser.isIE) {
		window.event.cancelBubble = true;
	} else {
		event.stopPropagation();
	}
}
function closeSubMenu(menu) {
	if (menu == null || menu.activeItem == null) {
		return;
	}
	if (menu.activeItem.subMenu != null) {
		closeSubMenu(menu.activeItem.subMenu);
		menu.activeItem.subMenu.style.visibility = "hidden";
		if (menu.activeItem.subMenu.iframeEl != null)
			menu.activeItem.subMenu.iframeEl.style.display = "none";
		menu.activeItem.subMenu = null;
	}

	menu.activeItem = null;
}
function menuMouseout(event) {
	var el;
	if (_activeButton == null) {
		return;
	}
	if (_browser.isIE) {
		el = window.event.toElement;
	} else if (event.relatedTarget != null) {
		el = (event.relatedTarget.tagName ? event.relatedTarget : event.relatedTarget.parentNode);
	}
	if (getContainer(el, "DIV", "menu") == null) {
		resetButton(_activeButton);
		_activeButton = null;
	}
}
function menuInit(menu) {
	var anchorList = menu.getElementsByTagName("A");
	if (anchorList.length <= 0) {
		return;
	}
	if (_browser.isIE) {
		if (_browser.platform != "Mac") {
			var menuWidth = anchorList[0].offsetWidth;
			anchorList[0].style.width = menuWidth + "px";
			var calculatedWidth = anchorList[0].offsetWidth - menuWidth;
			menuWidth -= calculatedWidth;
			anchorList[0].style.width = menuWidth + "px";
		} else {
			menu.style.width = "200px";
			menu.style.whiteSpace = "nowrap";
		}
	}
	if (_browser.isIE) {
		menu.style.lineHeight = "12px";
		var spanList = menu.getElementsByTagName("SPAN");
		for(var i = 0; i < spanList.length; i++) {
			if (hasClassName(spanList[i], "menuitemarrow")) {
				spanList[i].style.fontFamily = "Webdings";
				spanList[i].firstChild.nodeValue = "4";
			}
		}
	}
	if (_browser.isIE && _browser.version <7)
	{
		var iframeEl = document.createElement("IFRAME");
		iframeEl.frameBorder = 0;
		iframeEl.src = "javascript: ' '";
		iframeEl.style.display = "none";
		iframeEl.style.position = "absolute";
		iframeEl.style.zIndex = 99;
		menu.iframeEl = menu.parentNode.insertBefore(iframeEl, menu);
	}

	menu.isInitialized = true;
}
function getContainer(node, tagName, className) {
	while(node != null) {
		if (node.tagName != null && node.tagName == tagName && hasClassName(node, className)) {
			return node;
		}
		node = node.parentNode;
	}
	return node;
}
function hasClassName(el, name) {
	var list;
	list = el.className.split(" ");
	for(var i = 0; i < list.length; i++) {
		if (list[i] == name) {
			return true;
		}
	}
	return false;
}
function getPageOffsetLeft(el) {
	var x = el.offsetLeft;
	if (el.offsetParent != null) {
		x += getPageOffsetLeft(el.offsetParent);
	}
	return x;
}
function getPageOffsetTop(el) {
	var y = el.offsetTop;
	if (el.offsetParent != null) {
		y += getPageOffsetTop(el.offsetParent);
	}
	return y;
}
function elementTop(elementSource) {
	var top = 0;
	while (elementSource.tagName.toUpperCase() != "BODY") {
		top += elementSource.offsetTop;
		elementSource = elementSource.offsetParent;
	}
	return top;
}
function elementLeft(elementSource) {
	var left = 0;
	while (elementSource.tagName.toUpperCase() != "BODY") {
		left += elementSource.offsetLeft;
		elementSource = elementSource.offsetParent;
	}
	return left;
}
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

function hoverIfIE()
{

	if(!window.XMLHttpRequest)
	{
		$$('.verticalOuter').each(function(obj){
			obj.onmouseover=function() {
				this.addClassName('hover');
			}
			obj.onmouseout=function() {
				this.removeClassName('hover');
			}
		});
		if($$('.verticalOuter .menuitemarrow'))
		{
			$$('.verticalOuter a').each(function(obj){
			obj.attachEvent('onmouseover',addHover);
			obj.attachEvent('onmouseout',removeHover);
		});
		}
	}
}

function addHover()
{
	event.srcElement.up('.verticalOuter').addClassName('hover');
}

function removeHover()
{
	event.srcElement.up('.verticalOuter').removeClassName('hover');
}

hoverIfIE();