var mainNav;
var navItem = new Array();
function setupMenu(type){
	$('ul.mainNav').each(function(){
			this.className = this.className + " javascript";
			mainNav = this.getElementsByTagName('ul');
			this.style.position = 'relative';
			this.style.zIndex = '999';
	})
	zIndex = 99999;
	for(var i = 0; mainNav.length > i; i++){
		mainNav[i].style.position = 'absolute';
		mainNav[i].style.zIndex = '' + zIndex;
		mainNav[i].style.left = '80%';
		mainNav[i].style.top = '20%';
		mainNav[i].style.padding = '0';
		mainNav[i].style.visibility = 'hidden'
		mainNav[i].style.display = 'none';
		
		navItem[i] = mainNav[i].parentNode;
		navItem[i].style.position = 'relative';
		navItem[i].style.zIndex = ''+zIndex;
		navItem[i].navChild = mainNav[i];
		navItem[i].onmouseover = function(){
			this.navChild.style.display = 'block';
			$(this).siblings().each(function(){
				try {
					this.navChild.style.visibility = 'hidden';
					clearTimeout(this.timeout);
				}
				catch(e){
					// do nothing!!!
				}
			})
			clearTimeout(this.timeout);
			pLeft = $(this).offset().left;
			pWidth = $(this).width();
			cWidth = $(this.navChild).width();
			sWidth = $(window).width();
			sLeft = $(window).scrollLeft();
			
			if (this.navChild.rootNav == true) {
				if(pLeft + cWidth > sLeft + sWidth){
					this.navChild.style.left = ((sLeft + sWidth) - (pLeft+cWidth)) + 'px';
				}
				else {
					this.navChild.style.left = '0px';
				}
			}
			else {
				if (pLeft + (pWidth * 0.8) + cWidth > sWidth && pLeft > (cWidth * 0.8)) {
					this.navChild.style.left = '-80%';
				}
				else {
					this.navChild.style.left = '80%';
				}
			}
			this.navChild.style.visibility = 'visible';
		};
		
		navItem[i].onmouseout = function(){
			var self = this;
			this.timeout = window.setTimeout(function(){
				self.navChild.style.display = 'none';
				self.navChild.style.visibility = 'hidden';
			}, 100)
		};
		zIndex--;
	}
	if(type == 'top'){
		$('ul.mainNav').children('li').children('ul').each(function(){
				this.style.left = '0px';
				this.style.top = '100%';
				this.rootNav = true;
		})
	}
}
