// 
// -----------------------------------------------------------------------------------------
// *** MENU_FADING.JS ***
// -----------------------------------------------------------------------------------------
// Last Updated: 24/05/2003 by DarkAngel
// 
// Fading menu with customizable start Alpha, end Alpha and fade speed values.
// 
// NOTE: requires DETECT.JS and LAYERS.JS
// 
// Interface Usage in HTML pages:
//
// Usage: openMenu(object sender, string menuItemID, int X=0, int Y=18, bool relative=true)
// The menu item can be a single div or table with display style sets to none.
// 
// Recent Updates: Catches the X and Y coordinates of the activation link and draws the menu
// items in the correct position.
// 
// Note: It uses setInterval() javascript method.
// 
// -----------------------------------------------------------------------------------------
// Created for MYST site (http://www.myst.it) in 2003. Free for non-commercial usage.
// 
// 
// 



  /*
  ** Check if the browser is running in a low-resolution environment
  ** try to use the screen object (in N4 or e4). If not available, try to use Java.
  ** If not available, assume low-res.
  ** returns true if in a low-resolution environment (width &lt; 800 pixels)
  */
var resolution;
if (self.screen) resolution = screen.width;
else if (navigator.javaEnabled && navigator.javaEnabled())
	{
		resolution = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
	}
	else resolution = 800;

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,place,thestring;

if (checkIt('konqueror'))
{
	browser = "Konqueror";
	OS = "Linux";
}
else if (checkIt('safari')) browser = "Safari";
else if (checkIt('omniweb')) browser = "OmniWeb";
else if (checkIt('opera')) browser = "Opera";
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab";
else if (checkIt('msie')) browser = "Explorer";
else if (checkIt('netscape')) browser = "Netscape";
else if (!checkIt('compatible'))
{
	browser = "Mozilla";
	version = detect.charAt(8);
}
else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS)
{
	if (checkIt('linux')) OS = "Linux";
	else if (checkIt('x11')) OS = "Unix";
	else if (checkIt('mac')) OS = "Mac"
	else if (checkIt('win')) OS = "Windows"
	else OS = "Unknown";
}

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}



startAlpha = 0;
endAlpha = 90;
// MENU FADE-IN / FADE-OUT SPEED (lower = faster)
// IE calculates setInterval slower so assign a lower value
//
if ( (browser == "Netscape")||(browser == "Mozilla") ) fadeInSpeed = fadeOutSpeed = 20;
else fadeInSpeed = fadeOutSpeed = 10; 

var menuArray = new Array();

// 
// Show arguments and types passed to a function
//
// USAGE: showArguments(arguments) in a function
//
// -----------------------------------------------------------------------------------------



function browserFadeSupport()
{
	if (browser == "Opera") return false;
	else return true;
}



function showArguments(args)
{
    var o = '';
    o += 'Number of arguments = ' + args.length + '\n';
    for (var i=0;i < args.length; i++)
        o += 'Argument ' + i + ' typeof = ' + typeof args[i] + '\n';
    alert(o);
}
// -----------------------------------------------------------------------------------------


// -----------------------------------------------------------------------------------------


// 
// Calculate the X position of the object <img>
// -----------------------------------------------------------------------------------------
// 
function getX(img) { 
	var x = 0;
	if (!document.layers) {
		var onWindows = navigator.platform ? navigator.platform == "Win32" : false;
		var macIE45 = document.all && !onWindows && getExplorerVersion() == 4.5;
		var par = img;
		var lastOffset = 0;
		while(par){
			if( par.leftMargin && ! onWindows ) x += parseInt(par.leftMargin);
			if( (par.offsetLeft != lastOffset) && par.offsetLeft ) x += parseInt(par.offsetLeft);
			if( par.offsetLeft != 0 ) lastOffset = par.offsetLeft;
			par = macIE45 ? par.parentElement : par.offsetParent;
		}
	} else if (img.x) x += img.x;
	return x;
}

// -----------------------------------------------------------------------------------------


// 
// Calculate the Y position of the object <img>
// -----------------------------------------------------------------------------------------
// 

function getY(img) {
	var y = 0;
	if(!document.layers) {
		var onWindows = navigator.platform ? navigator.platform == "Win32" : false;
		var macIE45 = document.all && !onWindows && getExplorerVersion() == 4.5;
		var par = img;
		var lastOffset = 0;
		while(par){
			if( par.topMargin && !onWindows ) y += parseInt(par.topMargin);
			if( (par.offsetTop != lastOffset) && par.offsetTop ) y += parseInt(par.offsetTop);
			if( par.offsetTop != 0 ) lastOffset = par.offsetTop;
			par = macIE45 ? par.parentElement : par.offsetParent;
		}		
	} else if (img.y >= 0) y += img.y;
	return y;
}

// 
// -----------------------------------------------------------------------------------------



//
// menuItem class: every menu is an instance of this class, along with its values.
// -----------------------------------------------------------------------------------------


