// Madgex Limited
// Copyright (c) 2004 Madgex Limited. All Rights Reserved.
// Drop Menu - Core Components Project
// 12 Aug 2004 (23 Nov 2004)
// Version 1.6.3

// This code has been tested on IE 4-6 win, NS 6+ win, Firefox 1 win, and Mozilla 1+ win
// It does not conform to W3C DOM2 standard as it supports IE4. Do not use
// with XHTML strict doctype.

var is = new Is();

function Is () {
	var agt=navigator.userAgent.toLowerCase();
	this.major = parseInt(navigator.appVersion);
	this.minor = parseFloat(navigator.appVersion);
	this.gecko = (agt.indexOf('gecko') != -1);
	this.safari = (agt.indexOf('safari') != -1);
	this.camino = (agt.indexOf('camino') != -1);
    this.ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    this.ie4    = (this.ie && (this.major == 4) && (agt.indexOf("msie 4")!=-1) );
    this.ie4up  = (this.ie  && (this.major >= 4));
    this.ie5    = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.0")!=-1) );
    this.ie5_5  = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.5") !=-1));
    this.ie5up  = (this.ie  && !this.ie3 && !this.ie4);
    this.ie5_5up =(this.ie && !this.ie3 && !this.ie4 && !this.ie5);
    this.ie6    = (this.ie && (this.major == 4) && (agt.indexOf("msie 6.")!=-1) );
    this.ie6up  = (this.ie  && !this.ie3 && !this.ie4 && !this.ie5 && !this.ie5_5);
   	this.win = ((agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1));
    this.mac = (agt.indexOf("mac")!=-1);
}

var EventArray = new Array();
function MenuSystem(id){
	this.id = id;
	this.objectType = 'MenuSystem';
	this.childItems = new Array();
	AddEventArray(this);
	this.timeOut = 1000;
	this.menuPoped = false;
	this.menuTimer = '';
	this.timerState = false;
}

MenuSystem.prototype.AddChild = function(oRef){
	this.childItems[this.childItems.length] = oRef
}

MenuSystem.prototype.Build = function(){
	if((is.ie4up)||(is.gecko)){
		if((is.mac==false) || (is.ie==false)){
			MMGetElementById("StaticMenu").style.display='none';
			// Bug fix for NS 6.0 6.01
			if(is.gecko){
				MMGetElementById("StaticMenu").innerHTML = "";
			}
			var i;
			for (i = 0; i < this.childItems.length; i++) {
				if(arguments.length == 1){
					this.childItems[i].Build(arguments[0]);
				}else{
					this.childItems[i].Build();
				}
			}
		}	
	}
}

MenuSystem.prototype.SetTimer = function(){
	if(this.timerState){
		window.clearTimeout(this.menuTimer);
		this.menuTimer = window.setTimeout(this.id + '.Close()',this.timeOut);
	}else{
		this.menuTimer = window.setTimeout(this.id + '.Close()',this.timeOut);
		this.timerState = true;
	}	
}

MenuSystem.prototype.DisableTimer = function(){
	if(this.timerState){
		window.clearTimeout(this.menuTimer);
		this.timerState = false;
	}
}

MenuSystem.prototype.Close = function(){
	var i;
	for (i = 0; i < this.childItems.length; i++) {
		this.childItems[i].Close();
	}
}

function Menu(oParent,id){
	var me;
	if (this.constructor == Menu){
		me = this;
	}else{
		me = arguments[arguments.length-1]
	}
	me.id = id;
	this.objectType = 'Menu';
	me.childItems = new Array();
	me.isSubMenu = false;
	me.fixedPosition = false;
	me.permanentlyVisible = false;
	me.buildType = "All";
	me.isBuilt = false;
	me.left = 0;
	me.top = 0;
	me.zIndex = 100; 
	me.menuTopOffSet = -3;
	me.menuLeftOffSet = 0;
	me.sClassRef = CSSClassRef();
	me.textWidth =-1;	
	me.parent = oParent;
	oParent.AddChild(me);
}

Menu.prototype.CopyProperties = function(oRef){
	this.textWidth = oRef.textWidth;
	this.menuTopOffSet = oRef.textWidth;
	this.menuLeftOffSet = oRef.textWidth;
	this.menuTopOffSet = oRef.menuTopOffSet
	this.menuLeftOffSet = oRef.menuLeftOffSet 
}

