// variables
OPACITY_MAX = 100;
OPACITY_MIN = 30;
OPACITY_INC = 2;
OPACITY_DEC = 1;
FADE_SPEED = 50;
CurrentPageIndex = 0;
var timer = null;
var opacities = new Array(9);

// browser check
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);

// initialises the menu
function initialiseMenu(cellNum)
{
	// set the current page index to the proper value
	CurrentPageIndex = cellNum;

	for(i = 0; i < opacities.length; i++)
	{
		if(i == cellNum)
		{
			// make the current page's menu item maximum opacity
			opacities[i] = new Array(OPACITY_MAX, "");
		}
		else
		{
			// make all other menu item's minimum opacity
			opacities[i] = new Array(OPACITY_MIN, "");
		}
	}
}

// increases the opacity of the cell - called onmouseover() event
function darken(cellNum)
{
	// ensure any items that aren't at the minimum start heading down
	for(i = 0; i < opacities.length; i++)
	{
		opacity = opacities[i][0];
		// a menu item that is not moving in any direction but got stuck at a non min value
		if(opacity >= OPACITY_MAX && i != CurrentPageIndex && i != cellNum) 
		{
			// force it to head down
			opacities[i][1] = "down";
		}
	}
	
	// set the opacity direction to "up"	
	opacities[cellNum][1] = "up";	
	changeOpacity();	
}

// increases the opacity of the cell - called onmouseout() event
function fade(cellNum)
{
	// set the opacity direction to "down"
	opacities[cellNum][1] = "down";
	setTimeout("changeOpacity()", FADE_SPEED);
}

// changes the opacity of the cells in the menu
function changeOpacity()
{
	nextLoop = true;

	// loop through all the cells in the menu
	for(i = 0; i < opacities.length; i++)
	{
		// get the cell's current opacity and direction
		opacity = opacities[i][0];
		direction = opacities[i][1];

		// check the opacity direction of the cell
		if(direction == "up")
		{
			// increase the cell's opacity by the increment level
			opacity += OPACITY_INC;
			
			if(opacity > OPACITY_MAX) 
			{
				// stop increasing the opacity when we reach the max (by changing the opacity direction to "")
				opacities[i][1] = "";
				// reset the max correctly (stop opacity creep)
				opacities[i][0] = OPACITY_MAX;
			}
			else
			{
				opacities[i][0] = opacity;
				nextLoop = false;
			}
		}
		else if(direction == "down")
		{
			// decrease the cell's opacity by the decrement level
			opacity -= OPACITY_DEC;

			if(opacity < OPACITY_MIN)
			{
				// stop decreasing the opacity when we reach the min (by changing the opacity direction to "")
				opacities[i][1] = "";
				// reset the min correctly (stop opacity creep)
				opacities[i][0] = OPACITY_MIN;
			}
			else
			{
				opacities[i][0] = opacity;
				nextLoop = false;
			}
		}
		
		// make the opacity style change to the cell
		cell = menu_table.rows[i].cells[0];
		if(ie5)
		{ 
			cell.style.filter="alpha(opacity="+opacity+")";
		}
		if(ns6)
		{ 
			cell.style.opacity = opacity/100;
		}
	}
	
	if(!nextLoop)
	{
		// recursive call this function to actually do the darkening/fading
		timer = setTimeout("changeOpacity()", FADE_SPEED);
	}
	else
	{
		clearTimeout(timer);
	}
}
