/////////////////////////////////////////////////////////////////////
//
//    FILE: menus.js
//  AUTHOR: Scott Lininger, scott<at>scottlininger.com
// PURPOSE: Javascript/DHTML functions for popup menus.
//          
/////////////////////////////////////////////////////////////////////

// initialize variables to help account for differences
// in the various browser object models 
var isNS, isNS6, isMacIE, layerRef, styleRef, isDynamic, offset

// initialize a variable to store our currently active menu 
// number, or false if no menu is active
var activeMenuID = false;

// initialize arbitrary "maximums" for # of menus and options
var maxMenus = 12;
var maxOptions = 25;

// create an array of menu objects to store everything and
// run the preparation function to get it ready to receive data
var menu = new Array();
PrepareMenuArray();


// Determine what kind of browser the user has, and react accordingly
if (navigator.appName == "Netscape") {
 	isNS = true;
	if (navigator.appVersion.charAt(0) >= "5") {
		isNS6 = true;
	}
}

isMacIE = ( (navigator.userAgent.indexOf("IE 4")  > -1) && (navigator.userAgent.indexOf("Mac")  > -1) );

if (isNS) {
	if (isNS6) {
		layerRef="document.getElementById(";
		styleRef=").style";
	} else {
		layerRef="document[";
		styleRef="]";
	}
} else {
	layerRef = "document.all[";
	styleRef = "].style";
}

isDynamic = ( (document.layers || document.all || isNS6) && !isMacIE );

if (isNS && !isNS6) {
	isDynamic = false;
}


function PrepareMenuArray () {

	// create our arbitrary number of menus
	// to be sure we have more than will be needed
	for (var i=1; i<=maxMenus; i++) {
		menu[i] = new menuObject;
	}
}


function showMenu(menuID) {

	if (activeMenuID != menuID) {
		hideActiveMenu();
		
		activeMenuID = menuID;
		if (isDynamic) {
			// make the appropriate menu appear
			//MM_swapImage(top.menu[menuID].name,'','media/white/btn_'+top.menu[menuID].name+'_over.gif',1);


			displayX = getActiveMenuX();
			

			//eval(layerRef + '"menuBackground' + menuID + '"' + styleRef + '.top = displayY');
			//eval(layerRef + '"menuTitle' + menuID + '"' + styleRef + '.top = displayY');
			
			eval(layerRef + '"menuBackground' + menuID + '"' + styleRef + '.left = displayX');
			eval(layerRef + '"menuTitle' + menuID + '"' + styleRef + '.left = displayX');

			eval(layerRef + '"menuBackground' + menuID + '"' + styleRef + '.visibility = "visible"');
			eval(layerRef + '"menuTitle' + menuID + '"' + styleRef + '.visibility = "visible"');
		}
	}
}

