var Menus = new Array()		 //Array with menus
var menuIDs = new Array()	 //Array with the IDs of all the menus sets
var menuCount = 0		       //The number of menu sets
var menuPath = new Array() //Path followed in active menu set to current submenu
var menuCollapseTimeout		 //for Netscape 4 compatibility

var menuSubImg = "menuSArrow1.gif";	//image to indicate a sub menu
var menuBColor = "aqua"		 // menu border color
var menuBWidth = 2		     // menu border width
var menuSColor = "green"	 // menu seperator color
var menuSWidth = 1		     // menu seperator color
var menuCutOver = 5		     // pixels a submenu is covering the parent
var collapseID             // 
var collapseSet = false    // true if auto collapse is set - set when mouse moved outside menu item


function menuOpen(div) {
	for (var i = 0; i < tpMOUSE.menuLevel; i++) {
		if (menuPath[i] == div)
			return true;
	}
	return false;
}

function menuActiveLevel(m, item) {
	levelA = 0;
	for (var i = 0; i < tpMOUSE.menuLevel; i++) {
		if (m == menuPath[i]) {
			levelA = i+1;
			break;
		}
	}
	levelB = 0;
	if (item.sub && levelA != -1) {
		for (var i = 0; i < tpMOUSE.menuLevel; i++) {
			if (item.sub == menuPath[i]) {
				levelB = i+1;
				break;
			}
		}
		if (levelB == 0) 
			return levelA;
		else
			return tpMOUSE.menuLevel;
	}
	else
		return levelA;
}
function collapseMenus(toLevel) {
	status = ""
	if (toLevel < tpMOUSE.menuLevel) {
		for (var i = tpMOUSE.menuLevel; i > toLevel; i--) {
			var layer = Layers[menuPath[i-1].actBy.div]
			if (menuPath[i-1].CSS1) 
				layer.write('<font class="'+menuPath[i-1].CSS1+'">'+menuPath[i-1].actBy.text+'</font>')
			else
				layer.write('<font color="'+menuPath[i-1].f1+'">'+menuPath[i-1].actBy.text+'</font>')
			if (menuPath[i-1].img1)
				layer.setBGImage(menuPath[i-1].img1);
			else
				layer.setBGColor(menuPath[i-1].b1);
			menuPath[i-1].hide();
		}
	}
	tpMOUSE.menuLevel = toLevel;
}

function menuMouseOver(e) {
	if (collapseSet) {
	   clearTimeout(collapseID);
		 collapseSet = false ;
	} 
	if (tpMOUSE.menuLevel > 0) {
		collapseMenus(menuActiveLevel(this.mObj, this.mItem));
	}
	status = this.mItem.hint
	var layer = Layers[this.mItem.div]
	if (this.mObj.CSS2) 
		layer.write('<font class="'+this.mObj.CSS2+'">'+this.mItem.text+'</font>')
	else 
		layer.write('<font color="'+this.mObj.f2+'">'+this.mItem.text+'</font>')
	if (this.mObj.img2) 
		layer.setBGImage(this.mObj.img2)
	else
		layer.setBGColor(this.mObj.b2)
	if (this.mItem.sub) {
		if (!menuOpen(this.mItem.sub)) {
			this.mItem.sub.show()
			menuPath[tpMOUSE.menuLevel] = this.mItem.sub
			menuPath[tpMOUSE.menuLevel].actBy = this.mItem
			tpMOUSE.menuLevel++;
		}
	}
}

function menuMouseOut(e) {
	status = ""
	if (!collapseSet) { 
		collapseID=setTimeout("collapseMenus(0)", 500);
		collapseSet = true 
	} 
	
	var clear = true
	if (this.mItem.sub)
		clear = !menuOpen(this.mItem.sub)
	if (clear) {
		layer = Layers[this.mItem.div]
		if (this.mObj.CSS1)
			layer.write('<font class="'+this.mObj.CSS1+'">'+this.mItem.text+'</font>')
		else 
			layer.write('<font color="'+this.mObj.f1+'">'+this.mItem.text+'</font>')
		if (this.mObj.img1)
			layer.setBGImage(this.mObj.img1)
		else
			layer.setBGColor(this.mObj.b1)
	}
}

