// bw-menu.js
// simple dynamic menu
// 
//
// by Bill Weiman http://bw.org/
// Copyright (c) 2009 BHG LLC
//
// Version 1.0.0
// Created for Dynamic Menu Course at Linda.com
//
// configuration variables
var timeout   = 250; // milliseconds
var fadeSpeed = 500; //milliseconds
var useFade   = true;

//timers array
var timers = new Array();

//state array -- by id, value = active, false = inactive
var state = new Array();

//lastOpacity; USED TO PREVENT MULTIPLE TIMERS FROM making the fade flicker
var lastOpacity = new Array();



var msie = false;
if( navigator.appName == "Microsoft Internet Explorer" ) msie = true;

//entry point:set element to visible and clear its timers
function setMenu( id )
{
	var e = document.getElementById(id);
	e.style.visibility = "visible";
	state[id] = true;
	setOpactiy( id, 1 );
	if(timers[id]) {
		clearTimeout(timers[id]);
		timers[id] = undefined;
	}
}

//set element to hidden and reset its opacity
//typically called by a timer
//may be used as an entry point to bypass timer and fades
function hideMenu( id )
{
	var e = document.getElementById(id);
	state[id] = false;
	e.style.visibility = "hidden";
	if(useFade) setOpactiy( id, 1 );
}

//entry point: hide the menu using fade ( if enabled)
function clearMenu( id )
{
	if(useFade) timers[id] = setTimeout( 'fadeMenu( "' + id + '" )', timeout );
	else timers[id] = setTimeout( 'hideMenu( "' + id + '" )', timeout );
}



function setOpactiy( id, value )
{
	var e = document.getElementById(id);
	
	if(state[id]) value = 1;
	else if(lastOpacity[id] && (lastOpacity[id] < value)) value = lastOpacity[id];
	
	if(msie) e.style.filter = 'alpha(opacity=' + value * 100 + ')';
	else e.style.opacity = (value);

	if( value == 0 ) hideMenu( id );
	lastOpacity[id] = value;
}



function fadeMenu ( id )
{
	var start = 0;
	var end = 0;
	var s = Math.round( fadeSpeed / 25 );
	var timer = 0;
	var i;
	
	state[id] = false;
	
	for( i = s; i >= 0 ; i--) {
		setTimeout( "setOpactiy('" + id + "'," + ( i / s ) + ")", timer++ * fadeSpeed / s )
	}
}
	



