/**
 * Class for menu items and hotspots.
 * Wraps AbstractLayer. 
 */

	/**
	 * Constructor.
	 *
	 * @param aMenuNo: menu the item is in (int)
	 * @param aLayer: an AbstractLayer obj for the item (obj)
	 * @param aLinkHref: an href for the layer to click through to (string)
	 */
	 
	function MenuLayer(aMenuNo, aLayer, aLinkHref) {
	 	this.iParentMenu = aMenuNo;
		this.iLayer = aLayer; 
		this.iLinkHref = aLinkHref;
	}
	
	/**
	 * Get the AbstractLayer that displays the item HTML.
	 * returns: a reference to the layer
	 */
	MenuLayer.prototype.getLayer = function() {
		return this.iLayer;
	} 

	/**
	 * Get the menu the item is in.
	 * returns: the menu number
	 */
	MenuLayer.prototype.getMenu = function() {
		return this.iParentMenu;
	} 
	
	/**
	 * Get the href for the menu item to click through to.
	 * returns: the href
	 */
	MenuLayer.prototype.getLinkHref = function() {
		return this.iLinkHref;
	} 
		
	


// Browser detect globals
var gIe4 = document.all;
var gNs4 = document.layers;
var gNs6 = document.getElementById && !gIe4;

// Arrays of our menu items and hotspots
var gMenuLayers = new Array();
var gHotspots = new Array();


/**
 * Loads all menu item divs into gMenuLayers as MenuLayer objs.
 * @param aMenuNo: the menu to get divs for.
 */
function createMenu(aMenuNo) {
	if(gNs4) {
		// loop through the layers array
		var lLayers = document.layers;
		for (var i = 0; i < lLayers.length; i++) {
			// loop through the links array
			for (var j = 0; j < lLayers[i].document.links.length; j++) {	
				// find links in the current menu								
				var lMenu = lLayers[i].document.links[j].target;
				// add the links' parent divs to our new array 
				if(lMenu == "menu" + aMenuNo) {		
					gMenuLayers[gMenuLayers.length] = new MenuLayer(aMenuNo, new LinklistAbstractLayer(i), lLayers[i].document.links[j].href);		
				}
			}
		}			
	} else {
		// loop through the links array
		for (var j = 0; j < document.links.length; j++) {		
			if (document.links[j].target == ("menu" + aMenuNo)) {				
				// find divs that contains links in the current menu
				var lTag = document.links[j];
				// add the links' parent divs to our new array 				
				while(lTag.tagName != "DIV" || lTag.className != "websiteMAXMenu") {
					lTag = lTag.parentElement;
				}	
				// loop through divs array to match the 
				var lDivs = document.all.tags('DIV');
				for(var k = 0; k < lDivs.length; k++) {
					if(lDivs[k] == lTag) {
						gMenuLayers[gMenuLayers.length] = new MenuLayer(aMenuNo, new LinklistAbstractLayer(k), document.links[j].href);			
					}
				}
			}					
		}
	}
}

/**
 * Sets menu item styles and creates matching hotspot.
 * @param aMenuLayer: a MenuLayer obj
 * @param aLeftPos: postion of the layer from the left of the screen 
 * @param aTopPos: postion of the layer from the top of the screen
 */
function formatMenuLayer(aMenuLayer, aLeftPos, aTopPos) {
	var lMenu = aMenuLayer.getMenu();
	var lLayer = aMenuLayer.getLayer();
	lLayer.setBGColor(sMENU_DEFAULT_COLOUR);
	lLayer.clip(0, 0, sMENU_WIDTH, sMENU_HEIGHT);
	lLayer.moveTo(aLeftPos, aTopPos);
	// create a matching hotspot
	var lHotspot = new MenuLayer(lMenu, new LinklistAbstractLayer("menu" + lMenu + "itemAt" + aTopPos, sMENU_WIDTH));
	gHotspots[gHotspots.length] = lHotspot;
	var lHotspotLayer = lHotspot.getLayer();
	lHotspotLayer.hide();
	var lHTML = "<a style=\"background-color: transparent\" href=\"" + aMenuLayer.getLinkHref() + "\" ";
	lHTML += "onmouseover=\"gMenuLayers[" + (gHotspots.length - 1) + "].getLayer().setBGColor('" + sMENU_HIGHLIGHT_COLOUR + "')\" ";
	lHTML += "onmouseout=\"gMenuLayers[" + (gHotspots.length - 1) + "].getLayer().setBGColor('" + sMENU_DEFAULT_COLOUR + "'); startCloseTimer(" + lMenu + ")\">";	
	lHTML += "<img border=\"0\" width=\"" + sMENU_WIDTH + "\" height=\"" + sMENU_HEIGHT + "\" src=\"" + sSPACER_PATH + "\">";
	lHTML += "</a>";
	lHotspotLayer.setInnerHTML(lHTML);
	lHotspotLayer.setBGColor("transparent");
	lHotspotLayer.clip(0, 0, 800, 100);
	lHotspotLayer.clip(0, 0, sMENU_WIDTH, sMENU_HEIGHT);
	lHotspotLayer.moveTo(aLeftPos, aTopPos);
}

