//
//  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
//
cToolTip = {
	offsetX			: 20,
	offsetY			: 3,
	nTimeout 		: 1000,
	bShowDelayed	: true,
	nSteps			: 5,
	nSpeed			: 35
}

var hTarget = null
var hToolTop = null

cToolTip.showTip = function( nLeft, nTop, hTarget, sText )
{
	if( typeof sText == "undefined" )
	{
		sText = hTarget.getAttribute( "tooltip" )
		if( sText )
		{
			if( ( typeof sText == "undefined" ) || ( sText.length = 0 ) ) return
			if( sText.substr( 0, 5 ) == "data:" )
			{
				sText = eval( sText.substr( 5 ) )
			}
		}
		else
		{
			return
		}
	}
	
	if( hTarget.hToolTip )
	{
		this.hideTip( hTarget )
		return
	}
	
	hTarget.bStay = false
	
	hToolTip = document.createElement( "DIV" )
	hToolTip.className = "motooltip"
	
	if( this.bShowDelayed )
	{
		hToolTip.style.visibility = "hidden"
	}

	document.body.appendChild( hToolTip )
	hToolTip.innerHTML = sText

	hTarget.hToolTip = hToolTip
	
	hToolTip.onmousedown = function()
	{
		hTarget._onblur = hTarget.onblur
		hTarget.onblur = null
		hTarget.bStay = true
	}

	hToolTip.onmouseup = function()
	{
		hTarget.onblur = hTarget._onblur
		hTarget._onblur = null
		hTarget.bStay = false
		hTarget.focus()
	}
	
	hTarget.onblur = function()
	{
		if( ( hTarget.bStay ) || ( typeof hTarget.hToolTip == "undefined" ) ) return
		document.body.removeChild( hTarget.hToolTip )
		hTarget.hToolTip = null
		hTarget.onblur = null
		hTarget.onmouseout = null
	}

	/* just a quick hack better use cMoDomEvent */
	function mo()
	{
		hTarget.onmousemove = new Function('')
	}
	
	if( document.attachEvent ) 
	{
		hTarget.attachEvent( 'onmouseout', mo )
	}
	else if( document.addEventListener )
	{
		hTarget.addEventListener( 'mouseout', mo, false )
	}
	else if( document.all )
	{
		hTarget.onmouseout = mo
	}
	
	hTarget.onmousemove = function(hEvent)
	{
		if( hEvent == null ) hEvent = window.event
		if( ( hTarget.bStay ) || ( typeof hTarget.hToolTip == "undefined" ) ) return

		nLeft = hEvent.clientX
		nTop = hEvent.clientY
		cToolTip.setPos( hTarget, nLeft, nTop )
	}
	
	//correct position
    cToolTip.setPos( hTarget, nLeft, nTop )
	
	hToolTip.style.visibility = "visible"

	if( this.bShowDelayed )
	{
		this.showDelayed( 1 )
	}
}

cToolTip.setPos = function( hTarget, nLeft, nTop )
{
	var nDocWidth = document.width ? document.width : document.documentElement.offsetWidth - 25	
	var nDocHeight = document.height ? document.height : document.documentElement.offsetHeight	
	var aDocScroll = getScroll()
	
	if( nLeft > nDocWidth - hTarget.hToolTip.offsetWidth )
	{
		nLeft = nLeft - hTarget.hToolTip.offsetWidth - cToolTip.offsetX + aDocScroll.x + "px"
	}
	else
	{
		nLeft = nLeft + cToolTip.offsetX + aDocScroll.x + "px"
	}
    
	if( nTop > nDocHeight - hTarget.hToolTip.offsetHeight )
	{
		nTop = nTop - hTarget.hToolTip.offsetHeight - cToolTip.offsetY + aDocScroll.y + "px"
	}
	else
	{
		nTop = nTop + cToolTip.offsetY + aDocScroll.y + "px"
	}

	//nTop = nTop + cToolTip.offsetY + aDocScroll.y + "px"

	hTarget.hToolTip.style.left = nLeft
	hTarget.hToolTip.style.top = nTop
}

cToolTip.hideTip = function( hTarget )
{
	if( hTarget.hToolTip != null )
	{
		document.body.removeChild( hTarget.hToolTip )
		hTarget.hToolTip = null
		hTarget.onblur = null
		hToolTip = null
	}
}

cToolTip.getTarget = function( hEvent )
{
	hElm =  ( hEvent.srcElement ) ? hEvent.srcElement : hEvent.originalTarget
	if( typeof hElm.tagName == "undefined" ) return null
	while( ( hElm.tagName ) && !( /body/i.test( hElm.tagName ) ) )
	{
		if ( hElm.getAttribute( 'tooltip' ) != null )
		{
			return hElm
		}
		
		hElm = hElm.parentNode
	}
	return null
}

cToolTip.showDelayed = function( nStep )
{
	if( hToolTip == null ||  nStep>this.nSteps ) return
	if( ( bw.ie55 || bw.ie6 ) && ( hToolTip.filters == null || hToolTip.filters.alpha == null ) ) return
	nOpacity = nStep*1/this.nSteps
	nOpacity = Math.floor( nOpacity * 100 )
	cToolTip.setTipOpacity(nOpacity)
	nStep++
	this.hDelayedTimeout = setTimeout( 'cToolTip.showDelayed('+nStep+')' , this.nSpeed )
}

cToolTip.setTipOpacity = function( nValue )
{
	if( hToolTip == null ) return
	if( bw.ie )
	{
		hToolTip.filters.alpha.opacity = nValue
	}
	else if( bw.ns5 )
	{
		hToolTip.style.MozOpacity = nValue+'%'
	}
}

cToolTip.show = function( hEvent, sText )
{
	if( !hEvent )
	{
		hEvent = window.event
		hEvent.returnValue = true
		hEvent.cancelBubble = false
	}
	hTarget = cToolTip.getTarget( hEvent )
	
	if( !hTarget )
	{
		return
	}
	else
	{
		cToolTip.showTip( hEvent.clientX, hEvent.clientY, hTarget, sText )
	}
}

cToolTip.hide = function( hEvent )
{
	if( !hEvent )
	{
		hEvent = window.event
		hEvent.returnValue = true
		hEvent.cancelBubble = false
	}
	hTarget = cToolTip.getTarget( hEvent )
	
	if( this.bShowDelayed )
	{
		clearTimeout( this.hDelayedTimeout )
	}

	if( !hTarget )
	{
		return
	}
	else
	{
		cToolTip.hideTip( hTarget )
	}
}


cToolTip.initAutoTips = function( )
{
	if( document.attachEvent ) 
	{
		document.attachEvent( 'onmouseover', cToolTip.show )
		document.attachEvent( 'onmouseout', cToolTip.hide )
	}
	else if( document.addEventListener )
	{
		document.addEventListener( 'mouseover', cToolTip.show, true )
		document.addEventListener( 'mouseout', cToolTip.hide, false )
	}
	else if( document.all )
	{
		document.onmouseover = cToolTip.show
		document.onmouseout = cToolTip.hide
	}
}

cToolTip.init = function()
{
	this.bShowDelayed = bw.ie55 || bw.ie6 || bw.ns5
}

cToolTip.init()

//helper functions
function getScroll()
{
	if ( typeof document.body.scrollTop != "undefined") 
	{
		var ieBox = document.compatMode != "CSS1Compat"
		var cont = ieBox ? document.body : document.documentElement
		return {x : cont.scrollLeft, y : cont.scrollTop}
	}
	else 
	{
		return {x : window.pageXOffset, y : window.pageYOffset}
	}
}
