/* Dynamic menus v3.2.1 - menu_logic.js
*
*  Verified with: Mozilla 0.9.4, IE4, IE5, IE5.5, IE6, NN4.08+ 
*
*  Known issues: For Gecko (Mozilla, Netscape 6, etc.):
*	Mozilla 0.9.4 is the only gecko browser currently supported by this code.
*   All other gecko browsers simply don't have a complete enough DOM to do what's needed
*
*  Known issues: For IE5 (Under Windows 98)
*	Under windows 98, IE5 will intermittently not display the "dm_Image" item
*	Work-around: use IE4 code.  It's faster anyway.
*
*
*  Copyright 2001, Webifi Inc. - Business Internet Solutions - All Rights Reserved
*  Author:  John Kielkopf - john@webifi.com
*  Revision Date: 11-16-2001
*  May not be used without permission from author.
* 
*  Customized for Datacard Group
*/

// ************************************
// *  Browser Testing
// ************************************
var NS4 = (document.layers) ? 1 : 0;
var IE4 = (document.all&&(!document.getElementById)) ? 1 : 0;
var IE5 = (document.getElementById && document.all) ? 1 : 0;
// If we are IE4 then we are going to add some functionality to make it act
// more like IE5... this should help simplify code, although it may complicate browser
// detection for other scripts.
if (IE4){
	document.getElementById = ie4getElementById;
};
var NS6 = (document.getElementById && (!document.all)) ? 1 : 0;


// ***************************************
// * Global Preferences (User Modifiable)
// ***************************************

if(!dm_absolutePath){
//	var dm_absolutePath = "http://www.datacard.com"; //if absolute path has already been set, we'll use it, else, it's nothing.
	var dm_absolutePath = ""; //if absolute path has already been set, we'll use it, else, it's nothing.
	// If needed, "dm_absolutePath" should be set by the calling page, before including this file.  
	// It's to allow use of the same code from an external site.
}
if(!dm_homeUrl){
	var dm_homeUrl = dm_absolutePath; //if absolute path has already been set, we'll use it, else, it's nothing.
	// If needed, "dm_absolutePath" should be set by the calling page, before including this file.  
	// It's to allow use of the same code from an external site.
}

//var dm_imagePath = dm_absolutePath + "/images/sidebar/"; // location of global menu images
var dm_imagePath = dm_absolutePath + ""; // location of global menu images
//var dm_scriptPath = dm_absolutePath + "/menus/scripts/"; // location of scripts
var dm_scriptPath = dm_absolutePath + ""; // location of scripts

var dm_minimumMenuHeight = 200; //If a menu needs to be squished to fit, this is the smallest the menu should get before the top position of the menu is raised from its spawn position. 0 = allow menu to raise as needed.
var dm_maximumMenuHeight = 0; //0 = use all space available, else the menu will start to scroll if it is larger.  Overridden by srollHeight of menu, unless dm_ignoreScrollHeight is true.
var dm_ignoreScrollHeight = true; //Ignores the "scroll height" parameter of a menu, allowing it to be as large as it needs, or allows dm_maximumMenuHeight to override menu's scrollhieght,
var dm_leftMargin = 12; // Note:  If you will be using the "left" arrows, this should be larger than the arrow width
var dm_borderSize = 1; //1
var dm_separationSize = 1; //1
var dm_normalItemHeight = 16;
var dm_doubleItemHeight = 29;
var dm_menuArrowWidth = 14; //Height of the menu arrow scales with the height of the menu item (normal, or double)
var dm_scrollArrowHeight = 13;
var dm_menuScrollArrowImageWidth = 11;
var dm_menuScrollArrowImageHeight = 10;
var dm_scrollArrowBackgroundColor = '#D9E7F0';
var dm_scrollArrowEffectColor = '#5997BD';
var dm_scrollSize = 18
var dm_scrollSpeed = 80
var dm_fontSize = '10px';
var dm_largeFontSize = '10px'; // Used for the header of dm_text items
var dm_fontFamily = 'verdana,arial';
var dm_fontWeight = 'bold';
var dm_borderColor = '#00609A';
var dm_separationColor = '#a3c5da'; 
var dm_backgroundColor = '#ffffff'; //#1D3960 
var dm_backgroundEffectColor = '#55708E'; //#D9E7F0
if(NS4){
	var dm_textColor = '#336699'; // Text Color if netscape 4
}else{
	var dm_textColor = '#000000'; // #336699 Text color for anyone else
};
var dm_textEffectColor = '#ffffff'; // #336699 will not work for NS4.  Set to same as dm_textColor for no effect
var dm_allowEffectOnAll = false; // if true, items without children, or HREFSs, will effect
var dm_menuTimer = 200; //number of msec delay before showing/killing menus. 
var dm_hideIntrusiveElements = true;  // Should the document be scrubbed for select lists that could get in the way?
									 // For this to work on netscape, forms that may get in the way of the menus
									 // must be put in an "<ilayer>" tag, and must be given an
									 // ID of "dm_intrusive_nnn" where nnn is your count (if you have 4 intrusive forms,
									 // the first would be 1, second 2, and so on.)
									 // ex: <ilayer id="dm_intrusive_1">[form, elements, etc..]</ilayer>
									 // This may complicate any scripts you have that interact with the form, 
									 // and it's elements under NS4.
									 //
									 // IE and Gecko will find the elements automatically.


