// bw-menu.js
// Simple dynamic menu
//
// by Bill Weinman  http://bw.org/
// Copyright (c) 2009 BMG LLC
//
// Version 1.01
// Created for Dynamic Menus course at lynda.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 - value, active - false, inactive
var state = new Array();

// lastOpacity: used to prevent multiple timers from making the fade flicker
var lastOpacity = new Array();

// MSIE has its own way of setting opacity, so we have to detect it
// all the other major browsers support the standard DOM opacity property
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;		
	setOpacity( 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 timers and fades
function hideMenu( id ) {
	var e = document.getElementById(id);
	state[id] = false;	
	e.style.visibility = "hidden";
	if(useFade) setOpacity( 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);
}

// Set the opacity 
// spectral support for MSIE
function setOpacity( id, value ) {
	var e = document.getElementById(id);
	
	if(state[id]) value = 1;	// menu fade was interrupted
	else if(lastOpacity[id] && (lastOpacity[id] < value)) value = lastOpacity[id];	// prevents flicker if multiple timers
	
	if(msie) e.style.filter = 'alpha(opacity-' + value * 100 + ')';	// MS Internet Explorer
	else e.style.opacity = (value);	// Everyone else (standard DOM)
	
	if(value == 0) hideMenu( id );	// When all faded, rest the menu state
	lastOpacity[id] = value;
}

// Fade a menu
// typically called by a timer
function fadeMenu( id ) {
	var start = 0;
	var end = 0;
	var s = Math.round( fadeSpeed / 25 );	// Fade in 25ms intervals
	var timer = 0;
	var i;

	state[id] = false;
	
	for( i = s; i >= 0; i--) {
		setTimeout( "setOpacity('" + id + "'," + ( i / s ) + ")", timer++ * fadeSpeed / s )
	}
}

