// ***********************************************************************************************
// Dropdown Menu Routines
//
// Author: Paul Stevens
// Created: Nov 2001
//
// Jan 2003 / Major Update May 2003
// Copyright (c) 2001 Netpaver
//
// ***********************************************************************************************

window.onerror = null;
window.defaultStatus = '';

var isNS4 = (document.layers ? true : false);
var isNS6 = (!document.all && document.getElementById) ? true : false;
var isIE4 = (document.all && !document.getElementById) ? true : false;
var isIE5plus = (document.all && document.getElementById) ? true : false;
var isIE = (isIE4 || isIE5plus);
var isNS = (isNS4 || isNS6);
var IEVersion = 0;
var isIE6 = false;
if (isIE) {
    var pos = navigator.appVersion.indexOf("MSIE ");
    if (pos != -1) {
        IEVersion = parseFloat(navigator.appVersion.substring(pos + 5));
    }
}
var useIframe = (IEVersion >= 5.5);

function DropDownMenu(name, variableName, x, y, height, leftSubmenuMargin) {
	this.initialised = false;
	this.name = name;
	this.variableName = variableName;
	this.x = x;
	this.y = y;
	this.height = height;
	this.itemCounter = 0;
	this.items = new Array();
	this.parent = null;
	this.openItem = null;
	this.indexCounter = 0;
	this.index = new Array();
	this.depth = 1;
	this.maxDepth = 1;
	this.moreImageWidth = 10;
    this.leftSubmenuMargin = leftSubmenuMargin;
    this.textColor = "#FFFFFF";
	if (!window.menus) {
		window.menus = new Array();
	}
	window.menus[window.menus.length] = this;
}

function DDMenuHeader(name, href, text, headerWidth, width, normalColor, highlightColor, moreImageName, altText, horizontalAlignment) {
    var item = new DDMenuItem(name, href, text, width, normalColor, highlightColor, moreImageName, altText);
    item.headerWidth = headerWidth;
    item.hAlignment = horizontalAlignment;
    return item;
}

function DDMenuItem(name, href, text, width, normalColor, highlightColor, moreImageName, altText) {
	this.name = name;
	this.href = href;
	this.text = text;
	this.headerWidth = width;
	this.width = width;
    this.normalColor = normalColor;
    this.highlightColor = highlightColor;
	this.moreImageName = moreImageName;
	this.altText = altText;
	this.status = 'closed';
	if (moreImageName != null) {
		this.moreImage = new Image();
		this.moreImage.src = moreImageName;
	}
	this.itemCounter = 0;
	this.items = new Array();
	this.menu = null;
	this.parent = null;
	this.level = 0;
	this.itemNo = 0;
	this.indexNo = 0;
	this.depth = 0;
}

DropDownMenu.prototype.add = function(menuItem) {
	menuItem.itemNo = this.items.length;
	this.items[menuItem.itemNo] = menuItem;

	menuItem.menu = this;
	menuItem.parent = this;
	menuItem.indexNo = this.index.length;
	this.index[menuItem.indexNo] = menuItem;
}

DDMenuItem.prototype.add = function(menuItem) {
	menuItem.itemNo = this.items.length;

	this.items[menuItem.itemNo] = menuItem;
	menuItem.menu = this.menu;
	menuItem.parent = this;
	menuItem.level = this.level + 1;
	menuItem.indexNo = this.menu.index.length;
	this.menu.index[menuItem.indexNo] = menuItem;
}

// initializes the menus once the page and all images are loaded
DropDownMenu.prototype.initialise = function() {
    // alert("DropDownMenu.initialise");
	if (parseInt(navigator.appVersion) > 3) {
		this.menuLayer = findLayer(window.document, this.name + 'Menu');

		initialiseMenuLayers(this, this.x, this.y);

		var offset = 0;
		for (var i=0; i < this.items.length; i++) {
			var item = this.items[i];
            item.unhighlight();

			if (item.items.length > 0) {
				item.menuLayer = findLayer(window.document, item.name + 'Menu');
                item.menuLayerDocument = useIframe ? window.frames[item.name + 'Menu'].document : window.document;
                if (useIframe) {
                    item.buildMenuLayers(item.menuLayerDocument);
                    item.menuLayer.style.width = item.width;
                    item.menuLayer.style.height = item.items.length * item.menu.height + 2;
                }
				if (document.layers) {
					initialiseMenuLayers(item, this.x + offset, this.y + this.menuLayer.clip.height);
				}
				else {
					initialiseMenuLayers(item, this.x + offset, this.y + this.menuLayer.offsetHeight);
				}
			}

			offset += item.headerWidth;

			item.initialise();
		}
		this.initialised = true;
	}
}

