/******************************************************************************
*
*  Copyright (c) 2009
*  cruiz10020@yahoo.com
*
*  tooltip.js
*
*  Description:
*    Dynamically renders tooltip div for the specified elements.
*
*  Structure:
*
*  Requires:
*    prototype.js - http://www.prototypejs.org/
*    JsHelper
*
*  Development History:
*    3-2-2009       - created
*
*******************************************************************************/

function ToolTip( element )
{
  // html elements
  this.mContainer = $( element );
  this.mToolTipContainer = null;
  
  // flags
  
  // constants
  this.CLASSNAME = 'tooltipContainer';
  this.TOOLTIP_ELEMENT_CLASSNAME = 'tooltip';
  
  if( ! this.mContainer )
  {
    this.mContainer = $( document.body );
  }
  
  
  
  this.init = function()
  {
    this.mToolTipContainer = $( document.createElement( 'div' ) );
    this.mToolTipContainer.addClassName( this.CLASSNAME );
    
    this.mToolTipContainer.style.position = 'absolute';
    this.mToolTipContainer.style.left = '0px';
    this.mToolTipContainer.style.top = '0px';
    this.mToolTipContainer.hide(); 
    
    document.body.appendChild( this.mToolTipContainer );
    
    var elements = this.mContainer.select( '.' + this.TOOLTIP_ELEMENT_CLASSNAME );
    for( var i = 0; i < elements.length; i++ )
    {
      if( elements[i] )
      {
        this.prepForTooltip( elements[i] );
      }
    }
  };
  
  this.prepForTooltip = function( element )
  {
    if( element && element.title )
    {
      
      element.ttText = element.title;
      element.title = '';
      
      var tt = this;
      element.onmouseover = function(e)
        {
          e = JsHelper.getEvent( e );
          var mousePosition = [ Event.pointerX( e ), Event.pointerY( e ) ];          
          tt.show( this, mousePosition );  
        };
      
      element.onmouseout = function()
        {
          tt.hide();
        };
    }
  };
  
  this.hide = function()
  {
    this.mToolTipContainer.hide();
  };
  
  this.show = function( element, offset )
  {
    this.mToolTipContainer.update( element.ttText );
    
    if( ! offset  )
    {
      offset = $(element).cumulativeOffset();
      offset[0] += Math.min( Math.round( element.getWidth() / 2 ), 20 );
      offset[1] += Math.min( Math.round( element.getHeight() + 5 ), 20 );
    }
    else
    {
      offset[0] += 10;
      offset[1] += 5; 
    }
    
    var windowSize = JsHelper.getWindowSize();
    if( ( offset[0] + this.mToolTipContainer.getWidth() ) >= windowSize[0] )
    {
      offset[0] =  windowSize[0] - ( this.mToolTipContainer.getWidth() + 5 );
    }
    
    if( ( offset[1] + this.mToolTipContainer.getHeight() ) >= windowSize[1] )
    {
      offset[1] -= Math.round( this.mToolTipContainer.getHeight() * 1.5 + 5 );
    }
    
    this.mToolTipContainer.style.left = ( offset[0]  ) + 'px';
    this.mToolTipContainer.style.top = ( offset[1] ) + 'px';
    
    this.mToolTipContainer.show();
  };
}
