
function findImage(name, doc) {
	var i, img;
	for (i = 0; i < doc.images.length; i++) {
    	if (doc.images[i].name == name) {
			return doc.images[i];
		}
	}
	return null;
}

function getElementWidth(Elem) {
	return document.getElementById(Elem).offsetWidth
}

function getElementLeft(Elem) {
	var elem = document.getElementById(Elem);
	xPos = elem.offsetLeft;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
		xPos += tempEl.offsetLeft;
		tempEl = tempEl.offsetParent;
	}
	return xPos;
}


function getElementTop(Elem) {
	var elem = document.getElementById(Elem);
	yPos = elem.offsetTop;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
	return yPos;
}


function moveXY(myObject, x, y) {
	obj = document.getElementById(myObject).style;
	obj.top = y + 'px';
	obj.left = x + 'px';	
}

function changeClass(Elem, myClass) {
	document.getElementById(Elem).className = myClass;
}

function changeBGColour(myObject, colour) {
	var obj = document.getElementById(myObject);
	if (obj) { obj.style.backgroundColor = colour; }
}

function changeBGImage(myObject, imageURL) {
	var obj = document.getElementById(myObject);
	if (obj && imageURL) { obj.style.backgroundImage = "url("+imageURL+")"; }
}

function changeImage(target, source) {
	var imageObj;
	imageObj = eval('document.images.' + target);
	if (imageObj) imageObj.src = eval(source).src; 
}



function bulletPoint(offURL, onURL) {	
	if (offURL && onURL) {
		this.offImage = new Image();
		this.offImage.src = offURL;
		this.onImage = new Image();
		this.onImage.src = onURL;
		this.URL = String(offURL);
	}
}

function openMe(newin) {
        flyout=window.open(newin,"flyout","")
}

function borderCell(B) {
	return '<td width="1" bgcolor="' + B + '"><img src="" width="1" height="1" border="0"></td>';
}

function borderRow(B, C) {
	return '<tr><td height="1" colspan="' + C + '" bgcolor="' + B + '"><img src="" width="1" height="1" border="0"></td></tr>';
}

function menuOver(RandInt) {
	clearTimeout(timeOn[RandInt]);
}

function menuOut(RandInt) {
	timeOn[RandInt] = setTimeout("hideAllMenus("+RandInt+")", 500);
}


function showMenu(m_No, eventObj, RandInt) {
	hideAllMenusTier(tier[RandInt][m_No]-1, RandInt);
	var borderModSize = borderMod[RandInt][m_No];
	changeBGColour('labelCell' + RandInt + m_No, onColours[RandInt][m_No]);
	changeClass('menuLink' + RandInt + m_No, onClass[RandInt][m_No]);
	if (labelBulletName[RandInt][m_No] != null){
		changeImage('menuBullet' + RandInt + m_No, labelBulletName[RandInt][m_No] + '.onImage');
	}
	menuActive[RandInt][m_No] = true;
	if (menuType[RandInt][m_No] != 'blank') {
		labelObj = 'labelCell'+RandInt+m_No;
		x = getElementLeft(labelObj)-borderModSize;
		y = getElementTop(labelObj) + document.getElementById(labelObj).offsetHeight;

		if (   eval("menus"+RandInt+"[m_No].align == 'center'")   )  x = x + ((getElementWidth(labelObj)-getElementWidth('menu'+RandInt+m_No))/2);
		if (   eval("menus"+RandInt+"[m_No].align == 'right'")   ) x = x + ((getElementWidth(labelObj)-getElementWidth('menu'+RandInt+m_No))) + (borderModSize*2);

		moveXY('menu' + RandInt + m_No, x, y);
	
		document.getElementById('menu' + RandInt + m_No).style.visibility = 'visible';
		return true;
	}
}

