/*
  cmiMenuEngineIE.js
  by Michael Fleming 
  for Constructive Media Inc.
  created: September 28, 1999
*/
	
loader = (isFrames) ? parent.document.body : window;
loader.onload = startIt;


isLoaded = false;

if (!window.menuVersion) {
	clickKill = showVisited = keepHilite = clickStart = false;
}

isWin = (navigator.appVersion.indexOf("Win") != -1)

mSecsVis = secondsVisible*1000;
isRight = (window.navFrLoc && navFrLoc == "right");

imgSuf = (isRight) ? ">"  : "ALIGN=RIGHT>";

imgStr = "<IMG SRC='" + imgSrc + "' WIDTH=" + imgWd + " HEIGHT=" + imgHt +" BORDER=0 VSPACE=2 " + imgSuf;
spStr = "";

areCreated = false;
menuLoc = null;

initVars();

/**
  * resets menu variables
  */
function initVars() {
	if(areCreated) {
		for(i=1; i<topCount; i++) {
			cur = eval("menuElement"+i);
			clearTimeout(cur.hideTimer);
			cur.hideTimer=null;
		}
		clearTimeout(allTimer);
	}
	
	topCount = 1;
	areCreated = false;
	beingCreated = false;
	isOverMenu = false;
	currentMenu = null;
	allTimer = null;
}




/*
 * function called when window loaded
 */
function startIt() {
	isLoaded = true;
	if (isFrames) {
		menuLoc = eval("parent.frames." + mainFrName);
			menuLoc.document.body.onunload = IEunloaded;
			} else {
	menuLoc = window;
	}
	menuLoc.nav = nav = window;
	if (clickKill) {
		menuLoc.document.onmousedown = clicked;
	}
	makeTop();	
}

function makeTop(){
	beingCreated = true;

	while(eval("window.cmiMenu" + topCount)) {
		makeMenuIE(false,topCount);
		topCount++;
	}

    status = "WIND POINT";
	areCreated = true;
	beingCreated = false;
}




function IEunloaded() {
	initVars();
	isLoaded = false;
	setTimeout("keepTrack()",50)
}

function keepTrack() {
	if (menuLoc.document.readyState == "complete") {
		initVars();
		startIt();
	}
	else {
		setTimeout("keepTrack()",50);
	}
}


function setMenuTree(isChild,menuParent) {
	if (!isChild) {
		this.menuWidth = this.array[0] ? this.array[0] : menuWidth;
		if (isCentered) {
		  this.menuLeft = calcOffset(this.array[1], templateWidth);
		} else {
		  this.menuLeft = this.array[1];
		}
		this.menuTop = this.array[2];
		this.menuFontColor = this.array[3] ? this.array[3] : fntCol;
		this.menuFontOver = this.array[4] ? this.array[4] : overFnt;
		this.menuBGColor = this.array[5] ? this.array[5] : backCol;
		this.menuBGOver = this.array[6] ? this.array[6] : overCol;
		this.menuBorCol = this.array[7] ? this.array[7] : borCol;
		this.menuSeparatorCol = this.array[8] ? this.array[8] : separatorCol;
		this.treeParent = this;
		this.startChild = this;
	}
	else {
		this.menuWidth = menuParent.menuWidth;
		this.menuLeft = menuParent.menuLeft;
		this.menuTop = menuParent.menuTop;
		this.menuFontColor = menuParent.menuFontColor;
		this.menuFontOver = menuParent.menuFontOver;
		this.menuBGColor = menuParent.menuBGColor;
		this.menuBGOver = menuParent.menuBGOver;
		this.menuBorCol = menuParent.menuBorCol;
		this.menuSeparatorCol = menuParent.menuSeparatorCol;
		this.treeParent = menuParent.treeParent;
	}

	this.maxItems = (isChild) ? this.array.length/3 : (this.array.length-9)/3;
	this.hasParent = isChild;
    this.setup = menuSetup;
	this.itemCount = 0;
}

