/********************
Versão alterada para usar jQuery ao invés de YUI.
********************/

var currentBalloonClass;
var balloonIsVisible;
var balloonIsSticky;
var balloonInvisibleSelects;
var balloonOK = true;
var Balloon = function () {
    this.minWidth = 60;
    this.maxWidth = 300;
    this.balloonTextSize = "4%";
    this.delayTime = 500;
    this.vOffset = 10;
    this.padding = 10;
    this.shadow = 20;
    this.balloonImage = "/img/balloons/balloon.png";
    this.ieImage = "/img/balloons/balloon_ie.png";
    this.stem = true;
    this.stemHeight = 32;
    this.stemOverlap = 3;
    this.upLeftStem = "/img/balloons/up_left.gif";
    this.downLeftStem = "/img/balloons/down_left.gif";
    this.upRightStem = "/img/balloons/up_right.gif";
    this.downRightStem = "/img/balloons/down_right.gif"
};
Balloon.prototype.showTooltip = function (A, B, F, E) {
    if (this.isIE() && !balloonOK) {
        return false
    }
    var G = A.type.match("mouseover", "i");
    if (balloonIsVisible && !balloonIsSticky && G) {
        return false
    }
    if (balloonIsVisible && balloonIsSticky && !F) {
        return false
    }
    var D = this.getEventTarget(A);
    if (F && G && this.isSameElement(D, this.currentElement)) {
        return false
    }
    this.firingElement = D;
    if (F) {
        this.hideTooltip(1)
    }
    balloonIsSticky = F;
    this.width = E;
    this.setActiveCoordinates(A);
    this.hideTooltip();
    if (this.isOldIE() && this.ieImage) {
        this.balloonImage = this.ieImage;
        this.ieImage = null
    }
    var C;
    if (C = B.match(/^url:(\S+)/)) {
        B = "";
        this.activeUrl = C[1]
    } else {
        if (C = B.match(/^load:(\S+)/)) {
            if (!document.getElementById(C[1])) {
                alert("problem locating element " + load[1])
            }
            B = document.getElementById(C[1]).innerHTML
        } else {
            if (B.match(/^(https?:|\/|ftp:)\S+$/i)) {
                this.activeUrl = B;
                B = ""
            }
        }
    }
    currentBalloonClass = this;
    this.balloon = this.makeBalloon();
    this.wrapper = this.wrapContents(B);
    this.timeoutTooltip = window.setTimeout(this.doShowTooltip, this.delayTime)
};
Balloon.prototype.doShowTooltip = function () {
    var F = currentBalloonClass;
    var K = F.balloon;
    if (balloonIsVisible) {
        return false
    }
    
    var $window = jQuery(window);
    
    F.currentElement = F.firingElement;
    F.parseIntAll();
    F.setStyle(this.container, "display", "none");
    var H = $window.width(); // YAHOO.util.Dom.getViewportWidth();
    var G = Math.round(H / 2);
    var D = $window.height(); // YAHOO.util.Dom.getViewportHeight();
    var I = $window.scrollLeft(); // YAHOO.util.Dom.getDocumentScrollLeft();
    var J = $window.scrollTop(); // YAHOO.util.Dom.getDocumentScrollTop();
    var A = J + Math.round(D / 2);
    var E = F.activeTop > A ? "up" : "down";
    var C = F.activeRight > G ? "left" : "right";
    if (balloonIsSticky) {
        var B = "margin-right:-" + Math.round(F.padding / 2 - 1) + "px;float:right;cursor:pointer;margin-top:-" + Math.round(F.padding / 2 - 1) + "px;float:right"
    }
    F.setBalloonStyle(E, C, H, I);
    balloonIsVisible = true;
    F.showHideSelect()
};
Balloon.prototype.makeBalloon = function () {
    var B = currentBalloonClass;
    var C = document.getElementById("balloon");
    if (C) {
        document.body.removeChild(C)
    }
    C = document.createElement("div");
    C.setAttribute("id", "balloon");
    var D = new Array("contents", "topRight", "bottomRight", "bottomLeft");
    for (var A = 0; A < D.length; A++) {
        var E = document.createElement("div");
        E.setAttribute("id", D[A]);
        C.appendChild(E);
        if (D[A] == "contents") {
            this.wrapper = E
        }
    }
    B.activeBalloon = C;
    return C
};
Balloon.prototype.setBalloonStyle = function (R, F, T, M) {
    var H = currentBalloonClass;
    var B = H.activeBalloon;
    if (typeof(H.shadow) != "number") {
        H.shadow = 0
    }
    if (!H.stem) {
        H.stemHeight = 0
    }
    var U = H.padding + H.shadow;
    var O = H.padding;
    H.setStyle(B, "background", "url(" + H.balloonImage + ") top left no-repeat");
    H.setStyle(B, "position", "absolute");
    H.setStyle(B, "padding-top", U);
    H.setStyle(B, "padding-left", U);
    H.setStyle(B, "top", -9999);
    document.body.appendChild(B);
    H.setStyle(B, "z-index", 999999);
    H.setStyle("bottomRight", "background", "url(" + H.balloonImage + ") bottom right no-repeat");
    H.setStyle("bottomRight", "position", "absolute");
    H.setStyle("bottomRight", "right", 0 - U);
    H.setStyle("bottomRight", "bottom", 0 - U);
    H.setStyle("bottomRight", "height", U);
    H.setStyle("bottomRight", "width", U);
    H.setStyle("topRight", "background", "url(" + H.balloonImage + ") top right no-repeat");
    H.setStyle("topRight", "position", "absolute");
    H.setStyle("topRight", "right", 0 - U);
    H.setStyle("topRight", "top", 0);
    H.setStyle("topRight", "width", U);
    H.setStyle("topRight", "z-index", -1);
    H.setStyle("bottomLeft", "background", "url(" + H.balloonImage + ") bottom left no-repeat");
    H.setStyle("bottomLeft", "position", "absolute");
    H.setStyle("bottomLeft", "left", 0);
    H.setStyle("bottomLeft", "bottom", 0 - U);
    H.setStyle("bottomLeft", "height", U);
    H.setStyle("bottomLeft", "z-index", -1);
    if (this.stem) {
        var J = document.createElement("img");
        H.setStyle(J, "position", "absolute");
        B.appendChild(J);
        if (R == "up" && F == "left") {
            J.src = H.upLeftStem;
            var N = H.stemHeight + O - H.stemOverlap;
            H.setStyle(J, "bottom", 0 - N);
            H.setStyle(J, "right", 0)
        } else {
            if (R == "down" && F == "left") {
                J.src = H.downLeftStem;
                var N = H.stemHeight - (H.shadow + H.stemOverlap);
                H.setStyle(J, "top", 0 - N);
                H.setStyle(J, "right", 0)
            } else {
                if (R == "up" && F == "right") {
                    J.src = H.upRightStem;
                    var N = H.stemHeight + O - H.stemOverlap;
                    H.setStyle(J, "bottom", 0 - N);
                    H.setStyle(J, "left", H.shadow)
                } else {
                    if (R == "down" && F == "right") {
                        J.src = H.downRightStem;
                        var N = H.stemHeight - (H.shadow + H.stemOverlap);
                        H.setStyle(J, "top", 0 - N);
                        H.setStyle(J, "left", H.shadow)
                    }
                }
            }
        }
    }
    if (F == "left") {
        var I = T - H.activeLeft;
        H.setStyle(B, "right", I)
    } else {
        H.setStyle(B, "left", H.activeRight)
    }
    if (!H.width) {
        var Q = H.getLoc(B, "width");
        if (Q > H.maxWidth) {
            Q = H.maxWidth + 50
        }
        if (Q < H.minWidth) {
            Q = H.minWidth
        }
        H.setStyle(B, "width", Q)
    } else {
        H.setStyle(B, "width", H.width)
    }
    var S = H.padding + H.shadow;
    var G = H.getLoc(B, "x1");
    var C = H.getLoc(B, "x2");
    if (F == "left") {
        G += S
    }
    if (F == "right") {
        C += S
    }
    var E = M + T;
    if (F == "right" && C > (E - 30)) {
        H.setStyle(B, "width", (E - G) - 50)
    } else {
        if (F == "left" && G < (M + 30)) {
            H.setStyle(B, "width", (C - M) - 50)
        }
    }
    var A = H.getLoc(B, "width");
    var D = H.getLoc(B, "height");
    H.setStyle("topRight", "height", D);
    H.setStyle("bottomLeft", "width", A);
    var K = H.isOverlap("topRight", "bottomRight");
    var L = H.isOverlap("bottomLeft", "bottomRight");
    if (K) {
        H.setStyle("topRight", "height", D - K[1])
    }
    if (L) {
        H.setStyle("bottomLeft", "width", A - L[0])
    }
    if (R == "up") {
        var P = H.activeTop - H.vOffset - H.stemHeight - D;
        H.setStyle(B, "top", P);
        H.setStyle(B, "display", "inline")
    } else {
        var P = H.activeTop + H.vOffset + H.stemHeight;
        H.setStyle(B, "top", P)
    }
};
Balloon.prototype.hideTooltip = function (C) {
    if (C && typeof C == "object") {
        C = false
    }
    if (balloonIsSticky && !C) {
        return false
    }
    var B = currentBalloonClass;
    currentBalloonClass = null;
    if (B) {
        window.clearTimeout(B.timeoutTooltip)
    }
    if (balloonIsSticky && B) {
        B.currentElement = null
    }
    balloonIsVisible = false;
    balloonIsSticky = false;
    if (!B) {
        var A = document.getElementById("balloon");
        if (A) {
            Balloon.prototype.setStyle(A, "display", "none")
        }
    } else {
        if (B.activeBalloon) {
            B.setStyle(B.activeBalloon, "display", "none")
        }
    }
    Balloon.prototype.showHideSelect(1)
};
hideAllTooltips = function () {
    var A = currentBalloonClass;
    if (!A) {
        return
    }
    window.clearTimeout(A.timeoutTooltip);
    if (A.activeBalloon) {
        A.setStyle(A.activeBalloon, "display", "none")
    }
    balloonIsVisible = false;
    balloonIsSticky = false;
    currentBalloonClass = null
};
Balloon.prototype.setActiveCoordinates = function (A) {
    var C = this.getEventTarget(A);
    var E = this.eventXY(A);
    var D = C.getAttribute("coords");
    var F = C.tagName.match("img", "i");
    var B = this.getLoc(C, "height") > 50;
    if (!D && !F && !B) {
        this.activeTop = this.getLoc(C, "y1") - 10
    } else {
        this.activeTop = E[1] - 10
    }
    this.activeLeft = E[0] - 10;
    this.activeRight = this.activeLeft + 20;
    this.activeBottom = !D && this.getLoc(C, "y2");
    if (this.activeBottom) {
        this.activeBottom += 10
    } else {
        this.activeBottom = this.activeTop + 20
    }
};
Balloon.prototype.eventXY = function (A) {
    var C = new Array(2);
    var B = A || window.event;
    C[0] = B.pageX || B.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
    C[1] = B.pageY || B.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    return C
};
Balloon.prototype.getEventTarget = function (B) {
    var A;
    var C = B || window.event;
    if (C.target) {
        A = C.target
    } else {
        if (C.srcElement) {
            A = C.srcElement
        }
    }
    if (A.nodeType == 3) {
        A = A.parentNode
    }
    return A
};
Balloon.prototype.setStyle = function (B, A, C) {
    if (C && A.match(/left|top|bottom|right|width|height|padding|margin/)) {
        C += "px"
    }
    if (B) {
        var selector = typeof B == 'string' ? '#' + B : B;
        jQuery(selector).css(A, C); // YAHOO.util.Dom.setStyle(B, A, C)
    }
};
Balloon.prototype.getLoc = function (A, B) {
    var C = Region.getRegion(A); // YAHOO.util.Dom.getRegion(A);
    switch (B) {
    case ("y1"):
        return parseInt(C.top);
    case ("y2"):
        return parseInt(C.bottom);
    case ("x1"):
        return parseInt(C.left);
    case ("x2"):
        return parseInt(C.right);
    case ("width"):
        return (parseInt(C.right) - parseInt(C.left)) + 50;
    case ("height"):
        return (parseInt(C.bottom) - parseInt(C.top)) + 50;
    case ("region"):
        return C
    }
};
Balloon.prototype.parseIntAll = function () {
    this.padding = parseInt(this.padding);
    this.shadow = parseInt(this.shadow);
    this.stemHeight = parseInt(this.stemHeight);
    this.stemOverlap = parseInt(this.stemOverlap);
    this.vOffset = parseInt(this.vOffset);
    this.delayTime = parseInt(this.delayTime);
    this.width = parseInt(this.width);
    this.maxWidth = parseInt(this.maxWidth);
    this.minWidth = parseInt(this.minWidth)
};
Balloon.prototype.showHideSelect = function (F) {
    var D = currentBalloonClass || new Balloon;
    if (!this.isOldIE()) {
        return false
    }
    if (!F) {
        var B = document.getElementById("contents").getElementsByTagName("select");
        var E = new Object();
        for (var C = 0; C < B.length; C++) {
            var G = B[C].id || B[C].name;
            E[G] = 1
        }
        balloonInvisibleSelects = new Array();
        var A = document.getElementsByTagName("select");
        for (var C = 0; C < A.length; C++) {
            var G = A[C].id || A[C].name;
            if (D.isOverlap(A[C], D.activeBalloon) && !E[G]) {
                balloonInvisibleSelects.push(A[C]);
                D.setStyle(A[C], "visibility", "hidden")
            }
        }
    } else {
        if (balloonInvisibleSelects) {
            for (var C = 0; C < balloonInvisibleSelects.length; C++) {
                var G = balloonInvisibleSelects[C].id || balloonInvisibleSelects[C].name;
                D.setStyle(balloonInvisibleSelects[C], "visibility", "visible")
            }
            balloonInvisibleSelects = null
        }
    }
};
Balloon.prototype.isOverlap = function (E, C) {
    if (!E || !C) {
        return false
    }
    var D = this.getLoc(E, "region");
    var B = this.getLoc(C, "region");
    if (!D || !B) {
        return false
    }
    if (C.id == "balloon") {
        B.top = B.top - 30;
        B.left = B.left - 30;
        B.right = B.right + 30;
        B.bottom = B.bottom + 30
    }
    var A = D.intersect(B);
    if (A) {
        A = new Array((A.right - A.left), (A.bottom - A.top))
    }
    return A
};
Balloon.prototype.isSameElement = function (D, B) {
    if (!D || !B) {
        return false
    }
    var C = this.getLoc(D, "region");
    var A = this.getLoc(B, "region");
    var E = C.contains(A) && A.contains(C);
    return E ? true : false
};
Balloon.prototype.wrapContents = function (B) {
    var C = this.wrapper;
    if (!this.height) {
        this.height = 400
    }
    A += ' scrolling="no"';
    if (this.activeUrl) {
        var A = "height=" + this.height + ' width="100%" frameborder=0 src="' + this.activeUrl + '"';
        B = '<iframe name="bf" id="bf" ' + A + "></iframe>";
        this.activeUrl = null
    }
    C.innerHTML = B;
    return C
};
Balloon.prototype.isIE = function () {
    return document.all && !window.opera
};
Balloon.prototype.isOldIE = function () {
    if (navigator.appVersion.indexOf("MSIE") == -1) {
        return false
    }
    var A = navigator.appVersion.split("MSIE");
    return parseFloat(A[1]) < 7
};

