/*
  Tooltip par Johann Pardanaud (Nesquik69).
  Site du créateur : http://www.plune.fr

  Licence :
    Ce code est distribué sous les termes de la licence LGPL (http://www.gnu.org/licenses/lgpl.html).
*/

var Tooltip = {
  noNameFunctions: new Array(),
  topFromCursor: 10,
  leftFromCursor: 20
};

Tooltip.init = function(){
  var objreference = this;
  var target = document.getElementById('contenu').getElementsByTagName('div');
  var targetNbr = target.length;
  
  for(var i=0 ; i < targetNbr ; i++) {
    if(target[i].className == 'icone') {
      target[i].lastChild.style.display = 'none';
      this.addEvent(target[i], 'mouseover',
        (function(target) {
          return function(e){
            objreference.show(target,e);
          };
        })(target[i])
      );
    }
  }
};

Tooltip.show = function (target, e) {
  var objreference = this;
  var clone = target.lastChild.cloneNode(true);
  var infos = document.body.appendChild(clone);
  
  infos.style.margin = '0px'; // Pas le choix, sinon on peut avoir des bugs.
  infos.style.display = 'block';
  infos.style.position = 'absolute';
  
  this.update(infos, e);
  
  this.addEvent(target, 'mouseout', function(){objreference.hide(target, infos);}, true);
  this.addEvent(target, 'mousemove', function(e){objreference.update(infos, e);}, true);
};

Tooltip.update = function (infos, e) {
  var windowDimensions = this.getWindowDimensions();
  var scroll = this.getScroll();
  
  if(windowDimensions.width > (infos.offsetWidth + this.leftFromCursor + e.clientX + scroll.left)) {
    infos.style.left = e.clientX + this.leftFromCursor + scroll.left + 'px';
  } else {
    infos.style.left = e.clientX + scroll.left - (infos.offsetWidth + this.leftFromCursor) + 'px';
  }
  
  if(windowDimensions.height > (infos.offsetHeight + this.topFromCursor + e.clientY + scroll.top)) {
    infos.style.top = e.clientY + this.topFromCursor + scroll.top + 'px';
  } else {
    infos.style.top = e.clientY + scroll.top - (infos.offsetHeight + this.topFromCursor) + 'px';
  }
};

Tooltip.hide = function (target, infos) {
  this.removeEvent(target, 'mouseout', this.noNameFunctions[0]);
  this.removeEvent(target, 'mousemove', this.noNameFunctions[1]);
  this.noNameFunctions = new Array();
  infos.parentNode.removeChild(infos);
};

Tooltip.addEvent = function (target, theEvent, theFunction, noNameFunction) {
  if(noNameFunction != undefined && noNameFunction == true) {
    this.noNameFunctions.push(theFunction);
  }
  
  if(target.addEventListener) {
    target.addEventListener(theEvent, theFunction, false);
  } else if(target.attachEvent) {
    target.attachEvent('on'+theEvent, theFunction);
  }
};

Tooltip.removeEvent = function (target, theEvent, theFunction) {
  if(target.removeEventListener) {
    target.removeEventListener(theEvent, theFunction, false);
  } else if(target.detachEvent) {
    target.detachEvent('on'+theEvent, theFunction);
  }
};

Tooltip.getWindowDimensions = function(){
  var scroll = this.getScroll();
  var substract = 20;
  
  if (window.innerHeight) {
    return {
      width:self.innerWidth + scroll.left - substract,
      height:self.innerHeight + scroll.top - substract
    };
  } else if (document.documentElement && document.documentElement.clientHeight) {
    return {
      width:document.documentElement.clientWidth + scroll.left - substract,
      height:document.documentElement.clientHeight + scroll.top - substract
    };
  } else {
    return {
      width:document.body.clientWidth + scroll.left - substract,
      height:document.body.clientHeight + scroll.top - substract
    };
  }
};

Tooltip.getScroll = function(){
  var xScroll, yScroll = 0;
  
  if(self.pageXOffset || self.pageYOffset) {
    xScroll = self.pageXOffset;
    yScroll = self.pageYOffset;
  } else if (document.documentElement || document.documentElement.scrollTop){
    xScroll = document.documentElement.scrollLeft;
    yScroll = document.documentElement.scrollTop;
  } else if (document.body) {
    xScroll = document.body.scrollLeft;
    yScroll = document.body.scrollTop;
  }
  
  return {left:xScroll, top:yScroll};
};