/*-------------------------------------------------------------------------------
	A Better jQuery Tooltip
	Version 1.0
	By Jon Cazier
	jon@3nhanced.com
	01.22.08
-------------------------------------------------------------------------------*/

$.fn.tooltip = function(options){
	
	/* Setup the options for the tooltip that can be 
	   accessed from outside the plugin              */
	var defaults = {
		speed: 100,
		delay: 300,
		skipItems: 'div.items'
	};
	
	var options = $.extend(defaults, options);
	
	/* Create a function that builds the tooltip 
	   markup. Then, prepend the tooltip to the body */
	getTip = function() {
		var tTip = '<div id="tooltip" class="frame_small">' + 
		  '<div class="frame_tail_bottom"></div>' +
		  '<table border="0"><tbody>' +
		  '<tr><td class="frame_tl"></td><td class="frame_tc"></td><td class="frame_tr"></td></tr>' +
		  '<tr><td class="frame_ml"></td><td class="frame_mc"></td><td class="frame_mr"></td></tr>' +
		  '<tr><td class="frame_bl"></td><td class="frame_bc"></td><td class="frame_br"></td></tr>' +
		  '</tbody></table></div>';
		return tTip;
	}
	$("body").prepend(getTip());
	
	/* Give each item with the class associated with 
	   the plugin the ability to call the tooltip    */
	$(this).each(function(){
		
		if(!this.title || $(this).parents(options.skipItems).length || $(this).attr('user'))
			return;
		
		var $this = $(this);
		var tip = $('.frame_small');
		var tipInner = $('.frame_small .frame_mc');
		
		var tTitle = (this.title);
		this.title = "";
		
		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() {
				tipInner.html(tTitle);
				setTip(tTop, tLeft);
				setTimer();
			}, 
			function() {
				stopTimer();
				tip.hide();
			}
		);		   
		
		/* Delay the fade-in animation of the tooltip */
		setTimer = function() {
			$this.showTipTimer = setInterval("showTip()", defaults.delay);
		}
		
		stopTimer = function() {
			clearInterval($this.showTipTimer);
		}
		
		/* Position the tooltip relative to the class 
		   associated with the tooltip                */
		setTip = function(top, left){
			var topOffset = tip.height();
			var xTip = (left -  10) + "px";
			var yTip = (top - topOffset - 24) + "px";
			tip.css({'top' : yTip, 'left' : xTip});
		}
		
		/* This function stops the timer and creates the
		   fade-in animation                          */
		showTip = function(){
			stopTimer();
			tip.animate({"top": "+=20px", "opacity": "toggle"}, defaults.speed);
		}
	});
};