/////////////////////////////////////////////////////////////////////////////

/**
 * A region is a representation of an object on a grid.  It is defined
 * by the top, right, bottom, left extents, so is rectangular by default.  If 
 * other shapes are required, this class could be extended to support it.
 * @namespace YAHOO.util
 * @class Region
 * @param {Int} t the top extent
 * @param {Int} r the right extent
 * @param {Int} b the bottom extent
 * @param {Int} l the left extent
 * @constructor
 */
Region = function(t, r, b, l) {

    /**
     * The region's top extent
     * @property top
     * @type Int
     */
    this.top = t;
    
    /**
     * The region's top extent
     * @property y
     * @type Int
     */
    this.y = t;
    
    /**
     * The region's top extent as index, for symmetry with set/getXY
     * @property 1
     * @type Int
     */
    this[1] = t;

    /**
     * The region's right extent
     * @property right
     * @type int
     */
    this.right = r;

    /**
     * The region's bottom extent
     * @property bottom
     * @type Int
     */
    this.bottom = b;

    /**
     * The region's left extent
     * @property left
     * @type Int
     */
    this.left = l;
    
    /**
     * The region's left extent
     * @property x
     * @type Int
     */
    this.x = l;
    
    /**
     * The region's left extent as index, for symmetry with set/getXY
     * @property 0
     * @type Int
     */
    this[0] = l;

    /**
     * The region's total width 
     * @property width 
     * @type Int
     */
    this.width = this.right - this.left;

    /**
     * The region's total height 
     * @property height 
     * @type Int
     */
    this.height = this.bottom - this.top;
};

