// Last modification : 2005-09-13


// Tree class
function JSMenu(id,timeout,useTimeout,childOverlapX,childOverlapY,initialVisibility) {
	this.id = id
	this.rootMenu = null
	this.hideTimer = null
	this.timeout = timeout
	this.useTimeout = (useTimeout && useTimeout!="0") ? true : false
	this.childOverlapX = childOverlapX
	this.childOverlapY = childOverlapY
	this.initialVisibility = initialVisibility
	this.userOverTree = false
	this.isLoaded = false
	this.statusBarText = ''

	this.pageClick = JSMenu_pageClick
	this.hideTree = JSMenu_hideTree
	this.setup = JSMenu_setup
	this.startIt = JSMenu_startIt
	this.display = JSMenu_display
	this.moveTo = JSMenu_moveTo
}

function JSMenu_pageClick() {
	// without the following test, the onclick event will be ignored
	// (ie the items links won't work) in absolute positionned DIVs
	// because of the document.onmousedown event
	if (!this.userOverTree) { this.hideTree() }
}

function JSMenu_hideTree() {
	this.rootMenu.hideChildren()
	if (this.initialVisibility == "hidden") { this.rootMenu.display(false) }
	if (this.hideTimer) { 
		clearTimeout(this.hideTimer) 
		this.hideTimer = null
	}
}

function JSMenu_setup(rootMenuName) {
	this.rootMenu =  getObject(rootMenuName);
	
	var otherOnMouseDown = (document.onmousedown) ? document.onmousedown : new Function
	eval("document.onmousedown = function() { " + this.id + ".pageClick(); otherOnMouseDown() }")

	var otherOnLoad = (window.onload) ? window.onload : new Function
	eval("window.onload = function() { " + this.id + ".startIt(); otherOnLoad() }")
}

function JSMenu_startIt() {
    this.rootMenu.posX = getObjectPositionX(this.rootMenu)
    this.rootMenu.posY = getObjectPositionY(this.rootMenu)
    this.isLoaded = true
}

function JSMenu_display(on) {
	if (this.isLoaded) {
		this.rootMenu.display(on)
	}
}

function JSMenu_moveTo(X,Y) {
	if (this.isLoaded) {
		this.rootMenu.moveTo(X,Y)
	}
}



// Menu class
function JSMenu_initMenu(menuName,tree,childrenArray) {
	var mnu = getObject(menuName)
	mnu.tree = tree
	mnu.posX = 0
	mnu.posY = 0
	
	mnu.childrenItems = new Array()
	if (childrenArray) {
		for (var i=0; i<childrenArray.length; i++) {
			mnu.childrenItems[i] = getObject(childrenArray[i])
		}
	}

	mnu.onmouseover = JSMenu_menuOnMouseOver
	mnu.onmouseout = JSMenu_menuOnMouseOut
	mnu.isVisible = JSMenu_menuIsVisible
	mnu.display = JSMenu_menuDisplay
	mnu.hideChildren = JSMenu_menuHideChildren
	mnu.moveTo = JSMenu_menuMoveTo
}

function JSMenu_menuOnMouseOver() {
	if (this.tree.useTimeout) { 
		if (this.tree.hideTimer) { 
			clearTimeout(this.tree.hideTimer) 
			this.tree.hideTimer = null
		}
	}
}

function JSMenu_menuOnMouseOut() {
	if (this.tree.useTimeout) { 
		if (this.tree.hideTimer) { 
			clearTimeout(this.tree.hideTimer) 
			this.tree.hideTimer = null
		}
		this.tree.hideTimer = setTimeout(this.tree.id + '.hideTree()',this.tree.timeout) 
	}
}

function JSMenu_menuIsVisible() {
	return (this.style.visibility == "visible")
}

function JSMenu_menuDisplay(on) {
	this.style.visibility = (on) ? "visible" : "hidden"
}

function JSMenu_menuHideChildren() {
	if (this.isVisible()) {
		for (var i=0; i<this.childrenItems.length; i++) {
			this.childrenItems[i].hideChildMenu()
		}
	}
}

function JSMenu_menuMoveTo(X,Y) {
	X = parseInt(X)
	Y = parseInt(Y)
	this.style.left = X + "px"
	this.style.top = Y + "px"
	this.posX = X
	this.posY = Y
}