function menuMouseClick(e) {
	status = ""
	var activate = true
	if (this.mItem.sub)
		activate = !menuOpen(this.mItem.sub)
	if (activate) {
		collapseMenus(0)
		prevMenuSelect = null
		href = this.mItem.href
		if (href.substring(0, 4) == "java")
			eval(href)
		else
			location.href = href
	}
	
}

function menuMouseDown(e) {
	if (is.ie)
		event.cancelBubble = true;
	else if(is.ns6)
		e.stopPropagation();
	else if(is.ns4)
		clearTimeout(menuCollapseTimeout);
}

function showMenu() {
	for (var i = 0; i < this.cc; i++) {
		Layers[this.c[i].div+'E'].show()
		Layers[this.c[i].div].show()
		Layers[this.c[i].div+'b'].show()
		if (i > 0)
			Layers[this.c[i].div+'s'].show()
		if (this.c[i].sub)
			Layers[this.c[i].div+'p'].show()
	}
}

function hideMenu() {
	for (var i = 0; i < this.cc; i++) {
		Layers[this.c[i].div+'E'].hide()	
		Layers[this.c[i].div+'b'].hide()
		if (i > 0) Layers[this.c[i].div+'s'].hide()
		if (this.c[i].sub)
			Layers[this.c[i].div+'p'].hide()
		Layers[this.c[i].div].hide()
	}
}

function setSubMenuBGImage(obj, img1, img2) {
	obj.img1 = img1
	obj.img2 = img2
	for (var i = 0; i < obj.cc; i++) {
		if (img1)
			Layers[obj.c[i].div].setBGImage(img1)		
		else
			Layers[obj.c[i].div].setBGColor(obj.b1)
		if (obj.c[i].sub)
			setSubMenuBGImage(obj.c[i].sub, img1, img2)
	}	
}

function setMenuBGImages(img1, img2) {
	setSubMenuBGImage(this, img1, img2)
}

function setSubMenuBGColor(obj, col1, col2) {
	obj.b1 = col1 || obj.b1
	if (col1)
		obj.img1 = null
	obj.b2 = col2 || obj.b2
	if (col2)
		obj.img2 = null
	for (var i = 0; i < obj.cc; i++) {
		if (col1)
			Layers[obj.c[i].div].setBGColor(obj.b1)
		if (obj.c[i].sub)
			setSubMenuBGColor(obj.c[i].sub, col1, col2)
	}
}

function setMenuBGColors(col1, col2) {
	setSubMenuBGColor(this, col1, col2)
}

function menuObj(parent, startX, startY, width, height, fcolor1, bgcolor1, fcolor2, bgcolor2, zIndex, img1, img2, CSSClass1, CSSClass2, sub) {
	this.parent = parent
	this.x = startX
	this.y = startY
	this.w = width
	this.h = height
	this.f1 = fcolor1
	this.b1 = bgcolor1
	this.f2 = fcolor2
	this.b2 = bgcolor2
	this.z =zIndex
	this.c = null			//Array with menu items
	this.cc = 0				//Counter of "c"
	this.img1 = img1
	this.img2 = img2
	this.CSS1 = CSSClass1
	this.CSS2 = CSSClass2
	this.isSub = sub	
	this.bc = menuBColor
	this.sc = menuSColor
	this.bw = menuBWidth
	this.sw = menuSWidth
	
	this.show = showMenu
	this.hide = hideMenu
	this.setBGColors = setMenuBGColors
	this.setBGImages = setMenuBGImages
}

function tpMenuItem(sub, iName, iText, iHRef, iHint) {
	this.sub = sub
	this.div = iName
	this.text = iText
	this.href = iHRef
	this.hint = iHint
}