Menu.prototype.AddChild = function(oRef){
	if(this.parent.objectType != "MenuSystem"){
		oRef.CopyProperties(this.parent.parent);
	}
	else{
		for (i = 0; i < this.childItems.length; i++) {
			if(this.childItems[i].objectType == oRef.objectType){
				oRef.CopyProperties(this.childItems[i]);
			}
		}
	}
	this.childItems[this.childItems.length] = oRef;
}

Menu.prototype.Build = function(){
	if(arguments.length == 1){
		this.buildType = arguments[0]
	}
	if(((this.buildType == "Visible") && (this.permanentlyVisible == true)) || ((this.buildType == "Hidden") && (this.permanentlyVisible == false)) || (this.buildType == "All")) {
	
		var textTableWidth = '';
		if((is.ie5_5up)&&(is.win)){
			document.write ('<iframe id="' + this.id + 'iframe" src="javascript:false;" scrolling="no" frameborder="0" style="position:absolute; top:0px; left:0px; display:none;"></iframe>');
		}
		var menuStyle = "";
		if(this.permanentlyVisible == false) menuStyle = 'position: absolute; display: none;'
		if(this.fixedPosition) {
			menuStyle += 'position: absolute; left: ' + this.left + 'px; top: ' + this.top + 'px;'
		}
		if(this.isSubMenu){
			menuStyle += ' z-index: ' + (this.zIndex+1) + ';' 
		}else{
			menuStyle += ' z-index: ' + this.zIndex + ';'
		}	
		document.write ('<div id="' + this.id + '"  onmouseout="mmMenuOut()" onmouseover="mmMenuOver()" style="' + menuStyle + '">');
		document.write ('<table id="' + this.id + 'Table" class="mmMenu" border="0" cellpadding="0" cellspacing="0"><tr><td>');
		document.write ('<div id="' + this.id + 'Internal" class="mmMenuInternal" onmousedown="window.event.cancelBubble = true">');
		if(this.textWidth > 0) textTableWidth = ' width="' + (this.textWidth + 10) + '"';
		document.write ('<table border="0" cellpadding="0" cellspacing="0"' + textTableWidth + '>');
		var i;
		for (i = 0; i < this.childItems.length; i++) {
			if(arguments.length == 1){
				this.childItems[i].Build();
			}else{
				this.childItems[i].Build(arguments[0]);
			}
		}
		if(this.textWidth > -1){
			document.write ('<tr class="mmItem">');
			document.write ('<td class="mmMenuBlank" width = "' + this.textWidth + '">' + Spacer(1,1) + '</td>');
			document.write ('<td class="mmMenuBlank" width = "' + 10 + '">' + Spacer(1,1) + '</td></tr>');
		}
		document.write ('</table></div></td></tr></table></div>');
		oDiv = MMGetElementById(this.id);
		if (is.ie4up){
			oDiv.onmouseover = mmMenuOver;
			oDiv.onmouseout = mmMenuOut;
		}else{
			oDiv.addEventListener("mousedown", DocumentMouseDown, true);
			oDiv.addEventListener("onmouseover", DocumentMouseDown, true);
		}
		if(this.fixedPosition){
			this.DisplayIFrameOverlay();
		}
		this.isBuilt = true;
	}
}

Menu.prototype.Display = function(ileft,itop,iwidth){
	if(arguments.length < 3){
		ileft = 200;
		itop = 200;
		iwidth = 100;
	}
	if(this.isBuilt)
	{
		oDiv = MMGetElementById(this.id);
		oDiv.style.left = ileft + this.menuLeftOffSet + 'px';
 		oDiv.style.top = itop + this.menuTopOffSet + 'px';
		oDiv.style.display = 'block';
		this.CheckPageEdge(ileft,itop,iwidth);
		this.DisplayIFrameOverlay();
	}
}

Menu.prototype.DisplayIFrameOverlay = function(){
	if((is.ie5_5up)&&(is.win)){
		oIFrame = MMGetElementById(this.id + 'iframe');
		oIFrame.style.width = parseInt(oDiv.offsetWidth) + 'px';
		oIFrame.style.height = parseInt(oDiv.offsetHeight) + 'px';
		oIFrame.style.top = parseInt(oDiv.style.top) + 'px';
		oIFrame.style.left = parseInt(oDiv.style.left) + 'px';
		oIFrame.style.zIndex = parseInt(oDiv.style.zIndex) - 1;
		oIFrame.style.display = 'block';
	}
}

