/**
 * CodaBubble extension for jQuery library
 *
 * Creates tooltips "coda bubble" style
 *
 * @author Carlo Tasca
 * @version 1.0
 *
 * opts:
 *
 * @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
 */

jQuery.fn.codaBubble = function(opts){
		var bubble = this;
		opts = jQuery.extend({
			animDistance : 20,
			position : 'center',
			duration : 400,
	  		delay : 0,
	  		width : 200,
	  		bubbleImagesPath : ""
			
		},opts||{});
		  
	    function bubbleHtmlWrapper(bubbleHtml, position) {
			position = jQuery.extend({side: 'bottom'});
	    	return '<table class="popup"><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 class="bottom"><div class="pointer '+position+'" /></td><td class="corner bottomright"/></tr></table>';
	    }  
		return jQuery(bubble).each(function (i) {
	    var bubbleHtml = $("<div></div>").append(jQuery('.popup', this).clone().removeClass("popup")).remove().html();
		var bubbleHtmlContainer = jQuery('.popup', this);
	    bubbleHtmlContainer.after(bubbleHtmlWrapper(bubbleHtml, opts.position)).remove();
	   
	    var distance = opts.animDistance;
	    var time = opts.bubbleTime;
	    var hideDelay = opts.hideDelay;
	    var hideDelayTimer = null;
	    var beingShown = false;
	    var shown = false;
	    var trigger = jQuery('.trigger', this);
	    var popup = jQuery('.popup', this).css('opacity', 0);
	    
	    jQuery([trigger.get(0), popup.get(0)]).hover(function () {
	      popup.css("width", opts.width + "px");
	      var triggerWidth = jQuery(trigger.get(0)).css('width');
	      if (hideDelayTimer) clearTimeout(hideDelayTimer);
	      if (beingShown || shown) {
	        return;
			
	      } else {
	        beingShown = true;
			
	        popup.css({
	          top: -popup.outerHeight() + 10,
	          left: -popup.outerWidth() * 0.5 + trigger.outerWidth() * 0.5,
	          display: 'block'
	        })
	        .animate({
	          top: '-=' + distance + 'px',
	          opacity: 1
	        }, time, 'swing', function() {
	          beingShown = false;
	          shown = true;
			  
			  //fix ugly font in IE
			  if(this.style.filter)
			  	this.style.removeAttribute('filter');
	        });
	      }
	    }, 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);
	    });
	  });
}
