/*
 * This script was created by Erik Arvidsson (erik@eae.net)
 * for WebFX (http://webfx.eae.net)
 * Copyright 2001
 *
 * For usage see license at http://webfx.eae.net/license.html
 *
 * Created:		2001-01-12
 * Updates:		2001-11-20	Added hover mode support and removed Opera focus hacks
 *				2001-12-20	Added auto positioning and some properties to support this
 *				2002-08-13	toString used ' for attributes. Changed to " to allow in args
 *				2004-07-06	Hide form select inputs on menu display (by Laurent Tordjman <laurent@music75.com>)
 *						Make it work with Safari browser (Mac platform)						
 */
// check browsers
var ua = navigator.userAgent
var opera = /opera [56789]|opera\/[56789]/i.test(ua) || /Safari/.test(ua)
var ie = !opera && /MSIE/.test(ua)
var ie50 = ie && /MSIE 5\.[0-5]/.test(ua)
var ie6 = ie && /MSIE [6789]/.test(ua)
var ieBox = ie && (document.compatMode == null || document.compatMode != 'CSS1Compat')
var moz = !opera && /gecko/i.test(ua)
var nn6 = !opera && /netscape.*6\./i.test(ua)
// define the default values
webfxMenuDefaultWidth			= 200
webfxMenuDefaultBorderLeft		= 1
webfxMenuDefaultBorderRight		= 1
webfxMenuDefaultBorderTop		= 1
webfxMenuDefaultBorderBottom	= 1
webfxMenuDefaultPaddingLeft		= 1
webfxMenuDefaultPaddingRight	= 1
webfxMenuDefaultPaddingTop		= 1
webfxMenuDefaultPaddingBottom	= 1
webfxMenuDefaultShadowLeft		= 0
webfxMenuDefaultShadowRight		= ie && !ie50 && /win32/i.test(navigator.platform)? 4 :0
webfxMenuDefaultShadowTop		= 0
webfxMenuDefaultShadowBottom	= ie && !ie50 && /win32/i.test(navigator.platform)? 4 : 0
webfxMenuItemDefaultHeight		= 18
webfxMenuItemDefaultText		= 'Untitled'
webfxMenuItemDefaultHref		= 'javascript:void(0)'
webfxMenuSeparatorDefaultHeight	= 6
webfxMenuDefaultEmptyText		= 'Empty'
webfxMenuDefaultUseAutoPosition	= nn6? false : true
// other global constants
webfxMenuImagePath				= ''
webfxMenuUseHover				= opera? true : false
webfxMenuHideTime				= 500
webfxMenuShowTime				= 200
var webFXMenuHandler = {
	idCounter		:	0,
	nbOpen			:	0, // count opened menus
	idPrefix		:	'mnuWebfx',
	all				:	{},
	getId			:	function(){return this.idPrefix + this.idCounter++},
	overMenuItem	:	function(oItem){
		if(this.showTimeout) window.clearTimeout(this.showTimeout)
		if(this.hideTimeout) window.clearTimeout(this.hideTimeout)
		var jsItem = this.all[oItem.id]
		if(webfxMenuShowTime <= 0) this._over(jsItem)
		else //this.showTimeout = window.setTimeout(function(){webFXMenuHandler._over(jsItem)}, webfxMenuShowTime)
			// I hate IE5.0 because the piece of shit crashes when using setTimeout with a function object
			this.showTimeout = window.setTimeout("webFXMenuHandler._over(webFXMenuHandler.all['" + jsItem.id + "'])", webfxMenuShowTime)
	},
	outMenuItem	: function(oItem){
		if(this.showTimeout) window.clearTimeout(this.showTimeout)
		if(this.hideTimeout) window.clearTimeout(this.hideTimeout)
		var jsItem = this.all[oItem.id]
		if(webfxMenuHideTime <= 0) this._out(jsItem)
		else //this.hideTimeout = window.setTimeout(function(){webFXMenuHandler._out(jsItem)}, webfxMenuHideTime)
			this.hideTimeout = window.setTimeout("webFXMenuHandler._out(webFXMenuHandler.all['" + jsItem.id + "'])", webfxMenuHideTime)
	},
	blurMenu : function(oMenuItem){
		window.setTimeout("webFXMenuHandler.all['" + oMenuItem.id + "'].subMenu.hide()", webfxMenuHideTime)
	},
	_over : function(jsItem){
		jsItem.parentMenu.hideAllSubs()
		if(jsItem.subMenu) jsItem.subMenu.show()
	},
	_out : function(jsItem){
		// find top most menu
		var root = jsItem, m
		if(root instanceof WebFXMenuButton) m = root.subMenu
		else {
			m = jsItem.parentMenu
			while(m.parentMenu && !(m.parentMenu instanceof WebFXMenuBar)) m = m.parentMenu
		}
		if(m) m.hide()
	},
	hideMenu : function(menu){
		if(this.showTimeout) window.clearTimeout(this.showTimeout)
		if(this.hideTimeout) window.clearTimeout(this.hideTimeout)
		this.hideTimeout = window.setTimeout("webFXMenuHandler.all['" + menu.id + "'].hide()", webfxMenuHideTime)
	},
	showMenu : function(menu, src, dir){
		if(this.showTimeout) window.clearTimeout(this.showTimeout)
		if(this.hideTimeout) window.clearTimeout(this.hideTimeout)
		if(!dir) dir = 'vertical'
		menu.show(src, dir)
	}
}
function WebFXMenu(){
	this._menuItems	= []
	this._subMenus	= []
	this.id			= webFXMenuHandler.getId()
	this.top		= 0
	this.left		= 0
	this.shown		= false
	this.parentMenu	= null
	webFXMenuHandler.all[this.id] = this
}
WebFXMenu.prototype.width			= webfxMenuDefaultWidth
WebFXMenu.prototype.emptyText		= webfxMenuDefaultEmptyText
WebFXMenu.prototype.useAutoPosition	= webfxMenuDefaultUseAutoPosition
WebFXMenu.prototype.borderLeft		= webfxMenuDefaultBorderLeft
WebFXMenu.prototype.borderRight		= webfxMenuDefaultBorderRight
WebFXMenu.prototype.borderTop		= webfxMenuDefaultBorderTop
WebFXMenu.prototype.borderBottom	= webfxMenuDefaultBorderBottom
WebFXMenu.prototype.paddingLeft		= webfxMenuDefaultPaddingLeft
WebFXMenu.prototype.paddingRight	= webfxMenuDefaultPaddingRight
WebFXMenu.prototype.paddingTop		= webfxMenuDefaultPaddingTop
WebFXMenu.prototype.paddingBottom	= webfxMenuDefaultPaddingBottom
WebFXMenu.prototype.shadowLeft		= webfxMenuDefaultShadowLeft
WebFXMenu.prototype.shadowRight		= webfxMenuDefaultShadowRight
WebFXMenu.prototype.shadowTop		= webfxMenuDefaultShadowTop
WebFXMenu.prototype.shadowBottom	= webfxMenuDefaultShadowBottom
WebFXMenu.prototype.add = function(menuItem){
	this._menuItems[this._menuItems.length] = menuItem
	if(menuItem.subMenu){
		this._subMenus[this._subMenus.length] = menuItem.subMenu
		menuItem.subMenu.parentMenu = this
	}
	menuItem.parentMenu = this
}
WebFXMenu.prototype.show = function(relObj, sDir){
	if(this.useAutoPosition) this.position(relObj, sDir)
	var divElement = _getElt(this.id)
	if(divElement){
		divElement.style.left = opera? this.left : this.left + 'px'
		divElement.style.top = opera? this.top : this.top + 'px'
		divElement.style.visibility = 'visible'
	}
	if(!this.shown){
		this.shown = true
		webFXMenuHandler.nbOpen++
	}
	if(this.parentMenu) this.parentMenu.show()
	else _setSelect('hidden')
}
WebFXMenu.prototype.hide = function(){
	this.hideAllSubs()
	var divElement = _getElt(this.id)
	if(divElement) divElement.style.visibility = 'hidden'
	if(this.shown){
		this.shown = false
		if(!--webFXMenuHandler.nbOpen) _setSelect('visible')
	}
}
WebFXMenu.prototype.hideAllSubs = function(){
	for(var i=0; i < this._subMenus.length; i++) if(this._subMenus[i].shown) this._subMenus[i].hide()
}
WebFXMenu.prototype.toString = function(){
	var top = this.top + this.borderTop + this.paddingTop
	var str = '<div id="' + this.id + '" class="webfx-menu" style="' +
	'width:' + (ieBox? this.width : this.width - this.borderLeft - this.paddingLeft - this.borderRight - this.paddingRight) + 'px;' +
	(this.useAutoPosition? 'left:' + this.left + 'px;top:' + this.top + 'px;' : '') +
	(ie50? 'filter:none' : '') +
	'">'
	if(this._menuItems.length == 0) str += '<span class="webfx-menu-empty">' + this.emptyText + '</span>'
	else
		for(var i=0,mi; i < this._menuItems.length; i++){ // loop through all menuItems
			str += (mi = this._menuItems[i])
			if(!this.useAutoPosition){
				if(mi.subMenu && !mi.subMenu.useAutoPosition) mi.subMenu.top = top - mi.subMenu.borderTop - mi.subMenu.paddingTop
				top += mi.height
			}
		}
	str += '</div>'
	for(var i=0; i < this._subMenus.length; i++){
		this._subMenus[i].left = this.left + this.width - this._subMenus[i].borderLeft
		str += this._subMenus[i]
	}
	return str
}
// WebFXMenu.prototype.position defined later
function WebFXMenuItem(sText, sHref, sClick, sToolTip, oSubMenu){
	this.text = sText || webfxMenuItemDefaultText
	this.href = sHref == null || sHref == ""? webfxMenuItemDefaultHref : sHref
	this.sClick = sClick
	this.subMenu = oSubMenu
	if(oSubMenu) oSubMenu.parentMenuItem = this
	this.toolTip = sToolTip
	this.id = webFXMenuHandler.getId()
	webFXMenuHandler.all[this.id] = this
}
WebFXMenuItem.prototype.height = webfxMenuItemDefaultHeight
WebFXMenuItem.prototype.toString = function(){
	return	'<a id="' + this.id + '" href="' + this.href + '"' +
			(ie? ' style="width: 100%"' : '') +
			(this.toolTip? ' title="' + this.toolTip + '"' : '') +
			(this.sClick? ' onClick="' + this.sClick + '"' : '') +
			' onmouseover="webFXMenuHandler.overMenuItem(this)"' +
			(webfxMenuUseHover? ' onmouseout="webFXMenuHandler.outMenuItem(this)"' : '') +
			(this.subMenu? ' unselectable="on" tabindex="-1"' : '') +
			'>' +
			(this.subMenu? '<img src="' + webfxMenuImagePath + 'arrowr.png" border=0 class="arrow">' : '') +
			this.text +
			'</a>'
}
function WebFXMenuSeparator(){
	this.id = webFXMenuHandler.getId()
	webFXMenuHandler.all[this.id] = this
}
WebFXMenuSeparator.prototype.height = webfxMenuSeparatorDefaultHeight
WebFXMenuSeparator.prototype.toString = function(){
	return '<div id="' + this.id + '"' + (webfxMenuUseHover? ' onmouseover="webFXMenuHandler.overMenuItem(this)" onmouseout="webFXMenuHandler.outMenuItem(this)"' : '') + '></div>'
}
function WebFXMenuBar(){
	this._parentConstructor = WebFXMenu
	this._parentConstructor()
	webFXMenuHandler.nbOpen--
}
WebFXMenuBar.prototype = new WebFXMenu
WebFXMenuBar.prototype.toString = function(){
	var str = '<div id="' + this.id + '" class="webfx-menu-bar">'
	for(var i=0; i < this._menuItems.length; i++) str += this._menuItems[i] // loop through all menuButtons
	str += '</div>'
	for(i=0; i < this._subMenus.length; i++) str += this._subMenus[i]
	return str
}
function WebFXMenuButton(sText, sHref, sToolTip, oSubMenu){
	this._parentConstructor = WebFXMenuItem
	this._parentConstructor(sText, sHref, sToolTip, oSubMenu)
}
WebFXMenuButton.prototype = new WebFXMenuItem
WebFXMenuButton.prototype.toString = function(){
	return	'<a id="' + this.id + '" href="' + this.href + '"' +
			(this.toolTip? ' title="' + this.toolTip + '"' : '') +
			(webfxMenuUseHover? ' onmouseover="webFXMenuHandler.overMenuItem(this)" onmouseout="webFXMenuHandler.outMenuItem(this)"' : ' onfocus="webFXMenuHandler.overMenuItem(this)"' + (this.subMenu? ' onblur="webFXMenuHandler.blurMenu(this)"' : '')) +
			'>' +
			this.text +	(this.subMenu? ' <img src="' + webfxMenuImagePath + 'arrowd.png" border=0 class="arrow" align="absmiddle">' : '') +
			'</a>'
}
/* Position functions */
function getInnerLeft(el){
	if(el == null || ieBox && el == document.body || !ieBox && el == document.documentElement) return 0
	return getLeft(el) + getBorderLeft(el)
}
function getLeft(el){
	return el? el.offsetLeft + getInnerLeft(el.offsetParent) : 0
}
function getInnerTop(el){
	if(el == null || ieBox && el == document.body || !ieBox && el == document.documentElement) return 0
	return getTop(el) + getBorderTop(el)
}
function getTop(el){
	return el? el.offsetTop + getInnerTop(el.offsetParent) : 0
}
function getBorderLeft(el){
	return ie? el.clientLeft : parseInt(window.getComputedStyle(el, null).getPropertyValue("border-left-width"),10)
}
function getBorderTop(el){
	return ie? el.clientTop : parseInt(window.getComputedStyle(el, null).getPropertyValue("border-top-width"),10)
}
function opera_getLeft(el){
	return el? el.offsetLeft + opera_getLeft(el.offsetParent) : 0
}
function opera_getTop(el){
	return el? el.offsetTop + opera_getTop(el.offsetParent) : 0
}
function getOuterRect(el){
	return {
		left:	opera? opera_getLeft(el) : getLeft(el),
		top:	opera? opera_getTop(el) : getTop(el),
		width:	el.offsetWidth,
		height:	el.offsetHeight
	}
}
// mozilla bug! scrollbars not included in innerWidth/height
function getDocumentRect(el){
	return {
		left:	0,
		top:	0,
		width:	ie? (ieBox? document.body.clientWidth : document.documentElement.clientWidth) : window.innerWidth,
		height:	ie? (ieBox? document.body.clientHeight : document.documentElement.clientHeight) : window.innerHeight
	}
}
function getScrollPos(el){
	return {
		left:	ie? (ieBox? document.body.scrollLeft : document.documentElement.scrollLeft) : window.pageXOffset,
		top:	ie?	(ieBox? document.body.scrollTop : document.documentElement.scrollTop) :	window.pageYOffset
	}
}
/* end position functions */
WebFXMenu.prototype.position = function(relEl, sDir){
	var dir = sDir, piRect // find parent item rectangle, piRect
	if(!relEl){
		var pi = this.parentMenuItem
		if(!this.parentMenuItem) return
		relEl = _getElt(pi.id)
		if(!dir) dir = pi instanceof WebFXMenuButton? "vertical" : "horizontal"
		piRect = getOuterRect(relEl)
	} else if(relEl.left != null && relEl.top != null && relEl.width != null && relEl.height != null) piRect = relEl // got a rect
	else piRect = getOuterRect(relEl)
	var menuEl = _getElt(this.id)
	var menuRect = getOuterRect(menuEl)
	var docRect = getDocumentRect()
	var scrollPos = getScrollPos()
	var pMenu = this.parentMenu
	piRect.left += 5
	if(dir == "vertical"){
		if(piRect.left + menuRect.width - scrollPos.left <= docRect.width) this.left = piRect.left
		else if(docRect.width >= menuRect.width) this.left = docRect.width + scrollPos.left - menuRect.width
		else this.left = scrollPos.left
		if(piRect.top + piRect.height + menuRect.height <= docRect.height + scrollPos.top) this.top = piRect.top + piRect.height
		else if(piRect.top - menuRect.height >= scrollPos.top) this.top = piRect.top - menuRect.height
		else if(docRect.height >= menuRect.height) this.top = docRect.height + scrollPos.top - menuRect.height
		else this.top = scrollPos.top
	} else {
		if(piRect.top + menuRect.height - this.borderTop - this.paddingTop <= docRect.height + scrollPos.top) this.top = piRect.top - this.borderTop - this.paddingTop
		else if(piRect.top + piRect.height - menuRect.height + this.borderTop + this.paddingTop >= 0) this.top = piRect.top + piRect.height - menuRect.height + this.borderBottom + this.paddingBottom + this.shadowBottom
		else if(docRect.height >= menuRect.height) this.top = docRect.height + scrollPos.top - menuRect.height
		else this.top = scrollPos.top
		var pMenuPaddingLeft = pMenu? pMenu.paddingLeft : 0
		var pMenuBorderLeft = pMenu? pMenu.borderLeft : 0
		var pMenuPaddingRight = pMenu? pMenu.paddingRight : 0
		var pMenuBorderRight = pMenu? pMenu.borderRight : 0
		if(piRect.left + piRect.width + menuRect.width + pMenuPaddingRight + pMenuBorderRight - this.borderLeft + this.shadowRight <= docRect.width + scrollPos.left) this.left = piRect.left + piRect.width + pMenuPaddingRight + pMenuBorderRight - this.borderLeft
		else if(piRect.left - menuRect.width - pMenuPaddingLeft - pMenuBorderLeft + this.borderRight + this.shadowRight >= 0) this.left = piRect.left - menuRect.width - pMenuPaddingLeft - pMenuBorderLeft + this.borderRight + this.shadowRight
		else if(docRect.width >= menuRect.width) this.left = docRect.width + scrollPos.left - menuRect.width
		else this.left = scrollPos.left
	}
}
function _getElt(id){return document.getElementById?document.getElementById(id):document.layers?document.layers[id]:document.all?document.all[id]:null}
function _setSelect(mode){for(var i=0,j,ptr;i<document.forms.length;i++)for(j=0,ptr=document.forms[i].elements;j<ptr.length;j++)if(ptr[j].type.substr(0,6)=='select')ptr[j].style.visibility=mode}