function showMenuSide(m_No, eventObj, myTier, RandInt) {
	hideAllMenusTier(tier[RandInt][m_No]-1, RandInt);
	var borderModSize = borderMod[RandInt][m_No];
	changeBGColour('menuLabel' + RandInt + m_No, onColours[RandInt][m_No]);
	changeBGColour('labelCell' + RandInt + m_No, onColours[RandInt][m_No]);
	changeClass('menuLink' + RandInt + m_No, onClass[RandInt][m_No]);
	if (labelBulletName[RandInt][m_No] != null) changeImage('menuBullet' + RandInt + m_No, labelBulletName[RandInt][m_No] + '.onImage');
	menuActive[RandInt][m_No] = true;
	if (menuType[RandInt][m_No] != 'blank') {
		labelObj = 'labelCell'+RandInt+m_No;
		x = getElementLeft(labelObj);
		y = getElementTop(labelObj) - borderModSize;

		if (   eval("menus"+RandInt+"[m_No].align=='right'")   ) x = x + getElementWidth(labelObj);
		else x = x - getElementWidth('menu'+m_No);

		moveXY('menu' + m_No, x, y);
	
		document.getElementById('menu' + RandInt + m_No).style.visibility = 'visible';
		return true;
	}
}

function hideAllMenus(RandInt) {
	for (var i = 1; i < (currentMenuNo[RandInt]+1); i++) {
		if(menuActive[RandInt][i] == true) hideMenu(i, RandInt);
	}
}

function hideAllMenusTier(myTier, RandInt) {
	for (var i = 1; i < (currentMenuNo[RandInt]+1); i++) {
		if( tier[RandInt][i] > myTier && menuActive[RandInt][i] == true) hideMenu(i, RandInt);
	}
}

function hideMenu(m_No, RandInt) {
	changeBGColour('labelCell' + RandInt + m_No, offColours[RandInt][m_No]);
	changeClass('menuLink' + RandInt + m_No, offClass[RandInt][m_No]);
	if (labelBulletName[RandInt][m_No] != null){
		changeImage('menuBullet' + RandInt + m_No, labelBulletName[RandInt][m_No] + '.offImage');
	}
	menuActive[RandInt][m_No] = false;
	document.getElementById('menu' + RandInt + m_No).style.visibility = 'hidden';
	return true;
}