function getActiveMenuX() {

	if (isDynamic) {
		
		if (isNS) {
			windowWidth = window.innerWidth - 16;
			windowHeight = window.innerHeight- 16;
		} else {
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		if (isNS6) {
			windowWidth += 16;
		}
			
		menuButtonsWidth = 510;
		return ((windowWidth - menuButtonsWidth) / 2 + menu[activeMenuID].left - 181 - 19 - 12);
	}
}

function rollOut(menuID) {
	if (menuID != activeMenuID) {
		//MM_swapImgRestore()
	}  
}

function hideActiveMenu() {
	if (activeMenuID) {
		
		//MM_swapImgRestore();
		//MM_swapImage(top.menu[activeMenuID].name,'',top.menu[activeMenuID].buttonOffURL,1);
		//MM_swapImage(top.menu[activeMenuID].name,'','media/white/btn_'+top.menu[activeMenuID].name+'_off.gif',1);

		if (isDynamic) {
			eval(layerRef + '"menuTitle' + activeMenuID + '"' + styleRef + '.visibility = "hidden"');	
			eval(layerRef + '"menuBackground' + activeMenuID + '"' + styleRef + '.visibility = "hidden"');	
			eval(layerRef + '"menuHighlight"' + styleRef + '.visibility = "hidden"');	
		}
		activeMenuID = false;
	}  
}

function showHighlight(menuID,optionID) {
	
	if (isDynamic) {
		displayX = getActiveMenuX();
		displayY = top.menu[menuID].top + 11 +(top.menu[menuID].optionHeight * optionID)

		eval(layerRef + '"menuHighlight"' + styleRef + '.visibility = "visible"');	
		eval(layerRef + '"menuHighlight"' + styleRef + '.left = '+displayX);
		eval(layerRef + '"menuHighlight"' + styleRef + '.top = '+displayY);
		
	}

}
function hideHighlight() {
	
	if (isDynamic) {
		//eval(layerRef + '"menuHighlight"' + styleRef + '.visibility = "hidden"');	
	}

}

function BuildMenus() {

	if (isDynamic) {

		// now loop through the menus and options to build the dynamic layers
		for (var i=1; menu[i].option[1].title != ""; i++) {
			// now loop through the menus and options to build the dynamic styles
			document.write('<STYLE MEDIA="screen" TYPE="text/css"><!--\n');
			document.write('#menuTitle' + i + ' { position: absolute; z-index: ' + (200 + i) + '; top: ' + menu[i].top + 'px; left: ' + menu[i].left + 'px; visibility: hidden; }\n');
			document.write('#menuBackground' + i + ' { position: absolute; z-index: ' + (1 + i) + '; top: ' + menu[i].top + 'px; left: ' + menu[i].left + 'px; visibility: hidden; }\n');
			document.write('-->\n</STYLE>');

			document.write('<DIV ID="menuTitle' + i +'">');
			document.write('<img src="/media/white/btn_'+menu[i].name+'_over.gif" height="31">');
			// custom code for Mozilla, since layers in front absorb rollovers
			if (isNS && i < 5) {
				document.write('<a href="javascript:;" onMouseOver="showMenu('+(i+1)+')"><img src="/media/white/spacer.gif" height="23" width=45 alt="" border=0></a>');	
			}
			document.write('<br><img src="/media/white/menu_cap.gif" width="' + menu[i].optionWidth + '" height=5 alt="" border=0>');
			document.write('<table border=0 cellpadding=0 cellspacing=0 width="' + menu[i].optionWidth + '">');
			for (var j=1; menu[i].option[j].title != ""; j++) {
				
				
				//document.write('<td width=13>');
				//document.write('<img src="/media/white/menu_capleft.gif" width=13 height=21 alt="">');
				
				document.write('<tr><td height="' + menu[i].optionHeight + '">');
				document.write('<a href="' + menu[i].option[j].url + '" ');
				if (menu[i].option[j].target != "") {
					document.write('target="' + menu[i].option[j].target + '" ');
				}
				//document.write('class="menuOption" onclick="top.hideActiveMenu()" onmouseover="MM_swapImage(\'option' + i + '-' + j + '\',\'\',\'media/white/menu_over.gif\',1)" onmouseout="MM_swapImgRestore()">');
				document.write('class="menuOption" onclick="top.hideActiveMenu()" onmouseover="top.showHighlight('+i+','+j+')" onmouseout="top.hideHighlight()">');
				document.write( '<img src="'+menu[i].option[j].icon+'" width=12 height=22 alt="" border=0 align="absmiddle">' );
				document.write( menu[i].option[j].title );
				document.write('</a></td></tr>');
			}
			document.write('</table></DIV>');
		
			document.write('<DIV ID="menuBackground' + i +'">');
			document.write('<img src="/media/white/spacer.gif" width=1 height=31 alt="" border=0><br>');
			document.write('<img src="/media/white/menu_captop.gif" width=162 height=5 alt="" border=0><table border=0 cellpadding=0 cellspacing=0><tr><td>');
			for (var j=1; menu[i].option[j].title != ""; j++) {

				document.write('<IMG SRC="' + menu[i].bgImgSrc + '" WIDTH="' + menu[i].optionWidth + '" HEIGHT="' + (menu[i].optionHeight) + '" BORDER="0" NAME="option' + i + '-' + j + '" ><br>');

			}
			document.write('<IMG SRC="/media/white/menu_cap.gif" WIDTH="' + menu[i].optionWidth + '" HEIGHT=5 alt=""><br>');
			document.write('</td></tr></table></DIV>');
		}


		
		document.write('<STYLE MEDIA="screen" TYPE="text/css"><!--\n');
		document.write('#menuHighlight { position: absolute; z-index: 199; top: 100px; left: 100px; visibility: hidden; }\n');
		document.write('-->\n</STYLE>');
		document.write('<DIV ID="menuHighlight"><img src="/media/white/menu_over.gif" width="162" height="25"></div>');
	}

}


// A custom object to hold popup menu information
function menuObject () {
	this.name = "";
	this.top = 25;
	this.left = 113;
	this.optionHeight = 25;
	this.menuImageWidth = 110;
	this.optionWidth = 162;
	this.bgImgSrc = "/media/white/menu_off.gif";
	this.option = new Array();
	for (var i=1; i<=maxOptions; i++) {
		this.option[i] = new optionObject;
	}

}

// A custom object to hold popup menu option information
function optionObject () {
	this.title = "";
	this.url = "#";
	this.target = "";
	this.icon = "/media/white/icon_default.gif";
}

if (isDynamic) {


	if (menu) {

		var lastX, lastY;

		document.onmouseup = top.hideActiveMenu;
		
		if (document.all) {
			window.onscroll = top.hideActiveMenu;
		} else if (document.layers) {
			lastX = this.pageXOffset;
			lastY = this.pageYOffset;
			setInterval ('checkScroll()', 100);
		}

		function checkScroll () {
			if (lastX != this.pageXOffset || lastY != this.pageYOffset ) {
				lastX = this.pageXOffset;
				lastY = this.pageYOffset;
				top.hideActiveMenu();
			}
		}
	}







	// now we define the menus themselves
	if (menu) {
		top.menu[1].name = "games";
		top.menu[1].left = 100;
			top.menu[1].option[1].title = "Caravans of Ahldarahd";
			top.menu[1].option[2].title = "Children of Fire";
			top.menu[1].option[3].title = "The Pitch: TV Edition";
			top.menu[1].option[1].url = "/caravans.php";
			top.menu[1].option[2].url = "/cof.php";//cof.php";
			top.menu[1].option[3].url = "/pitch.php";//pitchtv.php";


		top.menu[2].name = "previews";
		top.menu[2].left = 169;
			/*top.menu[2].option[1].title = "Garage Band";
			top.menu[2].option[2].title = "PROBE!";
			top.menu[2].option[3].title = "MurderBook";
			top.menu[2].option[4].title = "Cops &amp; Robbers";
			top.menu[2].option[1].url = "garageband.php";//garageband.php";
			top.menu[2].option[2].url = "probe.php";//murderbook.php";
			top.menu[2].option[3].url = "murderbook.php";//cannibal.php";
			top.menu[2].option[4].url = "copsrobbers.php";//cannibal.php";*/
      top.menu[2].option[1].title = "Children of Fire RPG";
      top.menu[2].option[1].url = "cof_rpg.php";//cannibal.php";*/


		top.menu[3].name = "store";
		top.menu[3].left = 252;
			/*top.menu[3].option[1].title = "<span style=color:gray>Shop Games</span>";
			top.menu[3].option[2].title = "<span style=color:gray>Shop Components</span>";
			top.menu[3].option[3].title = "<span style=color:gray>Find a Retailer</span>";
			top.menu[3].option[4].title = "<span style=color:gray>Shopping Cart</span>";
			top.menu[3].option[5].title = "<span style=color:gray>Account Details</span>";
			top.menu[3].option[1].url = "#";//shopgames.php";
			top.menu[3].option[2].url = "#";//shopcomponents.php";
			top.menu[3].option[3].url = "#";//findretailer.php";
			top.menu[3].option[3].url = "#";//cart.php";
			top.menu[3].option[3].url = "#";//account.php";*/
      top.menu[3].option[1].title = "Buy Caravans";
      top.menu[3].option[2].title = "Buy Children of Fire";
      top.menu[3].option[3].title = "Buy The Pitch!";
      top.menu[3].option[1].url = "caravans_buynow.php";//shopgames.php";
			top.menu[3].option[2].url = "cof_buy.php";//shopcomponents.php";
			top.menu[3].option[3].url = "pitch_buy.php";//findretailer.php";


		top.menu[4].name = "community";
		top.menu[4].left = 317;
			/*top.menu[4].option[1].title = "<span style=color:gray>Recent Cons</span>";
			top.menu[4].option[2].title = "<span style=color:gray>Playtester Program</span>";
			top.menu[4].option[3].title = "<span style=color:gray>Demo Program</span>";
			top.menu[4].option[4].title = "<span style=color:gray>Web Links</span>";
			top.menu[4].option[1].url = "#";//forums.php";
			top.menu[4].option[2].url = "#";//cons.php";
			top.menu[4].option[3].url = "#";//playtesters.php";
			top.menu[4].option[4].url = "#";//demos.php";*/
      top.menu[4].option[1].title = "Caravans on BGG";
      top.menu[4].option[2].title = "Children of Fire on BGG";
      top.menu[4].option[3].title = "The Pitch! on BGG";
      top.menu[4].option[1].url = "http://boardgamegeek.com/boardgame/17071/caravans-of-ahldarahd";
			top.menu[4].option[2].url = "http://boardgamegeek.com/boardgame/18564/children-of-fire";
			top.menu[4].option[3].url = "http://boardgamegeek.com/boardgame/19434/the-pitch-tv-edition";//playtesters.php";
			top.menu[4].option[1].target = "_blank";
      top.menu[4].option[2].target = "_blank";
      top.menu[4].option[3].target = "_blank";


		top.menu[5].name = "download";
		top.menu[5].left = 416;

			top.menu[5].option[1].title = "Catalog PDF";
			top.menu[5].option[2].title = "Box Art PDF";
			top.menu[5].option[1].url = "/BlindLuck2005Releases.pdf";
			top.menu[5].option[2].url = "/BlindLuckBoxArt.pdf";
			top.menu[5].option[1].target = "_blank";
			top.menu[5].option[2].target = "_blank";


		top.menu[6].name = "company";
		top.menu[6].left = 710;
			top.menu[6].option[1].title = "About Us";
			top.menu[6].option[2].title = "News";
			top.menu[6].option[3].title = "Contact Us";
			top.menu[6].option[4].title = "Info for Retailers";
			top.menu[6].option[5].title = "Info For Distributors";
			top.menu[6].option[6].title = "Legal";
			top.menu[6].option[1].url = "/aboutus.php";
			top.menu[6].option[2].url = "/news.php";
			top.menu[6].option[3].url = "/contactus.php";
			top.menu[6].option[4].url = "/retailers.php";
			top.menu[6].option[5].url = "/distributors.php";
			top.menu[6].option[6].url = "/legal.php";

		BuildMenus();
	}
} 

