/* These are some routines to handle a hierarchical pull-down or pop-up menu system */
/* (c)2007-2009, Red Beagle Web Development */
/* Revision 090806 Added fade-in pulldown menus */

var collapseCount=0;			//	This indicates the most recent collapse request. Only the most recent collapse will be honored
var thisPage='';
var thisImage='';
var collapseDelay=400;			//	milliseconds of delay until the menu disappears, after rollout.
var revealMe;					//	This is for a "fade-in" of a pulldown menu
var lastRevealed='';			//	Remember which div was faded in so we don't repeat a fade-in
var oStep=2;					//	Opacity scale is 1-10, this is the step size
var oMax=9;						//	Max opacity of the faded-in menu. Recommend either 9 or 10
var oDelay=35;					//	Speed of the fade in

function highlight() {		// Hightlight an area
	for (i=0; i<highlight.arguments.length; i+=2) {
		if (highlight.arguments[i]!=thisPage) {
			var obj=document.getElementById(highlight.arguments[i]);
			var str=highlight.arguments[i+1];
			if (str.indexOf(".")>-1) {	// Then it's an image
				obj.src=imageDir+str;
			} else {
				obj.style.background=str;
			}
		}
	}
}

function newImage(arg) { 
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function setActivePageButton(aa, bb) {		// The image with the id thisPage should stay frozen at image thisImage.
											// This is intended to indicate the active page. The menu should still operate like normal
	collapseMenu(0);
	if (aa.length>0) {
		thisPage=aa;
		thisImage=bb;
		document.getElementById(thisPage).src=imageDir+thisImage;
	}
	for (i=0; i<preloadImage.length; i++) {
		imgz=newImage(imageDir+preloadImage[i]);
//		document.getElementById(divwImages[i]).width=imgz.width;
//		document.getElementById(divwImages[i]).height=imgz.height;
	}
}

function rossReveal(id) {								// Reveal a pulldown menu
	revealMe=document.getElementById(id);				// Get the item to reveal
	document.getElementById(id).style.display="block";	// Set the display from none to block
	if (lastRevealed!=id) rossRevealOpacity(1);			// If this is a new menu (not currently being revealed) then begin fade in
	lastRevealed=id;									// Remember that this is the current id being faded in,
}

function rossRevealOpacity(revealOpacity) {
	revealMe.style.opacity = revealOpacity/10;
	revealMe.style.filter = 'alpha(opacity=' + revealOpacity*10 + ')';
	if (revealOpacity<oMax) setTimeout("rossRevealOpacity("+Math.min((revealOpacity+oStep), oMax)+")", oDelay);
}

function rossConceal() {			// Conceal all the <div> in the argument list
	for(i=0; i<rossConceal.arguments.length; i++) {
		document.getElementById(rossConceal.arguments[i]).style.display="none";
		if (lastRevealed==rossConceal.arguments[i]) lastRevealed='';
	}
}

function cancelCollapse() {			// Cancel the current collapse request(s)
									// Simply incrementing collapseCount will cause collapseMenu to fail for any
									// scheduled collapse.
	collapseCount++;
}

function scheduleCollapse() {		// Schedule a new collapse. Note this increments the collapseCount, so no previous collapse request will be honored
	collapseCount++;
	setTimeout("collapseMenu("+collapseCount+")", collapseDelay);
}

function collapseMenu(z) {			// Collapse all menus when I get to this point.
	if (z==collapseCount) {
		for (i=0; i<allMenus.length; i++) {
			document.getElementById(allMenus[i]).style.display="none";
		}
		collapseFlag=false;
		lastRevealed='';
	}
}

// locateMenu(target, source submenu, source menu): locate a menu on the screen in keeping w proper locations.
// target is placed just to the right of its submenu.
function locateMenu(targ, source, source2) {
	var obj=document.getElementById(targ);
	var obj2=document.getElementById(source);
	var obj3=document.getElementById(source2);
	var left=(obj2.offsetWidth+obj2.offsetLeft+obj3.offsetLeft+1);	// +1 for the border width, -1 in the next line for same
	// If the source item has a forced width, impose this width instead of the natural width, object2.offsetWidth
	if (typeof forcedWidth[source]!="undefined") left=(forcedWidth[source]+obj2.offsetLeft+obj3.offsetLeft+1);
	var ttop=(obj2.offsetTop+obj3.offsetTop-1);
	obj.style.left=left+"px";
	obj.style.top=ttop+"px";
}

// locateMenu(target, source submenu, source menu): locate a menu on the screen in keeping w proper locations.
// target is placed just to the right of its submenu.
function locateMenuSouth(targ, source, source2) {
	var obj=document.getElementById(targ);
	var obj2=document.getElementById(source);
	var obj3=document.getElementById(source2);
//	left=(obj2.offsetWidth+obj2.offsetLeft+obj3.offsetLeft+1);	// +1 for the border width, -1 in the next line for same
	// If the source item has a forced width, impose this width instead of the natural width, object2.offsetWidth
	ttop=(obj2.offsetHeight+obj2.offsetTop+obj3.offsetTop+1);	// +1 for the border width, -1 in the next line for same
//	if (!isNaN(forcedWidth[source])) ttop=(forcedWidth[source]+obj2.offsetTop+obj3.offsetTop+1);
	if (typeof forcedWidth[source]!="undefined") ttop=(forcedWidth[source]+obj2.offsetTop+obj3.offsetTop+1);
	left=(obj2.offsetLeft+obj3.offsetLeft-1);
	obj.style.left=left+"px";
	obj.style.top=ttop+"px";
}

function rossNewWindow(mypage, myname, w, h, scroll)
{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = "height="+h+",width="+w+",top="+wint+",left="+winl+",scrollbars="+scroll
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function rossFindHeight(arg) {
	obj=document.getElementById(arg);
	if (obj.offsetHeight) return obj.offsetHeight;
	else return 0;
}

function talkHeight() {
	maxxie=0;
	for(i=0; i<talkHeight.arguments.length; i++) { // alert(isNaN(talkHeight.arguments[i]));
		if (isNaN(talkHeight.arguments[i])) maxxie=Math.max(maxxie, rossFindHeight(talkHeight.arguments[i]));
		else maxxie=Math.max(maxxie, parseInt(talkHeight.arguments[i]));
	}
	for(i=0; i<talkHeight.arguments.length; i++) {
		if (typeof(talkHeight.arguments[i])!="number") document.getElementById(talkHeight.arguments[i]).style.height=maxxie+"px";;
	}
}

//  This function is intended to fix a problem I've seen in MSIE, where mouseout occurs too soon.
//  This may occur as a result of a menu element being more narrow than its container, the parentNode.
//  So set the width of the object to the width of its parent-22. This is hacky.
//  22 comes from leftBorder=1, leftPadding=10, rightpadding=10, rightBorder=1
function correctWidth(id) {
	return true;
//	obj=document.getElementById(id);
//	obj.style.width=(obj.parentNode.offsetWidth-22)+"px";
}