function menuBar(barName, barWidth, orientation, i_Bor, o_Bor, RandInt) {
	this.numLabels = 0;
	this.i_Bor = i_Bor;
	this.o_Bor = o_Bor;
	this.orientation = orientation;
	this.labelText = new Array();
	this.rowText = new Array();
	this.offClass = 'MenuLabelLink'+RandInt;
	this.onClass = 'MenuLabelLinkOn'+RandInt;
	this.bulletAlign = 'left';
	this.targetType = 'self'; // self, iframe, frame, new
	this.targetFrame = '_self'; // _self, _blank or (i)frame name

	this.addLabel = function(bullet, labelText, menuNo, offColour, onColour, labelURL, align, textureOffURL, textureOnURL, textureOffsetX, textureOffsetY, labelwidth, labelheight) {
		this.numLabels += 1;
		tier[RandInt][menuNo] = 0;
		if (this.o_Bor != null) borderMod[RandInt][menuNo] = 1;
		else borderMod[RandInt][menuNo] = 0;	
		if (menuNo != null) {
			onColours[RandInt][menuNo] = onColour;
			offColours[RandInt][menuNo] = offColour;
			onClass[RandInt][menuNo] = this.onClass;
			offClass[RandInt][menuNo] = this.offClass;
			labelBulletName[RandInt][menuNo] = bullet;
		}
		
		temp = new String('');
		this.rowText[this.numLabels] = new String('');
		if (this.orientation == 'vertical') this.rowText[this.numLabels] += '<tr id="labelRow'+ RandInt + menuNo + '">';

		temp += '<td id="labelCell' + RandInt + menuNo + '" bgcolor="' + offColour + '" valign="middle" style="width:'+labelwidth+'px; height:'+labelheight+'px; ';
		if (textureOffURL) temp += ' background-image: url('+textureOffURL+'); background-position: '+textureOffsetX+'px '+textureOffsetY+'px; ';
		temp += '"';
		
		temp += ' onmouseout="menuOut('+RandInt+'); "onmouseover="menuOver('+RandInt+'); ';
		if (this.orientation == 'vertical') temp += 'return !showMenuSide(' + menuNo + ', event, tier['+RandInt+'][' + menuNo + '], '+RandInt+');" ';
		else temp += 'return !showMenu(' + menuNo + ', event, '+RandInt+');" ';
		if (this.targetType=='self') temp += ' onclick="document.location.href=\'' + labelURL + '\';" ';
		if (this.targetType=='new') temp += ' onclick="openMe(\'' + labelURL + '\'); return false;" ';
		if (this.targetType=='frame') temp += ' onclick="parent.' + this.targetFrame + '.document.location.href=\'' + labelURL + '\';" ';
		if (this.targetType=='iframe') temp += ' onclick="' + this.targetFrame + '.location.href=\'' + labelURL + '\';" ';
		temp += '><div class="myMenuLabel' + align + RandInt +'" id="menuLabel' + RandInt + menuNo +'">';
		temp += '<a href="' + labelURL +'" target="' + this.targetFrame + '" class="' + this.offClass + '" id="menuLink' + RandInt + menuNo +'">';
		
		if (bullet) temp += '<img src="' + eval(bullet + ".URL") + '" border="0" align="' + this.bulletAlign + '" id="menuBullet' + RandInt + menuNo + '" name="menuBullet' + RandInt + menuNo + '">';
			
		temp += labelText + '</a></div></td>';		
		this.labelText[this.numLabels] = new String(temp);

	}
	
	this.writeMenuBar = function() {
		o_Bor = this.o_Bor;
		i_Bor = this.i_Bor;
		menuBarStr = new String();
		menuBarStr += '<div id="' + barName + RandInt + '"><table border="0" cellpadding="0" cellspacing="0">';
		if (this.orientation == 'vertical') {
			if (o_Bor != null) menuBarStr += borderRow(o_Bor, 3);
			for (var count = 0; count < this.numLabels; count++) {		
				menuBarStr += this.rowText[count+1];
				if (o_Bor != null) menuBarStr += borderCell(o_Bor);
				menuBarStr += this.labelText[count+1];
				if (o_Bor != null) menuBarStr += borderCell(o_Bor);				
				menuBarStr += '</tr>';
				if (i_Bor != null && count < (this.numLabels-1)) {
					if (o_Bor != null) menuBarStr += '<tr>' + borderCell(o_Bor) + borderCell(i_Bor) + borderCell(o_Bor) + '</tr>';
					else menuBarStr += borderRow(i_Bor, 1);
				}
			}
			if (o_Bor != null) menuBarStr += borderRow(o_Bor, 3);
		} else {
			if (o_Bor != null) {
				if (i_Bor != null) menuBarStr += borderRow(o_Bor, ((this.numLabels*2)+1));
				else menuBarStr += borderRow(o_Bor, (this.numLabels+2));
			}
			menuBarStr += '<tr>';
			if (o_Bor != null) menuBarStr += borderCell(o_Bor); 
			for (var count = 0; count < this.numLabels; count++) {		
				menuBarStr += this.rowText[count+1];			
				menuBarStr += this.labelText[count+1];
				if (i_Bor != null && count < (this.numLabels-1)) {
					menuBarStr += borderCell(i_Bor);
				}
			}
			if (o_Bor != null) {
				menuBarStr += borderCell(o_Bor);
				if (i_Bor != null) menuBarStr += borderRow(o_Bor, ((this.numLabels*2)+1));
				else menuBarStr += borderRow(o_Bor, (this.numLabels+2)); 
			}
			menuBarStr +=  '</tr>';
		}
		menuBarStr += '</table></div>';
		document.write(menuBarStr);
	}
}


