/** * based on Carlo Tasca's codaBubble
 * @author Carlo Tasca, Martin Tschofen: added pixie dust
 * Requires pngFIX for IE
 * OPTIONS: * 
 * @param {array} distances Distances of bubbles from their triggers  
 * @param {array} leftShifts Left positions of bubbles 
 * @param {array} bubbleTimes Life times for bubbles 
 * @param {array} hideDelays Hide delay times for bubbles 
 * @param {array} bubbleWidths Hide delay times for bubbles 
 * @param {string} bubbleImagesPath Path to skin for bubbles 
 * @param {boolean} msieFix Fix for IE png rendering. Replaces pngs with gifs if true (default) 
 * @param {boolean} msiePop If false removes bubble in IE
 * @param {selector} children inside the containter with content */
(function($) {

$.fn.mtBubble = function(opts){
    var bubble = this;
    opts = $.extend({
        distances: [20],
        leftShifts: [30],
        bubbleTimes: [400],
        hideDelays: [0],
        bubbleWidths: [200],
        bubbleImagesPath: "../images",
        msieFix: true,
        msiePop: true,
		children: '.partner',
		trackBubble: false,
		showAbove: true
    }, opts ||
    {});
    function bubbleHtmlWrapper(bubbleHtml, addTail){
		var tail = "";
		var pos = -120;
		if (addTail === true) {
			tail = '<img style="display:block;border:0 !important" width="30" height="29" border="0" alt="" />';
		}
		if (opts.showAbove === false){
			pos = 120;
		}
		return '<table class="popup" style="opacity: 0; top: -'+pos+'px; left: -33px; display: none;"><tr><td class="corner topleft"/><td class="top"/><td class="corner topright"/></tr><tr><td class="left"/><td class="bubble_content">' + bubbleHtml + '</td><td class="right"/></tr><tr><td class="corner bottomleft"/><td height="29" class="bottom">'+tail+'</td><td class="corner bottomright"/></tr></table>';
    }
    return $(bubble).each(function(i){
		var $obj = $(this);
        var bubbleHtml = '';
        $('.info', this).hide().after(bubbleHtmlWrapper(bubbleHtml,opts.trackBubble));
		var $bubbleContent = $('table.popup .bubble_content', this);
		$bubbleContent.width(opts.bubbleWidths[0] - 19*2);
		if (opts.trackBubble == true) {
			$('.popup td.bottom img', this).attr('src', opts.bubbleImagesPath + '/bubble-tail2.png');
		}
        if (opts.msieFix) {
            if ($.browser.msie) {
				$('table.popup td:not(.bubble_content)', this).fixPNG();
            }
        }
        var distance = opts.distances[i];
        var time = opts.bubbleTimes[i];
        var hideDelay = opts.hideDelays[i];
        var hideDelayTimer = null;
        var beingShown = false;
        var shown = false;
        var popup = $('.popup', this).css('opacity', 0);
        $([this, popup.get(0)]).mouseover(function(){
            $(popup).css("width", opts.bubbleWidths[i] + "px");
            var triggerWidth = $(this).css('width');
            if (hideDelayTimer) 
                clearTimeout(hideDelayTimer);
            if (beingShown || shown) {
                return;
            }
            else {
                beingShown = true;
				var pos = -80;
				if(opts.showAbove === false){
					pos = 60;
				}
                popup.css({
                    top: pos,
                    left: opts.leftShifts[i],
                    display: 'block'
                }).animate({
                    top: '-=' + distance + 'px',
                    opacity: 1
                }, time, 'swing', function(){
                    beingShown = false;
                    shown = true;
                });
            }
        }).mouseout(function(){
            if (hideDelayTimer) 
                clearTimeout(hideDelayTimer);
            hideDelayTimer = setTimeout(function(){
                hideDelayTimer = null;
                popup.animate({
                    top: '-=' + distance + 'px',
                    opacity: 0
                }, time, 'swing', function(){
                    shown = false;
                    popup.css('display', 'none');
                });
            }, hideDelay);
        });
		$(opts.children+' a.mug img.big', $(this)).css({opacity:0}).mouseout(function(){
			var z = 0;
			if (jQuery.support.cssFloat){
				z = 'auto';
			} else {
				$(this).parent().css({zIndex:z})
			}
			$(this).next().css({
				opacity: 1/*top:'0',left:'0',width:'50px',height:'50px',zIndex:z*/
			})
			$(this).css({
				opacity: 0,zIndex: z,borderColor:'white'
			});
		});
		$(opts.children, $(this)).mouseover(function(){
			var $obj = $(this);
			if (opts.trackBubble == true) {
				var l = $obj.position().left + ($obj.width() / 2);
				var itemLeft = l - opts.bubbleWidths[0] / 2 + 10;
				$obj.parent().find('.popup').css('left', itemLeft);
			}
			var ctnt = $('.info', this).html()
			var $this = $('a.mug img.trigger:not(img.big)',this);
			$bubbleContent.html(ctnt);
			/*var w = $this.width();
			var h = $this.height();
			var loc = $this.offset();
			loc.left = loc.left - 5;
			loc.top = loc.top -5;*/
			if (!jQuery.support.cssFloat) {
				$('a.mug', this).css({
					zIndex: 1000
				})
			}
			if ($this.css('opacity') == 1) {
				$this.css({
					opacity: 0/*top:'-5px',left:'-5px',width:'60px',height:'60px',zIndex:500*/
				})
				$('a.mug img.big', this).css({
					opacity: 1,zIndex: 500,borderColor:'#eeeeee'
				});
			}
			
		})
        if (!opts.msiePop && $.browser.msie) {
            $(popup).remove();
        }
    });
}
})(jQuery);
