ESHA.namespace("widget");

ESHA.widget.Rollover = function(el, userConfig) {
    if (arguments.length > 0) {
        ESHA.widget.Rollover.superclass.constructor.call(this, el, userConfig);
    }
}

ESHA.widget.Rollover.prototype = new YAHOO.widget.Tooltip();

/**
 * Reference to the Rollover's superclass, Tooltip
 * @type class
 * @final
 */
ESHA.widget.Rollover.superclass = YAHOO.widget.Tooltip.prototype;
ESHA.widget.Rollover.icon;
ESHA.widget.Rollover.text;
ESHA.widget.Rollover.xOffset;
ESHA.widget.Rollover.yOffset;

/**
 * Initializes the class's configurable properties which can be changed using the SimpleDialog's Config object (cfg).
 */
ESHA.widget.Rollover.prototype.initDefaultConfig = function() {
    ESHA.widget.Rollover.superclass.initDefaultConfig.call(this);

    // Add dialog config properties
    this.cfg.addProperty("icon", { value:"none", handler:this.configIcon, suppressEvent:true } );
    this.cfg.addProperty("text", { value:"", handler:this.configText, suppressEvent:true, supercedes:["icon"] } );
    this.cfg.addProperty("xOffset", { value:"none", handler:this.configXOffset, suppressEvent:true } );
    this.cfg.addProperty("yOffset", { value:"none", handler:this.configYOffset, suppressEvent:true } );
    
}

/**
* The default event handler fired when the user moves the mouse while over the context element.
* @param {DOMEvent} e   The current DOM event
* @param {object}   obj The object argument
*/
ESHA.widget.Rollover.prototype.onContextMouseMove = function(e, obj) {
    //do nothing; we want the rollover to stay at the original position that it was first shown
    //obj.pageX = YAHOO.util.Event.getPageX(e);
    //obj.pageY = YAHOO.util.Event.getPageY(e);

};


/**
* Processes the showing of the Rollover by setting the timeout delay and offset of the Rollover.
* @method doShow
* @param {DOMEvent} e   The current DOM event
* @return {Number}  The process ID of the timeout function associated with doShow
*/
ESHA.widget.Rollover.prototype.doShow = function(e, context) {
    
    //Offset from original YUI tooltip code
    var yYUIOffset = 25;
    if (this.browser == "opera" && context.tagName == "A") {
        yYUIOffset += 12;
    }
    
    var me = this;
    return setTimeout(
        function() {
            if (me._tempTitle) {
                me.setBody(me._tempTitle);
            } else {
                me.cfg.refireEvent("text");
            }

            //dynamically set the position according to the context element provided
            var region = YAHOO.util.Dom.getRegion(context);
            
            //use xOffset and yOffsets provided
            var xPos = region.right;
            if (me.xOffset) 
                xPos += me.xOffset;
            var yPos = region.top;
            if (me.yOffset)
                yPos += me.yOffset;
            
            me.moveTo(xPos, yPos + yYUIOffset);
            if (me.cfg.getProperty("preventoverlap")) {
                me.preventOverlap(xPos, yPos);
            }
            
            YAHOO.util.Event.removeListener(context, "mousemove", me.onContextMouseMove);

            me.show();
            me.hideProcId = me.doHide();
        },
    this.cfg.getProperty("showdelay"));
};

/**
 * The default event handler fired when the "xOffset" property is changed.
 */
ESHA.widget.Rollover.prototype.configXOffset = function(type, args, obj) {
    var offset = args[0];
    if (offset) {
        this.xOffset = offset;        
    }
}

/**
 * The default event handler fired when the "yOffset" property is changed.
 */
ESHA.widget.Rollover.prototype.configYOffset = function(type, args, obj) {
    var offset = args[0];
    if (offset) {
        this.yOffset = offset;        
    }
}

/**
 * The default event handler fired when the "text" property is changed.
 */
ESHA.widget.Rollover.prototype.configText = function(type, args, obj) {
    var text = args[0];
    if (text) {
        this.text = text;
        this.setBodyHTML();
    }
}

/**
 * Fired when the "icon" property is set.
 */
ESHA.widget.Rollover.prototype.configIcon = function(type,args,obj) {
    var icon = args[0];
    if (icon && icon != "none") {
        var iconHTML = "<img src=\"" + icon + "\" class=\"icon\" />";
        this.icon = iconHTML;
        this.setBodyHTML();
    }
}

/**
 *  Used to set the body innerHTML with both the text and the icon attributes
 */
ESHA.widget.Rollover.prototype.setBodyHTML = function(type, args, obj) {
    var bodyHtml = "";
    if (typeof this.icon != 'undefined') {
        bodyHtml = this.icon;
    }

    if (typeof this.text != 'undefined') {
        bodyHtml = bodyHtml + this.text;
    }

    this.body.innerHTML = bodyHtml;
}