function menu(menuWidth, orientation, i_Bor, o_Bor, RandInt) {
	currentMenuNo[RandInt] += 1;
	this.numItems = 0;
	this.i_Bor = i_Bor;	
	this.o_Bor = o_Bor;	
	this.itemText = new Array();
	this.rowText = new Array();
	this.align = 'left';
	this.offClass = 'MenuItemLink' + RandInt;
	this.onClass = 'MenuItemLinkOn' + RandInt;
	this.orientation = orientation;
	this.bulletAlign = 'left';
	this.targetType = 'self'; // self, iframe, frame, new
	this.targetFrame = '_self'; // _self, _blank or (i)frame name

	this.addItem = function(bullet, itemText, menuNo, offColour, onColour, itemURL, align, textureOffURL, textureOnURL) {
		this.numItems += 1;
		
		var tempId = currentMenuNo[RandInt] + '_' + this.numItems;
	
		if (menuNo != null) {
			tier[RandInt][menuNo] = tier[RandInt][currentMenuNo[RandInt]] + 1;
			onColours[RandInt][menuNo] = onColour;
			offColours[RandInt][menuNo] = offColour;
			onClass[RandInt][menuNo] = this.onClass;
			offClass[RandInt][menuNo] = this.offClass;
			labelBulletName[RandInt][menuNo] = bullet;
			if (this.o_Bor != null) borderMod[RandInt][menuNo] = 1;	
			else borderMod[RandInt][menuNo] = 0;	
		}
		
		temp = new String('');
		this.rowText[this.numItems] = new String('');
		
		if (menuNo != null) {
			if (this.orientation =='vertical') this.rowText[this.numItems] += '<tr id="labelRow'+ RandInt + menuNo + '">';
			temp += '<td id="labelCell' + RandInt + menuNo + '" bgcolor="' + offColour + '" valign="middle" ';
			if (textureOffURL) { temp += 'style="background-image: url('+textureOffURL+'); " '; }
		} else {
			if (this.orientation =='vertical') this.rowText[this.numItems] += '<tr>';
			temp += '<td id="itemCell' + RandInt + tempId + '" bgcolor="' + offColour + '" valign="middle" class="' + align +'Menu'+RandInt+'" ';
			if (textureOffURL) { temp += 'style="background-image: url('+textureOffURL+'); " '; }
		}		

		if (this.targetType=='self') temp += ' onclick="document.location.href=\'' + itemURL + '\';" ';
		if (this.targetType=='new') temp += ' onclick="openMe(\'' + itemURL + '\'); return false;" ';
		if (this.targetType=='frame') temp += ' onclick="parent.' + this.targetFrame + '.document.location.href=\'' + itemURL + '\';" ';
		if (this.targetType=='iframe') temp += ' onclick="' + this.targetFrame + '.location.href=\'' + itemURL + '\';" ';
		if (menuNo != null) {
			if (this.orientation =='vertical') temp += ' onmouseover="menuOver('+RandInt+'); return !showMenuSide(' + menuNo + ', event, tier['+RandInt+'][' + menuNo + '], '+RandInt+'); " onmouseout=" menuOut('+RandInt+'); "';
			else temp += ' onmouseover="menuOver('+RandInt+'); return !showMenu(' + menuNo + ', event, '+RandInt+'); " onmouseout=" menuOut('+RandInt+'); "';
		} else {
			temp += ' onmouseover="changeClass(\'menuLink'+RandInt + tempId + '\',\'' + this.onClass + '\'); hideAllMenusTier(tier['+RandInt+'][' + currentMenuNo[RandInt] + ', '+RandInt+'], '+RandInt+'); menuOver('+RandInt+');  changeBGColour(\'itemCell' + RandInt + tempId + '\', \'' + onColour + '\'); changeImage(\'menuItemBullet' + RandInt + tempId + '\', \'' + bullet + '.onImage\'); changeBGImage(\'itemCell\' + \''+RandInt+'\' + \''+tempId+'\',  \''+textureOnURL+'\'); " onmouseout="menuOut('+RandInt+'); changeClass(\'menuLink' + RandInt + tempId + '\',\'' + this.offClass + '\'); changeBGColour(\'itemCell' + RandInt + tempId + '\', \'' + offColour + '\'); changeImage(\'menuItemBullet' + RandInt + tempId + '\', \'' + bullet + '.offImage\'); changeBGImage(\'itemCell\' + \''+RandInt+'\' + \''+tempId+'\',  \''+textureOffURL+'\'); "';
		}
		temp += '><div class="myMenuItem' + RandInt + align + '" ';

		if (menuNo != null) temp += ' id="menuLabel' + RandInt + menuNo +'"';
		else temp += ' id="menuItem' + RandInt + tempId +'"';
		
		temp += '><a href="' + itemURL +'" target="' + this.targetFrame + '" class="' + this.offClass + '"';
		if (menuNo != null) temp += 'id="menuLink' + RandInt + menuNo +'"';
		else temp += 'id="menuLink' + RandInt + tempId +'"';
		temp +='>';
		
		if (bullet) {
			if (menuNo != null) temp += '<img src="' + eval(bullet + ".URL") + '" border="0" align="' + this.bulletAlign + '" id="menuBullet' + RandInt + menuNo + '" name="menuBullet' + menuNo + '">';
			else temp += '<img src="' + eval(bullet + ".URL") + '" border="0" align="' + this.bulletAlign + '" id="menuItemBullet' + RandInt + tempId + '" name="menuItemBullet' + tempId + '">';
		}
			
		temp += itemText + '</a></div></td>';	
		this.itemText[this.numItems] = new String(temp);
	}
	
	this.writeMenu = function() {
		var menuStr = new String();
		o_Bor = this.o_Bor;
		i_Bor = this.i_Bor;
		if (this.numItems == 0) menuType[RandInt][currentMenuNo[RandInt]] = 'blank';
		else menuType[RandInt][currentMenuNo[RandInt]] = 'default';
		menuStr += '<div id="menu' + RandInt + currentMenuNo[RandInt] + '" name="menu' + currentMenuNo[RandInt] + '" class="myMenu' + RandInt + '" width="' + menuWidth + '"';
		menuStr += ' style="width:' + menuWidth + ';"><table border="0" cellpadding="0" cellspacing="0" width="' + menuWidth + '">';
		if (this.orientation == 'vertical') {
			if (o_Bor != null) menuStr += borderRow(this.o_Bor, 3);
			for (var count = 0; count < this.numItems; count++) {
				menuStr += this.rowText[count+1];
				if (o_Bor != null) menuStr += borderCell(o_Bor);
				menuStr += this.itemText[count+1];
				if (o_Bor != null) menuStr += borderCell(o_Bor);
				menuStr += '</tr>';
				if (i_Bor != null && count < (this.numItems-1)) {
					if (o_Bor != null) menuStr += '<tr>' + borderCell(o_Bor) + borderCell(i_Bor) + borderCell(o_Bor) + '</tr>';
					else menuStr += borderRow(i_Bor, 1);
				}
			}
			if (o_Bor != null) menuStr += borderRow(o_Bor, 3);
		} else {
			if (o_Bor != null) {
				if (i_Bor != null) menuStr += borderRow(o_Bor, ((this.numItems*2)+1));
				else menuStr += borderRow(o_Bor, (this.numItems+2));
			}
			menuStr += '<tr>';
			if (o_Bor != null) menuStr += borderCell(o_Bor); 
			for (var count = 0; count < this.numItems; count++) {		
				menuStr += this.rowText[count+1];			
				menuStr += this.itemText[count+1];
				if (i_Bor != null && count < (this.numItems-1)) {
					menuStr += borderCell(i_Bor);
				}
			}
			if (o_Bor != null) {
				menuStr += borderCell(o_Bor);
				if (i_Bor != null) menuStr += borderRow(o_Bor, ((this.numItems*2)+1));
				else menuStr += borderRow(o_Bor, (this.numItems+2)); 
			}
			menuStr +=  '</tr>';
		}
		menuStr += '</table></div>';
		document.write(menuStr);
	}
}
