/* Original Javascript by Alessandro Fulciniti http://pro.html.it - http://web-graphics.com */
var theTooltipWidthes=Array();
var theTooltipXOffset=Array();
var theTooltipYOffset=Array();
var isIE=(navigator.appName.indexOf("Microsoft")!=-1);

function enableTooltips(tooltipClass,tooltipWidth,xoffset,yoffset){
  if (!theTooltipWidthes[tooltipClass])
    {
	theTooltipWidthes[tooltipClass] = (tooltipWidth+"px") || ("200px");
	theTooltipXOffset[tooltipClass] = xoffset;
	theTooltipYOffset[tooltipClass] = yoffset;

	var links,i,h;

	if(!document.getElementById || !document.getElementsByTagName) return;

        var h=document.getElementById("btc");
        if (!h)
          {
            h = document.createElement("div");
            h.id = "btc";
            h.setAttribute("id","btc");
            h.style.position = "absolute";
            h.style.zIndex= 35000;
            document.getElementsByTagName("body")[0].appendChild(h);
          }

	links = document.getElementsByTagName("a");
	for(i=0; i<links.length; i++) {
	  if($(links[i]).hasClassName(tooltipClass)) {
                  Prepare(links[i],tooltipClass);
		}
	}

	spans = document.getElementsByTagName("span");
	for(j=0; j<spans.length; j++) {
	  if($(spans[j]).hasClassName(tooltipClass)) {
                  Prepare(spans[j],tooltipClass);
		}
	}
    }
}

function Prepare(el,tooltipClass){

	var tooltip, tooltipTitle, tooltipLink, tooltipTop, tooltipTopLeft, tooltipTopRight, tooltipContent, tooltipBottom, tooltipBottomLeft, tooltipBottomRight;

/* Création de l'élément principal de l'infoBulle */
	tooltip = document.createElement("div");
	tooltip.className = "tooltip";
        //TEMPtooltip.style.width = theTooltipWidthes[tooltipClass];


/* Partie supérieure */
	tooltipTop = document.createElement("div");
	tooltipTop.className = "tooltipTop";

		/* Arrondi supérieur à gauche */
		tooltipTopLeft = document.createElement("div");
		tooltipTopLeft.className = "tootltipTopLeft";
		tooltipTop.appendChild(tooltipTopLeft);

		/* Arrondi supérieur à droite */
		tooltipTopRight = document.createElement("div");
		tooltipTopRight.className = "tootltipTopRight";
		tooltipTop.appendChild(tooltipTopRight);

/* Contenu texte du Tooltip */
	tooltipContent = document.createElement("div");
	tooltipContent.className = "tooltipContent";

        if (!el.nextSibling)
          {
            var a=12;
          }
        else if ($(el.nextSibling).hasClassName("tooltipTextContent"))
          {
            tooltipTextContent = el.nextSibling;
            tooltipTextContent.style.position = "static";
            tooltipContent.appendChild(tooltipTextContent);
          } 
        else 
          {
            tooltipTitle = el.getAttribute("title");
            if(tooltipTitle == null || tooltipTitle.length == 0)
              {
                return;
              }
            else
              {
                el.removeAttribute("title");
              }
            tooltipContent.appendChild(document.createTextNode(tooltipTitle));
          }

/* Partie inférieure */
	tooltipBottom = document.createElement("div");
	tooltipBottom.className = "tooltipBottom";

	/* Arrondi inférieur à gauche */
	tooltipBottomLeft = document.createElement("div");
	tooltipBottomLeft.className = "tooltipBottomLeft";
	tooltipBottom.appendChild(tooltipBottomLeft);

	/* Arrondi inférieur à droite */
	tooltipBottomRight = document.createElement("div");
	tooltipBottomRight.className = "tooltipBottomRight";
	tooltipBottom.appendChild(tooltipBottomRight);


/* Construction du tooltip */
	tooltip.appendChild(tooltipTop);
	tooltip.appendChild(tooltipContent);
	tooltip.appendChild(tooltipBottom);
	/*setOpacity(tooltip);*/
	el.tooltip = tooltip;
        el.tooltipClass=tooltipClass;
	el.onmouseover = showTooltip;
	el.onmouseout = hideTooltip;
	el.onmousemove = Locate;
}

function showTooltip(e) {
        Elem=document.getElementById("btc");
        Elem.tooltipClass=this.tooltipClass;
	Elem.appendChild(this.tooltip);
        Elem.style.top = "0px";
        Elem.style.left = "0px";
        var dimensions = $(Elem).getDimensions();
        if (isIE || dimensions.width<=0)
          this.tooltip.style.width = theTooltipWidthes[Elem.tooltipClass];
        else
          this.tooltip.style.width=(dimensions.width+10)+'px';

	Locate(e);
}

function hideTooltip(e) {
	var d = document.getElementById("btc");
	if(d.childNodes.length>0) {
		d.removeChild(d.firstChild);
	}
}

function setOpacity(el) {
	el.style.filter = "alpha(opacity:95)";
	el.style.KHTMLOpacity = "0.95";
	el.style.MozOpacity = "0.95";
	el.style.opacity = "0.95";
}

function Locate(e) 
{
  Elem=document.getElementById("btc");
  if (Elem && Elem.tooltipClass)
    {
      var posx=0, posy=0;
      if (e == null) 
        {
          e = window.event;		
        }
      if (e.pageX || e.pageY) 
        {
          posx = e.pageX;
          posy=e.pageY;
        }
      else if (e.clientX || e.clientY) 
        {
          if(document.documentElement.scrollTop)
            {
              posx = e.clientX+document.documentElement.scrollLeft;
              posy = e.clientY+document.documentElement.scrollTop;
            }
          else 
            {
              posx = e.clientX+document.body.scrollLeft;
              posy = e.clientY+document.body.scrollTop;
            }
        }
      /*
        var docWidth = 0; 
        var docHeight = 0; 
        //opera Netscape 6 Netscape 4x Mozilla 
        if (window.innerWidth || window.innerHeight)
        { 
        docWidth = window.innerWidth; 
        docHeight = window.innerHeight; 
        } 
        if (document.body.clientWidth || document.body.clientHeight)
        { 
        docWidth = document.body.clientWidth; 
        docHeight = document.body.clientHeight; 
        } 
      */
      var dimensions = $(Elem).getDimensions();
      var docViewPort=document.viewport.getDimensions();
      posx=posx-20+theTooltipXOffset[Elem.tooltipClass];
      posy=posy+10+theTooltipYOffset[Elem.tooltipClass];
      if (docViewPort.width>0 && posx+dimensions.width+10>docViewPort.width)
        posx=docViewPort.width-(dimensions.width+10);
      if (posx<0)
        posx=0;
      if (docViewPort.height>0 && posy+dimensions.height+10>docViewPort.height)
        posy=docViewPort.height-(dimensions.height+10);
      if (posy<0)
        posy=0;
      /*
        var w=parseInt(theTooltipWidthes[Elem.tooltipClass]);
        if (docWidth>0
        && posx+w+10>docWidth)
        posx=docWidth-(w+10);
        if (posx<0)
        posx=0;
      */
      
      Elem.style.top = posy + "px";
      Elem.style.left = posx + "px";
    }
}