function openMenu(aMenuNo) {
	// close any open menus
	closeAllMenus();	
	
	// display all layers in that menu
	for(var i = 0; i < gMenuLayers.length; i++) {
		if(gMenuLayers[i].getMenu() == aMenuNo) {
			gMenuLayers[i].getLayer().show();
			gHotspots[i].getLayer().show();
		}
	}	
}

/**
 * Closes all menus.
 */
function closeAllMenus() {
	for(var i = 0; i < gMenuLayers.length; i++) {
		gMenuLayers[i].getLayer().hide();
		gHotspots[i].getLayer().hide();			
	}	
} 

/**
 * Closes a menu if it does not have a lit menu item.
 * @param aMenuNo: the menu to close
 */
function closeMenu(aMenuNo) {
	var lLit = false;
	for(var i = 0; i < gMenuLayers.length; i++) {
		if(gMenuLayers[i].getMenu() == aMenuNo && gMenuLayers[i].getLayer().getBGColor() == sMENU_HIGHLIGHT_COLOUR) {
			lLit = true;
		}
	}
	if(!lLit) {
		for(var i = 0; i < gMenuLayers.length; i++) {
			if(gMenuLayers[i].getMenu() == aMenuNo) {
				gMenuLayers[i].getLayer().hide();
				gHotspots[i].getLayer().hide();			
			}		
		}	
	}
} 

/**
 * Timer to close menus.
 * @param aMenuNo: the menu to close
 */
function startCloseTimer(aMenuNo) {
	setTimeout("closeMenu(" + aMenuNo + ")", sMENU_TIMEOUT);
}

/**
 * Converts all linklist divs into menu items.
 */
function initDHTMLLinklists() {	
	var lMenuNo = 0;
	if(gNs4) {	
		var lLayers = document.layers;
		// loop through every link in the page
		for(var i = 0; i < lLayers.length; i++) {	
			for(var j = 0; j < lLayers[i].document.links.length; j++) {	
				// if the link is inside a linklist					
				if (lLayers[i].document.links[j].target == "menu" + lMenuNo) {
					// make the linklist into a menu				
					createMenu(lMenuNo++);	
				}
			}	
		}		
	} else {
		// loop through every link in the page	
		for(var j = 0; j < document.links.length; j++) {
			// if the link is inside a linklist		
			if(document.links[j].target == "menu" + lMenuNo) {
				// make the linklist into a menu
				createMenu(lMenuNo++);
			}			
		}	
		// now set all the menu item layers to the correct height and width
		// (so the bgcolor extends beyond the content)
		for(var m = 0; m < document.images.length; m++) {
			if(document.images[m].name == "linklist_menu_height_spacer") {
				document.images[m].height = sMENU_HEIGHT;
			}
			if(document.images[m].name == "linklist_menu_width_spacer") {
				document.images[m].width = sMENU_WIDTH;
			}			
		}	
	}	
	// now position them all and make hotspots!
	var lLeft = new String();	
	var lTop = new String();
	var lPos = new String();
	for(var k = 0; k < lMenuNo; k++) {
		// extract menu positions from user array
		lPos = sMENU_POSITIONS[k];
		lLeft = lPos.substring(0, lPos.indexOf(","));
		lTop = lPos.substring(lPos.indexOf(",") + 1, lPos.length);		
		for(var l = 0; l < gMenuLayers.length; l++) {
			if(gMenuLayers[l].getMenu() == k) {
				// position layers
				formatMenuLayer(gMenuLayers[l], lLeft, lTop);
				lTop = parseInt(lTop) + sMENU_HEIGHT;
			}
		}
	}	
}
