/*
** DHTML Tool tip function.
**
** Written by Ralf Seidel.
*/ 

/**
** Timer id for delayed tool tip hiding.
*/
var ttHideTimerId  = 0;

/**
** Get the default width for the default layer.
**
** @return Golden cut of document width.
*/
function ttDefaultWidth()
{
    return document.body.clientWidth * 0.382;
}

/**
** Get the tooltip lay. Create it if it not already exists. 
*/
function ttGetToolTipLayer()
{
	if ( !document.getElementById ) 
		return;
		
	var tt = document.getElementById("Tooltip");
	if ( !tt ) {
		tt = document.getElementById("tooltip");
	}
	if ( !tt ) {
		tt = document.createElement( "div" );
		tt.id = "Tooltip";
		tt.className = "tooltip";
		if ( tt.addEventListener ) {
			// Firefox
			tt.addEventListener( "mouseover", ttClearHideTimer, false );
			tt.addEventListener( "mouseout", ttHideImmediatly, false );
		} else if ( tt.attachEvent ) {
			// IE
			tt.attachEvent( "onmouseover", ttClearHideTimer );
			tt.attachEvent( "onmouseout", ttHideImmediatly );
		} else {
			tt.onmouseover = function( e ) { ttClearHideTimer(); };
			tt.onmouseout  = function( e ) { ttHideImmediatly(); };
		}
		
		document.body.appendChild( tt );
		//alert( tt.parentNode.innerHTML );
	}
	return tt;
}

/**
** Show the tool tip layer.
**
** @param text The (HTML) text to display.
** @param x The x-coordinate of the reference element.
** @param y The y-coordinate of the reference element.
*/
function ttShow( text, x, y, cx ) 
{
	window.clearTimeout( ttHideTimerId );
	var tt = ttGetToolTipLayer();
	if ( tt && tt != null ) {
	    if ( !cx )
		    cx  = ttDefaultWidth();
		
		if ( text ) 
			tt.innerHTML  = text;
		if ( x ) 
			tt.style.left = x + "px";
		if ( y ) 
			tt.style.top  = y + "px";
		
		tt.style.width   = cx + "px";
		tt.style.display = "block";
	}
}

function ttClearHideTimer() 
{
	window.clearTimeout( ttHideTimerId );
}

/**
** Hide the tool tip layer.
**
** @param bNow If true the layer is made invisible immediatly. I
**        If false the function will initiate a timeout which will - when elapsed - 
**        call this function again with bNow set true. i.e. bNow == false is
**        will delay the action.
*/
function ttHide( bNow ) 
{
	if ( !document.getElementById ) 
		return;
		
	if ( bNow ) {
		ttHideImmediatly();
	} else {
		ttHideTimerId  = window.setTimeout( "ttHideImmediatly", 500 );
	}
}

function ttHideImmediatly()
{
	var tt = document.getElementById( "Tooltip" );
	if ( !tt ) {
		tt = document.getElementById( "tooltip" );
	}
	if ( tt ) {
		tt.style.display = "none";
	}
}


/**
** Show a foot note.
**
** @param refCtrl The HTML <a> element which is referencing the foot note.
** @param fnId The id of the referenced foot note HTML element (optional).
*/
function ttShowFootnote( refCtrl, fnId ) 
{
    // The foot note html element.
    var fn;  
	
	// If foot note element is not provided use the id of the referencing
	// element to figure out the referenced element.
	if ( !fnId ) {
	    if ( refCtrl && refCtrl.id ) {
		    fnId = refCtrl.id.replace( /ref/, '' );
	    } else if ( refCtrl && refCtrl.name ) {
		    fnId = refCtrl.name.replace( /ref/, '' );
	    }
	}
	
	if ( fnId && document.getElementById ) {
		fn = document.getElementById( fnId );
	}
	if ( fn ) {
		var x  = refCtrl.offsetLeft + 20;
		var y  = refCtrl.offsetTop;
		var cx = ttDefaultWidth();
		var parent = refCtrl.offsetParent;
	    var text;
		
		// figure out absolute position on page.
		while ( parent != null ) {
			x+= parent.offsetLeft;
			y+= parent.offsetTop;
			parent = parent.offsetParent;
		}
		
		if ( x + cx > document.body.clientWidth ) {
		    if ( x - cx - 30 >= 0 ) {
		        x = x - cx - 30;
		    } else {
		        x = document.body.clientWidth - cx;
		    }
        }
			
		// remove first anchor element in foot note text.
		text = fn.innerHTML.replace( /^<a[^<]+<\/a>/i, '' );
		
		ttShow( text, x, y  );
	}
}
