/*
	Tooltip klasse für Javascript
	Version 1.04
	16.09.2007
	
	ChangeLog:
	1.01a -> 1.02a:
		- setCoords angepasst auf dynamische Richtungsveränderung des Tooltips wenn zuwenig Platz vorhanden ist
	1.02a -> 1.04:
	  - alle Methoden auf Prototype's bind AsEventListener umgestellt
	  - zusätzliche Methode _show eingefügt
	  - sucht von allen Elementen nun die Attribute tooltip heraus
	
	Bruno
*/
function Tooltip(width) {
	var self = this;
	this.tooltipobj = null;
	this.state = false;
	this.x = 0;
	this.y = 0;
	this.width = width;
	
	Event.observe( window, "load", this.init.bindAsEventListener(this) );
}

/* zwecks kompatibilität */
Tooltip.prototype.show = function(msg,evt) {
	if ( this.tooltipobj == null ) return;
	var self = this;
	var element = Event.element(evt);
	Event.observe(element,"mouseout", this.hide.bindAsEventListener(this));
	
	this.setCoords(this.x,this.y);
	this.tooltipobj.innerHTML = msg;
	this.tooltipobj.style.display = "block";
	this.state = true;
}

Tooltip.prototype._show = function(evt,obj) {
  var Message = Event.element(evt);
  Message = Element.readAttribute(Message, "tooltip");
  obj.show(Message,evt);
}

Tooltip.prototype.hide = function(evt) {
	Event.stopObserving( Event.element(evt), "mouseout", this.hide.bindAsEventListener(this) );
	this.state = false;
	this.tooltipobj.style.display = "none";
}

Tooltip.prototype.setCoords = function(x, y) {
	var dimensions = Element.getDimensions(this.tooltipobj);

	var screenWidth,screenHeight;
	if (window.innerHeight) { // all escreenWidthcept EscreenWidthplorer
		screenWidth = window.innerWidth;
		screenHeight = window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) { // EscreenWidthplorer 6 Strict Mode
		screenWidth = document.documentElement.clientWidth;
		screenHeight = document.documentElement.clientHeight;
	}
	else if (document.body) { // other EscreenWidthplorers
		screenWidth = document.body.clientWidth;
		screenHeight = document.body.clientHeight;
	}
	
	if ( (x + dimensions.width) > (screenWidth) ) {
		x -= dimensions.width + 12;
	} else {
		x += 12;
	}
	
	if ( (y + dimensions.height) > (screenHeight) ) {
		y -= dimensions.height;
	} 
	
	this.tooltipobj.style.top = y + "px";
	this.tooltipobj.style.left = x + "px";
}

Tooltip.prototype.mousemove = function(evt) {
	this.x = Event.pointerX(evt)
	this.y = Event.pointerY(evt);
	
	if ( this.state ) {
		this.setCoords( this.x, this.y );		
	}
}

Tooltip.prototype.init = function() {
	var self = this;
	this.tooltipobj = $("tooltip");
	if (!this.tooltipobj) {
    alert("Div#tooltip ist nicht vorhanden");
    return;
  }
	if ( this.width > 0 ) { this.tooltipobj.style.width = this.width + "px"; } else { this.tooltipobj.style.minWidth = "50px"; }
	
	var eleList = $$("*[tooltip]");
	for( i=0; i < eleList.length; i++ ) {
		Event.observe( eleList[i], "mouseover", this._show.bindAsEventListener(null,this));
	}
	Event.observe( document, "mousemove", this.mousemove.bindAsEventListener(this));
}
