(function ($) {
    $.fn.bubble = function (options) {
        var defaults = {
            'trigger' : 'this',
            'popup' : 'next',
            'distance' : 10,
            'hideDelay' : 50,
            'effectTime' : 1,
            'position' : 'below',
            'staticCounter' : 1,
            'thickboxCounter' : 1000
        };

        var settings = $.extend({}, defaults, options);

        if (options) {
            defaults.staticCounter = defaults.thickboxCounter;
            $('.overlib-popup').each(function(){
                if ($(this).attr('id') >= defaults.thickboxCounter) {
                    $(this).remove();
                }
            });

            defaults.staticCounter = defaults.thickboxCounter;
        }

        return this.each(function () {
            var hideDelayTimer = null;

            var trigger = (settings.trigger == 'this') ? $(this) : $(settings.trigger, this);
            var popup = (settings.popup == 'next') ? $(this).next() : $(settings.popup, this);
            var counter = defaults.staticCounter++;

            if (!$(popup).hasClass('overlib-popup')) {
                $(popup).addClass('overlib-popup');
                $(popup).addClass('overlib-popup-' + counter);
            }

            if(!$(popup).attr('id')) {
                if (popup.find('.overlib-container').length == 0) {
                    var isIE = $.browser.msie;

                    var tmpContent = popup.html();
                    var newContent = new Array();
                    if (isIE) {
                        newContent.push('<table cellspacing="0" cellpadding="0" class="overlib-container overlib-gif">');
                    } else {
                        newContent.push('<table cellspacing="0" cellpadding="0" class="overlib-container">');
                    }
                    newContent.push('<tr>');
                    if (settings.position == 'above') {
                        newContent.push('<td class="overlib-top-left-14-14"><div class="overlib-14-14"></div></td>');
                        newContent.push('<td class="overlib-top-top-right-14" colspan="2"></td>');
                    } else {
                        newContent.push('<td class="overlib-top-left-14-16"><div class="overlib-14-16"></div></td>');
                        newContent.push('<td class="overlib-top-pointer-16"></td>');
                        newContent.push('<td class="overlib-top-right-14-16"><div class="overlib-14-16"></div></td>');
                    }
                    newContent.push('</tr>');
                    newContent.push('<tr>');
                    newContent.push('<td class="overlib-left"></td>');
                    newContent.push('<td class="overlib-content"><div>');
                    newContent.push('<div class="close-button"><img src="/images/overlib/close1.gif" border="0" width="10" height="10" alt="" onclick="javascript:$(\'.overlib-popup\').hide();" /></div>');
                    newContent.push(tmpContent);
                    newContent.push('</div></td>');
                    newContent.push('<td class="overlib-right"></td>');
                    newContent.push('</tr>');
                    newContent.push('<tr>');
                    if (settings.position == 'above') {
                        newContent.push('<td class="overlib-bottom-left-14-24"><div class="overlib-14-24"></div></td>');
                        newContent.push('<td class="overlib-bottom-pointer-24"></td>');
                        newContent.push('<td class="overlib-bottom-right-24"><div class="overlib-14-24"></div></td>');
                    } else {
                        newContent.push('<td class="overlib-bottom-left-14-14"><div class="overlib-14-14"></div></td>');
                        newContent.push('<td class="overlib-bottom-bottom-right-14" colspan="2"></td>');
                    }
                    newContent.push('</tr>');
                    newContent.push('</table>');
                    popup.html(newContent.join(''));

                    settings.autowrap = false;
                }

                var triggerPosition = trigger.offset({border: true, padding: true});
                var triggerWidth = trigger.innerWidth();
                var triggerHeight = trigger.innerHeight();

                popup.css({
                    width:'auto',
                    height:'auto',
                    visibility:'hidden',
                    display:'block'
                }).find('.overlib-content div').css({
                    overflow:'visible',
                    height:'auto',
                    width:'auto'
                });

                var popupWidth = popup.innerWidth();

                if (popupWidth > 450) {
                    popupWidth = 450;
                }

                if (popupWidth == 0) {
                    popupWidth = 450;
                }

                popup.css({width:popupWidth, display:'none', visibility:'visible'});

                popup.attr('id', counter);
                $('body').append($(popup));
            }

            $([trigger.get(0), popup.get(0)]).mouseover(function () {

                if (popup.attr('id')) {
                    if (hideDelayTimer) clearTimeout(hideDelayTimer);

                    if (!popup.is(':visible')) {

                        popup.css({
                            width:'auto',
                            height:'auto',
                            visibility:'hidden',
                            display:'block'
                        }).find('.overlib-content div').css({
                            overflow:'visible',
                            height:'auto',
                            width:'auto'
                        });

                        var triggerPosition = trigger.offset({border: true, padding: true});
                        var triggerWidth = trigger.innerWidth();
                        var triggerHeight = trigger.innerHeight();

                        var popupHeight = popup.innerHeight();
                        var popupWidth = popup.innerWidth();

                        if (popupWidth > 450) {
                            popupWidth = 450;
                        }

                        if (popupWidth == 0) {
                            popupWidth = 450;
                        }

                        var position = {
                            top: (settings.position == 'above') ? triggerPosition.top-popupHeight : triggerPosition.top+triggerHeight,
                            left: triggerPosition.left-parseInt(popupWidth/2)+parseInt(triggerWidth/2)
                        };

                        if (20+popupHeight+position.top-$(document).scrollTop() > $(window).height()) {
                            popup.find('.overlib-content div').css({overflowY:'auto', overflowX:'visible', height:$(document).scrollTop()+$(window).height()-position.top-50});
                            position.left -= 7;
                        }

                       popup.css({width:popupWidth, display:'none', visibility:'visible'});
                       var popupHeight = popup.innerHeight();

                       popup.css({
                            display: 'block',
                            top: position.top,
                            left: (position.left < 0) ? 0 : position.left
                        }).animate({
                            opacity: 1,
                            top: ((settings.position == 'above') ? '-=' : '+=') + settings.distance + 'px'
                        }, settings.effectTime);
                    }
                }
            }).mouseout(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);

                hideDelayTimer = setTimeout(function () {
                    hideDelayTimer = null;
                    popup.animate({
                        top: ((settings.position == 'above') ? '-=' : '+=') + settings.distance + 'px',
                        opacity: 0
                    }, settings.effectTime, 'swing', function () {
                        popup.css('display', 'none');
                    });
                }, settings.hideDelay);
            });
        });
    }
})(jQuery);
