/**	waiDynMenus.js ©2002 WA Interactive
*	Written by Jeremy Wyman 9.6.2002 for Kendall-Jackson Wine Estates
*	Language: JavaScript1.2 - IE 4.01 / Gecko 1.0 / Netscape 6.1 or better browser
*	Known Problems/Conflict: IE4.5 on the Macintosh, iCab, Opera 6.02, and Netscape Navigator 4.x
*	waiMenuData.js or equivalent data source file required
*/
IE4 = document.all;
NS4 = document.layers;
GECKO = navigator.userAgent.indexOf("Gecko") + 1;
MSDOM = IE4 && (navigator.appVersion.indexOf("5.") + 1 || navigator.appVersion.indexOf("6.") + 1 );
MACOS = navigator.appVersion.indexOf("Mac") + 1;
areCreated = false;
loaded = false;

menuCount = 1;
menuList = [];
buildList = [];

arrStr = "<img src=\"/images/a_trans.gif\" style=\"width:16;height:7;clip:rect(0px 0px 16px 7px);\">";

function buildMenus() {
	if (!MSDOM) return false;
	beingCreated = true;
	while (eval("window.menuArray" + menuCount)) {
		makeMenu(false, menuCount);
		menuCount++;
	}
	for(menuCount = 0; menuCount < buildList.length; menuCount += 2) {
		makeMenu(buildList[menuCount], buildList[menuCount+1]);
	}
	areCreated = true;
	beingCreated = false;
	window.status = "";
}

function makeMenu(parMenu, mCount) {
	window.status = "Building Menu " + mCount;
	D = document;
	nMenu = D.createElement("div");
	nMenu.setup = menuSetup;
	nMenu.setup(parMenu,mCount);
}

function menuSetup(parMenu, mCount) {
	D = document;    
	this.array = eval("menuArray" + mCount);
	this.childList = [];
	this.num = mCount;
	this.parMenu = parMenu;
	this.overMenu = false;
	this.buttonSetup = buttonSetup;
	this.hide = hide;
	this.hideAll = hideAll;
	this.id = "menu" + mCount;
	with (this.style) {
		position = "absolute";
		left = this.array[0][0];
		top = (GECKO) ? this.array[0][1] - (2 * menuPadding) : this.array[0][1];
		width = menuWidth - (2 * menuBorderWidth);
		height = menuHeight * (this.array.length - 1);
		borderColor = menuBorderColor;
		borderWidth = menuBorderWidth;
		borderStyle = "solid";
		cursor = "hand";
		visibility = "hidden"        
	}
	this.noWrap = true;
	menuList[menuList.length] = this.id;
	D.body.appendChild(this);
	this.buttonSetup();
}

function buttonSetup() {
	for(i = 1; i < this.array.length; i++) {
		D = document;
		nSpan = D.createElement("div");
		with (nSpan) {
			id = this.id + "span" + i;
			with (style) {
				width= "100%";
				height = (GECKO) ? menuHeight - (2 * menuPadding + 2 * borderWidth) : menuHeight;
				backgroundColor = (this.parMenu) ? menuBgColorDn : menuBgColorUp;
                borderWidth = 0;
				borderStyle = "none";
			    fontFamily = menuTextFace;
				fontSize = menuTextSize;
				color = (this.parMenu) ? menuTextColorDn : menuTextColorUp;
				padding = menuPadding;
				paddingLeft = menuPaddingLeft + 5;
				textAlign = menuAlign;
				lineHeight = menuHeight - (2 * menuPadding) + "px";
				margin = 0;
				zIndex = 100;
			    if (smallCaps) fontVariant = "small-caps";
			}
			innerHTML = this.array[i][0];
		}
		nSpan.setup = itemSetup;
		nSpan.setup(this.parMenu);
		nSpan.href = this.array[i][1];
		nSpan.txOver = this.array[i][2];
		nSpan.parMenu = this.id;
		nSpan.childMenu = false;
		if (this.array[i][3]) {
			nSpan.childMenu = this.id+ "_" +this.array[i][3];
            nSpan.style.backgroundImage = "url('/images/a_trans.gif')";
            nSpan.style.backgroundRepeat= "no-repeat";
            nSpan.style.backgroundAlign = "right";
            nSpan.style.backgroundPositionX = "100%";
            nSpan.style.backgroundPositionY = "50%";
			this.childList[this.childList.length] = this.id + "_" + this.array[i][3];
			buildList[buildList.length] = this.id;
			buildList[buildList.length] = this.num + "_" + this.array[i][3];
		}
		this.appendChild(nSpan);
	}
}