/**
 * Returns true if this region contains the region passed in
 * @method contains
 * @param  {Region}  region The region to evaluate
 * @return {Boolean}        True if the region is contained with this region, 
 *                          else false
 */
Region.prototype.contains = function(region) {
    return ( region.left   >= this.left   && 
             region.right  <= this.right  && 
             region.top    >= this.top    && 
             region.bottom <= this.bottom    );

    // this.logger.debug("does " + this + " contain " + region + " ... " + ret);
};

/**
 * Returns the area of the region
 * @method getArea
 * @return {Int} the region's area
 */
Region.prototype.getArea = function() {
    return ( (this.bottom - this.top) * (this.right - this.left) );
};

/**
 * Returns the region where the passed in region overlaps with this one
 * @method intersect
 * @param  {Region} region The region that intersects
 * @return {Region}        The overlap region, or null if there is no overlap
 */
Region.prototype.intersect = function(region) {
    var t = Math.max( this.top,    region.top    ),
        r = Math.min( this.right,  region.right  ),
        b = Math.min( this.bottom, region.bottom ),
        l = Math.max( this.left,   region.left   );
    
    if (b >= t && r >= l) {
        return new Region(t, r, b, l);
    } else {
        return null;
    }
};

/**
 * Returns the region representing the smallest region that can contain both
 * the passed in region and this region.
 * @method union
 * @param  {Region} region The region that to create the union with
 * @return {Region}        The union region
 */