function menuItem(id, startAlpha, endAlpha) {
	this.action = "";
	this.id = id;
	this.startAlpha = startAlpha;
	this.currentAlpha = this.startAlpha;
	this.endAlpha = endAlpha;
	this.counter = 0;
	this.isVisible = false;
	
	this.fadeIn = function()
	{
		// if the browser does not support fading, set the counter to match fadeInSpeed.
		if (!browserFadeSupport()) this.counter = fadeInSpeed;

		this.isVisible = true;
		$(this.id).setStyle('display','block');
		
		if (this.counter < fadeInSpeed)
		{
			this.currentAlpha = Math.floor((this.endAlpha - this.startAlpha) * (this.counter / fadeInSpeed) + (this.startAlpha));
			this.setAlpha();
			this.counter++;
		}
		else
		{
			this.currentAlpha = this.endAlpha;
			this.setAlpha();
			if (this.action!="")
			{
				clearInterval(this.action);
				this.action = "";
			}
		}
	}

	this.fadeOut = function()
	{
		// if the browser does not support fading, set the counter to zero.
		if (!browserFadeSupport()) this.counter = 0;

		if (this.counter > 0)
		{
			this.currentAlpha = Math.floor((this.endAlpha - this.startAlpha) * (this.counter / fadeOutSpeed) + (this.startAlpha));
			this.setAlpha();
			this.counter--;
		}
		else
		{
			this.isVisible = false;
			$(this.id).setStyle('display','none');
			if (this.action!="")
			{
				clearInterval(this.action);
				this.action = "";
			}
		}
	}

	this.setAlpha = function()
	{
	    $(this.id).setOpacity(this.currentAlpha/100);
//		if ( (browser == "Netscape")||(browser == "Mozilla") )
//		{
//			/* Gecko-based browsers */
//			$(this.id).style.MozOpacity = this.currentAlpha/100;
//		}
//		else
//		{
//			/* Explorer-based browsers */
//			$(this.id).filters.alpha.opacity = this.currentAlpha;
//		}
	}
}

// -----------------------------------------------------------------------------------------


// 
// displays a menu item (starts fade-in loop until it reaches endAlpha value)
// 
// Usage: openMenu(object sender, string menuItemID, int X=0, int Y=18, bool relative=true)
// 								
// 	note: for object sender, use "this" for most cases.
// -----------------------------------------------------------------------------------------

function openMenu() 
{
	// catch arguments or set default values
	// -------------------------------------------

	// required: exit if not found
	var obj = (arguments[0]) ? arguments[0] : null;
	var id = (arguments[1]) ? arguments[1] : null;

	if ((obj == null)||(id == null)) { return; }

	// optional: if not found, use default values
	var offSetX = (arguments[2]) ? arguments[2] : 0;
	var offSetY = (arguments[3]) ? arguments[3] : 18;
	var relative = (arguments[4]) ? arguments[4] : true;
	var operaFixY = (arguments[5]) ? arguments[5] : null;

	if (offSetX >= 2000) offSetX = 0;
	if (offSetY >= 2000) offSetY = 0;

	if ( (operaFixY)&&(browser == "Opera") ) offSetY = offSetY + operaFixY;
	// -------------------------------------------
	// set X & Y coordinates of the item depending on the activation object (obj)
	// -------------------------------------------
	var menuX = (relative==true) ? (getX(obj)+offSetX) : offSetX;	
	var menuY = (relative==true) ? (getY(obj)+offSetY) : offSetY;
	$(id).setStyle('left',menuX);
	$(id).setStyle('top',menuY);
	if (!menuArray[id])
	{
		menuArray[id] = new menuItem(id, startAlpha, endAlpha);
	}
	clear(id);
	closeMenu(id);
	if ( (menuArray[id].counter < fadeInSpeed) && (menuArray[id].action == "") )
	{
		menuArray[id].action = setInterval("menuArray['"+id+"'].fadeIn()", fadeInSpeed);
		eval(menuArray[id].action);
	}
}

// -----------------------------------------------------------------------------------------


// 
// hide a menu item (starts fade-out loop until it reaches startAlpha value)
// -----------------------------------------------------------------------------------------

function closeMenu(id) {
	
//	myRE = new RegExp("hover", "g");
//	inThere = id.match(myRE);

	inThere = id.indexOf("hover");
	if (inThere != -1) return;

	for(n=0;n<10;n++) 
	{
		if ( (menuArray["menulist"+n]!=null) && (menuArray["menulist"+n].isVisible) && (id != "menulist"+n) )
		{
			clear("menulist"+n);
			menuArray["menulist"+n].action = setInterval("menuArray['menulist"+n+"'].fadeOut()", fadeOutSpeed);
			eval(menuArray["menulist"+n].action);
		}
		if ( (menuArray["menulist"+n+"hover"]!=null) && (menuArray["menulist"+n+"hover"].isVisible) && (id != "menulist"+n+"hover") )
		{
			clear("menulist"+n+"hover");
			menuArray["menulist"+n+"hover"].action = setInterval("menuArray['menulist"+n+"hover'].fadeOut()", fadeOutSpeed);
			eval(menuArray["menulist"+n+"hover"].action);
		}
	}
}

// -----------------------------------------------------------------------------------------

function clear (id) {
	if (menuArray[id].action != "") {
		clearInterval(menuArray[id].action);
		menuArray[id].action = "";
	}
}

// put this event here for XHTML compliance (cannot be placed inside the body page tag)
window.onresize = function() { closeMenu("all"); };
// 
// -----------------------------------------------------------------------------------------
// *** MENU_FADING.JS *** End of File
// -----------------------------------------------------------------------------------------


