// -----------------------------------------------------------------------------------
//
//	Anatips v0.1
//	by Christophe Gimenez - http://www.anaema.com
//	Last Modification: 10/2/2008
//
//	Licensed under the Creative Commons Attribution 2.5 License 
//		- http://creativecommons.org/licenses/by/2.5/
//      - Free for use in both personal and commercial projects
//      - Attribution requires leaving author name, author link, and the license 
//		  info intact.
//	
// -----------------------------------------------------------------------------------
if(!ANAEMA) var ANAEMA = {};

ANAEMA.Anatips = Class.create({
	objTip:   undefined,
	tipsArray:  [],
	css		 : "",
	
	// Constructor runs on completion of the DOM loading. 
	initialize: function() {	
		this.tipsArray = [];		
		objBody = $$('body')[0];
		this.objTip = objBody.appendChild(Builder.node('div',{id:'tool_tip'}));
		this.setupTooltips();
		this.css = $('tool_tip').getStyle('max-width') ;
	},	
	
	// Scans the DOM to find all elements with an 'anatips' class attribute and 
	// bind them to the 3 events handlers
	setupTooltips: function() {
		me = this;
		cnt = 0;
		
		// alten Zustand ggf. wieder herstellen (title-Attribut initialisieren)
		$$('.tooltips').each(function(el){				
			if( el.readAttribute('tmp')) {
				el.writeAttribute( 'title', el.readAttribute('tmp')) ;
				el.removeAttribute('tmp');
			}
		})
		
		$$('.tooltips').each(function(el){				
			if(el.title && el.title.length > 0) {				
				el.observe('mouseover', (function(event) { event.stop; me.handleMouseOver(event) }).bind(this));
				el.observe('mousemove', (function(event) { event.stop; me.handleMouseMove(event) }).bind(this));
				el.observe('mouseout',  (function(event) { event.stop; me.handleMouseOut(event) }).bind(this));
				me.tipsArray[cnt] = el.title;
				el.tip_num = cnt++;
				
				// fuer IE und Firefox title-Attribut aendern
				el.writeAttribute( 'tmp', el.readAttribute('title')) ;
				el.removeAttribute('title');
			}
		})		
	},		
	
	// Called when mouse enters the target : setup and show container
	handleMouseOver: function(event){
		target = event.findElement('.tooltips');
		if (target) {
			this.objTip.innerHTML = this.tipsArray[target.tip_num];	
				
			// ggf. Tooltip verbreitern
			if( this.objTip.select('.situation') == "") {
				this.objTip.style.maxWidth = this.css ;				
			} else {
				this.objTip.style.maxWidth = "450px" ;
			}
			
			// Position berechnen
			this.handleMouseMove(event) ;
			
			// Tooltip anzeigen
			this.objTip.style.visibility = "visible";
						
			// Disable inner images's alt attribute (prevents IE auto alt 
			// tooltips)
			image = event.findElement('.tooltips img'); 
			if(image) image.alt = '';
		}
	},
	
	// Called when mouse moves over the target : move the tip container
	handleMouseMove: function(event){
		target = event.findElement('.tooltips');
		if (target) {
						
			// Tooltip positionieren (wenn Ende der Seite erreicht ist, wird 
			// der Tip nach oben anstatt nach unten positioniert)
	    	var posY   = 8 ;
	    	var posX   = 18 ;
			
			var hoehe  = this.objTip.offsetHeight ; 
			var breite = this.objTip.offsetWidth ; 			

			var rand   = $('inhalt').cumulativeOffset().top +
					     $('inhalt').getDimensions().height ;
			var seite  = $('container').cumulativeOffset().left +
					     $('container').getDimensions().width ;
						
			if( event.pointerY() + hoehe > rand) {
				posY = 0 - hoehe + posY ;
			}
			if( event.pointerX() + breite > seite) {
				posX = -26 - breite + posX ;
			}

			// Postion setzen
			this.objTip.style.left = (event.pointerX() + posX) + 'px';
			this.objTip.style.top  = (event.pointerY() + posY) + 'px'; 
		}
	},
	
	// Called when mouse leaves the target : hide the tip container
	handleMouseOut: function(event){
		this.objTip.style.visibility = "hidden";
	}
	
});

document.observe('dom:loaded', function () { ANAEMA.tips = new ANAEMA.Anatips(); });