/*
 *	SuperTip - jQuery plugin for some cool hover tip effect
 *
 *	Copyright (c) 2008 Aleksandar Ruzicic
 *
 *	Dual licensed under the MIT and GPL licenses:
 *	  http://www.opensource.org/licenses/mit-license.php
 *    http://www.gnu.org/licenses/gpl.html
 *
 *	+ supports hoverIntent plugin (recomended but not required)
 *	+ supports Metadata plugin
 *
 */

(function($)
{
	if (!$.easing.backout)
	{
		$.extend($.easing,{backout:function(x,t,b,c,d){var s=1.70158;return c*((t=t/d-1)*t*((s+1)*t+s)+1)+b;}});
	}

	$.fn.superTip = function($options)
	{
		var opts = $.extend({}, $.fn.superTip.settings, $options || {});

		return $(this).each(function()
		{
			var $this = $(this);

			var o = $.meta && o.readMetadata ? $.extend({}, opts, $this.data()) : opts;

			$(o.selector, this).each(function()
			{
				this.tip = $('<div class="' + o.tipClass + '"/>').html
				(
					o.sourceType == 'tag' ?
						$(o.source, o.context || this)[o.removeTag ? 'remove' : 'andSelf']().html() :
						$(this).attr(o.source)
				)
				.appendTo($(o.tipHolder, o.tipContext));

				this.title = o.source == 'title' && o.sourceType != 'tag' && o.removeTitle ? '' : this.title;

				var overHandler = function()
				{
					$(o.tipHolder + ' > .' + o.tipClass, o.tipContext).not(this.tip).animate(o.outAnim, o.outSpeed, o.outEasing);
					this.tip.css(o.resetCSS).animate(o.inAnim, o.inSpeed, o.inEasing);
				};

				var outHandler = function() { this.tip.animate(o.outAnim, o.outSpeed, o.outEasing); };

				if ($.isFunction($(this).hoverIntent) && o.useHoverIntent)
				{
					$(this).hoverIntent
					({
						sensitivity: o.sensitivity,
						interval: o.interval,
						over: overHandler,
						out: outHandler
					});
				}
				else
				{
					$(this).hover(overHandler, outHandler);
				}

			});
		});
	};

	$.fn.superTip.settings =
	{
		selector: 'a',
		tipHolder: '#tip',
		tipContext: 'body',
		tipClass: 'mmtip',
		source: 'title',
		sourceType: 'attr',		//	attr || tag
		sourceContext: 'body',	//	when sourceType == 'tag'
		removeTitle: true,
		removeTag: false,
		readMetadata: true,
		useHoverIntent: true,
		// animations
		inAnim: {left: 0},
		inSpeed: 'slow',
		inEasing: 'backout',
		resetCSS: {left:-600, top:0},
		outAnim: {top: 40},
		outSpeed: 'slow',
		outEasing: null,
		// hoverIntent settings
		sensitivity: 1,
		interval: 50
	};
})(jQuery);