//
//  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
//

cMoMenu = {
	xOffset	:	-10,
	yOffset	:	5,
	hideDelay :	1500,
	nSteps : 8,
	nSpeed : 30,
	scrolling :	false
}

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

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

	//find position of menu element
	nPosX = ( ( bw.ns4 ) ? hLink.x : getSize( 'offsetLeft', hLink ) ) + this.xOffset
	nPosY = ( ( bw.ns4 ) ? hLink.y : getSize( 'offsetTop', hLink ) ) + this.yOffset
	nPosY += ( ( bw.ns4 ) ? 0 : hLink.offsetHeight )

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

	//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( hMenu.hElement, 'mouseover', cMoMenu.stopHide, false )
	cDomEvent.addEvent( hMenu.hElement, 'mouseout', cMoMenu.startHide, false )
	
	window.moMenu = hMenu
}

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.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 = 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
function getMenuElement( 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 
}

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
	}
	
	this.getWidth = function() { return getWidth( this.hElement ) }
	this.getHeight = function() { return getHeight( this.hElement ) }
	this.getLeft = function() { return getLeft( this.hElement ) }
	this.getTop = function() { return getTop( this.hElement ) }
}

function getWidth( hElement )
{
	return  ( bw.ie4 ) ? hElement.pixelWidth : hElement.offsetWidth
}

function getHeight( hElement )
{
	return ( bw.ie4 ) ? hElement.pixelHeight : hElement.offsetHeight
}

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

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

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