var _ie = false;
var _timeoutShow = 150;
var _timeoutHide = 250;
var _width = 0;

function setWidth(){
	if (window.innerHeight) {
		_width = window.innerWidth;
	} else {
		_width = document.documentElement.clientWidth;
	}
}

function getAbsoluteLeft(obj) {
	var posLeft = 0;
	while (obj.offsetParent) {posLeft += obj.offsetLeft; obj = obj.offsetParent;}
	return posLeft;
}

function initPage()
{
	setWidth();
	if (window.attachEvent && !window.opera) var _ie = true;
	var _menu = document.getElementById('menu');
	if (_menu) {
		var _li = _menu.getElementsByTagName('li');
		for (var i=0; i<_li.length; i++) {
			if (_li[i].getElementsByTagName('div')[0])
				_li[i].div = _li[i].getElementsByTagName('div')[0];
			else _li[i].div = false;
			
			_li[i].onmouseover = function(){
				if (_ie && this.className.indexOf('hover') == -1)
					this.className += ' hover';
				var _this = this;
				if (this.timer) clearTimeout(this.timer);
				
				if (this.div) {

					this.timer2 = setTimeout(function(){
						_this.style.position = 'relative';
						_this.div.style.display = 'block';
						var _pos = getAbsoluteLeft(_this.div) + _this.div.offsetWidth;
						if (_pos > _width) {
							_this.div.style.left = -_this.div.offsetWidth+8+'px';
						}
					},_timeoutShow)
				}
			};
			
			_li[i].onmouseout = function(){				
				if (_ie) this.className = this.className.replace('hover','');

				var _this = this;
				if (this.timer2) clearTimeout(this.timer2);
				if (this.div) {
					this.timer = setTimeout(function(){
						_this.style.position = 'static';
						_this.div.style.display = 'none';
						_this.div.style.left = 184+'px';
					},_timeoutHide)
				}
			};			
		}
	}
}
if (window.addEventListener) {
	window.addEventListener("load", initPage, false);
	window.addEventListener("resize", setWidth, false);
}
else if (window.attachEvent) {
	window.attachEvent("onload", initPage);
	window.attachEvent("onresize", initPage);
}