Menu.prototype.CheckPageEdge = function(iparentleft,iparenttop,iparentwidth){

		oDiv = MMGetElementById(this.id);
		oTable = MMGetElementById(this.id + 'Table');
		pageWidth = 0;
		pageHeight = 0;
		pageScrollTop = 0;
		pageScrollLeft = 0;

		if( typeof( window.innerWidth ) == 'number' ) {
			pageHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
			pageHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			pageHeight = document.body.clientHeight;
		}
		if( typeof( window.pageYOffset ) == 'number' ) {
			pageScrollTop = window.pageYOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			pageScrollTop = document.body.scrollTop;
		} else if( document.documentElement && ( document.documentElement.scrollTop ) ) {
			pageScrollTop = document.documentElement.scrollTop;
		}

		var a = parseInt(oDiv.style.top);
		var b = oDiv.offsetHeight;
		var c = pageHeight;
		var d = pageScrollTop;
		
		if(((a-d) + b) > c)
		{
			oDiv.style.top = (c - b) + 'px';
		}
		if((parseInt(oDiv.style.top)-d) < 0)
		{
			oDiv.style.top = d + 'px';
		}
}



Menu.prototype.Close = function(){
	if(this.permanentlyVisible == false){
		if((is.ie5_5up)&&(is.win)){
			oIFrame = MMGetElementById(this.id + 'iframe');
			if ( oIFrame != null )
					oIFrame.style.display = 'none';
		}
		oDiv = MMGetElementById(this.id);
		if ( oDiv )
			oDiv.style.display = 'none';
	}
	if(this.Parent){
		this.Parent.RemoveHighLight();
	}
	var i;
	for (i = 0; i < this.childItems.length; i++) {
		if(this.childItems[i].hasChild){
			this.childItems[i].childMenu.Close();
		}
	}
}

Menu.prototype.CloseSubMenus = function(id){
	var i;
	for (i = 0; i < this.childItems.length; i++) {
		if((this.childItems[i].hasChild) && (this.childItems[i].id != id)){
			this.childItems[i].childMenu.Close();
		}
	}
}

function MenuItem(oParent,id,link,text,iconSrc){
	var me;
	if (this.constructor == MenuItem){
		me = this;
		this.objectType = 'MenuItem';
	}else{
		me = arguments[arguments.length-1];
	}
	me.id = id;
	me.link = link;
	me.text = text;
	me.highLighted = false;
	me.hasChild = false;
	me.childMenu = '';
	me.textWidth = -1;
	me.sClassRef = CSSClassRef();
	me.parent = oParent
	me.parent.AddChild(me);
}

MenuItem.prototype.CopyProperties = function(oRef){
	this.textIndent = oRef.textIndent;
	this.textWidth = oRef.textWidth;
}

MenuItem.prototype.AddChild = function(oRef){
	oRef.CopyProperties(this.parent);
	this.childMenu = oRef;
	this.hasChild = true;
	oRef.isSubMenu = true;
	oRef.Parent = this;
}

MenuItem.prototype.Build = function(){
	var textCellWidth = '';
	this.SetCurrentStyle();
	document.write ('<tr class="mmItem" id="' + this.id + '" onmousedown="mmDown(\'' + this.id + '\')" onmouseover="mmOver(\'' + this.id + '\')" onmouseout="mmOut(\''  + this.id + '\')">');
	if(this.textWidth > 0) textCellWidth = ' width="' + this.textWidth + '"';
	document.write ('<td class="' + this.textCSSCurrent + '" nowrap="nowrap"' + textCellWidth + '>' + this.text + '</td>');
	if (this.hasChild){
  			document.write ('<td class="' + this.arrowCSSCurrent + '" width = "10"><img border="0" src="../images/arrow.gif" width="10" height="10"/></td>');
	}else{
  			document.write ('<td class="' + this.arrowCSSCurrent + '" width = "10">' + Spacer(1,1) + '</td>');
	}
	document.write ('</tr>'); 
}

MenuItem.prototype.Link = function(){
	if(this.link != '')
	{
		if((is.ie5up)||(is.gecko)){
			oBaseColl = document.getElementsByTagName('BASE');
		}
		if(is.ie4){
			oBaseColl = document.all.tags['BASE'];
		}
		var sBase = '';
		if ( oBaseColl && oBaseColl.length) 
		{
			sBase = oBaseColl[0].href ;
		}
		document.location.href = sBase + this.link;	
	}	
}

