//
//Tool tips for tables
//--------------------------------------------------------
//

//
//TOOLTIPS
//
var previousElement;
var timerElement;
var hoverTimerID;
var tipWin;
var tipText;

//Show Tool Tip
function stt( element )
{
    if ( top != window )
    {
        if ( top.sttIn )
		    top.sttIn( element, window );
		return;
	}
	else
	    sttIn( element, window );
}

function sttIn( element, win )
{
    htt();
    if ( element.parentNode.parentNode.nodeName == "TD" )
    {
        if ( element.offsetWidth <= element.parentNode.parentNode.offsetWidth )
            return;
    }
    else if ( element.nodeName == "SPAN" )
    {
       var sizer = document.getElementById('sizerDiv' );
       sizer.innerHTML = "<span>" + htmlSafe( element.innerHTML ) + "</span>";
       
       var offsetChange = 0;
       if ( win == undefined && element.id.indexOf( 'btnt' ) == 0 ) 
            offsetChange = 15;
       
       if ( element.offsetWidth >= ( sizer.childNodes[0].offsetWidth -  offsetChange ) )
            return;
    }
	
	tipWin			= win;
	timerElement	= element;
	
	if ( window.showModelessDialog )
	    tipText = timerElement.innerText;
	else
	    tipText = getText( timerElement );
	
	tipText         = htmlSafe( tipText );
	hoverTimerID	= window.setTimeout( 'startTableHover()', 700 );
}

//Show Text Tool Tip
function sttt( element, text )
{
	if ( top != window )
	{
	    if ( top.stttIn )
		    top.stttIn( element, window, text );
		return;
    }
	else
	    stttIn( element, window, text );
}

function stttIn( element, win, text )
{
    htt();

	tipWin			= win;
	timerElement	= element;
	tipText			= text;
	hoverTimerID	= window.setTimeout( 'startTableHover()', 1000 );
}

//Hide Tool Tip
function htt()
{
    if ( top != window )
    {
        if ( top.htt )
		    top.htt();
		return;
	}
	
	if ( hoverTimerID != null )
	{
		window.clearTimeout( hoverTimerID );
		hoverTimerID = null;
	}
}

var tableToolTipPopup;
			
//Begin showing the hovering tool tip table
function startTableHover()
{
	try
	{
		if ( previousElement != null && timerElement == previousElement )
			return;
		
		previousElement = timerElement;
		endTableHover();
	
		var posX = 0;
		var posY = 0;
		var walker = timerElement;
		
		do
		{
			posX += walker.offsetLeft;
			posY += walker.offsetTop;
			walker = walker.offsetParent;
		}
		while ( walker.nodeName != "BODY" )
		
		posX -= ( window.createPopup ? walker.scrollLeft : window.pageXOffset );
		posY -= ( window.createPopup ? walker.scrollTop : window.pageYOffset );
		
		var winC;
		
		if ( tipWin != null && tipWin != window )
		{
		    if ( tipWin.name.indexOf( "popup" ) == 0 )
		    {
			    var winC = popups[tipWin.name].winControl;

			    posX += winC.offsetLeft + 2;
			    posY += winC.offsetTop + 17;
		    }
		    else
		    {
			    posX += ( divSidebar == null ? 0 : divSidebarUn.offsetWidth );
			    posY += 90;
		    }
		}
		
		if ( window.createPopup ) //IE
	    {
	        if ( top == window )
		    {
		        posX    = posX + document.body.scrollLeft;
                posY    = posY + document.body.scrollTop;
            }
		}
		else //FF
		{
		    if ( top.document != walker.ownerDocument )
		    {
		        posX    = posX - walker.ownerDocument.body.scrollLeft;
                posY    = posY - walker.ownerDocument.body.scrollTop;
            }
		}
		
		if ( tableToolTipPopup == null )
		{
		    var style = "left:" + posX + "px;top:" + posY + "px";
        	
	        if ( window.createPopup ) //IE
		        tableToolTipPopup = document.createElement( "<div id=toolTip class=tooltipCell style=\"" + 
		            style + "\" onmouseout='endTableHover()'>" );
	        else //FF
	        {
		        tableToolTipPopup  = document.createElement( "div" );
		        tableToolTipPopup.setAttribute( "style", style );
		        tableToolTipPopup.setAttribute( "onmouseout", "endTableHover()" );
		        tableToolTipPopup.className    = "tooltipCell";
		        tableToolTipPopup.id           = "toolTip";
	        }
	        
	        tableToolTipPopup.onclick       = clTimer;
			tableToolTipPopup.ondblclick    = dclTimer;
			tableToolTipPopup.innerHTML     = tipText;
			
			var toolCenter = document.getElementById('toolcenter');
			if ( toolCenter != null && toolCenter != undefined )
	            document.getElementById('toolcenter').appendChild( tableToolTipPopup );
	        else
	            document.body.appendChild( tableToolTipPopup );
		} 
		else
		{
			tableToolTipPopup.style.left		= posX + "px";
			tableToolTipPopup.style.top			= posY + "px";
			tableToolTipPopup.innerHTML			= tipText;
			tableToolTipPopup.style.display		= "block";
		}
		
		var winWidth	= 0;
	    var winHeight	= 0;
    	
    	if ( top.document != walker.ownerDocument )
        {
	        var mainWin		= top.document.getElementById( 'sesMain' );
	        winWidth	    = mainWin.offsetWidth + ( top.divSidebar == null ? 0 : top.divSidebarUn.offsetWidth );
	        winHeight	    = mainWin.offsetHeight + 91;
	        
            var toolTip     = document.getElementById('toolTip');
            var overHang    = ( toolTip.offsetWidth + toolTip.offsetLeft ) - winWidth;
    	    
            if ( overHang > 0 )
                 toolTip.style.left = ( toolTip.offsetLeft - overHang ) + "px";
    	         
            overHang    = ( toolTip.offsetHeight + toolTip.offsetTop ) - winHeight;
    	    
    	    if ( overHang > 0 )
                 toolTip.style.top = ( toolTip.offsetTop - overHang ) + "px";
        }
	}
	catch ( err ) { return; } //if the page has changed
}