// ********************************************
// ****   DO NOT MODIFY BELOW THIS POINT   ****
// ********************************************
// *************************
// * Script setup and defs
// *************************

// Global Variables

var dm_Tab = 0;	
var dm_Normal = 1;
var dm_Double = 2;
var dm_Image = 3;
var dm_Text = 4;
var dm_dTab = 5;
var dm_cColors = 90;
var dm_cScroller = 91;
var dm_cFont = 92;
var dm_cSizes = 93;
var dm_cArrows = 94;
var dm_Inherit = 95;
var dm_preloadList = new Array();
var dm_currentMenu = false;
var dm_showRequestId, dm_showRequestName, dm_showRequestParent;
var dm_globalHideRequests = new Array();
var dm_currentEvent = 0;
var dm_loadComplete = false;
var dm_prematureRequest = false;
var dm_activeMenus = new Array();
var dm_geckoMenuFix = new Array();
var dm_activeScroller = false;
var leftPos = 0;
var topPos = 0;
var lastElementName;
var dm_intrusiveElements = new Array();
var dm_origWidth = window.innerWidth;
var dm_origHeight = window.innerHeight;

function dm_resizeHandler(){
   if (window.innerWidth != dm_origWidth || window.innerHeight != dm_origHeight){
      window.location.reload();
   }
}

// Preload Menu "Arrows"
//var dm_menuArrow = preloadImage ("menuArrow.gif");
var dm_menuArrow = preloadImage ("pixel.gif");
//var dm_menuArrow_ro = preloadImage ("menuArrow_ro.gif");
var dm_menuArrow_ro = preloadImage ("pixel.gif");
//var dm_menuArrowLeft = preloadImage ("menuArrowLeft.gif");
var dm_menuArrowLeft= preloadImage ("pixel.gif");
//var dm_menuArrowLeft_ro = preloadImage ("menuArrowLeft_ro.gif");
var dm_menuArrowLeft_ro= preloadImage ("pixel.gif");
//var dm_menuArrowLarge = preloadImage ("menuArrowLarge.gif");
dm_menuArrowLarge= preloadImage ("pixel.gif");
//var dm_menuArrowLarge_ro = preloadImage ("menuArrowLarge_ro.gif");
var dm_menuArrowLarge_ro= preloadImage ("pixel.gif");
//var dm_menuArrowLeftLarge = preloadImage ("menuArrowLeftLarge.gif");
dm_menuArrowLeftLarge= preloadImage ("pixel.gif");
//var dm_menuArrowLeftLarge_ro = preloadImage ("menuArrowLeftLarge_ro.gif");
dm_menuArrowLeftLarge_ro= preloadImage ("pixel.gif");
//var dm_menuArrowUp = preloadImage ("menuArrowUp.gif");
var dm_menuArrowUp= preloadImage ("pixel.gif");
//var dm_menuArrowUp_ro = preloadImage ("menuArrowUp_ro.gif");
var dm_menuArrowUp_ro= preloadImage ("pixel.gif");

