//
//  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	:	0,
	hideDelay :	800,
	nSteps : 8,
	nSpeed : 30,
	scrolling :	false,
	sDefaultEvent : 'mouseover',
	sDefaultScrollDir : 'td' /*td=top to down, lr=left to right*/,
	sScrollDir : ''
}

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
		}
	}		
	
	var hMenuInner = new cDomObject( sMenuId )
	var bScrolling = cMoMenu.scrolling || ( hLink.getAttribute( 'menuscroll' ) == 'true' )
	if( bScrolling )
	{
		var sScrollDir = hLink.getAttribute( 'menuscrolldir' )
		if( sScrollDir != null && sScrollDir.toString().length > 0 )
		{
			if( sScrollDir != 'td' && sScrollDir != 'lr' )
			{
				sScrollDir = cMoMenu.sDefaultScrollDir
			}
		}
		else
		{
			sScrollDir = cMoMenu.sDefaultScrollDir
		}
		
		if( sScrollDir == 'td' )
		{
			hMenuInner.hStyle.left = 0 + 'px'
			hMenuInner.hStyle.top = - hMenuInner.getHeight()  + 'px'
		}
		else
		{
			hMenuInner.hStyle.left =  - hMenuInner.getWidth()  + 'px'
			hMenuInner.hStyle.top =  0 + '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 += cDomObject.getOffsetParam( hLink, 'offsetLeft' ) + this.xOffset
	nPosY += cDomObject.getOffsetParam( hLink, 'offsetTop' ) + this.yOffset
	
	if( bScrolling )
	{
		if( sScrollDir == 'td' )
		{
			nPosY += hLink.offsetHeight
		}
		else
		{
			nPosX += hLink.offsetWidth
		}
	}

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

	var hMenu = new cDomObject( sMenuId+'Container' )

	var sMenuDisplayPos = hLink.getAttribute( 'menuposition' )
	if( sMenuDisplayPos == 'right' )
	{
		nPosX += ( cDomObject.getWidth( hLink ) - hMenuInner.getWidth() )
	}
	
	
	nPosY += hLink.offsetHeight

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

	window.moMenu = hMenu
	cMoMenu.onShowMenu()

	//if it's a scrolling menu - start the scroll
	if( bScrolling && !hMenu.mover )
	{
		hMenu.mover = new cMover( hMenuInner.hElement )
		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
	}
	
	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 )
}

//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
		cMoMenu.onHideMenu()
		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 )
{
	cDomEvent.init( e )
	var hLink = cMoMenu.getMenuElement( cDomEvent.e )
	if( hLink != null )
	{
		//if( cDomEvent.e.type )
		var sMenu = hLink.getAttribute( 'menu' )
		var sEvent = hLink.getAttribute( 'menuevent' )

		if( sEvent == null || sEvent.length == 0 )
		{
			sEvent = cMoMenu.sDefaultEvent
		}

		if( sEvent == cDomEvent.type )
		{
			cMoMenu.showMenu( hLink, sMenu )
		}
	}
}

cMoMenu.install = function()
{
	cDomEvent.addEvent( document, 'mouseover', cMoMenu.doActivate, false )
	cDomEvent.addEvent( document, 'click', 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 
}