DDMenuItem.prototype.initialise = function() {
	for (var i=0; i < this.items.length; i++) {
		var item = this.items[i];
        item.unhighlight();
		if (item.items.length > 0) {
			item.menuLayer = findLayer(window.document, item.name + 'Menu');
            item.menuLayerDocument = useIframe ? window.frames[item.name + 'Menu'].document : window.document;
            if (useIframe) {
                item.buildMenuLayers(item.menuLayerDocument);
                item.menuLayer.style.width = item.width;
                item.menuLayer.style.height = item.items.length * item.menu.height + 2;
            }
            // alert(item.name + ": height: " + item.menuLayer.offsetHeight + " width: " + item.menuLayer.offsetWidth);

			var x = this.x + this.width;
			var y = this.y + i * this.menu.height;

			initialiseMenuLayers(item, x, y);
			if (isNS4) {
				item.menuLayer.menuItem = item;
			}
			item.initialise();
		}
	}
}

function initialiseMenuLayers(menu, x, y) {
    // alert("X: " + x + " Y: " + y);
	menu.x = x;
	menu.y = y;

	if (!menu.menuLayer) {
		return;
	}
	if (isNS4) {
		menu.height = menu.menuLayer.clip.height;
		menu.menuLayer.left = x;
		menu.menuLayer.top = y;
	}
	else {
		menu.height = menu.menuLayer.offsetHeight;

        if (isIE4) {
    		menu.menuLayer.style.pixelLeft = x;
    		menu.menuLayer.style.pixelTop = y;
        }
        else {
    		menu.menuLayer.style.left = x;
    		menu.menuLayer.style.top = y;
        }
	}
}

DropDownMenu.prototype.mouseOut = function(indexNo) {
	var	event = window.event;
	var item = this.openItem;

	if (!item) {
		return;
	}

    var eventX, eventY;
    eventX = event.clientX;
    eventY = event.clientY;

    if (isIE) {
        eventX += getPageScrollX();
        eventY += getPageScrollY();
    }

    // alert("Event x: " + event.clientX + " y: " + event.clientY + " Adjusted Event x: " + eventX + " y: " + eventY + " Menu x: " + this.x + " y: " + this.y + " Item x: " + item.x + " y: " + item.y);

	if (!item.inBounds(eventX, eventY)) {
	    if (item.parent != null) {
    	    while (item.parent != null && item.level > 0) {
    	        if (item.parent.inBounds(eventX - 2, eventY)) {
    	            return;
    	        }
                item.hide();
    			this.openItem = item.parent;
    	        item = item.parent;
    	    }
    	    this.clearMenus();
    	}
    	else {
    	    this.clearMenus();
    	}
	}
}

DDMenuItem.prototype.inBounds = function(x, y) {
	var left = this.x;
	var top = this.y;
	var right = this.x + this.width - 1;
	var bottom = this.y + this.height - 1;
	if (this.parent == this.menu) {
		top = this.menu.y;
	}

    // alert("X=" + x + ", Y=" + y + ", LEFT=" + left + ", RIGHT=" + right + ", TOP=" + top + ", BOTTOM=" + bottom);

    if (isIE) {
    	if (y >= 0 && (y < top + 2 || y > bottom)) {
    		return false;
    	}
    	else if (x >= 0 && (x < left + 2 || x > right)) {
    		return false;
    	}
    }
    else if (isNS6) {
    	if (y >= 0 && (y < top + 2 || y + 2 > bottom)) {
    		return false;
    	}
    	else if (x >= 0 && (x < left + 15 || x + 10 > right)) {
    		return false;
    	}
    }
	return true;
}

DDMenuItem.prototype.hide = function() {
	if (document.layers) {
		this.menuLayer.visibility = 'hidden';
	}
	else {
		this.menuLayer.style.visibility = 'hidden';
	}
	if (this.menu.openItem == this) {
		if (this.parent != menu) {
			this.menu.openItem = this.parent;
		}
		else {
			this.menu.openItem = null;
		}
	}
}

DropDownMenu.prototype.clearMenus = function() {
	if (this.openItem) {
		var item = this.openItem;
		item.hide();
		while (item.parent != null && item.parent != this) {
			item.parent.hide();
			item = item.parent;
		}
		this.openItem = null;
	}
}