//var dm_menuArrowDown = preloadImage ("menuArrowDown.gif");
var dm_menuArrowDown= preloadImage ("pixel.gif");
//var dm_menuArrowDown_ro = preloadImage ("menuArrowDown_ro.gif");
var dm_menuArrowDown_ro= preloadImage ("pixel.gif");

// ************************************
// * Get Browser specific code
// ************************************

if(NS4){
		document.write('<script language="JavaScript1.2" src="'+dm_scriptPath+'menu_logic_ns4.js" type="text/javascript"><\/script>');
};
if(IE4){
		document.write('<script language="JavaScript1.2" src="'+dm_scriptPath+'menu_logic_ie4.js" type="text/javascript"><\/script>');
};
if(NS6||IE5){
		document.write('<script language="JavaScript1.2" src="'+dm_scriptPath+'menu_logic_dom.js" type="text/javascript"><\/script>');
};

// ************************************
// * Misc. functions
// ************************************

//This is only here for compatibility with datacard.com.  this function does nothing
function preloadSideMenuImages(){
	// Do nothing
};

//This is only here for compatibility with datacard.com.  this function does nothing
function writesideMenus(){
	// Do nothing
};

//This is only here for compatibility with datacard.com.  this function does nothing
function writeMenus(){
	// Do nothing
};

// Return a preloaded image	
function preloadImage(tsImageName){
	tempImage = new Image();
	tempImage.src = dm_imagePath + tsImageName;
	return tempImage;
};
// This function is used to give IE4 a "getElementById()" method.
function ie4getElementById(tsElementId){
	return eval("document.all." + tsElementId);
};

// Check if we have native javascript support for push() and pop() methods in arrays, 
// if not, make them.  This will probably be the case for IE4 and IE5.
if (!dm_globalHideRequests.push){
	Array.prototype.push = function(v){
		this[this.length] = v;
	};
};
	
if (!dm_globalHideRequests.pop){
	Array.prototype.pop = function() {
		var r = this[this.length - 1];
		this.length--;
		return r;
	};
};
	
if (!dm_globalHideRequests.shift){
	Array.prototype.shift = function() {
		var i = 0;
		var l = this.length - 1;
		if (i==l){
			var r = this[l];
			this.length--;
			return r;
		}else{
			var r = this[i];
			for (c=i; c<l; c++){
				this[c]=this[c+1];
			};
			this.length--;
			return r;
		};
	};
};

// And create some more array methods
// This one will let you pop an item out of the middle of the array
Array.prototype.extract = function(i) {
	var l = this.length - 1;
	if (i==l){
		var r = this[l];
		this.length--;
		return r;
	}else{
		var r = this[i];
		for (c=i; c<l; c++){
			this[c]=this[c+1];
		};
		this.length--;
		return r;
	};
};
	
// This one will find an item in the array, and return it's position, +1
// If nothing is found, 0 is returnd
Array.prototype.find = function(search,increment,startpos) {
	if(!increment){
		increment=1;
	};
	if(!startpos){
		startpos=0;
	};
	for (var i=startpos; i<this.length; i+=increment){
		if (this[i] == search){
			return i+1;
		};
	};
	return 0;
};

// This one will find an item in the array, and return it's position, +1
// If nothing is found, 0 is returnd
Array.prototype.findMin = function() {
	var curMin;
	var curPos = 0;
	if(this.length){
		curMin = this[0];
		curPos = 1;
		for(var i=0;i<this.length;i++){
			if(curMin<this[i]){
				curMin = this[i];
				curPos = (i+1);
			};
		};
	};
	return curPos;
};

// This one will push a "," delimited string onto an array
Array.prototype.pushList = function(v) {
	v+=','; // add a comma to the end
	var z = 0;
	var i =0;
	do	{
		i = v.indexOf(',',z);
		this.push(v.slice(z,i));
		z = i+1;
	} while (z < v.length);
};

