/*
	drop down menu scripting by Charles Fahey, Best Pitch Multi Media (http://www.bestpitch.com)
	©2006-2007 Best Pitch Multi Media
	Usage licensed to gilfahey.com
*/

// browser test
var IE4 = (document.all && !document.getElementById)? true : false;
var W3C = (document.getElementById)? true : false;

// default 5 second life for drop down menu
var ddm_timeout = 5;

// set default seconds count for timer
var s=0;

// set timeout as variable which can be called to stop the execution of the timer (TimerLoop)
var x=setTimeout('TimerLoop()',1000);

/*
	make a layer disappear after a timed number of seconds
*/
function TimerLoop() {
	// increase seconds count
	s=s+1;
	// if display time has expired, hide menu
	if (s == ddm_timeout) { hide_menu();}
	// and reset variable
	x=setTimeout('TimerLoop()',1000);
}

/*
	stop a running timer
*/
function TimerStop() {
	// stop execution of timer
	clearTimeout(x);
	// reset seconds counter
	s=0;
}

/*
	make drop down menu appear on mouse over/click
*/
function show_menu() {
	// based on DOM, display layer
	(W3C||IE4)? document.getElementById('nav_menu').style.visibility="visible" : document.getElementById('nav_menu').visibility="show";
	// stop the timer (in case it was already running)
	TimerStop();
	// now begin timer again
	TimerLoop();
}

/*
	make drop down disappear on timer limit
*/
function hide_menu() {
	// based on DOM, hide layer
	(W3C||IE4)? document.getElementById('nav_menu').style.visibility="hidden" : document.getElementById('nav_menu').visibility="hide";
}

/*
	keep the layer displaying by resetting the timer
	called on mouseover of link in drop down menu
*/
function hold_layer() {
	// stop the timer
	TimerStop();
	// restart the timer
	TimerLoop();
}

/*
	reposition drop down menu according to link position
	hrefObj = the specific navigation link object
*/
function pos_pop_menu(hrefObj) {
	
	// get the proper position for the drop down
	curPos = findPos(hrefObj);
	
	// position div horizontally
	document.getElementById("nav_menu").style.left = curPos.left + 'px';
	document.getElementById("nav_menu").style.top = curPos.top + 'px';
	
	// display menu
	show_menu();
	
}

/* determine horizontal position of menu */
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	
	var returnObj = new Object();
	returnObj.left = curleft;
	returnObj.top = curtop;
	
	return returnObj;
}

/* meausre the width of the menu to resize the button to match */
function setWidth() {

	var curWidth = document.getElementById("nav_menu").offsetWidth;
	
	var targetWidth = curWidth-7;
	
	document.getElementById("categorySelect").style.width = targetWidth + 'px';

}