
jQuery.fn.floatingPopup = function() {
    return this.each(function() {

        $(this).bind('mousemove', ShowFloatingPopup);
        $(this).bind('mouseout', HideFloatingPopup);

    });
};

function ShowFloatingPopup(event) {

    var $document = $(window);
    var $divFlutuante = $('#' + $(this).attr('rel'));
    
    var pY = CalcCoord(event.pageY, $divFlutuante.height(), $document.height(), $document.scrollTop(), 10) - $(this).offset().top;
    var pX = CalcCoord(event.pageX, $divFlutuante.width(), $document.width(), $document.scrollLeft(), 10) - $(this).offset().left;

    var pX2 = CalcCoord(event.pageX, $divFlutuante.width(), $document.width(), $document.scrollLeft(), 10) - $(this).offset().left;

    if ($divFlutuante.hasClass("rightTop")) {
        pX = pX + 25;
        pY = pY - 25;
    }
    else if ($divFlutuante.hasClass("leftTop")) {
    pX = pX - $divFlutuante.width() - 12;
        pY = pY - 25;
    }
    else if ($divFlutuante.hasClass("rightBottom")) {
        pX = pX + 25;
        pY = pY - $divFlutuante.height() + 60;
    }
    else if ($divFlutuante.hasClass("leftBottom")) {
        pX = pX - $divFlutuante.width() - 12;
        pY = pY - $divFlutuante.height() + 60;
    }


    $divFlutuante.css({ top: pY + 'px', left: pX + 'px', display: 'block' });

}
function HideFloatingPopup(event) {

    var $divFlutuante = $('#' + $(this).attr('rel'));
    $divFlutuante.css({ display: 'none' });

}
function CalcCoord(currentPosition, elementSize, documentSize, documentScroll, safeMargin) {
    return currentPosition;
//    //Se há espaço para pôr embaixo, ponha-o.
//    if (currentPosition < documentSize + documentScroll - elementSize - 2 * safeMargin) {
//        return currentPosition + safeMargin;

//    } else {

//        //Se não há espaço para virar para cima, "cole-o" no topo da janela.
//        if (currentPosition - documentScroll < elementSize + 2 * safeMargin) {
//            return documentScroll;

//            //Se há espaço para virar para cima, vire-o.
//        } else {
//            return currentPosition - elementSize - safeMargin;
//        }
//    }

}

