var bookMenuTimerTicks;
var bookMenuTimerID = null;
var bookMenuTransition = null;
var bookMenuVisible = false;

function bookMenuInit()
{
	if ( !inBooksMenuSection)
	{
		var element = document.getElementById('book-menu');
		element.onmouseout = bookMenuDoMouseOut; // the event gets passed as the 1st argument
		element.onmouseover = bookMenuDoMouseEnter; // the event gets passed as the 1st argument
		setObjectOpacity( element, 0);
		element.style.visibility = 'visible';
		
		element = document.getElementById('main-menu-item-books');
		element.onmouseout = bookMenuDoMouseOut;
		element.onmouseover = mainMenuItemBooksDoMouseEnter;
	}
}

function bookMenuDoMouseEnter()
{
	if ( bookMenuTimerID != null) clearTimeout( bookMenuTimerID);
	bookMenuTimerID = null;
}

function mainMenuItemBooksDoMouseEnter()
{
	if ( bookMenuTimerID != null) clearTimeout( bookMenuTimerID);
	bookMenuTimerID = null;
	if ( !bookMenuVisible)
	{
		if ( bookMenuTransition != null) bookMenuTransition.halt();
		bookMenuTransition = null;
		var transition = new Transition(SineCurve, 250, function(percentage) {
			var element = document.getElementById("book-menu");
			setObjectOpacity( element, ( percentage) * 100.0);
		});
		bookMenuTransition = transition;
		//element.onmouseover = '';
		transition.run();
		bookMenuVisible = true;
	}
}

function bookMenuDoMouseOut()
{
	bookMenu_refreshTimeout( true);
}

/* function bookMenu_refreshTimeout( doRestart)
updates the main menu timeout timer; if doRestart is true then restarts
the timer; handles fading of the currently visible main menu element
*/
function bookMenu_refreshTimeout( doRestart)
{
	if ( doRestart)
	{
		bookMenuTimerTicks = 10;
		if ( bookMenuTimerID != null) clearTimeout( bookMenuTimerID);
		bookMenuTimerID = null;
	}
	if ( bookMenuTimerID == null)
	{
		bookMenuTimerID = self.setTimeout("bookMenu_refreshTimeout( false)", 20);
	} else
	{
		bookMenuTimerTicks = bookMenuTimerTicks - 1;
		if (( bookMenuTimerTicks <= 0) && ( bookMenuVisible))
		{
			if ( bookMenuTransition != null) bookMenuTransition.halt();
			bookMenuTransition = null;
			var transition = new Transition(SineCurve, 250, function(percentage) {
				var element = document.getElementById("book-menu");
				setObjectOpacity( element, (1 - percentage) * 100.0);
			});
			bookMenuTransition = transition;
			//element.onmouseover = '';
			transition.run();
			bookMenuVisible = false;
		
		} else
		{
			if (( bookMenuTimerTicks < 18))
			{
				//bookMenuElement_changeOpacity( visiblebookMenuItem, (bookMenuTimerTicks / 18) * 100);
			}
			bookMenuTimerID = self.setTimeout("bookMenu_refreshTimeout( false)", 20);
		}
	}
}

addEvent(window,'load',bookMenuInit);
