/**
 * @author Remy Sharp
 * @date 2008-03-27
 * @url http://jqueryfordesigners.com/coda-popup-bubbles/
 * @license Creative Commons License - ShareAlike http://creativecommons.org/licenses/by-sa/3.0/
 *
 * See URL for markup examples and screencast
 */

$(document).ready(function() {
	$(".bubble").wrapInner('<td class="bubble-center"></td>')
		.prepend("<td class='bubble-left'></td>")
		.append("<td class='bubble-right'></td>")
		.wrapInner("<tr class='bubble-middle'></tr>")
		.prepend("<tr class='bubble-top'>\n<td class='bubble-top-left'></td>\n<td class='bubble-top-center'></td>\n<td class='bubble-top-right'></td>\n</tr>")
		.append("<tr class='bubble-bottom'>\n<td class='bubble-bottom-left'></td>\n<td class='bubble-bottom-center'></td>\n<td class='bubble-bottom-right'></td>\n</tr>")
		.wrapInner("<table cellpadding='0' cellspacing='0'></table>");
	
	$(".bubble td").not('.bubble-center').css({padding: '0px', margin: '0px'});
	$(".bubble .bubble-center").css({padding: '20px', margin: '0px'});
});



(function ($) {
	$.fn.bubble = function (options) {
		var defaults = {
			'trigger' : '.trigger',
			'popup' : '.bubble',
			'distance' : 10,
			'hideDelay' : 500,
			'effectTime' : 250
		};
		
		var settings = $.extend({}, defaults, options);
		
		return this.each(function () {
			var hideDelayTimer = null;

			var trigger = $(settings.trigger, this);
			var popup = $(settings.popup, this);

			$([trigger.get(0), popup.get(0)]).mouseover(function () {
				if (hideDelayTimer) clearTimeout(hideDelayTimer);

				if (popup.is(':animated, :visible')) {
					return;
				} else {			
					popup.css({
							display: 'block',
							top: trigger.position().top - popup.height() + 10,
							left: trigger.position().left - popup.width()/2 + 24
						}).animate({ top: '-=' + settings.distance + 'px'  }, { queue: false, duration: settings.effectTime}).fadeIn(settings.effectTime);
				}
			}).mouseout(function () {
				if (hideDelayTimer) clearTimeout(hideDelayTimer);

				hideDelayTimer = setTimeout(function () {
					hideDelayTimer = null;
					popup.animate({ top: '-=' + settings.distance + 'px' }, { queue: false, duration: settings.effectTime, easing: 'swing'}).fadeOut(settings.effectTime);
				}, settings.hideDelay);
			});
		});
	}
})(jQuery);