function addMenuItem(parentID, text, href, hint) {
	p = Menus[parentID];
	if (!p.c)
		p.c = new Array();
	menuItemNum = p.cc;
	var e1, e2;
	if (p.cc == 0) {
		e1 = 0;
		e2 = 1;
	}
	else {
		e1 = 1;
		e2 = 0;
	}
	hint = hint || ""
	//border div
	addLayer(parentID+p.cc+'b',p.parent,p.x,p.y+e1*p.bw+((p.h+p.sw)*p.cc),
			 p.w+2*p.bw,p.h+ (1+e2)*p.bw,p.bc,p.z,"hidden");
		
	//seperator div
	if (p.cc > 0) 
		addLayer(parentID+p.cc+'s',p.parent,p.x+p.bw,p.y+p.bw + p.h*p.cc + p.sw*(p.cc-1),
				 p.w,p.sw,p.sc, p.z,"hidden");
	//menu item div
	addLayer(parentID+p.cc,p.parent,p.x+p.bw,p.y+p.bw+(p.h+p.sw)*p.cc,
			 p.w,p.h,p.b1,p.z,"hidden");
	//event capture div
	addLayer(parentID+p.cc+'E',p.parent,p.x+p.bw,p.y+p.bw+(p.h+p.sw)*p.cc,
			 p.w,p.h,"transparent",p.z+12,"hidden");
	//submenu pointer div		
	if (href == null) {
		addLayer(parentID+p.cc+'p',p.parent,p.x+p.w-p.bw-menuCutOver-5,p.y+p.bw+((p.h+p.sw)*p.cc)+(p.h-13)/2,
			10,13,"transparent",p.z+1,"hidden");
	}	
	menuLayes = false;
	p.c[p.cc] = new tpMenuItem(null, parentID+p.cc,text,href,hint)
	p.cc++;
}

function addSubMenu(parentID, id, width, text, hint, CSSClass1, CSSClass2) {
	addMenuItem(parentID, text, null, hint);
	p = Menus[parentID];
	CSSClass1 = CSSClass1 || p.CSS1
	CSSClass2 = CSSClass2 || p.CSS2
	p.c[p.cc-1].sub = new menuObj(p.parent,p.x+p.w+2*p.bw-menuCutOver, p.y+p.h*(p.cc-1), width, p.h, p.f1, p.b1, p.f2, p.b2, p.z+1, p.img1, p.img2, CSSClass1, CSSClass2, true);		
	Menus[id] = p.c[p.cc-1].sub;
}

function addMenu(id, parent, startX, startY, width, height, fcolor1, bgcolor1,
				 fcolor2, bgcolor2, img1, img2, CSSClass1, CSSClass2, zIndex) {				  
	Menus[id] = new menuObj(parent, startX,startY,width,height,fcolor1,bgcolor1,fcolor2,bgcolor2,zIndex,img1,img2, CSSClass1, CSSClass2)
	menuIDs[menuCount] = id
	menuCount++
}
function menuMOver(e) {
	this.mObj.mOver(e)
}
function menuMOut(e) {
	this.mObj.mOut(e)
}
function menuMClick(e) {
	this.mObj.mClick(e)
}
function menuMDown(e) {
	this.mObj.mDown(e)
}
function addMenuMouseEvents(m, j) {
	var cobj, dbj
	if (is.ns && !is.ns6) {
		cobj = Layers[m.c[j].div+'E']
		dobj = Layers[m.c[j].div]
	}
	else {
		cobj = Layers[m.c[j].div+'E'].HTM
		dobj = Layers[m.c[j].div].HTM
	}
	dobj.mObj = m
	dobj.mItem = m.c[j]
	dobj.mOver = menuMouseOver
	dobj.mOut = menuMouseOut
	dobj.mClick = menuMouseClick
	dobj.mDown = menuMouseDown
	
	cobj.mObj = dobj
	cobj.onmouseover = menuMOver 
	cobj.onmouseout = menuMOut
	cobj.onmouseup = menuMClick
	cobj.onmousedown = menuMDown
	if (is.ns4) {
		cobj.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT | Event.MOUSEUP | Event.MOUSEDOWN)
	}
}

function initTPMenu(m) {
	for (var j = 0; j < m.cc; j++) {
		Layers[m.c[j].div+'E'].setBGImage(wwwBase+'jscripts/images/tpMenus/transparent.gif')
		if (m.CSS1)
			Layers[m.c[j].div].write('<font class="'+m.CSS1+'">'+m.c[j].text+'</font>')
		else
			Layers[m.c[j].div].write('<font color="'+m.f1+'">'+m.c[j].text+'</font>')
		if (m.c[j].sub) {
			Layers[m.c[j].div+'p'].write('<img src="'+wwwBase+'jscripts/images/tpMenus/'+menuSubImg+'">')			
		}
		addMenuMouseEvents(m, j);
		if (m.c[j].sub) {
			initTPMenu(m.c[j].sub);
		}
	}
}

function initMenus() {
	for (var i = 0; i < menuCount; i++) {
		var m = Menus[menuIDs[i]]
		initTPMenu(m)
		m.setBGImages(m.img1, m.img2)
		m.show()
	}
}
           
