Element.implement({
    tooltip: function(message, options) { 
    	if (!$type(message))
    	    return false;

        if ($type(this.tipbody)== "element")
            this.tipbody.dispose();
		
		var options = $merge({
            eventStart: "mouseenter",
            eventEnd: "mouseleave",
            Offset: 20,
            opacity: 1,
			gravity: 'top'
        }, options);

        var coords = this.getCoordinates();
        this.tipbody = new Element("div", {
            styles: {
                position: "absolute",
                left: 0,
                top: -100,
                opacity: options.opacity
            },
			class: "tooltipS"
        }).adopt(new Element("div", {
            class: "tooltipS-inner",
            html: message.replace(/ /g, "&nbsp;")
        })).inject(document.body);
			
        this.addEvent(options.eventEnd, function() {
            if ($type(this.tipbody) == "element") {
                this.tipbody.dispose();
                this.tipbody = null;
                this.removeEvent(options.eventEnd);
            }
        }.bind(this));

        var t = this.tipbody.getSize();
		var actualWidth = this.tipbody.offsetWidth, actualHeight = this.tipbody.offsetHeight;
		scroll = window.getScroll();
		
		switch (options.gravity) {
			case 'top':
				this.tipbody.setStyles({left: coords.left + (coords.width / 2) - (actualWidth / 2),top: coords.top - actualHeight - options.Offset/2 + + scroll.y,width: t.x}).addClass('tooltipS-south');
				break;
			case 'bottom':
				this.tipbody.setStyles({left: coords.left + (coords.width / 2) - (actualWidth / 2),top: coords.top + coords.height + options.Offset,width: t.x}).addClass('tooltipS-north');
				break;
			case 'left':
				this.tipbody.setStyles({left: coords.left - actualWidth - options.Offset,top: coords.top + (coords.height / 2) - (actualHeight/2),width: t.x}).addClass('tooltipS-east');
				break;
			case 'right':
				this.tipbody.setStyles({left: coords.left  + coords.width + options.Offset,top: coords.top + (coords.height / 2) - (actualHeight/2),width: t.x}).addClass('tooltipS-west');
				break;
		}
		
        return this;
    }
});