function makeMenuIE(isChild,menuCount,menuParent) {

	menu = makeElement("menuElement" + menuCount);
	menu.array = eval("cmiMenu" + menuCount);

	menu.setMenuTree = setMenuTree;
	menu.setMenuTree(isChild,menuParent);

	menu.itemStr = "";
	
	while (menu.itemCount < menu.maxItems) {
		menu.itemCount++;
		itemName = "item" + menuCount + "_" + menu.itemCount;

		arrayPointer = (isChild) ? (menu.itemCount-1)*3 :((menu.itemCount-1)*3)+9;
		dispText = menu.array[arrayPointer];
		hasMore = menu.array[arrayPointer + 2];
		//if the menu item has a submenu, append a tag to include an image file, otherwise
		// just display the text
		htmStr = (hasMore) ? imgStr + dispText : dispText;
		menu.itemStr += "<SPAN ID=" + itemName + " STYLE=\"width:" + menu.menuWidth + "; z-index:2; \">" + htmStr + "</SPAN><BR>";
		 
		if (hasMore) { //if there's more, call on ourselves again to grab the submenu item(s)
			makeMenuIE(true,menuCount + "_" + menu.itemCount,menu);
			menu = menu.parentMenu;
		}	
	}

	menu.innerHTML = menu.itemStr;
	itemColl = menu.children.tags("SPAN");
	for (i=0; i<itemColl.length; i++) {
		it = itemColl(i);
		it.setup = itemSetup;
		it.setup(i+1,menu.array);
	}
	menu.lastItem = itemColl(itemColl.length-1);
	menu.setup(isChild,menuParent);
}

function makeElement(whichEl,whichWidth,whichParent,whichContainer) {

		elStr = "<DIV   ID=" + whichEl + " STYLE='position:absolute; z-index:2;'></DIV>";
		menuLoc.document.body.insertAdjacentHTML("BeforeEnd",elStr);
		if (isFrames) eval(whichEl + "= menuLoc." + whichEl);

	return eval(whichEl);
}

function itemSetup(whichItem,whichArray) {
	this.onmouseover = itemOver;
	this.onmouseout = itemOut;
	this.container =  this.offsetParent;

	arrayPointer = (this.container.hasParent) ? (whichItem-1)*3 : ((whichItem-1)*3)+9;

	this.dispText = whichArray[arrayPointer];
	this.linkText = whichArray[arrayPointer + 1];
	this.hasMore = whichArray[arrayPointer + 2];

	if (this.hasMore) {
		this.child = eval("menuElement" + this.id.substr(4));
		this.child.parentMenu = this.container;
		this.child.parentItem = this;
	}

	if (this.linkText) {
			this.onclick = linkIt;
			this.style.cursor = "hand";
	}

	
	
		with (this.style) {
			padding = itemPad;
			if (isRight && !this.hasMore) paddingLeft = parseInt(padding)+imgSiz;
			color = this.container.menuFontColor;
			fontSize = fntSiz + "pt";
			fontWeight = (fntBold) ? "bold" : "normal";
			fontStyle =	(fntItal) ? "italic" : "normal";
			fontFamily = fntFam;
			borderBottomWidth = separator + "px";
			borderBottomColor = this.container.menuSeparatorCol;
			borderBottomStyle = "solid";
			backgroundColor = this.container.menuBGColor;
		}
	
}	

function menuSetup(hasParent,openCont,openItem) {
	this.onmouseover = menuOver;
	this.onmouseout = menuOut;
	
	this.showIt = showIt;
	this.keepInWindow = keepInWindow;
	this.hideTree = hideTree
	this.hideParents = hideParents;
	this.hideChildren = hideChildren;
	this.hideTop = hideTop;
	this.hasChildVisible = false;
	this.isOn = false;
	this.hideTimer = null;

	this.childOverlap = (perCentOver != null) ? ((perCentOver/100) * this.menuWidth) : childOverlap;
	this.currentItem = null;
	this.hideSelf = hideSelf;
		
	if (hasParent) {
		this.hasParent = true;
		this.parentMenu = openCont;
	}
	else {
		this.hasParent = false;
	}

	
	
		with (this.style) {
			width = this.menuWidth;
			borderWidth = borWid;
			borderColor = this.menuBorCol;
			borderStyle = borSty;
		}

		this.lastItem.style.border="";
		this.fullHeight = this.scrollHeight;
		this.showIt(false);
		this.onselectstart = cancelSelect;
		this.moveTo = moveTo;
		this.moveTo(0,0);
	
}

function popUp(menuName,e){

	if (!isLoaded) return;
	linkEl = event.srcElement;
	if (clickStart) linkEl.onclick = popMenu;
	if (!beingCreated && !areCreated) startIt();
	linkEl.menuName = menuName;	
	if (!clickStart) popMenu(e);
}

