/**
 * Tooltip
 * Gamboa gamboa AT_ANTI_SPAM co.sapo.pt
 */


var Tooltip = Class.create();

Tooltip.prototype = {
    
    initialize: function(element) {
        /* vars */
        var options = Object.extend({
            show: false,            // id of tooltip div
            showWrite: false,       // id of tooltip div
            where: 'mouse',         // show option
            position: false,        // position defined
            textAttr: false,        // attribute with text
            delay: 0,               // time delay in seconds
            zindex: 3               // z-index
        }, arguments[1] || {});

        this.element =  $(element);
        this.show = $(options.show);
        //
        // set element to write text
        //
        if(options.showWrite == false) {
            this.showWrite = $(options.show);
        } else {
            this.showWrite = $(options.showWrite);
        }
        this.options = options;

        this.offSetX = 0;
        this.offSetY = 0;

        this.activeOver=false;
        this.activeOut=true;

        this.auxTimeout = false;
        this.tmpText = false;
        if(options.textAttr == false) {
            this.attr = 'title';
        } else {
            this.attr = options.textAttr;
        }

        this.eventMouseOver = this.onOver.bindAsEventListener(this);
        this.eventMouseOut = this.onOut.bindAsEventListener(this);
        this.eventMouseMove = this.setPosition.bindAsEventListener(this);

        Event.observe(this.element, 'mouseover', this.eventMouseOver);
    },
    
    setTopLeft: function (elm, left, top) {
        elm.style.left = left+'px';
        elm.style.top = top+'px';
    },
                
    setPosition: function(e) {
        var pointer = this.update(e);
        switch(this.options.where) {
            //
            // same mouse position (default)
            //
            case 'mouse':
            default:
                this.setTopLeft(this.show, (pointer[0] + 5), (pointer[1] + 5));
                break;

            //
            // position on right of the page, 
            // left -> defined by user or comulativeOffset + width of element
            // top -> top of comulativeOffset of element
            //
            case 'right':
                var pos = Position.cumulativeOffset(this.element);
                if(this.options.position != false) {
                    this.setTopLeft(this.show, this.options.position, pos[1]);
                } else {
                    this.setTopLeft(this.show, (pos[0] + parseInt(this.element.style.width)), pos[1]);
                }
                break;
            //
            // position on left of the page, 
            // left -> defined by user or comulativeOffset(element) - width of tooltip
            // top = top of comulativeOffset of element
            // 
            case 'left':
                var pos = Position.cumulativeOffset(this.element);
                if(this.options.position != false) {
                    this.setTopLeft(this.show, this.options.position, pos[1]);
                } else {
                    this.setTopLeft(this.show, (pos[0] - parseInt(this.show.style.width)), pos[1]);
                }
                break;
            //
            // personal position relative of element comulativeOffset
            // 
            case 'personal':
                var pos = Position.cumulativeOffset(this.element);
                if(this.options.position != false) {
                    this.setTopLeft(this.show, this.options.position[0], this.options.position[1]);
                }
                break;
        }
    },

    writeContent: function(e) {
        this.tmpText = this.element.getAttribute(this.attr);
        this.showWrite.innerHTML = this.tmpText;
        if(this.options.textAttr == false) {
            this.element[this.attr] = '';
        }
    },

    writeTitle: function(e) {
        if(this.options.textAttr == false) {
            this.element.title = this.tmpText;
        }
    },

    showElm: function(elm) {
        this.auxTimeout = setTimeout("Element.show('"+elm+"')", (this.options.delay * 1000));
    },
    
    hideElm: function(elm) {
        clearTimeout(this.auxTimeout);
        Element.hide(elm);
    },

    onOver: function(e) {
        this.show.style.zIndex=this.options.zindex;
        this.setPosition(e);
        this.writeContent(e);
        if(!this.activeOver && this.activeOut) {
            this.showElm(this.show.id);
            Event.stopObserving(this.element, "mouseover", this.eventMouseOver);
            Event.observe(this.element, 'mouseout', this.eventMouseOut);
            Event.observe(document, 'mousemove', this.eventMouseMove);
        }
        this.activeOut = false;
        this.activeOver = true;
    },
    
    onOut: function(e) {
        this.writeTitle(e);
        if(this.activeOver && !this.activeOut) {
            this.hideElm(this.show.id);
            Event.observe(this.element, 'mouseover', this.eventMouseOver);
            Event.stopObserving(this.element, "mouseout", this.eventMouseOut);
            Event.stopObserving(document, "mousemove", this.eventMouseMove);
        }
        this.activeOut=true;
        this.activeOver=false;
    },

    update: function(e) {
        return [Event.pointerX(e), Event.pointerY(e)];
    }
        


}

