// MenuList Object
// a cascading menu widget utilizing the List Object
// 19990326

// Copyright (C) 1999 Dan Steinman
// Distributed under the terms of the GNU Library General Public License
// Available at http://www.dansteinman.com/dynapi/

// Thanks to: Knut Dale <Knut.S.Dale@eto.ericsson.se>

var theTimer = null // alsch (+) Timer zum automatischen Schließen desn Menues
var openMenu = null // alsch (+) aktuell geoeffnetes Menue
var menuOut = false // alsch(+)
var tertOverlap = -2 // srt (+) overlap für tertiary menus
var tertSize = 120

function MenuList() {
	// main-menu constructor (x,y,width,itemH)
	// sub-menu constructor (parentMenu,parentItemIndex)

	this.name = "MenuList"+(MenuList.count++)
	this.obj = this.name + "MenuListObject"
	eval(this.obj + "=this")
	if (arguments.length==4) {
		this.isChild = false
		this.x = arguments[0]
		this.y = arguments[1]
		this.w = arguments[2]
		this.itemH = arguments[3]
		this.subOnSelect = false
		this.offsetX = -1
		this.offsetY = 0
	}
	else {
		this.isChild = true
		this.parent = arguments[0]
		var index = arguments[1]
		this.parent.list.items[index].hasImage = true
		this.parent.list.items[index].hasChild = true
		this.parent.list.items[index].child = this
		this.x = this.parent.w - tertOverlap
		this.y = this.parent.list.items[index].y
		//this.w = (arguments.length==3)? arguments[2] : this.parent.w
		this.w = arguments[2]
		this.itemH = this.parent.itemH
		this.childShown = null
		this.subOnSelect = this.parent.subOnSelect
		this.offsetX = this.parent.offsetX
		this.offsetY = this.parent.offsetY
	}

	this.visibility = 'hidden' // alsch menu initial unsichtbar (vorher 'inherit')
	this.zIndex = 9
	this.overOpen = false

	this.list = new List(1,1,this.w-2,this.itemH)
	this.list.visibility = 'inherit'
	this.list.allowDeselect = true
	this.list.menulist = this

	if (this.isChild) {
		this.list.image = this.parent.list.image
		this.list.color = this.parent.list.color
		this.list.itemSpacing = this.parent.list.itemSpacing
		this.list.fontname = this.parent.list.fontname
		this.list.fontsize = this.parent.list.fontsize
	}

	this.cssChildren = ''
	this.divChildren = ''

	this.build = MenuListBuild
	this.activate = MenuListActivate
	this.showMenu = MenuListShowMenu
	this.hideMenu = MenuListHideMenu
	this.show = MenuListShow
	this.hide = MenuListHide
	this.toggle = MenuListToggle
	this.select = MenuListSelect
	if (this.isChild) this.onSelect = this.parent.onSelect
	else this.onSelect = new Function()

	this.timeout = 0 // alsch(+) neu Timeout im ms, nach dem das Menue zugeht (0=kein timeout)
	this.id = null // alsch(+) eindeutige ID des Menues
	this.frameBorder = null // alsch (+) y-Wert, ab dem das Submneue bottom-alligned noch oben verschoben wird
	this.selectWithChilds = false // alsch(+) true = eintraege mit submenue klickbar, false = nicht klickbar
}
function MenuListBuild(write) {
	for (var i=0;i<this.list.items.length;i++) {
		if (this.list.items[i].hasChild) {
			this.list.items[i].child.overOpen = this.overOpen
			this.list.items[i].child.build()
		}
	}
	this.list.overOpen = this.overOpen
	this.list.build()
	this.css = css(this.name,this.x+this.offsetX,this.y+this.offsetY,null,null,null,(this.isChild)?'hidden':this.visibility,this.zIndex)+
	css(this.name+'ListW',0,0,this.w,this.list.h+2,bgMenu)+
	this.list.css+
	this.cssChildren

	this.div = '<div id="'+this.name+'">\n'+
	'<div id="'+this.name+'ListW">\n'+
	this.list.div+
	'</div>\n'+
	this.divChildren+
	'</div>\n'

	if (this.isChild) {
		this.parent.cssChildren += this.css
		this.parent.divChildren += this.div
	}
}
function MenuListActivate() {
	this.list.activate()
	this.lyr = new DynLayer(this.name)
	this.h = this.list.h+2
	if (is.ns5) {// tut nix
	    this.lyr.setbg = DynLayerSetbg
	    this.lyr.setbg("#ff0000")//rahmen
	}
	if (is.ns4 && !this.isChild) {
		this.lyr.clipInit()
		this.lyr.clipTo(0,this.w,this.h,0)
	}
	this.list.onSelect = new Function(this.obj+'.select(); return false;')
	for (var i=0;i<this.list.items.length;i++) {
		if (this.list.items[i].hasChild) this.list.items[i].child.activate()
	}
}
function MenuListMouseOver() {
//	alert('MenuListMouseOver')
	return true
}
function MenuListMouseOut(e) {
	var x = (is.ns)? e.pageX : event.x+document.body.scrollLeft
    var y = (is.ns)? e.pageY : event.y+document.body.scrollTop
//	alert('MenuListMouseOut: this.lyr.x='+this.lyr.x+', this.lyr.h='+this.lyr.h+'mouseX='+x+', mouseY='+y)
	return true
}