function dm_findIntrusiveElements(){
	var tmpElem;
	var tmpTopPos, tmpLeftPos;
	if(IE5||IE4||NS6){
		for(var i=0;i<document.forms.length;i++){
			for(var i2=0;i2<document.forms[i].length;i2++){
				tmpElem = document.forms[i].elements[i2];
				if((tmpElem.type=='select-one')||(tmpElem.type=='select-multiple')){
					tmpTopPos = 0;
					tmpLeftPos = 0;
					do {
					    tmpTopPos += tmpElem.offsetTop;
					    tmpLeftPos += tmpElem.offsetLeft;
						tmpElem = tmpElem.offsetParent;
					} while (tmpElem);
					tmpElem = document.forms[i].elements[i2];
					tmpElem.dm_absoluteLeft = tmpLeftPos;
					tmpElem.dm_absoluteTop = tmpTopPos;
					tmpElem.dm_hideCue = 0;
					tmpElem.dm_inc = dm_inrecmentHideCue;
					tmpElem.dm_dec = dm_decrementHideCue;
					dm_intrusiveElements.push(tmpElem);
				};
			};
		};
	};
	if(NS4){
		var tmpElem;
		for(var i=0;i<document.layers.length;i++){
			if(document.layers[i].id.substr(0, 12) == "dm_intrusive"){
				tmpElem = document.layers[i];
				tmpElem.dm_absoluteLeft = tmpElem.pageX;
				tmpElem.dm_absoluteTop = tmpElem.pageY;
				tmpElem.offsetWidth = outerWidth;
				tmpElem.offsetHeight = outerHeight;
				tmpElem.dm_hideCue = 0;
				tmpElem.dm_inc = dm_inrecmentHideCue;
				tmpElem.dm_dec = dm_decrementHideCue;
				dm_intrusiveElements.push(tmpElem);
			};
		};
	};
};

function dm_bindIntrusiveElements(tmpObject){
	tmpObject.dm_intrusiveElements = new Array();
	var tmpElem;
	for(var i=0;i<dm_intrusiveElements.length;i++){
		tmpElem = dm_intrusiveElements[i];
		if((tmpElem.dm_absoluteLeft<(tmpObject.dm_absoluteLeft+tmpObject.dm_absoluteWidth)&&(tmpElem.dm_absoluteLeft+tmpElem.offsetWidth)>tmpObject.dm_absoluteLeft)&&(tmpElem.dm_absoluteTop<(tmpObject.dm_absoluteTop+tmpObject.dm_absoluteLength)&&(tmpElem.dm_absoluteTop+tmpElem.offsetHeight)>tmpObject.dm_absoluteTop)){
			tmpObject.dm_intrusiveElements.push(tmpElem);
		};
	};
};

function dm_inrecmentHideCue(){
	if(this.dm_hideCue==0){
		if(NS4){
			this.visibility = "hide";
		}else{
			this.style.visibility = "hidden";
		};
	};
	this.dm_hideCue++;
};

function dm_decrementHideCue(){
	if(this.dm_hideCue>0){
		this.dm_hideCue--;
		if(this.dm_hideCue==0){
			if(NS4){
				this.visibility = "show";
			}else{
				this.style.visibility = "visible";
			};
		};
	};
};

// *****************************
// * Menu generation code
// *****************************

function initMenus(){
	do{
		dm_makeMenu(dm_preloadList.shift())	;
	}while(dm_preloadList.length);
	dm_loadComplete = true;
	
	if(dm_prematureRequest){
		setTimeout('dm_showMenu("'+dm_prematureRequest+'",false)',10);
	};
	if(dm_hideIntrusiveElements){
		dm_findIntrusiveElements();
	};
	
	//Look if news is used
	if(typeof dn_firstLoop != "undefined"){
  		dn_scrollnews(0);
 	};
	
};

