/**
 * CaptionBubble - A CodaBubble inspired plug-in
 * @author: Justin Alpino
 * @url: http://www.jalpino.com
 * @published: 06/12/2010
 *
 */
if(typeof jQuery != 'undefined') {
	jQuery(function($) {
		
		// Plug-in
		$.fn.extend({
			captionBubble: function(options) {
				
				var settings = $.extend({}, $.fn.captionBubble.defaults, options);
				var bubble, init = false, animDelay, activeanchor;
				
				/**
				 * Initializes the plug-in by build dom elements
				 */
				_init = function( o, t ){
					if( ! document.getElementById('bubble') ){
						$('body').prepend('<div id="bubble"><div><div id="bubble-txt">&nbsp;</div><div id="bubble-point"><img src="'+ o.imgBase +'bubble/point.png" alt="" /></div></div></div>')
						t.css("zIndex","9999");
						bubble = $('#bubble');
						bubble.mouseover(function(){
							clearTimeout(animDelay);
						}).mouseout(function(){
							animDelay = setTimeout( function(){
							_hideBubble(activeanchor);
							},350);
						});
						_cleanBubble();
					}
				};
				
				/**
				 * Cleans out the text in the bubble caption
				 */
				_cleanBubble = function (){
					$('#bubble-txt').html();
					bubble.css("opacity","0.0");
					bubble.show();
				};
				
				/**
				 * Displays the caption bubble
				 */
				_showBubble = function( anchor ){
					
					// animate bubble
					var isActive = anchor.data('isActive') ? anchor.data('isActive') : false;
					var isAnimating = bubble.data('isAnimating') ? bubble.data('isAnimating') : false;
					//console.log(isActive +"=="+ isAnimating);
					if( ! isActive && !isAnimating ){
						
						var a = anchor;
						bubble.data('isAnimating', true );
						a.data('isActive', true);
						
						activeanchor = a;
						
						// get anchor positioning
						var ap = a.offset();
						
						// Populate content
						$('#bubble-txt').html( a.data("captiontxt") );
						bubble.show();
						
						// get dimensions
						var d = {
							w : bubble.width(),
							h : bubble.height(),
							aw : a.width(),
							ah : a.height()
						}
						
						// position the caption bubble;
						bubble.css({
							"top" : ap.top-bubble.height(), 
							"left" : ap.left - d.w + 25,
							"zIndex" : 1
						});
						
						
						bubble.stop(true,true).animate({
					          top: '-=10px',
					          opacity: 1.0
					        }, 350, 'swing', function() {
					          bubble.data('isAnimating', false );
					          return false;
					    });
					}
					
					
				};
				
				/**
				 * Hides the caption bubble
				 */
				_hideBubble = function( anchor ){
					var a = anchor;
					var isanimating = bubble.data('isAnimating') ? bubble.data('isAnimating') : false;
					if( ! isanimating ){
						var d = {
							w : bubble.width(),
							h : bubble.height()
							}
						
						bubble.stop(true,true).animate({
						  top: '-=' + d.h + 'px',
					          opacity: 0.0
					        }, 150, 'swing', function() {
					          a.data('isActive',false);
					          bubble.data('isAnimating',false);
					          clearTimeout(animDelay);
					    });
					}
				};
				
				/**
				 * Run the Plugin
				 */
				return this.each(
					function() {
						
						if($.fn.jquery < '1.2.6') {return;}
						var $t = $(this);
						var o = $.metadata ? $.extend({}, settings, $t.metadata()) : settings;
						var $bub = bubble;
						
						if( !init ) _init( o, $t );
						
						var ttl = $t.parent().attr("title");
						$t.parent().attr("title","").click(function(){return false;});
						var text = ttl;
						
						$t.attr("title","")
						  .data("captiontxt", text )
						  .mouseover( function(e){
						  	var self = this;
						  	if( self !== activeanchor)
						  		setTimeout(function(){ _showBubble($(self)); }, 100);
							else
								_showBubble($(self));
						  		  
						  })
						  .mouseout( function(e){
						  	var self = this;	  	
						  	animDelay = setTimeout( function(){ _hideBubble( $(self) ); }, 350 );
						  });
				
					}
				);
			}
		});
		
		// Plug-in defaults
		$.fn.captionBubble.defaults = {
			imgBase : "img/"
		};
	});
}
