/**
 * ############################################################
 * ##       Project:    ZZP Verzekeraar i.s.m.               ##
 * ##                   Kendall Mason                        ##
 * ##       File:       clientscript/tooltip.js              ##
 * ##       Date:       10-03-2008                           ##
 * ## ------------------------------------------------------ ##
 * ## ToolTip functies                                       ##
 * ## ------------------------------------------------------ ##
 * ############################################################
 * ############################################################
 * ##     Author:       Chris Blokland                       ##
 * ##     In opdracht:  Kendall Mason                        ##
 * ##     Copyright:    dtpXpress / SixSenses                ##
 * ############################################################         
 */
 

function ToggleMouseCapturing(do_capturing)
{
  
  if( do_capturing )
  {
    if (document.layers) { // Netscape
      document.captureEvents(Event.MOUSEMOVE);
      document.onmousemove = captureMousePosition;
    } else if (document.all) { // Internet Explorer
        document.onmousemove = captureMousePosition;
    } else if (document.getElementById) { // Netcsape 6
        document.onmousemove = captureMousePosition;
    }
  } else {
    if (document.layers) { // Netscape
      document.captureEvents(null);
      document.onmousemove = null
    } else if (document.all) { // Internet Explorer
        document.onmousemove = null;
    } else if (document.getElementById) { // Netcsape 6
        document.onmousemove = null;
    }
  }
}

ToggleMouseCapturing(true);

// Globals
xMousePos = 0; // horizontale positie op het scherm( links -> rechts )
yMousePos = 0; // Verticale positie van het scherm( boven -> onder )

var tooltips_enabled=true;
var cur_el          = null;
var stop_moving     = false;
var last_str        = '';
var fade_timer      = null;
var is_in_option    = false;
var ignore_doc_click= false;

function ToggleToolTips( state )
{
  tooltips_enabled=state;  
  
  if( tooltips_enabled == false )
  {
    hideToolTip();
  }
}

document.body.onclick = function(e)
{
  
  if( !ignore_doc_click )
  {
    if( cur_el && !cur_el.options )
    {
      
      hideToolTip();
    }
  }
  ignore_doc_click = false;
}


 function set_tooltip( el, str )
 {
   var tooltip  = getToolTipContainer(); 
   
   //setDebug( "Str: "+str );
   
   if( last_str == str )
   {
     stop_moving = false;
     clearInterval(fade_timer);
     return;
   }
   last_str = str;
   
   if( tooltips_enabled )
   {
     // Fader aanmaken indien nog niet bestaat
     var fader = getFader();      
   
     if( !tooltip )
     {
       alert( 'De tooltip div is niet in het document aanwezig..' );
       return false;
     
     } else {
       
       var tooltip_content   = document.getElementById( "tooltip_content" );
        
       cur_el                = el;
       tooltip_content.innerHTML     = "<div class=\"tooltip_content\">"+str+"</div>";
       
       if( cur_el && cur_el.options )
       {
         for( var i=0; i<cur_el.options.length; i++ )
         {
           cur_el.options[i].onmouseover = function(e)
           {
             
             clearInterval(fade_timer);             
             stop_moving  = true;
             
             is_in_option = true;
           }
           
           cur_el.options[i].onclick = function(e)
           {

             stop_moving = true;
             
             hideToolTip();
           }
           
           cur_el.options[i].onmouseout = function(e)
           {
             stop_moving = false;
             //is_in_option = false;
           }
         }
       } else {
         
         cur_el.onclick = function(e)
         {
           if( this.getAttribute( "type" ) == 'checkbox' || this.getAttribute( "type" ) == 'radio' )
           {
             hideToolTip();
           } else {
            ignore_doc_click = true;
           }
         }
       }

       cur_el.onmouseout = function(e)
       {
       
           // Timer instellen die um doet verdwijnen, als er in de tussentijd een variable stop_fadeout op true word gezet
           // dan verdwijnt die alsnog niet, deze word gezet op een option element mouseover van deze select
                                         
           fade_timer = window.setInterval( "hideToolTip();", 800 );
           stop_moving = true;                
           
           //setDebug( "onMouseOut fired<br />FadeTimer: "+fade_timer );          

       }

       stop_moving = false;
       displayToolTip();              
       updatePos(true);
       clearInterval(fade_timer);         
       
       tooltip.onmouseover = function( e )
       {
         clearInterval(fade_timer);         
       }
       
       tooltip.onmouseout = function( e )
       {
         fade_timer = window.setInterval( "hideToolTip();", 800 );
       }
       
       tooltip.onclick = function(e) 
       {
        ignore_doc_click = true;       
       }
     }
   }
 } // end function set_tooltip
 
 function displayToolTip()
 {
   
   var fader = getFader();  
   
   // oppacity
   
   ToggleMouseCapturing(true);
   fader.setMode( "fadein" );
   fader.doFade();      
 }
 
 function hideToolTip()
 {    
   clearInterval( fade_timer );
   
   var fader = getFader();
	   
   fader.setMode( "fadeout" );           
      
   //tooltip.style.display = 'none';
   fader.doFade();
   
   window.setTimeout( "is_in_option = false;", 800 );
   
   //setDebug( "hideToolTip fired" );    
   cur_el = null;
   last_str = '';  
   
 }
 
 function getToolTipContainer()
 {
   // ToolTip div aanmaken
   var tooltip_container   = document.getElementById( 'tooltip' );
    
   if( !tooltip_container )
   {
      return false;
   } else {
     return tooltip_container;
   }
      
 }
var show_debug=true;
var mouse_debug = 'MouseX(Left): '+xMousePos+'<br />\nMouseY(Top): '+yMousePos;

function captureMousePosition(e) 
{
   var tooltip = getToolTipContainer();
    
    
   if (document.layers) 
   {        
       xMousePos = e.pageX;
       yMousePos = e.pageY;        
   } else if (document.all) {        
       xMousePos = window.event.x+document.body.scrollLeft;
       yMousePos = window.event.y+document.body.scrollTop;        
   } else if (document.getElementById) {
       xMousePos = e.pageX;
       yMousePos = e.pageY;        
   }
    
   
   updatePos(false);
}

function updatePos(force_repositioning)
{
  
  if( stop_moving == false )
  {
    var tooltip        = getToolTipContainer();
    var tooltip_height = tooltip.offsetHeight;
    
    if( tooltip )
    {
      if( force_repositioning || ( cur_el && !cur_el.options ) ) 
      {
        
        if( !is_in_option )
        {       
          
          var iLeft = xMousePos;       
          var iTop  = (yMousePos-(tooltip_height+25)); 
          
          if( is_ie )
          {            
            if (document.documentElement && document.documentElement.scrollTop)
            {
              iTop    += document.documentElement.scrollTop;
            } else if (document.body && document.body.scrollTop) {
              iTop    += document.body.scrollTop;
            }
          }
          
          tooltip.style.left = iLeft+"px";
          tooltip.style.top  = iTop+"px"
          
        }
      }
    }
  }
}

function findPos(obj) 
{
  if( typeof( obj ) == 'string' )
  {
    obj     = document.getElementById(obj);
    
  }
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}	
	return [curleft,curtop];
}


function getFader()
{
  return AddFader( "tooltip_fader", getToolTipContainer() );
}