/**
 *
 * Can show a pu_magic_tooltip over an element
 * Content of pu_magic_tooltip is the title attribute value of the element
 * Tested with Firefox, IE6, IE5.5, IE7, Konqueror
 *
 * To use it :
 * 1.include this script on your page
 * 2.insert this element somewhere in your page
 *       <div id="pu_magic_tooltip"></div>
 * 3. style it in your CSS stylesheet (set color, background etc..). You must set
 *     this two style too :
 *     div#pu_magic_tooltip { position:absolute; visibility:hidden; ... }
 * 4.the end. test it ! :-)
 *
 * @version 1.1
 * @copyright 2004-2007 Laurent Jouanneau. 
 * @link http://ljouanneau.com/soft/javascript
 * @licence release under LGPL Licence
 */


// the pu_magic_tooltip object
var pu_magic_tooltip = {
    // setup properties of pu_magic_tooltip object
    id:"pu_magic_tooltip",
    offsetx : 10,
    offsety : 10,
    _x : 0,
    _y : 0,
    _pu_magic_tooltipElement:null,
    _saveonmouseover:null
}

/**
* Open pu_magic_tooltip. The title attribute of the htmlelement is the text of the pu_magic_tooltip
* Call this method on the mouseover event on your htmlelement
* ex :  <div id="myHtmlElement" onmouseover="pu_magic_tooltip.show(this)"...></div>
*/
pu_magic_tooltip.show = function (htmlelement) {

    // we save text of title attribute to avoid the showing of pu_magic_tooltip generated by browser
    var text=htmlelement.getAttribute("title");
    htmlelement.setAttribute("title","");
    htmlelement.setAttribute("title_saved",text);

	if(document.getElementById){
        this._pu_magic_tooltipElement = document.getElementById(this.id);
	}else if ( document.all ) {
        this._pu_magic_tooltipElement = document.all[this.id].style;
	}

    this._saveonmouseover = document.onmousemove;
    document.onmousemove = this.mouseMove;

    this._pu_magic_tooltipElement.innerHTML = text;

    this.moveTo(this._x + this.offsetx , this._y + this.offsety);

    if(this._pu_magic_tooltipElement.style){
        this._pu_magic_tooltipElement.style.visibility ="visible";
    }else{
        this._pu_magic_tooltipElement.visibility = "visible";
    }
   return false;
}

/**
* hide pu_magic_tooltip
* call this method on the mouseout event of the html element
* ex : <div id="myHtmlElement" ... onmouseout="pu_magic_tooltip.hide(this)"></div>
*/
pu_magic_tooltip.hide = function (htmlelement) {
    htmlelement.setAttribute("title",htmlelement.getAttribute("title_saved"));
    htmlelement.removeAttribute("title_saved");

    if(this._pu_magic_tooltipElement.style){
        this._pu_magic_tooltipElement.style.visibility ="hidden";
    }else{
        this._pu_magic_tooltipElement.visibility = "hidden";
    }
    document.onmousemove=this._saveonmouseover;
}



// Moves the pu_magic_tooltip element
pu_magic_tooltip.mouseMove = function (e) {
   // we don't use "this" because this method is assign to an event of document
   // and so is dereferenced
    if(e == undefined)
        e = event;

    if( e.pageX != undefined){ // gecko, konqueror,
        pu_magic_tooltip._x = e.pageX;
        pu_magic_tooltip._y = e.pageY;
    }else if(event != undefined && event.x != undefined && event.clientX == undefined){ // ie4 ?
        pu_magic_tooltip._x = event.x;
        pu_magic_tooltip._y = event.y;
    }else if(e.clientX != undefined ){ // IE6,  IE7, IE5.5
        if(document.documentElement){
            pu_magic_tooltip._x = e.clientX + ( document.documentElement.scrollLeft || document.body.scrollLeft);
            pu_magic_tooltip._y = e.clientY + ( document.documentElement.scrollTop || document.body.scrollTop);
        }else{
            pu_magic_tooltip._x = e.clientX + document.body.scrollLeft;
            pu_magic_tooltip._y = e.clientY + document.body.scrollTop;
        }
    /*}else if(event != undefined && event.x != undefined){ // IE6,  IE7, IE5.5
        pu_magic_tooltip.x = event.x + ( document.documentElement.scrollLeft || document.body.scrollLeft);
        pu_magic_tooltip.y = event.y + ( document.documentElement.scrollTop || document.body.scrollTop);
    */
    }else{
        pu_magic_tooltip._x = 0;
        pu_magic_tooltip._y = 0;
    }
    pu_magic_tooltip.moveTo( pu_magic_tooltip._x +pu_magic_tooltip.offsetx , pu_magic_tooltip._y + pu_magic_tooltip.offsety);

}

// Move the pu_magic_tooltip element
pu_magic_tooltip.moveTo = function (xL,yL) {
    if(this._pu_magic_tooltipElement.style){
        this._pu_magic_tooltipElement.style.left = xL +"px";
        this._pu_magic_tooltipElement.style.top = yL +"px";
    }else{
        this._pu_magic_tooltipElement.left = xL;
        this._pu_magic_tooltipElement.top = yL;
    }
}

