//===============================================
// Tooltips script
//
// By Florian Arroseres
// florian.arroseres@gmail.com
// 08/06/2009
//
// Displays a tooltip from the text
// of title attribute
//===============================================

var ttips_object;

//===============================================
//
//===============================================
toolTips = function(){
  ttips_object = this;
  
  this.ttips_elements = new Array();
  this.ttips_element = '';
  this.ttips_title = '';
  this.ttips_mousex = '';
  this.ttips_mousey = '';

  //=============================================
  // Init function
  //=============================================
  this.init = function(){
    this.getElementsWithTitle();
    this.createToolTipsElements();

    for(var i = 0 ; i < this.ttips_elements.length ; i++){
      
      //=========================================
      // On mouse move event function
      //=========================================
      this.ttips_elements[i].onmousemove = function(event){
        ttips_object.setTooltipPosition(event);

        if(this.title != '')
          ttips_object.ttips_title = this.title;
        
        this.title = '';

        ttips_object.ttips_element.innerHTML = ttips_object.ttips_title;
        
        ttips_object.ttips_element.style.display = 'block';
        ttips_object.ttips_element.style.position = 'absolute';
        ttips_object.ttips_element.style.left = (ttips_object.ttips_mousex + 10) + 'px';
        ttips_object.ttips_element.style.top = (ttips_object.ttips_mousey + 15) + 'px';
      }

      //=========================================
      // On mouse out event function
      //=========================================
      this.ttips_elements[i].onmouseout = function(){
        ttips_object.ttips_element = document.getElementById('tooltip');
        ttips_object.ttips_element.style.display = 'none';
        this.title = ttips_object.ttips_title;
      }
    }
  };

  //=============================================
  // Get mouse left and top position
  //=============================================
  this.setTooltipPosition = function(event){
     var e = event || window.event;
     var scroll = new Array((document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft,(document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop);;

     this.ttips_mousex = e.clientX + scroll[0] - document.body.clientLeft;
     this.ttips_mousey = e.clientY + scroll[1] - document.body.clientTop;
  };

  //=============================================
  // Parse html and push elements with title
  // in an array
  //=============================================
  this.getElementsWithTitle = function(elem){
    var Elements = (typeof(elem) == 'undefined')? document.body.childNodes : elem.childNodes ;

    if(Elements == null) return false;
    
    for(var i=0 ; i < Elements.length ; i++){
      var o_element = Elements[i];
      
      this.getElementsWithTitle(o_element);
      
      if(o_element.title != '' && typeof(o_element.title) != 'undefined' )
        this.ttips_elements.push(o_element);
    }
  };

  //=============================================
  // Create html elements of tooltip
  //=============================================
  this.createToolTipsElements = function(){
    this.ttips_element = document.createElement('div');
    this.ttips_element.className = 'tooltip';
    this.ttips_element.id = 'tooltip';
    this.ttips_element.style.display = 'none';

    body = document.body;
    firstChild = body.firstChild;

    body.appendChild(this.ttips_element);
  };

  //=============================================
  //
  //=============================================
  this.init();
}