/*
 *
 * Options:
 *	vertical - where to vertically align the popup in relation to the target, top, middle, bottom or px offset
 *	horizontal - where to horizontally align the popup in relation to the target, left, right, center or px offset
 *	top - To be added to vertical
 *	left - To be added to horizontal
 *	ajax - whether to uses an ajax request, true or false
 *	hasTail - whether the popup has a tail - true or false
 *	effectTime - the timing of the effect
 *	text: the text for the box or the url if ajax is set to true
 *	formatData: a function that can format ajax data: format function(data) . must returns the formatted data.
 *	complete: a callback after the popup has been created
 */

(function($)
{
	$.fn.popup = function(options)
	{

		var defaults = {
			vertical: 'top',
			horizontal: 'center',
			top: 0,
			left: 0,
			ajax: true,
			popupClass: 'jqueryPopup',
			hasTail: true,
			effectTime: 400,
			text: '',
			formatData: null,
			complete: null
		};

		var options = $.extend(defaults, options);
		var data = '';
		var currentPopup = null;
		var obj = null;

		return this.each(function()
		{
			obj = $(this);

			if( options.ajax )
			{
				$.getJSON(options.text, function(returnData)
				{
					data = returnData;
					if( options.formatData )
					{
						data = options.formatData(returnData);
					}

					addHover();
				});
			}
			else
			{
				data = options.text;
				addHover();
			}
		});


		function addHover()
		{
			obj.mouseover(doPopup);

			obj.mouseout(clearPopup);
		}

		function doPopup()
		{
			//
			// Hover Over
			//
			removeExistingPopup();

			var popup = createPopup();
			$('body').append(popup);

			var objPos = findPos(obj[0]);
			var popupPos = findPos(popup[0]);
			popup.css('position', 'absolute');
			popup.find('.middle .center').html(data);

			var top = 0;
			switch(options.vertical)
			{
				case 'top':
					top = objPos.top - popup.outerHeight();
					break;

				case 'middle':
					top = objPos.top + (obj.outerHeight()/2) - (popup.outerHeight()/2);
					break;

				case 'bottom':
					top = objPos.top + obj.outerHeight();
					break;

				default:
					top = objPos.top + options.vertical;
			}

			var left = 0;
			switch(options.horizontal)
			{
				case 'left':
					left = objPos.left - popup.outerWidth();
					break;

				case 'right':
					left = objPos.left + obj.outerWidth();
					break;

				case 'center':
					left = objPos.left + (obj.outerWidth() / 2) - (popup.outerWidth()/2);
					break;

				default:
					left = objPos.left + options.horizontal;
			}

			top += options.top;
			left += options.left;

			popup.css('top', top + 5);
			popup.css('left', left);
			//popup.css('display', 'none');

			popup.animate({opacity: 1,top: '-=' + 5 + 'px'}, {duration:options.effectTime,complete:options.complete});
		}

		function clearPopup(event, count)
		{
			if( !$(event.relatedTarget).parents('.' + options.popupClass).length > 0 || !event )
			{
				if( currentPopup )
				{
					currentPopup.fadeOut(500);
				}
			}
		}

		/*
		 * Remove any existing popup
		 * 
		 */
		function removeExistingPopup()
		{
			var existingPopups = $('.' + options.popupClass);
			if( existingPopups.length >= 1)
			{
				for( var i=0; i<=(existingPopups.length-1);i++)
				{
					$(existingPopups[i]).remove();
				}
			}
		}

		function createPopup()
		{
			var popup = $('<table class="' + options.popupClass + '" cellpadding="0" cellspacing="0"></table>');

			var row = '<td class="left"></td><td class="center"></td><td class="right"></td>';

			var topRow = $('<tr class="top"></tr>');
			topRow.append(row)
			
			var middleRow = $('<tr class="middle"></tr>');
			middleRow.append(row)
			
			var bottomRow = $('<tr class="bottom"></tr>');
			bottomRow.append(row)

			if( options.hasTail )
			{
				var tail = $('<div class="tail"></div>');

				bottomRow.find('.left').append(tail);
			}

			popup.append(topRow);
			popup.append(middleRow);
			popup.append(bottomRow);

			popup.mouseout(clearPopup);

			currentPopup = popup;
			return popup;
		}

		function findPos(obj)
		{
			var curleft = curtop = 0;

			if (obj.offsetParent)
			{
				curleft = obj.offsetLeft
				curtop = obj.offsetTop
				while (obj = obj.offsetParent)
				{
					curleft += obj.offsetLeft
					curtop += obj.offsetTop
				}
			}
			return {left:curleft,top:curtop};
		}

	};
})(jQuery);