function MenuListSelect() {
	var i = this.list.selectedIndex
	if (i!=null) {
	if (this.childShown==i) {
		this.hideMenu(this.childShown)
	}
	else {
		this.hideMenu()
		if (this.list.items[i].hasChild) {
			this.showMenu(i)
		}
		if (!this.list.items[i].hasChild || this.subOnSelect) {
			this.onSelect(this.isChild?this.parent.id:this.id) // alsch (+) neu: id als Argument
		}
	}
	}

	// alsch (+): klick auf eintrag mit submenue
	if (this.list.downOnParent)	{
		this.list.downOnParent = false
		this.onSelect(this.isChild?this.parent.id:this.id)
	}
	// ende
}
function MenuListHideMenu() {
	var i = this.childShown
	if (i!=null && this.list.items[i]!=null) {
		this.childShown = null
		this.list.items[i].child.lyr.hide()
		this.list.items[i].child.hideMenu()
		if (this.list.items[i].child.list.selectedIndex!=null) {
			this.list.items[i].child.list.deselect(this.list.items[i].child.list.selectedIndex)
		}
		if (is.ns && !this.isChild) this.lyr.clipTo(0,this.w,this.h,0)
	}
}
// Submenue anzeigen
function MenuListShowMenu(i) {
	if (is.ns && !this.isChild) this.lyr.clipTo(0,this.lyr.w,this.lyr.h,0)

	if (is.ns) this.lyr.clipTo(0,this.lyr.w,this.lyr.h,-this.lyr.w) // alsch (+) anderes clipping für netscape

	this.list.items[i].child.lyr.show()

	//this.list.items[i].child.lyr.moveTo(null,0) // alsch (+) Submenü auf Höhe des Parent schieben

	// alsch (+) submenüs bottom alligned anzeigen, falls sie über die untere Framegrenze ragen
	if (this.frameBorder) {
		y1 = this.list.items[i].child.lyr.y
		y2 = (this.list.items[i].child.lyr.y+this.list.items[i].child.lyr.h)

		if ((this.list.items[i].child.lyr.y+this.list.items[i].child.lyr.h)>this.frameBorder) {
			mvDelta = y2 - this.frameBorder
			y1 = y1 - mvDelta
			this.list.items[i].child.lyr.moveTo(this.w-1,y1)
		}
	}
	// (+) ende


	// alsch (+) falls wir zu nah am rechten fensterrand sind, submenü links ausklappen
	if (this.lyr.x+2*this.w > getW()) {
		this.list.items[i].child.lyr.moveTo(-(this.w-1-tertOverlap),null)
	}
	// (+) ende
	this.childShown = i

	openMenu = this // alsch (+) menue merken
}
function MenuListToggle() {
	if (!this.visible) this.show()
	else this.hide()
}
function MenuListHide() {
	this.hideMenu()
	this.list.deselect(this.list.selectedIndex)
	this.lyr.hide()
	this.visible = false

	openMenu = null // alsch (+) menue merken
}
function MenuListShow() {
	this.lyr.css.visibility = 'inherit'

	// Bei Bedarf (am Rand) Menü nach links verschieben... (alsch)
	yMenu	= this.lyr.x+this.w
	yBorder	= getW()
	if (yMenu > yBorder)
		this.lyr.moveTo(this.lyr.x-(yMenu-yBorder), this.lyr.y)

	this.visible = true
	openMenu = this // ole (+) menue merken
}
function MenuListRedirect() {
	this.hide()
	location.href = this.list.value
}
// alsch (+) Funktionen die Over-/Out-Events für Menüeinträge behandeln
//           und nach Timeout die Menues schliessen
function notifyListOut() {
	menuOut = true
	if(!theTimer && this.timeout  && openMenu) {
		theTimer=setTimeout("closeMenu()",this.timeout)
	}
}
function notifyListOver() {
	menuOut = false
	if (theTimer) {
		clearTimeout(theTimer)
		theTimer = null
	}
}
function closeMenu() {
	clearTimeout(theTimer)
	theTimer = null
	if (openMenu && menuOut)
		openMenu.hide()
	disableAllButtons();
}
// ende
MenuList.count = 0