function popMenu(e){
	if (!isLoaded || !areCreated) return true;

	eType = event.type;
	if (clickStart && eType != "click") return true;
	hideAll();

	linkEl = event.srcElement;
	
	currentMenu = eval(linkEl.menuName);
	currentMenu.hasParent = false;
	currentMenu.treeParent.startChild = currentMenu;
	
	menuLocBod = menuLoc.document.body;
	if (!isFrames) {
		xPos = (currentMenu.menuLeft) ? currentMenu.menuLeft : (event.clientX + menuLocBod.scrollLeft);
		yPos = (currentMenu.menuTop) ? currentMenu.menuTop : (event.clientY + menuLocBod.scrollTop);
	}
	else {
		switch (navFrLoc) {
			case "left":
				xPos = (currentMenu.menuLeft) ? currentMenu.menuLeft : menuLocBod.scrollLeft;
				yPos = (currentMenu.menuTop) ? currentMenu.menuTop : event.clientY + menuLocBod.scrollTop;
				break;
			case "top":
				xPos = (currentMenu.menuLeft) ? currentMenu.menuLeft : event.clientX + menuLocBod.scrollLeft;
				yPos = (currentMenu.menuTop) ? currentMenu.menuTop : menuLocBod.scrollTop;
				break;
			case "bottom":
				xPos = (currentMenu.menuLeft) ? currentMenu.menuLeft : event.clientX + menuLocBod.scrollLeft;
				yPos = (currentMenu.menuTop) ? currentMenu.menuTop : menuLocBod.scrollTop + menuLocBod.clientHeight;
				break;
			case "right":
				xPos = (currentMenu.menuLeft) ? currentMenu.menuLeft : menuLocBod.scrollLeft+menuLocBod.clientWidth;
				yPos = (currentMenu.menuTop) ? currentMenu.menuTop : event.clientY + menuLocBod.scrollTop;
				break;
		}
	}

	currentMenu.moveTo(xPos,yPos);
	currentMenu.keepInWindow()
	currentMenu.isOn = true;
	currentMenu.showIt(true);

	return false;
}

function menuOver(e) {
	this.isOn = true;
	isOverMenu = true;
	currentMenu = this;
	if (this.hideTimer) clearTimeout(this.hideTimer);
}

function menuOut() {
	theEvent = menuLoc.event;
	if (theEvent.srcElement.contains(theEvent.toElement)) return;
	this.isOn = false;
	isOverMenu = false;
	menuLoc.status = "";
	if (!clickKill) allTimer = setTimeout("currentMenu.hideTree()",10);  
}

function itemOver(){
	if (keepHilite) {
		if (this.container.currentItem && this.container.currentItem != this) {
		  with (this.container.currentItem.style) {
		    backgroundColor = this.container.menuBGColor;
		    color = this.container.menuFontColor;
		  }
		}
	}

	
	theEvent = menuLoc.event;
	if (theEvent.srcElement.tagName == "IMG") return;
	this.style.backgroundColor = this.container.menuBGOver;
	this.style.color = this.container.menuFontOver;
	
	//menuLoc.status = this.linkText;

	this.container.currentItem = this;

	if (this.container.hasChildVisible) {
		this.container.hideChildren(this);
	}

	if (this.hasMore) {
		horOffset = (isRight) ? (this.container.childOverlap - this.container.menuWidth) : (this.container.menuWidth - this.container.childOverlap);

		this.childX = this.container.style.pixelLeft + horOffset;
		this.childY = this.offsetTop + this.container.style.pixelTop + childOffset;

		this.child.moveTo(this.childX,this.childY);
		this.child.keepInWindow();
		this.container.hasChildVisible = true;
		this.container.visibleChild = this.child;
		this.child.showIt(true);
	}
	 for (var i=0; i<document.all.length; i++) {
	 o = document.all(i)
	 if (o.type == 'select-one' || o.type == 'select-multiple') {
	  // todo: add check for select in div?
	  if (o.style) o.style.display = 'none';
	 }
	}

}

function itemOut() {
  theEvent = menuLoc.event;
  if (theEvent.srcElement.contains(theEvent.toElement) || (theEvent.fromElement.tagName=="IMG" && theEvent.toElement.contains(theEvent.fromElement)))
    return;
  if (!keepHilite) {
	this.style.backgroundColor = this.container.menuBGColor;
	this.style.color = this.container.menuFontColor;
	for (var i=0; i<document.all.length; i++) {
	 o = document.all(i)
	 if (o.type == 'select-one' || o.type == 'select-multiple') {
	  // todo: add check for select in div?
	  if (o.style) o.style.display = 'inline';
	 }
	}
  }
}

function moveTo(xPos,yPos) {
	this.style.pixelLeft = xPos;
	this.style.pixelTop = yPos;
}

function showIt(on) {

		this.style.visibility = (on) ? "visible" : "hidden";
		if (keepHilite && this.currentItem) {
			with (this.currentItem.style) {
			backgroundColor = this.menuBGColor;
				
				color = this.menuFontColor;
			}
		}

	this.currentItem = null;
}