function clTimer( evt )
{
    if ( evt == undefined ) evt = event;
    tgt = getEventTarget( evt );
    
    try
	{
	    window.setTimeout( "delayedTimerClick();", 100 );
	}
	catch ( err ) { endTableHover(); return; } //if the page has changed
}

function delayedTimerClick()
{
    if ( timerElement == null || timerElement == undefined )
        return;
      
    clickElement( timerElement );
}

function dclTimer( evt )
{
    if ( evt == undefined ) evt = event;
    tgt = getEventTarget( evt );
	try
	{
		timerElement.fireEvent( 'ondblclick' );
	}
	catch ( err ) { endTableHover(); return; } //if the page has changed
}

//Stop hovering the tool tip table
function endTableHover()
{
	try
	{
		if ( tableToolTipPopup != null )
			tableToolTipPopup.style.display = "none";
		
		previousElement			= null;
	}
	catch ( err ) { return; } //if the page has changed
}

//
//Text Area Plus/Minus
//
var lastTA  = null;
var taPop   = null;
var taCLast = false;

function taF( evt )
{
    tgt = getEventTarget( evt );
    
    if ( tgt == null || tgt == undefined )
		return;
	
	taCLast = false;
	if ( taPop != null )
	    taPop.style.display		= "none";
	
	var posX    = 0;
	var posY    = 0;
	var walker  = tgt;
	lastTA      = tgt;
	var isRO    = ( window.createPopup ? lastTA.getAttribute( 'readonly' ) : 
	    lastTA.getAttribute( 'readonly' ) != null );
	
	if ( lastTA.getAttribute( 'origh' ) == undefined )
	    lastTA.setAttribute( 'origh', lastTA.offsetHeight )
	
	do
	{
		posX += walker.offsetLeft;
		posY += walker.offsetTop;
		walker = walker.offsetParent;
	}
	while ( walker.nodeName != "BODY" )
	
	posX -= ( window.createPopup ? walker.scrollLeft : window.pageXOffset );
	posY -= ( window.createPopup ? walker.scrollTop : window.pageYOffset );
	posX    = posX + document.body.scrollLeft;
    posY    = posY + document.body.scrollTop;
    
    if ( window.createPopup )
    {
        posX    = posX - 10;
        posY    = posY + 1;
    }
    else
        posX    = posX - 11;
	
	if ( taPop == null )
	{
	    var style = "left:" + posX + "px;top:" + posY + "px";
    	
        if ( window.createPopup ) //IE
	        taPop = document.createElement( "<div id=taPop class=" + ( isRO ? "taPMRO" : "taPM" ) +
	            " style=\"" + style + "\">" );
        else //FF
        {
	        taPop  = document.createElement( "div" );
	        taPop.setAttribute( "style", style );
	        taPop.className    = ( isRO ? "taPMRO" : "taPM" );
	        taPop.id           = "taPop";
        }
        
        taPop.innerHTML     = "<div class=taPMP onmousedown=\"taPMP();\">+</div><br/><div class=taPMM onmousedown=\"taPMM();\">-</div>";
		document.body.appendChild( taPop );
	} 
	else
	{
		taPop.style.left		= posX + "px";
		taPop.style.top			= posY + "px";
		taPop.style.display		= "block";
	}
	
	if ( taPop.offsetWidth > 12 )
	    taPop.style.left = ( posX - ( taPop.offsetWidth - 12 ) ) + "px" 
}

function taPMP()
{
    if ( lastTA == null )
        return;
    
    lastTA.style.height = ( lastTA.offsetHeight + 100 ) + "px";
    taCLast = true;
}

function taPMM()
{
   if ( lastTA == null )
        return;
   
   if ( lastTA.offsetHeight > 120 )
        lastTA.style.height = ( lastTA.offsetHeight - 100 ) + "px";
   taCLast = true;
}