function dm_makeSeparator(tdm_menuWidth){
	var newRow = document.createElement("tr");
	newRow.appendChild(dm_makeSideBorder(dm_separationSize))
	var newCell = document.createElement("td");
	with (newCell){
		bgColor = dm_separationColor;
		height = dm_separationSize;
		width = "100%";
		colSpan = 2
	};
	newCell.appendChild(dm_makeImage('', dm_imagePath + 'shim.gif', tdm_menuWidth - (dm_borderSize*2), dm_separationSize));
	newRow.appendChild(newCell)
	newRow.appendChild(dm_makeSideBorder(dm_separationSize))
	//newRow.innerHTML = '<td bgcolor='+dm_borderColor+' width='+dm_borderSize+'><img src="'+dm_imagePath+'shim.gif" height='+dm_separationSize+' width='+dm_borderSize+'></td><td colspan="2" bgcolor='+dm_separationColor+' width='+(tdm_menuWidth-(dm_borderSize*2))+'><img src="'+dm_imagePath+'shim.gif" height='+dm_separationSize+' width=width='+(tdm_menuWidth-(dm_borderSize*2))+'></td><td bgcolor='+dm_borderColor+' width='+dm_borderSize+'><img src="'+dm_imagePath+'shim.gif" height='+dm_separationSize+' width='+dm_borderSize+'></td>';
	return newRow;
};

function dm_makeSeparatorTag(){
	return '<tr><td bgcolor='+dm_borderColor+'><img src="'+dm_imagePath+'shim.gif" height='+dm_separationSize+' width=1></td><td colspan="2" bgcolor='+dm_separationColor+'><img src="'+dm_imagePath+'shim.gif" height='+dm_separationSize+' width=1></td><td bgcolor='+dm_borderColor+'><img src="'+dm_imagePath+'shim.gif" height='+dm_separationSize+' width=1></td></tr>';
};

function dm_makeSideBorder(borderHieght){
var newCell = document.createElement("td");
	with (newCell){
		bgColor = dm_borderColor;
		height = borderHieght;
		width = dm_borderSize;
		style.width = dm_borderSize;
	};
	newCell.appendChild(dm_makeImage('', dm_imagePath + 'shim.gif', dm_borderSize, borderHieght));
	return newCell;
};

function dm_makeSideBorderTag(borderHieght){
	return '<td bgcolor='+dm_borderColor+'><img src="'+dm_imagePath+'shim.gif" height='+borderHieght+' width='+dm_borderSize+'></td>';
};

function dm_makeBorder(){
	var newRow = document.createElement("tr");
	
	var newCell = document.createElement("td");
	with (newCell){
		bgColor = dm_borderColor;
		height = dm_borderSize;
		colSpan = 4;
	};
	newCell.appendChild(dm_makeImage('', dm_imagePath + 'shim.gif', dm_borderSize, 1));
	newRow.appendChild(newCell);
	
	return newRow;
};

function dm_makeBorderTag(borderWidth){
	if(!borderWidth){boderWidth = 1};
	return '<td colspan=4 bgcolor='+dm_borderColor+'><img src="'+dm_imagePath+'shim.gif" height='+dm_borderSize+' width='+borderWidth+'></td>';
};

function dm_makeImage(imageId, imageSrc, width, height){
	var newImageObject = document.createElement("img");
	newImageObject.src = imageSrc;
	newImageObject.width = width;;
	newImageObject.height = height;
	if(imageId){
		newImageObject.id = imageId;
	};
	return newImageObject;
};

function dm_makeImageTag(imageId, imageSrc, width, height){
	return '<img id='+imageId+' name='+imageId+' src="'+imageSrc+'" width='+width+' height='+height+' border=0>';
};

// ********************************
// * Event handelers
// ********************************

function dm_tabOver(){
	dm_currentMenu = this.dm_masterParent;
	if(this.dm_child){
		dm_showMenu(this.dm_child, this);
	};
	if(this.dm_activeChild){
		dm_suppressHideForTree(this.dm_activeChild);
	}else{
		dm_suppressHideForTree(this.dm_masterParent);
	};
};

function dm_tabOut(){
	if(this.dm_activeChild){
		dm_requestHideForTree(this.dm_activeChild);
	}else{
		dm_requestHideForTree(this.dm_masterParent);
	};
};

function dm_normalOver(){
	dm_currentMenu = this.dm_masterParent;
	dm_effectOn(this);
	if(this.dm_masterParent.dm_parent){
		dm_effectOn(this.dm_masterParent.dm_parent);
	};
	if(this.dm_child){
		dm_requestShow(this.dm_child, this);
	};
	if(this.dm_activeChild){
		dm_suppressHideForTree(this.dm_activeChild);
	}else{
		dm_suppressHideForTree(this.dm_masterParent);
	};
};