function findLayer(doc, find) {
	if (document.layers) {
		var i;
		for (i = 0; i < doc.layers.length; i++) {
			layer = doc.layers[i];
			if (layer.name == find) {
				return layer;
			}
			result = findLayer(layer.document, find);
			if (result != 'undefined') {
				return result;
			}
		}
		return 'undefined';
	}
	else {
        if (isIE4) {
    		return eval('document.all.' + find);
        }
        else {
            return doc.getElementById(find);
        }
	}
}

/*
function getObjectLeft(obj) { // IE4
	var offset = 0;
	if (navigator.platform.substr(0, 3) == 'Mac') {
		if (obj.tagName == 'IMG' || obj.tagName == 'TD' || obj.tagName == 'TABLE' || obj.tagName == 'DIV') {
			offset = obj.offsetLeft;
		}
		if (obj.parentElement) {
			offset += getObjectLeft(obj.parentElement);
		}
		return offset;
	}
	else {
		offset = obj.offsetLeft;
		if (obj.offsetParent) {
			offset += getObjectLeft(obj.offsetParent);
		}
		return offset;
	}
}

function getObjectTop(obj) { // IE4
	var offset = 0;
	if (navigator.platform.substr(0, 3) == 'Mac') {
		if (obj.tagName == 'IMG' || obj.tagName == 'TD' || obj.tagName == 'TABLE' || obj.tagName == 'DIV') {
			offset = obj.offsetTop;
		}
		if (obj.parentElement) {
			offset += getObjectTop(obj.parentElement);
		}
		return offset;
	}
	else {
		offset = obj.offsetTop;
		if (obj.offsetParent) {
			offset += getObjectTop(obj.offsetParent);
		}
		return offset;
	}
}
*/

function assert(obj) {
	if (!obj) {
		alert('Invalid Evaluation');
	}
	return obj;
}

DropDownMenu.prototype.show = function(indexNo) {
	this.clearMenus();
	var item = this.index[indexNo];
	item.show();
}

DDMenuItem.prototype.show = function() {
	// alert("SHOW");
	if (this.parent != null && this.parent != this.menu) {
		this.parent.show();
	}
	if (this.menuLayer) {
    	if (document.layers) {
    		this.menuLayer.visibility = 'visible';
    	}
    	else {
    		this.menuLayer.style.visibility = 'visible';
    	}
    	this.status = 'open';
	    this.menu.openItem = this;
	}
	// alert("Current: " + this.text);
}

DropDownMenu.prototype.highlight = function(indexNo) {
	var item = this.index[indexNo];

    if (isIE5plus || isNS6) {
        var doc = document;
        if (item.parent && item.parent.menuLayerDocument) {
            doc = item.parent.menuLayerDocument;
        }
        var row = doc.getElementById(item.name);
        if (row) {
            row.style.background = item.highlightColor;
        }
    }
	else if (isNS4) {
		if (this.openItem) {
			var hideItem = this.openItem;
			while (hideItem != this && hideItem != item && hideItem != item.parent) {
				this.openItem.hide();
				hideItem = hideItem.parent;
			}
		}
		if (item.items.length > 0) {
			item.show();
		}
	}
}

DropDownMenu.prototype.unhighlight = function(indexNo) {
	var item = this.index[indexNo];
    item.unhighlight();
}

DDMenuItem.prototype.unhighlight = function() {
    if (isIE5plus || isNS6) {
        var doc = document;
        if (this.parent && this.parent.menuLayerDocument) {
            doc = this.parent.menuLayerDocument;
        }
        var row = doc.getElementById(this.name);
        if (row) {
            row.style.background = this.normalColor;
        }
    }
}

DropDownMenu.prototype.buildMenu = function() {
    document.writeln(this.getStyleSheet());

	this.buildMenuTitles();
	for (var i = 0; i < this.items.length; i++ ) {
        if (useIframe) {
            this.items[i].buildIframe();
        }
        else {
    		this.items[i].buildMenuLayers(document);
        }
	}
	this.initialise();
}

DropDownMenu.prototype.getStyleSheet = function() {
	var NL = "\n";
	var result =
		'<STYLE TYPE="text/css">' + NL +
		'    .drop-down-menu-table {border: 1 black solid;}' + NL +
		'    .drop-down-menu {font-family: Arial, Helvetica, sans-serif; font-size: 9pt; color: ' + this.textColor + '; cursor: hand;}' + NL +
		'    div.drop-down-menu {position: absolute; visibility: hidden; z-index: 320;}' + NL +
		'    .drop-down-menu-iframe {position: absolute; visibility: hidden; z-index: 320;}' + NL +
		'    div.drop-down-menu-titles {position: absolute; z-index: 320;}' + NL +
		'    a.drop-down-menu {text-decoration: none;}' + NL +
		'    a.drop-down-menu:hover {color: ' + this.textColor + '; text-decoration: none;}' + NL +
		'</STYLE>';
    return result;
}

