/*-------------------------------------------------------------------------------
	A Better jQuery Tooltip
	Version 1.0
	By Jon Cazier
	jon@3nhanced.com
	01.22.08
-------------------------------------------------------------------------------*/

$.fn.thoughtBubble = function(options){
	
	/* Setup the options for the tooltip that can be 
	   accessed from outside the plugin              */
	var defaults = {
		speed: 200,
		delay: 500,
		animation: {"opacity": "toggle"},
		animationOut: {"opacity": "toggle"}
		//TODO:user defined offsets/relative positions
	};
	
	var options = $.extend(defaults, options);
	
	//This increases usability for people with js disabled
	//Tips will display in their relative positions in that case
	$(this).next().each(function() {
		var $this = $(this);
		$this.hide();
		$this.css('position','absolute');
	});
	
	$(this).next().find('.close-tip').click(function(e) {
		var tip = $(this).parents('.tip');
		if (tip.css('display') != 'none') {
			tip.animate(defaults.animationOut, defaults.speed/2);
			e.preventDefault();
		}
	});
	
	/* Give each item with the class associated with 
	   the plugin the ability to call the tooltip    */
	$(this).each(function(){
		
		var $this = $(this);
		var tip = $this.next('.tip');
		
		var offset = $this.offset();
		var tLeft = offset.left;
		var tTop = offset.top;
		var tWidth = $this.width();
		var tHeight = $this.height();
		
		/* Mouse over and out functions*/
		$this.hover(
			function() {
				if (tip.css('display') == 'none') {
					stopTimer();
					setTip(tip, tTop, tLeft, tWidth, tHeight);
					setTimer(tip);
				}
			}, 
			function() {
				stopTimer();
			}
		);		   
		
		/* Delay the fade-in animation of the tooltip */
		setTimer = function(tip) {
			$this.showTipTimer = setInterval(function() { showTip(tip) }, defaults.delay);
		}
		
		stopTimer = function() {
			clearInterval($this.showTipTimer);
		}
		
		/* Position the tooltip relative to the class 
		   associated with the tooltip                */
		setTip = function(tip, top, left, width, height){
			var topOffset = tip.height();
			var borderHeight = parseInt(tip.css('border-top-width')) + parseInt(tip.css('border-bottom-width'));
			
			//Set position of tip: vertical=middle
			//horizontal=right side + 5px 
			var xTip = (left+width+5)+"px";
			var yTip = (top-topOffset+(height/2)-borderHeight)
			//TODO: user defined offsets and relative positions
			
			//bounds checking
			if (yTip < 0) { 
				yTip = "0px"; 
			} else {
				yTip += "px";	
			}
			//TODO: horizontal bounds checking
			tip.css({'top' : yTip, 'left' : xTip});
		}
		
		/* This function stops the timer and creates the
		   fade-in animation                          */
		showTip = function(tip){
			$('.tip').each(function() {
				var $this = $(this);
				if ($this.css('display') != 'none') {
					$this.animate(defaults.animationOut, defaults.speed/2);
				}
			});
			stopTimer();
			if (defaults.animation.opacity == undefined) {
				tip.show();
			}
			tip.animate(defaults.animation, defaults.speed);
		}
	});
};