function keepInWindow() {
	scrBars = 20;
	botScrBar = (isFrames && navFrLoc=="bottom") ? (borWid*2) : scrBars;
	rtScrBar = (isFrames && navFrLoc=="right") ? (borWid*2) : scrBars;
	

    	winRight = (menuLoc.document.body.scrollLeft + menuLoc.document.body.clientWidth) - rtScrBar;
		rightPos = this.style.pixelLeft + this.menuWidth;
	
		if (rightPos > winRight) {
			if (this.hasParent) {
				parentLeft = this.parentMenu.style.pixelLeft;
				newLeft = ((parentLeft - this.menuWidth) + this.childOverlap);
				this.style.pixelLeft = newLeft;
			}
			else {
				dif = rightPos - winRight;
				this.style.pixelLeft -= dif;
			}
		}

		winBot = (menuLoc.document.body.scrollTop + menuLoc.document.body.clientHeight) - botScrBar;
		botPos = this.style.pixelTop + this.fullHeight;

		if (botPos > winBot) {
			dif = botPos - winBot;
			this.style.pixelTop -= dif;
		}
		
		winLeft = menuLoc.document.body.scrollLeft;
		leftPos = this.style.pixelLeft;

		if (leftPos < winLeft) {
			if (this.hasParent) {
				parentLeft = this.parentMenu.style.pixelLeft;
				newLeft = ((parentLeft+this.menuWidth) - this.childOverlap);
				this.style.pixelLeft = newLeft;
			}
			else {
				this.style.pixelLeft = 5;
			}
		}
}

function linkIt() {
//3.07 added colon (:)
	if (this.linkText.indexOf("javascript:")!=-1) eval(this.linkText)
	else menuLoc.location.href = this.linkText;
}

function popDown(menuName){
	if (!isLoaded || !areCreated) return;
	whichEl = eval(menuName);
	whichEl.isOn = false;
	if (!clickKill) whichEl.hideTop();
}

function hideAll() {
	for(i=1; i<topCount; i++) {
		temp = eval("menuElement" + i + ".startChild");
		temp.isOn = false;
		if (temp.hasChildVisible) temp.hideChildren();
		temp.showIt(false);
	}	
}

function hideTree() { 
	allTimer = null;
	if (isOverMenu) return;
	if (this.hasChildVisible) {
		this.hideChildren();
	}
	this.hideParents();
}


function hideTop() {
	whichTop = this;
	(clickKill) ? whichTop.hideSelf() : (this.hideTimer = setTimeout("if(whichTop.hideSelf)whichTop.hideSelf()",mSecsVis));
}

function hideSelf() {
	this.hideTimer = null;
	if (!this.isOn && !isOverMenu) { 
		this.showIt(false);
	}
}

function hideParents() {
	tempMenu = this;
	while (tempMenu.hasParent) {
		tempMenu.showIt(false);
		tempMenu.parentMenu.isOn = false;		
		tempMenu = tempMenu.parentMenu;
	}
	for (var i=0; i<document.all.length; i++) {
	 o = document.all(i)
	 if (o.type == 'select-one' || o.type == 'select-multiple') {
	  // todo: add check for select in div?
	  if (o.style) o.style.display = 'inline';
	 }
	}
	tempMenu.hideTop();
}

function hideChildren(item) {
	tempMenu = this.visibleChild;
	while (tempMenu.hasChildVisible) {
		tempMenu.visibleChild.showIt(false);
		tempMenu.hasChildVisible = false;
		tempMenu = tempMenu.visibleChild;
	}

	if (!this.isOn || !item.hasMore || this.visibleChild != this.child) {
		this.visibleChild.showIt(false);
		this.hasChildVisible = false;
	}
}

function cancelSelect(){return false}

function reDo(){
	if (loader.innerWidth==origWidth && loader.innerHeight==origHeight) return;
	initVars();
	NSresized=true;
	//menuLoc.location.reload();
	document.location.reload();
}

function clicked() {
	if (!isOverMenu && currentMenu!=null && !currentMenu.isOn) {
		whichEl = currentMenu;
		whichEl.hideTree();
	}
}

window.onerror = handleErr;

function handleErr(){
	arAccessErrors = ["permission","access"];
	mess = arguments[0].toLowerCase();
	found = false;
	for (i=0;i<arAccessErrors.length;i++) {
		errStr = arAccessErrors[i];
		if (mess.indexOf(errStr)!=-1) found = true;
	}
	return found;
}

function calcOffset(x, width) {
	var newx;
	if(window.innerWidth) {
   if (window.innerWidth < width) {
     newx = x
   }
   else {
     newx = ((window.innerWidth - width) / 2) + x;
   }	
	}	else {
		 if (document.body.clientWidth) {
     if (document.body.clientWidth < width) {
       newx = x
     } else {
      newx = ((document.body.clientWidth - width) / 2) + x;
     }	
	  }
	}
	return newx;
}

//end