// Copyright 2006-2007 javascript-array.com

var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;
var browser=navigator.appName

// open hidden layer
function mopen(id, objWidth) {	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	var container = document.getElementById("topMenuContainer");
	var topMenu = document.getElementById("topMenu");
	var ddWidth = ddmenuitem.offsetWidth - objWidth;
	ddmenuitem.style.visibility = 'visible';
	ddmenuitem.style.marginLeft = "-"+ddWidth/2+"px";
	/*if (browser == "Microsoft Internet Explorer"){
		if (container.offsetLeft > ddmenuitem.offsetLeft){
			var posNum = String(container.offsetLeft - ddmenuitem.offsetLeft).replace(/-/, "");
			var rePos = parseInt(ddWidth-posNum);
			ddmenuitem.style.marginLeft = "-"+rePos/2.6+"px";
		}
	}
	else {
		if (container.offsetLeft > ddmenuitem.offsetLeft){
			var posNum = String(container.offsetLeft - ddmenuitem.offsetLeft).replace(/-/, "");
			var rePos = parseInt(ddWidth-posNum);
			ddmenuitem.style.marginLeft = "-"+rePos/3+"px";
		}
	}*/
}
// close showed layer
function mclose(){
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime(){
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime(){
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose;

/*****top menu functions*****/

/*used with the top menu*/
var DDSPEED = 4;
var DDTIMER = 15;

//main function to handle the mouse events //
function ddMenu(id,d,submenu){
	var header = document.getElementById(id + '-ddheader');
	var container = document.getElementById(id + '-ddcontent');
	if (container){
		var tdCell = container.parentNode.parentNode;
		clearInterval(container.timer);
		if(d == 1){
	   clearTimeout(header.timer);
	   if (container.className == ""){
		   if(container.maxh && container.maxh <= container.offsetHeight){
				return
			}
		   else if(!container.maxh || container.maxh){
		     container.style.display = 'block';
		     container.style.height = 'auto';
		     container.maxh = container.offsetHeight;
		     container.style.height = '1px';
		   }
		}
	   container.timer = setInterval(function(){ddSlide(container,1)},DDTIMER);
	   }
		else if(submenu){
		   header.timer = setTimeout(function(){ddCollapse(container)},500);
		}
		else{
		   header.timer = setTimeout(function(){ddCollapse(container)},50);
		}
		if (tdCell.nodeName == "TD"){
			var obj = String(tdCell.offsetWidth/2-container.offsetWidth/2);
			var resize = obj.replace(/-/, "");
			container.style.marginLeft = "-"+resize+"px";
		}
	}
}

//collapse the menu //
function ddCollapse(container){
	container.timer = setInterval(function(){ddSlide(container,-1)},DDTIMER);
}

//cancel the collapse if a user rolls over the dropdown //
function cancelHide(id){
	var header = document.getElementById(id + '-ddheader');
	var container = document.getElementById(id + '-ddcontent');
	clearTimeout(header.timer);
	clearInterval(container.timer);
	if(container.offsetHeight < container.maxh){
	 container.timer = setInterval(function(){ddSlide(container,1)},DDTIMER);
	}
}

//incrementally expand/contract the dropdown and change the opacity //
function ddSlide(container,d){
	var currh = container.offsetHeight;
	var dist;
	if(d == 1){
		dist = (Math.round((container.maxh - currh) / DDSPEED));
}
else{
	dist = (Math.round(currh / DDSPEED));
}
	if(dist <= 1 && d == 1){
		dist = 1;
	}
	container.style.height = currh + (dist * d) + 'px';
	container.className = "menuMoving";
	if((currh < 5 && d != 1) || (currh > (container.maxh - 2) && d == 1)){
			if(d == -1){
				container.style.height = "0px";
				container.style.display = "none";
				container.className = "";
				//container.style.opacity = 0;
				//container.style.filter = 'alpha(opacity=0)';
			}
	 clearInterval(container.timer);
	}
}