//------------------------------------
//	JQUERY.TOOLTIP.JS
//	Author: 	Will Blackmore
//	Requires:	jquery 1.4.3
//	Version:	0.3
//------------------------------------

(function($){

	$.fn.tooltip = function(settings) {

		// Default options
		var defaults = {
			id:				'tooltip',		// ID of the tooltip element
			data:			'tooltip',		// The data tag you want to use
			style:			'',				// Optional class to give unique styles
			sticky:			true,			// Follow the mouse or stick to the element
			offsetX:		0,				// Nudge the tooltip left or right
			offsetY:		-10,			// Nudge the tooltip up or down
			margin:			5,				// The margin around the screen where the tooltip will change its position to stay visible
			opacity:		1,				// Opacity
			delay:			0,				// Delay before tooltip appears
			speed:			200,			// Fade in out speed. Out speed is half.
			position:		'top',			// Default position
			maxWidth:		200,			// The maximum width
			canclick:		true			// Can the trigger be clicked?
		};

		var o = $.extend(defaults, settings);

		var delayTimer = {};

		return this.live('mouseenter',function(){

			var $this = $(this);
			var content = $this.data(o.data);
			
			if( content == undefined ){
				content = $this.attr('title');
				$this.removeAttr('title');
			}

			//$this.mouseenter(function(){
				
				var mouse = '';
				
				if( !o.sticky ){
					$this.mousemove(function(e){
						mouse = e;
						$('#tooltip').css(getXY(mouse),true);
					});
				}
				
				$('#tooltip').remove();

				delayTimer = $.timer(o.delay,function(){

					$.clearTimer(delayTimer);

					o.sticky == false ? obj = mouse : obj = $this;

					$('<div/>',{
						'id':o.id,
						'class':o.style,
						'css':{
							maxWidth: o.maxWidth
						},
						'html':'<div id="' + o.id + '_content">' + content + '</div><span id="' + o.id + '_arrow"></span>'
					}).appendTo('body').css(getXY(obj)).hide().fadeTo(o.speed,o.opacity);
				
				});
			
			//});
			
			$this.mouseleave(function(){

				$.clearTimer(delayTimer);
				
				$this.unbind('mousemove');

				$('#tooltip').fadeOut(Math.round( o.speed / 2 ),function(){
					$(this).remove();
				});
			
			});
			
			if( !o.canclick ){
				$this.click(function(){
					return false;
				});
			}

		});
		
		//this = $(this);
		
		function getXY($this){

			var $tooltip = $('#tooltip');		// The tooltip
			var w = $tooltip.outerWidth();		// Tooltip width
			var h = $tooltip.outerHeight();		// Tooltip height
			var best = o.position;				// Best position
			var ow = 0;							// Object width
			var oh = 0;							// Object height
			
			if( !o.sticky ){
				var y = $this.pageY;
				var x = $this.pageX;
			}else{
				var y = $this.offset().top;
				var x = $this.offset().left;
				ow = $this.outerWidth(true);
				oh = $this.outerHeight(true);
			}
			
			if( o.position != 'top' && ( y + h + o.offsetY + o.margin ) >= $(window).height() ){
				best = 'top';
			}
			
			if( o.position != 'right' && ( x - w - o.offsetX - o.margin ) <= 0 ){
				best = 'right';
			}
			
			if( o.position != 'bottom' && ( y - h - o.offsetY ) < ( $(document).scrollTop() + o.margin ) ){
				best = 'bottom';
			}
			
			if( o.position != 'left' && ( x + w ) > ( $(window).width() - o.offsetX - o.margin ) ){
				best = 'left';
			}
			
			$tooltip.removeClass(o.id + '_' + best + ' ' + o.id + '_' + o.position).addClass('tooltip_' + best);
			
			if( best == 'top' ){
				
				y = y - h + o.offsetY;
				x = x - Math.round( w / 2 ) + Math.round( ow / 2 ) + o.offsetX;
			
			}else if( best == 'right' ){

				y = y - Math.round( h / 2 ) + Math.round( oh / 2 ) + o.offsetY;
				x = x + o.offsetX + ow;
				
			}else if( best == 'bottom' ){
				
				y = y + o.offsetY + oh;
				x = x - Math.round( w / 2 ) + Math.round( ow / 2 ) + o.offsetX;
				
			}else if( best == 'left' ){
				
				y = y - Math.round( h / 2 ) + Math.round( oh / 2 ) + o.offsetY;
				x = x - w + o.offsetX;
				
			}
			
			// Final ditch attempt to keep the thing on the screen
			if( x < o.margin ){
				x = o.margin;
			}
			
			if( y > ( $(document).scrollTop() + $(window).height() - o.margin - h ) ){
				y = ( $(document).scrollTop() + $(window).height() - o.margin - h );
			}
			
			if( y < $(document).scrollTop() + o.margin ){
				y = $(document).scrollTop() + o.margin;
			}

			return {top:y,left:x};
			
		}
	
	};

})(jQuery);