function dm_normalOut(){
	dm_effectOff(this);
	if(this.dm_child){
		dm_showRequestId = false;
	};
	if(this.dm_activeChild){
		dm_requestHideForTree(this.dm_activeChild);
	}else{
		dm_requestHideForTree(this.dm_masterParent);
	};
};

function dm_blockOver(){
	dm_currentMenu = this.dm_masterParent;
	if(this.dm_masterParent.dm_parent){
		dm_effectOn(this.dm_masterParent.dm_parent);
	};
	dm_suppressHideForTree(this.dm_masterParent);
};

function dm_blockOut(){
	dm_requestHideForTree(this.dm_masterParent);
	this.dm_hadMouseOn = false;
};

// ********************************
// * Handler tools
// ********************************

// Another bit of bug-fix for the Gecko core.
// Since Gecko will allow an "onmouseon" on an element, but then miss the "onmouseoff", we need
// to check to see if any items were left "on" by mistake.
// This only seems to happen the first time an element gets an event.
// It also only seems to happen on elements nested in absolutely positioned DIVs.
function dm_checkEffect(menuName){
	var menuObject = document.getElementById(menuName);
	if(menuObject.dm_effectOn&&(menuObject.dm_masterParent.dm_lastEffect!=menuObject)){
		dm_effectOff(menuObject);
		if(menuObject.dm_child){
			if(dm_showRequestId==menuObject.dm_child){
				dm_showRequestId = false;
			};
		};
		if(menuObject.dm_activeChild){
			dm_requestHide(menuObject.dm_activeChild);
		};
	};
};

function dm_effectOn(menuObject){
	if(NS6){
		if(menuObject.dm_masterParent.dm_lastEffect){
			if(menuObject.dm_masterParent.dm_lastEffect.dm_effectOn&&(menuObject.dm_masterParent.dm_lastEffect!=menuObject)){
				setTimeout('dm_checkEffect("'+menuObject.dm_masterParent.dm_lastEffect.id+'")', 100);
			};
		};
		menuObject.dm_masterParent.dm_lastEffect = menuObject;
	};
	if((!menuObject.dm_effectOn)&&menuObject.dm_allowEffect){
		if(menuObject.dm_image){
			if(menuObject.dm_onImage){
				menuObject.dm_image.src=menuObject.dm_onImage;
			};
		};
		if(menuObject.dm_itemType==dm_Normal||menuObject.dm_itemType==dm_Double){
			if(IE4 || IE5 || NS6){
				if(menuObject.dm_masterParent.dm_properties.dm_backgroundImage){
					menuObject.style.backgroundImage = menuObject.dm_masterParent.dm_properties.dm_backgroundImage;
				}
				menuObject.style.backgroundColor = menuObject.dm_masterParent.dm_properties.dm_backgroundEffectColor;
				menuObject.dm_textSpan.style.color = menuObject.dm_textSpan.dm_textEffectColor;
			};
			if(NS4){
				menuObject.bgColor = menuObject.dm_masterParent.dm_properties.dm_backgroundEffectColor;
			};
		};
		menuObject.dm_effectOn=true;
	};
};

function dm_effectOff(menuObject){
	if(menuObject.dm_effectOn){
		if(menuObject.dm_image){
			if(menuObject.dm_offImage){
				menuObject.dm_image.src=menuObject.dm_offImage;
			};
		};
		if(menuObject.dm_itemType==dm_Normal||menuObject.dm_itemType==dm_Double){
			if(IE4 || IE5 || NS6){
				menuObject.style.backgroundImage = 'none'
				menuObject.style.backgroundColor = menuObject.dm_bgColor;
				menuObject.dm_textSpan.style.color = menuObject.dm_textSpan.dm_textColor;
			};
			if(NS4){
				menuObject.bgColor = menuObject.dm_bgColor;
			};
		};
		menuObject.dm_effectOn=false;
	};
};

function dm_suppressHideForTree(menuObject){
	if(menuObject.dm_hideRequests.length){
		if(NS6){
			var i = menuObject.dm_hideRequests.findMin();
			if(i){
				menuObject.dm_hideRequests.extract(i-1);	
			};
		}else{
			menuObject.dm_hideRequests.shift();
		};
	};
	if(menuObject.dm_master){
		dm_suppressHideForTree(menuObject.dm_master);
	};
};