DropDownMenu.prototype.buildMenuTitles = function() {
	var doc = document;
	if (doc != 'undefined') {
		var output = '<DIV ID="' + this.name + 'Menu" CLASS="drop-down-menu-titles" ONMOUSEOUT="' + this.variableName + '.mouseOut();">';
		// var output = '<DIV ID="' + this.name + 'Menu" CLASS="drop-down-menu-title">';
		output += '<TABLE CELLSPACING="1" CELLPADDING="0" BORDER="0"><TR>';

		for (i = 0; i < this.items.length; i++) {
			var menu = this.items[i];
			var onClick = "";
		    var tableCellOnClick = "";
			if (!menu.href || menu.href == "") {
				onClick = ' ONCLICK="return false;"';
    		} else {
                if (menu.href.substring(0, 10).toLowerCase() == 'javascript') {
                    tableCellOnClick = ' onClick="' + menu.href + '"';
                } else {
                    tableCellOnClick = ' onClick="window.location=\'' + menu.href + '\'"';
                }
            }

            var onMouseOver = ' ONMOUSEOVER="' + this.variableName + '.highlight(' + menu.indexNo + ');' + this.variableName + '.show(' + menu.indexNo + ')"';
            var onMouseOut = ' ONMOUSEOUT="' + this.variableName + '.unhighlight(' + menu.indexNo + ')"';

            var NSOnMouseOver = (isNS) ? onMouseOver : "";
            var NSOnMouseOut = (isNS) ? onMouseOut : "";
            var IEOnMouseOver = (isIE) ? onMouseOver : "";
            var IEOnMouseOut = (isIE) ? onMouseOut : "";
            var	link = '<A HREF="' + menu.href + '"' + onClick + NSOnMouseOver + NSOnMouseOut + ' CLASS="drop-down-menu">' + menu.text + '</A>';
			output += '<TD ID="' + menu.name + '" WIDTH="' + menu.headerWidth +'" HEIGHT="' + this.height + '"' + tableCellOnClick + IEOnMouseOver + IEOnMouseOut + ' align ="' + menu.hAlignment + '" CLASS="drop-down-menu">' + link + '</TD>';
		}
		output += '</TR></TABLE>';
		output += '</DIV>';
		doc.writeln(output);
	}
}

DDMenuItem.prototype.buildIframe = function() {
	var NL = "\n";
    document.writeln('<IFRAME ID="' + this.name + 'Menu" NAME="' + this.name + 'Menu" CLASS="drop-down-menu-iframe" SCROLLING="NO" FRAMEBORDER="0" ONMOUSEOUT="' + this.menu.variableName + '.mouseOut(' + this.indexNo + ');" SRC="about:blank">' + NL + '</IFRAME>');
	for (var i = 0; i < this.items.length; i++) {
		this.items[i].buildIframe();
	}
    // this.menuLayer = document.getElementById(subMenu.name);
}

