/*
 * 	Easy Tooltip 1.0 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/4380/easy-tooltip--jquery-plugin
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
(function($) {

	$.fn.easyTooltip = function(options){
	  
		// default configuration properties
		var defaults = {	
			xOffset: 0,		
			yOffset: 10,
			tooltipId: "easyTooltip",
			clickRemove: false,
			content: "",
			useElement: ""
		}; 
			
		var options = $.extend(defaults, options);  
		var content;
				
		this.each(function() {  				
			var title = $(this).attr("title");				
			$(this).hover(function(e){											 							   
				content = (options.content != "") ? options.content : title;
				content = (options.useElement != "") ? $("#" + options.useElement).html() : content;
				$(this).attr("title","");									  				
				if (content != "" && content != undefined){
					// detect specific tooltips that need to be offset to the left
					if (options.tooltipId == "easyTooltip2") {			
						$("body").append("<div id='"+ options.tooltipId +"'>"+ content +"</div>");		
						$("#" + options.tooltipId)
							.css("position","absolute")
							.css("top",(e.pageY - $('#easyTooltip2').height() - 15) + "px")
							.css("left",(e.pageX - $('#easyTooltip2').width() - 22) + "px")  					
							.css("display","none")
							.fadeIn("fast")
							//alert(options.tooltipId);
					} else {
						$("body").append("<div id='"+ options.tooltipId +"'>"+ content +"</div>");		
						$("#" + options.tooltipId)
							.css("position","absolute")
							//.css("top",(e.pageY - options.yOffset) + "px")
							.css("top",(e.pageY - $('#easyTooltip1').height() - 15) + "px")
							.css("left",(e.pageX + options.xOffset) + "px")						
							.css("display","none")
							.fadeIn("fast")
					}
				}
			},
			function(){	
				$("#" + options.tooltipId).remove();
				$(this).attr("title",title);
			});
			   	   
			$(this).mousemove(function(e){
				// detect specific tooltips that need to be offset to the left
				if (options.tooltipId == "easyTooltip2") {
					$("#" + options.tooltipId)
						.css("top",(e.pageY - $('#easyTooltip2').height() - 15) + "px")
						.css("left",(e.pageX - $('#easyTooltip2').width() - 22) + "px")
				} else {
				    $("#" + options.tooltipId)
						//.css("top",(e.pageY - options.yOffset) + "px")
						.css("top",(e.pageY - $('#easyTooltip1').height() - 15) + "px")
						.css("left",(e.pageX + options.xOffset) + "px")
				}					
			});  
			if(options.clickRemove){
				$(this).mousedown(function(e){
					$("#" + options.tooltipId).remove();
					$(this).attr("title",title);
				});				
			}
		});
	  
	};

})(jQuery);