function dm_forceHideForTree(menuObject){
	menuObject.dm_hideThisObject();
	if(menuObject.dm_master){
		dm_forceHideForTree(menuObject.dm_master);
	};
};

function dm_requestHideForTree(menuObject){
	dm_requestHide(menuObject);
	if(menuObject.dm_master){
		dm_requestHideForTree(menuObject.dm_master);
	};
};

function dm_gotoHref(){
	if(NS4){
		setTimeout('window.location.href = "'+this.dm_href+'";', 10);
	}else{
		window.location.href = this.dm_href;
	};
};

// ********************************
// * Show / Hide menus
// ********************************

function dm_getEventId(){
	dm_currentEvent++;
	return dm_currentEvent;
};


function dm_requestHideById(menuName){
	if(dm_loadComplete){
		if(NS4){
			var menuObject = document.layers[menuName];
		}else{
			var menuObject = document.getElementById(menuName);
		};
		dm_requestHide(menuObject);
	};
};

function dm_requestHide(menuObject){
	if(menuObject.dm_isActive){
		var requestEventId = dm_getEventId();
		if(!NS6){
			dm_globalHideRequests.push(menuObject);
		};
		menuObject.dm_hideRequests.push(requestEventId);
		if(NS6){
			setTimeout('dm_processHideRequest('+requestEventId+', "'+menuObject.id+'")', dm_menuTimer);
		}else{
			setTimeout('dm_processHideRequest('+requestEventId+')', dm_menuTimer);
		};
	};
};

function dm_requestShow(menuName, menuParent){
	var requestEventId = dm_getEventId();
	dm_showRequestId = requestEventId;
	dm_showRequestName = menuName;
	dm_showRequestParent = menuParent;
	setTimeout('dm_processShowRequest('+requestEventId+')', dm_menuTimer);
};

function dm_processShowRequest(requestEventId){
	if(requestEventId==dm_showRequestId){
		dm_showMenu(dm_showRequestName, dm_showRequestParent);
	};
};

function dm_processHideRequest(requestEventId, menuName){
	if(NS6){
		var menuObject = document.getElementById(menuName);
		if(menuObject.dm_hideRequests.length){
			i = menuObject.dm_hideRequests.find(requestEventId);
			if(i){
				menuObject.dm_hideRequests.extract(i-1);
				menuObject.dm_hideThisObject();
			};
		};
	}else{
		var menuObject = dm_globalHideRequests.shift()	;
		if(menuObject.dm_hideRequests[0]==requestEventId){
			menuObject.dm_hideRequests.shift();
			menuObject.dm_hideThisObject();
		};
	};
};

function dm_hideThisObject(){
	if(!this.dm_neverHide){
		if(IE4 || IE5){
			this.style.visibility = 'hidden';
		};
		if(NS6){
			this.style.left = -500;
			if(this.dm_lastEffect){
				dm_effectOff(this.dm_lastEffect);
				if(this.dm_lastEffect.dm_child){
					if(dm_showRequestId==this.dm_lastEffect.dm_child){
						dm_showRequestId = false;
					};
				};
			};
		};
		if(NS4){
			this.visibility = 'hide';
		};
	}
	if(this.dm_activeChild){
		this.dm_activeChild.dm_hideThisObject();
		this.dm_activeChild = false;
	};
	if(this.dm_parent){
		this.dm_parent.dm_activeChild = false;
		dm_effectOff(this.dm_parent);
	};
	var i=dm_activeMenus.find(this);
	if(i){dm_activeMenus.extract(i-1)};
	this.dm_hideRequests.length = 0;
	this.dm_isActive = false;
	if(this.dm_isVisible){
		for(var i=0;i<this.dm_intrusiveElements.length;i++){
			this.dm_intrusiveElements[i].dm_dec();
		};
	};
	this.dm_isVisible = false;
};