DDMenuItem.prototype.buildMenuLayers = function(doc) {
	var NL = "\n";
	var subMenu = this;
	if (this.items.length == 0) {
		return;
	}

    var variableName = this.menu.variableName;
    var windowRef = "window";
	var output;
    if (useIframe) {
        doc.open();
        output = '<HTML>' + NL + '<BODY LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0">' + NL + this.menu.getStyleSheet();
        variableName = "parent.menu";
        windowRef = "parent";
    }
    else {
        output = '<DIV ID="' + subMenu.name + 'Menu" CLASS="drop-down-menu" ONMOUSEOUT="' + variableName + '.mouseOut(' + subMenu.indexNo + ');">';
    }

	output += '<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0" CLASS="drop-down-menu-table">';
	items = subMenu.items;

	for (j = 0; j < items.length; j++) {
		var onClick = "";
		var tableCellOnClick = "";
		if (!items[j].href || items[j].href == "") {
			onClick = ' ONCLICK="return false;"';
		} else {
            if (items[j].href.substring(0, 10).toLowerCase() == 'javascript') {
                tableCellOnClick = ' onClick="' + items[j].href + '"';
            } else {
                tableCellOnClick = ' onClick="' + windowRef + '.location=\'' + items[j].href + '\'"';
            }
        }

        var onMouseOver = ' ONMOUSEOVER="' + variableName + '.highlight(' + items[j].indexNo + ');' + variableName + '.show(' + items[j].indexNo + ')"';
        var onMouseOut = ' ONMOUSEOUT="' + variableName + '.unhighlight(' + items[j].indexNo + ')"';
        if (isNS4) {
            onMouseOut = ' ONMOUSEOUT="' + variableName + '.unhighlight(' + items[j].indexNo + '); ' + variableName + '.mouseout();" ';
        }

        var NSOnMouseOver = (isNS4) ? onMouseOver : "";
        var NSOnMouseOut = (isNS4) ? onMouseOut : "";
        var IEOnMouseOver = (!isNS4) ? onMouseOver : "";
        var IEOnMouseOut = (!isNS4) ? onMouseOut : "";

		var	link = '<A HREF="' + items[j].href + '"' + onClick + NSOnMouseOver + NSOnMouseOut + ' CLASS="drop-down-menu">' + items[j].text + '</A>';

		var more = "&nbsp;";
		if (items[j].items.length > 0) {
			more = '<IMG SRC="' + items[j].moreImageName + '" BORDER="0">';
		}

		output += '<TR ID="' + items[j].name + '" WIDTH="' + subMenu.width + '" HEIGHT="' + this.menu.height + '" CLASS="drop-down-menu">';
		output += '<TD WIDTH="' + this.menu.leftSubmenuMargin + '" HEIGHT="' + this.menu.height + '"' + tableCellOnClick + IEOnMouseOver + IEOnMouseOut + ' CLASS="drop-down-menu">&nbsp;</TD>';
        output += '<TD WIDTH="' + (subMenu.width - (this.menu.leftSubmenuMargin + this.menu.moreImageWidth)) + '" HEIGHT="' + this.menu.height + '"' + tableCellOnClick + IEOnMouseOver + IEOnMouseOut + ' CLASS="drop-down-menu">' + link + '</TD>';
		output += '<TD WIDTH="' + this.menu.moreImageWidth + '"' + tableCellOnClick + IEOnMouseOver + IEOnMouseOut + ' CLASS="drop-down-menu">' + more + '</TD>';
		output += '</TR>';
	}
	output += '</TABLE>';

    if (useIframe) {
        output += "</BODY></HTML>"
    }
    else {
    	output += '</DIV>';
    }

    // alert(output);
	doc.writeln(output);

    if (useIframe) {
        doc.close();
    }
    else {
    	for (var i = 0; i < this.items.length; i++ ) {
    		this.items[i].buildMenuLayers(document);
    	}
    }
}

if (!window.event && window.captureEvents) {
	// set up event capturing for mouse events (add or subtract as desired)
	window.captureEvents(Event.MOUSEOVER|Event.MOUSEOUT|Event.CLICK|Event.DBLCLICK);
	// set window event handlers (add or subtract as desired)
	window.onmouseover = WM_getCursorHandler;
	window.onmouseout = WM_getCursorHandler;
	window.onclick = WM_getCursorHandler;
	window.ondblclick = WM_getCursorHandler;
	// create an object to store the event properties
	window.event = new Object;
	window.event.mouseout = null;
}

function WM_getCursorHandler(e) {
	// set event properties to global vars (add or subtract as desired)
	window.event.clientX = e.pageX;
	window.event.clientY = e.pageY;
	window.event.x = e.layerX;
	window.event.y = e.layerY;
	window.event.screenX = e.screenX;
	window.event.screenY = e.screenY;

	// alert(e.type);
	if (e.type == "click") {
		if (window.menus) {
			for (var i = 0; i < window.menus.length; i++) {
				window.menus[i].clearMenus();
			}
		}
	}
	// route the event back to the intended function
	return routeEvent(e);
}

function instanceOf(object, constructor) {
    while (object != null) {
        if (object == constructor.prototype)
           return true;
        object = object.__proto__;
	}
	return false;
}

// return number of pixels document may be scrolled up
function getPageScrollX() {
    if (document.layers) {
        return window.pageXOffset;
    }
    else if (isNS6) {
        return window.scrollX;
    }
    else {
        return document.body.scrollLeft;
    }
    return -1;
}

// return number of pixels document may be scrolled to right
function getPageScrollY() {
    if (document.layers) {
        return window.pageYOffset;
    }
    else if (isNS6) {
        return window.scrollY;
    }
    else {
        return document.body.scrollTop;
    }
    return -1;
}

