/*
*	www.cosomedia.de 2009
*/

var ToolTipp = Class.create({

    initialize: function(options) {

		this.options = options;

		Event.observe(document, 'dom:loaded', function(){

			this.buildTooltip();
		}.bind(this));

    },

    buildTooltip: function(){

 		/*
 		*	Tooltipp Object erstellen
 		*/

		if(!$('tooltipp')){ // Wenn noch kein Tooltip gebaut wurde

	        var objBody = $$('body')[0];

			objBody.appendChild(Builder.node('div',{'class':'shadow left',id:'tooltipp'}, [
	            Builder.node('div',{'class':'inner', id:'tooltipp_content'})
	        ]));
 		}

 		this.tooltipp = $('tooltipp');

		 $("tooltipp_content").update("Laden...");

 		/*
 		*	Action erzeugen
 		*/

		$$(this.options.object).invoke('observe', 'mouseover', this.MouseOver.bindAsEventListener(this));
		$$(this.options.object).invoke('observe', 'mouseout',  this.MouseOut.bindAsEventListener(this));
		$$(this.options.object).invoke('observe', 'mousemove', this.MouseMove.bindAsEventListener(this));

    },

    MouseOver: function(e) {

		this.ChangeContent(e);
		this.tooltippAnimate(1);
    },

    MouseOut: function(e) {

		this.tooltippAnimate(0);
    },

    MouseMove: function(e) {

		if(this.options.track==true){ // Tooltipp der Mouse folgen
			this.tooltippMove(e);
		}
    },

    ChangeContent: function(e) {

		var el_start = e.element();
		var el = $(el_start).up(0)

		var suchstring = this.options.template.sub(/\{(.*?)\}/,function(match){

							var result = match[0];

							for(i=1;i<match.length;i++){

								result = result.sub("\{"+match[i]+"\}",$(el).readAttribute(match[i]));
							}

							return result;
						 });

		$("tooltipp_content").update(suchstring);
    },

	getPageSize: function() {

		windowWidth  = document.viewport.getDimensions().width;
		windowHeight = document.viewport.getDimensions().height;

		windowTop 	 = document.viewport.getScrollOffsets().top;
		windowLeft 	 = document.viewport.getScrollOffsets().left;

		return new Array(windowWidth,windowHeight,windowTop,windowLeft);
	},

    tooltippMove: function(e) {

		var MouseDifIE = Prototype.Browser.IE ? 4 : 0;

		var currentX = Event.pointerX(e)-MouseDifIE+5;
		var currentY = Event.pointerY(e)+5;

		var sizes 	= this.getPageSize();

		if((currentX+$(this.tooltipp).getWidth()) > (sizes[0]+sizes[3])){

			currentX = currentX-$(this.tooltipp).getWidth();
		}

		if((currentY+$(this.tooltipp).getHeight()) > (sizes[2]+sizes[1])){

			currentY = currentY-$(this.tooltipp).getHeight()-20;
		}

		$(this.tooltipp).setStyle({left:  currentX + "px", top: currentY + "px", "z-index" : "9999"});
	},

    tooltippAnimate: function(to) {

		if(to==0){

			$(this.tooltipp).hide();
		} else {

			$(this.tooltipp).show();
		}
    }
});

new ToolTipp({
        object:"a[text]", // Alle mit einem Attribut text
        delay: 0,
        track: true,
        duration: 0.4,
        template:"{text}"
});