function dm_showMenu(menuName, menuParent){
	if(dm_loadComplete){
		if(NS4){
			var menuObject = document.layers[menuName];
		}else{
			var menuObject = document.getElementById(menuName);
		};
		if (menuObject){
			menuObject.dm_showThisObject(menuParent);
		}else{
			dm_makeMenu(menuName, true, menuParent);
			if(NS4){
				var menuObject = document.layers[menuName];
			}else{
				var menuObject = document.getElementById(menuName);
			};
			if(NS6){
				dm_fixGeckoMenuSnap(menuObject);
			};
			menuObject.dm_showThisObject(menuParent);
		};
	}else{
		dm_prematureRequest = menuName;
	};
};

function dm_showThisObject(menuParent){
	if(!this.dm_isVisible){
		for(var i=0;i<this.dm_intrusiveElements.length;i++){
			this.dm_intrusiveElements[i].dm_inc();
		};
		if(this.dm_setMenuPosition){
			this.dm_setMenuPosition()
		}
	};
	this.dm_isVisible = true;
	if(NS4){
		this.visibility = 'show';
	};
	if(IE4 || IE5){
		this.style.visibility = 'visible';
	};
	if(NS6){
		this.style.left = this.dm_left;
	};
	if(menuParent){
		menuParent.dm_activeChild = this;
		menuParent.dm_masterParent.dm_activeChild = this;
	}else{
		if(dm_activeMenus.length){
			do{
				dm_activeMenus.pop().dm_hideThisObject();
			}while(dm_activeMenus.length);
		};
		if(this.dm_defaultTab){
			if(this.dm_defaultTab.dm_child){
				dm_showMenu(this.dm_defaultTab.dm_child, this.dm_defaultTab);
				dm_currentMenu = this;
			};
		};
	};
	this.dm_isActive = true;
	dm_activeMenus.push(this);
};

// ********************************
// * Find Anchor/Element position
// ********************************
	
// Get the top position of an element
function getAnchorTop(elemName){
	if (NS4) {
		// If using NN4, we can just return the position of the tag.
		// But we'll also dig for the element, incase it's nested in a layer
		if (document.images[elemName]){
			return document.images[elemName].y;
		}else{
			return getNestedAnchorTop(elemName, document);
		};
	};
	if (IE4 || IE5 || NS6){
		// with IE5+, and NN5/6 we need to dig for the position
		// Adding the position of each element as we dig 
		var elem = document.getElementById(elemName);
		var position = 0;
		do {
		    position += elem.offsetTop;
			elem = elem.offsetParent;
		} while (elem);
		return position;
	};
};

// We will only look 1 layer deep (shouldn't be hard to mod if we need more).
function getNestedAnchorTop(elemName, rootDocument){
	var tmpY = 0;
	var tmpY2 = 0;
	for(i=0; i < rootDocument.layers.length; i++){
		if (rootDocument.layers[i].document.images[elemName]){
			tmpY = rootDocument.layers[i].document.images[elemName].y;
			tmpY += rootDocument.layers[i].pageY;
		};
	};
	return tmpY;
};


// Get the position of an element
function getAnchorPos(elemName){
	lastElementName = elemName;
	topPos = 0;
	leftPos = 0;
	if (NS4) {
		if (document.images[elemName]){
			topPos = document.images[elemName].y;
			leftPos = document.images[elemName].x;
		}else{
			getNestedAnchorPos(elemName, document);
		};
	};
	if (IE4 || IE5 || NS6){
		var elem = document.getElementById(elemName);
		do {
		    topPos += elem.offsetTop;
		    leftPos += elem.offsetLeft;
			elem = elem.offsetParent;
		} while (elem);
	};
};

// We will only look 1 layer deep (shouldn't be hard to mod if we need more).
function getNestedAnchorPos(elemName, rootDocument){
	var tmpY = 0;
	var tmpX = 0;
	var posArray;
	for(i=0; i < rootDocument.layers.length; i++){
		if (rootDocument.layers[i].document.images[elemName]){
			tmpY = rootDocument.layers[i].document.images[elemName].y;
			tmpY += rootDocument.layers[i].pageY;
			tmpX = rootDocument.layers[i].document.images[elemName].x;
			tmpX += rootDocument.layers[i].pageX;
		};
	};
	topPos = tmpY;
	leftPos = tmpX;
};

// EOF