function itemSetup(flyout) {
	if (flyout) {
		this.txUp = menuTextColorDn;
		this.txDn = menuTextColorUp;
		this.bgUp = menuBgColorDn;
		this.bgDn = menuBgColorUp;
	}else{
		this.txUp = menuTextColorUp;
		this.txDn = menuTextColorDn;
		this.bgUp = menuBgColorUp;
		this.bgDn = menuBgColorDn;
	}
	this.onmouseout = menuUp;
	this.onmouseover = menuDn;
	this.onclick = clicked;
}

function openMenu(menu) {	
    if (NS4) return;
    if (!areCreated || beingCreated) return;
	OM = document.getElementById(menu);    
    if (!OM) return;  
	positionMenus();
    OM.style.visibility = "visible";  
}

function closeMenu(menu) {
	if (NS4) return;
	if (!areCreated || beingCreated) return;
	setTimeout("hideMenu('" +menu+ "')", 100);
}

function hideMenu(menu) {
	MO = document.getElementById(menu);    
    if (!MO) return false;
    if (!MO.overMenu) MO.hide();
}

function hide() {
	keepItOpen = false;
	for (i = 0; i < this.childList.length; i++) {
		MM = document.getElementById(this.childList[i]);        
		if (MM.overMenu) keepItOpen = true;
	}
	if (!keepItOpen) {
		this.style.visibility = "hidden";
		if (this.childList.length) this.hideAll;
		else if (this.parMenu) {
			MM = document.getElementById(this.parMenu);
			if (!MM.overMenu) setTimeout("hideMenu('" + MM.id + "')", 100);
		}
	}
}

function hideAllMenus() {    
    for (var x = 1; x < all_menus.length; x++) {      
      var root_menu = document.getElementById("menu" + x);      
      try {   
          root_menu.hide();         
      }
      catch(e) {}
   }
}

function hideAll() {
    //alert ("hideAll()" + this);
	for(k = 0; k < this.childList.length; k++) {
		MA = document.getElementById(this.childList[k]);
		MA.overMenu = false;
		closeMenu(this.childList[k]);
	}
}

function positionMenus() {
	D = document;
	dW = (MSDOM) ? D.body.clientWidth : document.width;
	pageOffset = parseInt((dW - pageWidth) / 2);
    leftEdge = D.getElementById("dynNavCol").offsetLeft + pageOffset;
    topEdge = D.getElementById("dynNavCol").offsetTop
	if (leftEdge < 0) leftEdge = 10;
	for (i = 0;i<menuList.length;i++) {
		MO = D.getElementById(menuList[i]);
		if (IE4) {  
            MO.style.pixelLeft = MO.array[0][0]+leftEdge;
            MO.style.pixelTop = MO.array[0][1]+topEdge;
        } else {   
            MO.style.left = MO.array[0][0]+leftEdge+(2*(menuPadding+menuBorderWidth+1));
            MO.style.top = MO.array[0][1]+topEdge+(2*(menuPadding+menuBorderWidth+1));
        }
	}
}

function menuUp() {
	parMenu = document.getElementById(this.parMenu);    
	parMenu.overMenu = false;
	with (this.style) {
		backgroundColor = this.bgUp;
		color = this.txUp;
	}    
	closeMenu(parMenu.id);	
	if (this.childMenu) closeMenu(this.childMenu);
}

var str_menu_to_open;
function menuDn() {
	D = document;    
	parMenu = D.getElementById(this.parMenu);
	parMenu.overMenu = true;
	with (this.style) {
		backgroundColor = this.bgDn;
		color = this.txDn;
	}
	if (this.childMenu) {
        str_menu_to_open = this.childMenu;
		setTimeout("openMenu(str_menu_to_open);", 110)
	}
	window.status = this.txOver;
}

function clicked() {
	if (this.href.indexOf("JavaScript:") != -1) eval(this.href);
	else window.location.href = this.href;
}