MenuItem.prototype.RollOver = function(){
	var oTR
	this.parent.parent.DisableTimer()
	this.parent.CloseSubMenus(this.id);
	oTR = MMGetElementById(this.id);
	if(this.highLighted == false){
		oTR.cells[0].setAttribute(this.sClassRef,"mmTextHighLighted");
		oTR.cells[1].setAttribute(this.sClassRef,"mmArrowHighLighted");
	}
	if(this.hasChild){
		//Fixes safari bug, cannot get pos on a tr
		oTD = oTR.cells[1];
		var ileft = getPageOffsetLeft(oTD) + oTD.offsetWidth;
 		var itop = getPageOffsetTop(oTD);
 		var iwidth = oTD.offsetWidth;
		this.childMenu.Display(ileft,itop,iwidth);
	}
}

MenuItem.prototype.RollOut = function(){
	if((this.highLighted == false)&&(this.hasChild == false)){
		this.RemoveHighLight();
	}
}

MenuItem.prototype.RemoveHighLight = function(){
	if(this.highLighted == false){
		var oTR = MMGetElementById(this.id);
		if ( oTR )
		{
			oTR.cells[0].setAttribute(this.sClassRef,"mmText");
			oTR.cells[1].setAttribute(this.sClassRef,"mmArrow");
		}
	}	
}

MenuItem.prototype.SetCurrentStyle = function(){
	this.itemCSSCurrent = 'mmItem';
	this.textCSSCurrent = 'mmText';
	this.arrowCSSCurrent = 'mmArrow';
	if(this.highLighted){
		this.itemCSSCurrent = 'mmItem';
		this.textCSSCurrent = 'mmTextOn';
		this.arrowCSSCurrent = 'mmArrowOn';
	}
}

function Inherit( base, derived ){
	for ( var i in base.prototype ){
		eval( 'derived.prototype.' + i + '= base.prototype.' + i );
	}
}	

function MMGetElementById(strRef){
	var oRef = '';
	if((is.ie5up)||(is.gecko)){
		oRef = document.getElementById(strRef);
	}
	if(is.ie4){
		oRef = document.all[strRef];
	}
	return oRef;
}

function CSSClassRef(){
	if (is.gecko) sRef =  "class" ;
	if (is.ie4up) sRef = "className";
	return sRef;
}

function getPageOffsetLeft(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function getPageOffsetTop(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}




function mmOver(vid){
	var oCurrentObject = eval(vid);
	oCurrentObject.RollOver();
	if(window.event){
		window.event.cancelBubble = true;
	}
}

function mmOut(vid){
	var oCurrentObject = eval(vid);
	oCurrentObject.RollOut();
	if(window.event){
		window.event.cancelBubble = true;
	}
}

function mmDown(vid){
	var oCurrentObject = eval(vid);
	oCurrentObject.Link();
}

function mmMenuOver(event){
	if(window.event){
		window.event.cancelBubble = true;
	}
}

function mmMenuOut(event){	
	if(window.event){
		window.event.cancelBubble = true;
	}
}

function mmPopMenu(event,vid){
	var oCurrentObject = eval(vid);
	oCurrentObject.Display();	
	if(window.event){
		window.event.cancelBubble = true;
	}
}

function mmCloseMenus(event){	
	DocumentMouseDown();
}

function AddEventArray(oRef){
	EventArray[EventArray.length] = oRef;
}

function DocumentMouseDown(){
	for (i = 0; i < EventArray.length; i++) {
		EventArray[i].Close();
	}
}

function DocumentMouseOver(){
	for (i = 0; i < EventArray.length; i++) {
		EventArray[i].SetTimer();
	}
}

function Spacer(w,h){
	return '<img border="0" src="../images/spacer.gif" width="' + w + '" height="' + h + '" />';
}

if (is.ie4up){
	document.onmousedown = DocumentMouseDown;
	document.onmouseover = DocumentMouseOver;
}else{
	document.addEventListener("mousedown", DocumentMouseDown, true);
	document.addEventListener("mouseover", DocumentMouseOver, true);
}

/* Copied from Creative's Menu.js file 8th November, 2006 */
var currentMenuItem

function toggleSubMenu(menuItemID)
{
	var menuItem = document.getElementById(menuItemID);
	var isTopLevelNav;
		
	menuItem.className = switchClassName(menuItem.className);	
	if(currentMenuItem!=null)
	{	
		if(currentMenuItem!=menuItem)
		{
			ensureMenuItemClosed(currentMenuItem);
		}
	}
	
	currentMenuItem = menuItem;	
}

function switchClassName(className)
{
	className = className.indexOf('Show')>-1?className.replace('Show','Hide'):className.replace('Hide','Show');
	return className;
}

function ensureMenuItemClosed(menuItem)
{
	if(menuItem.className.indexOf('Show')>-1)
	{
		menuItem.className = switchClassName(menuItem.className);
	}
}