// Item class
function JSMenu_initItem(itemName,parentMenuName,childMenuName,itemLink,backgroundColorOver,backgroundColorOut,textColorOver,textColorOut,imageSrc,imageSrcOver) {
	var item = getObject(itemName)
	item.parentMenu = getObject(parentMenuName)
	if (childMenuName) { item.childMenu = getObject(childMenuName) }
	else { item.childMenu = null }
	item.backgroundColorOver = backgroundColorOver
	item.backgroundColorOut = backgroundColorOut
	item.textColorOver = textColorOver
	item.textColorOut = textColorOut
	item.itemLink = itemLink
	item.imageSrc = imageSrc
	item.imageSrcOver = imageSrcOver
	item.imageObject = getObject(itemName+'image')
	item.linkObject = getObject(itemName+'link')

	item.onmouseover = JSMenu_itemOnMouseOver
	item.onmouseout = JSMenu_itemOnMouseOut
	item.onclick = JSMenu_itemOnClick
	item.hideChildMenu = JSMenu_itemHideChildMenu
}

function JSMenu_itemOnMouseOver() {
	// hide the parent menu's submenus
	this.parentMenu.hideChildren()

	// child menu	
	if (this.childMenu && this.childMenu.tree.isLoaded) { 
		// calculate the child menu's position
		var X = parseInt(this.parentMenu.posX) + parseInt(this.parentMenu.style.width)
		var Y = parseInt(this.parentMenu.posY) + this.offsetTop
		X-= parseInt(this.childMenu.tree.childOverlapX)
		Y+= parseInt(this.childMenu.tree.childOverlapY)

		// keep the menu inside the window
	    var ExtraSpace     		= 30;
		var MenuRightEdge  		= X + this.offsetWidth;
		var MenuBottomEdge 		= Y + this.offsetHeight;
		var WindowLeftEdge 		= (document.body.scrollLeft)?document.body.scrollLeft:window.pageXOffset;
		if (!WindowLeftEdge) { WindowLeftEdge = 0 }
		var WindowTopEdge  		= (document.body.scrollTop)?document.body.scrollTop:window.pageYOffset;
		if (!WindowTopEdge) { WindowTopEdge = 0 }
		var WindowWidth     	= (document.body.offsetWidth)?document.body.offsetWidth:window.innerWidth;
		var WindowHeight   		= (document.body.offsetHeight)?document.body.offsetHeight:window.innerHeight;
		var WindowRightEdge 	= WindowLeftEdge + WindowWidth - ExtraSpace;
		var WindowBottomEdge 	= WindowTopEdge + WindowHeight - ExtraSpace - 10;
		if (MenuRightEdge > WindowRightEdge) {
			var dif = MenuRightEdge - WindowRightEdge;
			X -= dif;
		}
		X = Math.max(5,X);
		if (MenuBottomEdge > WindowBottomEdge) {
			var dif = MenuBottomEdge - WindowBottomEdge;
			Y -= dif;
		}
		Y = Math.max(5,Y);
		

		// set the child menu's position
		this.childMenu.moveTo(X,Y)
		
		// show child menu
		this.childMenu.display(true) 
	}

	// rollover
	if (this.backgroundColorOver) { this.style.backgroundColor = this.backgroundColorOver }
	if (this.textColorOver) { 
		this.style.color = this.textColorOver 
		if (this.linkObject) { this.linkObject.style.color = this.textColorOver }
	}
	if (this.imageObject && this.imageSrcOver) { this.imageObject.src = this.imageSrcOver }
	this.style.cursor = 'pointer'
	
	// status bar
	if (!this.parentMenu.tree.statusBarText) {
		this.parentMenu.tree.statusBarText = window.status
	}
	window.status = this.itemLink

	// set state flag
	this.parentMenu.tree.userOverTree = true
}

function JSMenu_itemOnMouseOut() {
	// rollover
	if (!this.childMenu || !this.childMenu.isVisible()) {
		if (this.backgroundColorOut) { this.style.backgroundColor = this.backgroundColorOut }
		if (this.textColorOut) { 
			this.style.color = this.textColorOut 
			if (this.linkObject) { this.linkObject.style.color = this.textColorOut }
		}
		if (this.imageObject && this.imageSrcOver) { this.imageObject.src = this.imageSrc }
		this.style.cursor = 'auto'
	}

	// status bar
	window.status = this.parentMenu.tree.statusBarText
	if (this.parentMenu.tree.statusBarText) {
		this.parentMenu.tree.statusBarText = ''
	}

	// set state flag
	this.parentMenu.tree.userOverTree = false
}

function JSMenu_itemOnClick() {
	if (this.itemLink) {
		if (this.itemLink.indexOf("javascript:")!=-1) {
			eval(this.itemLink)
		}
		else {
			this.parentMenu.tree.hideTree()
			document.location = this.itemLink
		}
	}
}

function JSMenu_itemHideChildMenu() {
	// hide submenu
	if (this.childMenu) {
		this.childMenu.hideChildren()
		this.childMenu.display(false)
		this.onmouseout()
	}
}