Region.prototype.union = function(region) {
    var t = Math.min( this.top,    region.top    ),
        r = Math.max( this.right,  region.right  ),
        b = Math.max( this.bottom, region.bottom ),
        l = Math.min( this.left,   region.left   );

    return new Region(t, r, b, l);
};

/**
 * toString
 * @method toString
 * @return string the region properties
 */
Region.prototype.toString = function() {
    return ( "Region {"    +
             "top: "       + this.top    + 
             ", right: "   + this.right  + 
             ", bottom: "  + this.bottom + 
             ", left: "    + this.left   + 
             ", height: "  + this.height + 
             ", width: "    + this.width   + 
             "}" );
};

/**
 * Returns a region that is occupied by the DOM element
 * @method getRegion
 * @param  {HTMLElement} el The element
 * @return {Region}         The region that the element occupies
 * @static
 */
Region.getRegion = function(el) {
	
	var selector = typeof el == 'string' ? '#' + el : el;
	var $el = jQuery(selector);
	
	var isHidden = $el.is(':hidden');
	if (isHidden) $el.show();
	
	var p = $el.offset(),
	    t = p.top,
		r = p.left + $el.width(),
		b = p.top + $el.height(),
		l = p.left;
	
	if (isHidden) $el.hide();
	
	return new Region(t, r, b, l);
};
