//
//  This script was created
//  by Mircho Mirev
//  mo /mo@momche.net/
//
//	:: feel free to use it BUT
//	:: if you want to use this code PLEASE send me a note
//	:: and please keep this disclaimer intact
//

//additional info
//the activating link can have an attribute
//menu_callback
// which is a name of a function that will be called before the menu is shown
// it can test the cMoMenu.hLink var to check which is the link the Menu is activated for
// and must return an array with null or non null values
/*
 			return {
						menuId : sID, //id of the new
						xOffset : 10, // x offset
						yOffset : null // y offset
					}
*/

cMoMenu = {
	xOffset	:	0,
	yOffset	:	3,
	hideDelay :	1800,
	nSteps : 8,
	nSpeed : 30,
	scrolling :	false
}

cMoMenu.onHideMenu = function() {}
cMoMenu.onShowMenu = function() {}

cMoMenu.showMenu = function( hLink, sMenuId )
{
	cMoMenu.hLink = hLink
	if( hLink.menuon == true )
	{
		return
	}
	else
	{
		this.hideMenu()
	}

	var nPosX = 0
	var nPosY = 0

	var sCallback = hLink.getAttribute( 'menu_callback' )
	if( sCallback != null && sCallback.toString().length > 0 )
	{
		var hRes = eval( sCallback+'()' )
		if( hRes.menuId )
		{
			sMenuId = hRes.menuId
		}
		if( hRes.xOffset )
		{
			nPosX += hRes.xOffset
		}
		if( hRes.yOffset )
		{
			nPosY += hRes.yOffset
		}
	}

	hMenuInner = new getObject( sMenuId )
	bScrolling = cMoMenu.scrolling || ( hLink.getAttribute( 'menuscroll' ) == 'true' )
	if( bScrolling )
	{
		hMenuInner.hStyle.left = 0 + 'px'
		hMenuInner.hStyle.top = - hMenuInner.getHeight( )  + 'px'
	}
	else
	{
		hMenuInner.hStyle.left = 0 + 'px'
		hMenuInner.hStyle.top = 0 + 'px'
	}

	var hMenuDisplayItems = hLink.getAttribute( 'menu_displayitems' )
	if( hMenuDisplayItems != null && hMenuDisplayItems.toString().length > 0 )
	{
		sMenuDisplayItems = hMenuDisplayItems.toString()
		cMoMenu.filterItems( hMenuInner, sMenuDisplayItems )
	}


	//find position of menu element
	//first get offset attributes
	var nOX = 0
	var nOY = 0
	var snOX = hLink.getAttribute( 'menu_xoffset' )
	var snOY = hLink.getAttribute( 'menu_yoffset' )
	if( snOX != null && snOX.toString().length > 0 )
	{
		nOX = new Number( snOX.toString() )
		nPosX += nOX
	}
	if( snOY != null && snOY.toString().length > 0 )
	{
		nOY = new Number( snOY.toString() )
		nPosY += nOY
	}

	nPosX += ( ( bw.ns4 ) ? hLink.x : getObject.getSize( 'offsetLeft', hLink ) ) + this.xOffset
	nPosY += ( ( bw.ns4 ) ? hLink.y : getObject.getSize( 'offsetTop', hLink ) ) + this.yOffset
	nPosY += ( ( bw.ns4 ) ? 0 : hLink.offsetHeight )

	var sT = getObject.getScrollOffset( 'Top', hLink )
	nPosY -= sT
	var sL = getObject.getScrollOffset( 'Left', hLink )
	nPosX -= sL

	//or we can set exactly the position
	nOX = 0
	nOY = 0
	var snOX = hLink.getAttribute( 'menu_xpos' )
	var snOY = hLink.getAttribute( 'menu_ypos' )
	if( snOX != null && snOX.toString().length > 0 )
	{
		nOX = new Number( snOX.toString() )
		nPosX = nOX
	}
	if( snOY != null && snOY.toString().length > 0 )
	{
		nOY = new Number( snOY.toString() )
		nPosY = nOY
	}

	var hMenu = new getObject( sMenuId+'Container' )
	hMenu.hStyle.left = nPosX + 'px'
	hMenu.hStyle.top = nPosY + 'px'
	hMenu.hStyle.width = hMenuInner.getWidth( )
	hMenu.hStyle.height = hMenuInner.getHeight( )
	hMenu.hLink = hLink
	hMenu.hLink.menuon = true

	cMoMenu.onShowMenu()

	//if it's a scrolling menu - start the scroll
	if( bScrolling && !hMenu.mover )
	{
		hMenu.mover = new mover( hMenuInner )
		hMenu.mover.glideTo( 0, 0, cMoMenu.nSteps, cMoMenu.nSpeed )
	}

	//set a special class for the mouseover - emulate :hover
	sOC = hMenu.hLink.getAttribute( 'menuoverclass' )
	if( sOC )
	{
		hMenu.hLink.className = sOC
	}

	//capture events
	if( bw.ns4 )
	{
		hMenu.hElement.captureEvents( Event.MOUSEOUT || Event.MOUSEOVER )
	}

	cDomEvent.addEvent( hLink, 'mouseout', cMoMenu.startHide, false )
	cDomEvent.addEvent( hLink, 'mouseover', cMoMenu.stopHide, false )
	cDomEvent.addEvent( hMenu.hElement, 'mouseover', cMoMenu.stopHide, false )
	cDomEvent.addEvent( hMenu.hElement, 'mouseout', cMoMenu.startHide, false )

	window.moMenu = hMenu
	hMenu.hStyle.visibility = "visible"
}

