/*
 * jQuery Tip Plugin 
 * Version: 1.0 (Feb 03 2009)
 * Requires: jQuery v1.3.1
 * 
 * Order to put elements: AJAX, Content
 *
 */

var ajax_cache_data = {};

(function($){

$.fn.tip = function(options) {
	// Extend our default options with those provided.
	var opts = $.extend({}, $.fn.tip.defaults, options);
	var hover_box = $('<div class="' + opts.class_name + '"></div>').appendTo("body");
	$(hover_box).css({"position": "absolute", 
					  "zIndex": 99, 
					  "display": "none"});

	// Add event handlers to each item 
	$(this).each(function() {

		$(this).mouseover(function(mouse) {
			// Save a reference to 
			var hover_element = this;
			// Delay the loading of the tip by hover_delay.
			this.timeout_id = window.setTimeout(function() {
				// Weather to use AJAX or just load something on the page.
				if(opts.url) {
					opts.ajax_callback(hover_element);

					var tmp_params = [];
					for (var x in opts.url_parameters) {
						tmp_params.push([x, opts.url_parameters[x]].join('='));
					}

					var tmp_url = [opts.url, tmp_params.join('&')].join('?');
					if (opts.ajax_cache === true && ajax_cache_data[tmp_url]) {
						$(hover_box).html(ajax_cache_data[tmp_url]);
					} else {
						$.get(opts.url, opts.url_parameters, function(data) {							
							$(hover_box).html(data);

							// only if cacheing is enabled
							if(opts.ajax_cache === true) {
								ajax_cache_data[tmp_url] = data;
							}
						});
					}
				} else if(opts.content) {
					$(hover_box).html(opts.content);
				}

				$(hover_box).fadeIn(opts.fadein);	
			}, opts.hover_delay);
		});

		//
		// Note the difference between
		//  * mouse.clientX
		//  * mouse.screenX
		//  * mouse.pageX
		//
		$(this).mousemove(function(mouse) {
			// Get the hover box dimensions for calculations.
			var hover_box_width = hover_box.outerWidth();
			var hover_box_height = hover_box.outerHeight();
			
			// Determine if the box shows off the RIGHT side of the screen
			if((mouse.clientX + opts.tip_offset + hover_box_width) > $(window).width()) {
				// Make the box appear to the left of the cursor
				left_position = (mouse.pageX - opts.tip_offset - hover_box_width);
			} else {
				// Make the box appear to the right of the cursor.
				left_position = mouse.pageX + opts.tip_offset;
			}
			
			// Determine if the box shows off the BOTTOM of the screen
			if((mouse.clientY + opts.tip_offset + hover_box_height) > $(window).height()) {
				// Make the box appear to the left of the cursor
				top_position = mouse.pageY - opts.tip_offset - hover_box_height;
			} else {
				// Make the box appear to the right of the cursor.
				top_position = mouse.pageY + opts.tip_offset;
			}
			
			// Set the CSS to position the hover tip
			$(hover_box).css({left: left_position, top: top_position});
		});
		
		$(this).mouseout(function(mouse) {
			window.clearTimeout(this.timeout_id);
			$(hover_box).fadeOut(opts.fadeout);
		});
	
	});
};

// override these globally if you like (they are all optional)
$.fn.tip.defaults = {
	url:			null,
	url_parameters: "",
	content:		null,
	background:		"#FFFFFF",
	class_name:		"tooltip",
	fadein:			200,
	fadeout:		200,		// Speed when element fades out.
	hover_delay:	300,		// Number of sections mouse has to be over element.
	tip_offset: 	15,			// The distance the hover box from the cursor.
	pre_mouseover:	function(hover_element){},
	ajax_callback:	function(){},
	ajax_cache:		false
};

})(jQuery);
