
/**
 * Classe de gestion de tooltip personnalisé.
 *
 * @author Lapoz - pierre.guillaume@e-lixir.fr
 * @link http://www.e-lixir.fr
 * @copyright Copyright e-Lixir (c)
 *
 * Le constructeur prend en argument optionnel l'ID de la zone qui sera créée.
 */
var Tooltip = Class.create({
	initialize: function(id, length) {
		if(!id)
			id = "tooltip";
		this.length = !length ? 25 : length;
		this.offsetx = 20;
		this.offsety = 15;
		this._x = 0;
		this._y = 0;
		this.e = new Element("div", {"id": id, "style": "position:absolute;display:none;z-index:900;"});
		$$("BODY")[0].insert(this.e);

		return true;
	},

	moveTo: function(xL,yL) {
		if(this.e.style) {
			this.e.style.left = xL +"px";
			this.e.style.top = yL +"px";
		}
		else {
			this.e.left = xL;
			this.e.top = yL;
		}
	},

	show: function(e) {
		var text = e.getAttribute("title");
		e.setAttribute("title", "");
		e.setAttribute("title_saved", text);
		if(text && text.length > this.length)
			text = text.substr(0, this.length) + " ...";
		Event.observe(document, "mousemove", this.mouseMove.bind(this));
		this.e.update(text);
		this.moveTo(this._x + this.offsetx , this._y + this.offsety);
		this.e.show();

		return false;
	},

	hide: function(e) {
		e.setAttribute("title", e.getAttribute("title_saved"));
		e.removeAttribute("title_saved");
		this.e.hide();
		Event.stopObserving(document, "mousemove", this.mouseMove);

		return false;
	},

	mouseMove: function(e) {
		if(e == undefined)
			e = event;
		if(e.pageX != undefined) {
			this._x = e.pageX;
			this._y = e.pageY;
		}
		else if(event != undefined && event.x != undefined && event.clientX == undefined) {
			this._x = event.x;
			this._y = event.y;
		}
		else if(e.clientX != undefined ) {
			if(document.documentElement) {
				this._x = e.clientX + ( document.documentElement.scrollLeft || document.body.scrollLeft);
				this._y = e.clientY + ( document.documentElement.scrollTop || document.body.scrollTop);
			}
			else {
				this._x = e.clientX + document.body.scrollLeft;
				this._y = e.clientY + document.body.scrollTop;
			}
		}
		else {
			this._x = 0;
			this._y = 0;
		}
		this.moveTo(this._x + this.offsetx , this._y + this.offsety);
	}
});