//will only display menu items from the menu_displayitems node attribute list
//which in fact is a coma separated list of ids on A tags inside the menu
cMoMenu.filterItems = function( hMenuContainer, sList )
{
	var hMenuEl = null
	var hLinks = hMenuContainer.hElement.getElementsByTagName( 'a' )
	for( var nI = 0; nI < hLinks.length; nI++ )
	{
		hMenuEl = hLinks.item( nI )
		if( hMenuEl.id && sList.indexOf( hMenuEl.id ) >=0 )
		{
			hMenuEl.style.display = 'block'
		}
		else
		{
			hMenuEl.style.display = 'none'
		}
	}
}

cMoMenu.hideMenu = function()
{
	cMoMenu.stopHide()
	hMenu = window.moMenu
	if( hMenu != null )
	{
		if( hMenu.mover )
		{
			hMenu.mover.stop()
		}
		sOC = hMenu.hLink.getAttribute( 'menuoutclass' )
		if( sOC )
		{
			hMenu.hLink.className = sOC
		}
		cDomEvent.removeEvent( hMenu.hLink, 'mouseout', cMoMenu.startHide, false )
		hMenu.hStyle.visibility = "hidden"
		hMenu.hLink.menuon = false
		window.moMenu = null
		cMoMenu.onHideMenu()
	}
}

cMoMenu.startHide = function()
{
	if( cMoMenu.hideTimeout != null ) {	return; }
	cMoMenu.hideTimeout = setTimeout( "cMoMenu.hideMenu()", cMoMenu.hideDelay )
}

cMoMenu.stopHide = function()
{
	clearTimeout( cMoMenu.hideTimeout )
	cMoMenu.hideTimeout = null
}

cMoMenu.doActivate = function( e )
{
	hLink = cMoMenu.getMenuElement( e )
	if( hLink != null )
	{
		var sMenu = hLink.getAttribute( 'menu' )
		cMoMenu.showMenu( hLink, sMenu )
	}
}

cMoMenu.install = function()
{
	cDomEvent.addEvent( document, 'mouseover', cMoMenu.doActivate, false )
}

cMoMenu.install()

//helper functions

cMoMenu.getMenuElement = function( hEvent )
{
	if( hEvent == null )
	{
		hEvent = window.event
	}
	hElement = ( hEvent.srcElement ) ? hEvent.srcElement : hEvent.originalTarget

	if( hElement == null )
	{
		return null
	}
	try
	{
		if( typeof hElement.tagName == 'undefined' ) return null
	}
	catch( hException )
	{
		return null
	}
	while( ( hElement.tagName ) && !( /(body|html)/i.test( hElement.tagName ) ) )
	{
		//opera strangely returns non null result sometimes
		sAttr = hElement.getAttribute( 'menu' )
		if( sAttr != null && sAttr.toString().length > 0 )
		{
			return hElement
		}
		hElement = hElement.parentNode
	}
	return null
}


//misc objects
//a simple encapsulation object

function getObject( sId )
{
	if( bw.dom )
	{
		this.hElement = document.getElementById( sId )
		this.hStyle = this.hElement.style
	}
	else if( bw.ns4 )
	{
		this.hElement = document.layers[ sId ]
		this.hStyle = this.hElement
	}
	else if( bw.ie )
	{
		this.hElement = document.all[ sId ]
		this.hStyle = this.hElement.style
	}

}

getObject.prototype.getWidth = function( )
{
	return  ( bw.ie4 ) ? this.hElement.pixelWidth : this.hElement.offsetWidth
}

getObject.prototype.getHeight = function( )
{
	return ( bw.ie4 ) ? this.hElement.pixelHeight : this.hElement.offsetHeight
}

getObject.prototype.getLeft = function()
{
	if( bw.ie4 ) return this.hElement.style.pixelLeft
	if( bw.ns4 || bw.dom )
	{
		if( this.hElement.style.left.length == 0 )
		{
			return parseInt( this.hElement.style.offsetLeft )
		}
		else
		{
			return parseInt( this.hElement.style.left )
		}
	}
}

getObject.prototype.getTop = function( )
{
	if( bw.ie4 ) return this.hElement.style.pixelTop
	if( bw.ns4 || bw.dom )
	{
		if( this.hElement.style.top.length == 0 )
		{
			return parseInt( this.hElement.style.offsetTop )
		}
		else
		{
			return parseInt( this.hElement.style.top )
		}
	}
}

getObject.getSize = function( sParam, hLayer )
{
	nPos = 0
	while( hLayer != null )
	{
		nPos += eval( 'hLayer.' + sParam )
		hLayer = hLayer.offsetParent
	}
	return nPos
}

getObject.getScrollOffset = function( sParam, hLayer )
{
	nPos = 0
	while( hLayer != null && hLayer.tagName.toLowerCase() != 'body' )
	{
		nPos += eval( 'hLayer.scroll' + sParam )
		hLayer = hLayer.parentNode
	}
	return nPos
}

