
$.fn.betterTooltip = function(options){
	
	/* Setup the options for the tooltip that can be 
	   accessed from outside the plugin              */
	var defaults = {
		speed: 300,
		delay: 500
	};
	
	var options = $.extend(defaults, options);
	/* Give each item with the class associated with 
	   the plugin the ability to call the tooltip    */
	
	/* Create a function that builds the tooltip 
	   markup. Then, prepend the tooltip to the body */
	
		getTip = function() {
		
			var tTip = 
				"<div class='tip'>" +
					
				"</div>";
				
			return tTip;
		}
		
	$("#tooltips").prepend(getTip());
	
	$(this).each(function(){

		var $this = $(this);
		var tip = $('.tip');
		var tName = (this.rel);
		var tipInner = $(".tip");
		
		var tTitle = (this.title);
		
		this.title = "";
		
		var offset = $('#boxes').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.addClass(tName);
				tipInner.html(tTitle);
				
				setTip(tTop, tLeft, tName);
				setTimer();
			}, 
			function() {
				stopTimer();
				tip.hide();
				tipInner.removeClass(tName);
			}
		);		   
		
		/* 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, tName){
			var topOffset = tip.height();
			if(tName == 't_lgreen'){
				var leftMove = 0+5;
			}else if(tName == 't_yellow'){
				var leftMove = 200+10;
			}else if(tName == 't_dgreen'){
				var leftMove = 400+15;
			}else if(tName == 't_red'){
				var leftMove = 600+20;
			}
			var xTip = (left+leftMove)+"px";
			var yTip = (top-topOffset-200)+"px";
			tip.css({'bottom' : yTip, 'left' : xTip});
		
		}
		
		
		/* This function stops the timer and creates the
		   fade-in animation                          */
		showTip = function(){
			stopTimer();
			tip.animate({ "bottom": "-=200px","opacity": "toggle"}, defaults.speed);
		}
	});
};
