///////////////////////////////////////////////////////////////////////////
//
//		HERE'S WHERE WE ACTUALLY DEFINE SITE USAGE
//
///////////////////////////////////////////////////////////////////////////
(function($) {
$(function()	{
	new wvTooltips($('.wvTip'),	{
		position: {
			x: 'center',
			y: 'above'
		},
		corner: {
			x: 'right'
		},
		offset: {
			x: -11,
			y: -4
		}
	});
});



///////////////////////////////////////////////////////////////////////////
//
//		VERSIONING IS IMPORTANT FOR FUTURE BUG FIXING
//
///////////////////////////////////////////////////////////////////////////
var wvTooltipsDetails = {
	'author': 'Darren Waddell',
	'created': '11/06/08',
	'version': '1.02'
};



///////////////////////////////////////////////////////////////////////////
//
//		LASTLY, THE ACTUAL JAVASCRIPT PLUGIN
//
///////////////////////////////////////////////////////////////////////////

var wvTooltips = function(els, options){ 
	
	var defaults = {
		event: 'hover',
		className: 'tool',
		cursor: '',
		position: {	x: 'right',	y: 'below'	},
		corner: {	x: 'right',	y: 'bottom'	},
		offset: {	x: 0,		y: 0		},
		onShow: function(el)	{	return true;	},
		onHide: function(el)	{	return true;	},
		showDelay: 500,
		hideDelay: 0
	};
	var options = $.extend(defaults, options || {}); 
	var container, sleeve, title, text;
	var delay;
	var els = els.filter('[rel!=]');
	
	var _createTooltip = function()	{
		container = $('<div></div>').addClass(options.className + "-tip").addClass('tip').css({left:'-9999px', top:'0'}).appendTo($('body'));
		sleeve = $('<div></div>').addClass(options.className + "-sleeve").appendTo(container);
		title = $('<h2></h2>').addClass(options.className + "-title").appendTo(sleeve);
		text = $('<div></div>').addClass(options.className + "-text").appendTo(sleeve);
	};
	
	var _storeTooltips = function()	{
		els.each(function()	{
			$.data(this, 'tool-tip', $(this).attr('rel'));
			$(this).removeAttr('rel').removeAttr('alt'); // IE displays alt text over the top
		});
	};
	
	var _show = function(el)	{
		tip = $.data(el, 'tool-tip').split("::");
		if(tip.length > 1)	{
			title.css('display', '').text(tip[0]);	
			text.html(tip[1]);
		}
		else	{
			title.css('display', 'none').text('');	
			text.html(tip[0]);
		}
		_position(el);
		options.onShow(el);
	};
	
	var _position = function(el)	{
		elPos = $(el).offset();
		elWidth = $(el).width();
		elHeight = $(el).height();
		tipWidth = $(container).width();
		tipHeight = $(container).height();
		
		if(options.position.x == 'left')	
			x = elPos.left - tipWidth;
		else if(options.position.x == 'center')	
			x = elPos.left - (tipWidth/2);
		else	x = elPos.left + elWidth;
		
		if(options.position.y == 'above')	y = elPos.top - tipHeight;
		else	y = elPos.top + elHeight;
		
		if(options.corner.x == 'right')		x += elWidth;
		
		container.css({
			'left': x + options.offset.x,
			'top': y + options.offset.y
		});
	};
	
	var _hide = function(el)	{
		container.css('left', '-9999px');
		options.onHide(el);
	};
	
	var _bindEventToElements = function()	{
		if(options.event == 'click')
			els.toggle(	function(e)	{	_show(e.target);	}, 
						function(e)	{	_hide(e.target);	});
		else
			els.hover(function(e)	{
				var el = e.target;
				window.clearTimeout(delay);
				delay = window.setTimeout(function()	{
					_show(el);
				}, options.showDelay);
			}, function(e)	{
				var el = e.target;
				window.clearTimeout(delay);
				delay = window.setTimeout(function()	{
					_hide(el);
				}, options.hideDelay);
			});
	};
	
	var _setCursor = function()	{
		if(options.cursor != '')	els.css('cursor', options.cursor);	
	};
	
	var _init = function()	{
		_createTooltip();
		_storeTooltips();
		_bindEventToElements();
		_setCursor();
	}();
	
};
})(jQuery);