var Tooltip = Class.create();

Tooltip.prototype = {
    padding: 5,
	lastEffect: null,
	
    initialize: function() {    
        var objBody = $$('body')[0];

		objBody.appendChild(Builder.node('div',{id:'tooltip', style:'display: none;'}));
	},

	showTooltip: function(elem, content) {
		// Tooltip mit Inhalt füllen
		Element.update('tooltip', content);
		
		// Tooltip positionieren
		var tooltipLeft;
		var tooltipTop;
		var arrayTooltipSize = $('tooltip').getDimensions();
		var arrayElemOffset = Element.cumulativeOffset(elem);
		var arrayElemSize = Element.getDimensions(elem);
        var arrayPageSize = this.getPageSize();
		
		tooltipLeft = arrayElemOffset.left + (arrayElemSize.width - arrayTooltipSize.width) / 2;
		tooltipTop = arrayElemOffset.top - arrayTooltipSize.height - this.padding;
		
		tooltipLeft = Math.floor(tooltipLeft);
		tooltipTop = Math.floor(tooltipTop);

        $('tooltip').setStyle({ top: tooltipTop + 'px', left: tooltipLeft + 'px' });
		
		// Tooltip anzeigen
		if (this.lastEffect) this.lastEffect.cancel();
		this.lastEffect = Effect.Appear('tooltip', { duration: 0.3 });
		//Element.show('tooltip');
	}, 
	
	hideTooltip: function() {
		if (this.lastEffect) this.lastEffect.cancel();
		this.lastEffect = Effect.Fade('tooltip', { duration: 0.3 });
		//Element.hide('tooltip');
	},
	
	getScrollSize: function() {
	    var xScroll, yScroll;
		
		if (window.innerWidth && window.scrollMaxX) {	
			xScroll = window.innerWidth + window.scrollMaxX;
		} else if (document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
		}
		
		if (window.innerHeight && window.scrollMaxY) {	
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			yScroll = document.body.offsetHeight;
		}

		return [xScroll, yScroll];
	},

    getWindowSize: function() {
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		return [windowWidth, windowHeight];
	},

    getPageSize: function() {
		// get scroll size
		var scrollSize = this.getScrollSize();
		xScroll = scrollSize[0];
		yScroll = scrollSize[1];
		
		// get window size
		var windowSize = this.getWindowSize();
		windowWidth = windowSize[0];
		windowHeight = windowSize[1];
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;		
		} else {
			pageWidth = xScroll;
		}

		return [pageWidth,pageHeight];
	}
}

var tooltip;
document.observe('dom:loaded', function () { tooltip = new Tooltip(); });