var numMenus = 16; // Modify this to reflect how many menus there are.
var fadeInterval = 20; // Fade in at what framerate?
var fadeRate = 0.1; // Fade in/out how much per frame?
var timers = [];

if(getInternetExplorerVersion() != -1)
{
	// Because stupid IE is so slow, we will speed up the animation
	fadeRate = 0.3;    
}

if(isIpad())
{
	faderate = 0.4;
}

for(var i = 0; i < numMenus; i++) timers[i] = null;

function enterMenu(id)
{
	var o = document.getElementById("dropdown" + id).style;

	if(getInternetExplorerVersion() != -1)
	{
		// Because stupid IE is so stupid, we need to trick it into thinking it has a style property called opacity.
		if(!o.opacity) o.opacity = "0";
	}

	// Is there an animation in progress for this menu?
	if(timers[id] != null) clearTimeout(timers[id]);  // Kill it

	timers[id] = setTimeout("fadeIn(" + id + ")", fadeInterval);

	o.display = "inline"; // Make sure the menu is visible

}

function exitMenu(id)
{
	// Is there an animation in progress for this menu?
	if(timers[id] != null) clearTimeout(timers[id]);  // Kill it

	timers[id] = setTimeout("fadeOut(" + id + ")", fadeInterval);

}

function fadeIn(id)
{
	var o = document.getElementById("dropdown" + id).style;

	if(Number(o.opacity) < 1)
	{
		o.opacity = Number(o.opacity) + fadeRate;
		o.filter = "alpha(opacity = " + (o.opacity * 100) + ")"; // For stupid IE
		timers[id] = setTimeout("fadeIn(" + id + ")", fadeInterval);
	}
	else
	{
		o.opacity = 1;
		timers[id] = null;
	}
}

function fadeOut(id)
{
	var o = document.getElementById("dropdown" + id).style;

	var new_opacity = Number(o.opacity) - fadeRate;

	if(new_opacity > 0.01)
	{
		o.opacity = new_opacity;
		o.filter = "alpha(opacity = " + (new_opacity * 100) + ")"; // For stupid IE

		timers[id] = setTimeout("fadeOut(" + id + ")", fadeInterval);

	}
	else
	{
		o.opacity = 0;
		o.display = "none";
		timers[id] = null;
	}
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer')
	{
	    var ua = navigator.userAgent;
	    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
	    if (re.exec(ua) != null)
		rv = parseFloat( RegExp.$1 );
	}
    return rv;
}

function isIpad()
{
//    return (bool) strpos($_SERVER['HTTP_USER_AGENT'],'iPad');
}

function bodyMoveMouse(event)
{
    var targetX = 358,
        targetY = 159,
	targetAspect = 3,
	nearThreshold = 45,
        farThreshold = 90;

    var distance = Math.min(1, Math.max(0, Math.sqrt(Math.pow(((event.pageX - targetX) / targetAspect), 2) + Math.pow(event.pageY - targetY, 2)) - nearThreshold) / (farThreshold - nearThreshold));

    //    document.getElementById("logo").style.opacity = distance;

}
