/*
 * 	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: 5,		
			yOffset: 10,
			tooltipId: "easyTooltip",
			clickRemove: false,
			useElement: "",
			position: "left"
		}; 
			
		var options = $.extend(defaults, options);  
		var content;
				
		this.each(function() {  				
			var title = $(this).attr("title");	
			var tipWidth;
			$(this).hover(function(e){											 							   
				$(this).attr("title","");
				$("#" + options.useElement).show();
				if($.browser.msie) tipWidth = $("#" + options.useElement).outerWidth(true);
				else tipWidth = $("#" + options.useElement).width();
				$("#" + options.useElement).width(tipWidth);
				
				
				// MP: Using e.target instead of e.pageY/pageX because it doesnt work in modal windows
				var targ;
				if (!e) var e = window.event;
				if (e.target) targ = e.target;
				else if (e.srcElement) targ = e.srcElement;
				if (targ.nodeType == 3) // defeat Safari bug
					targ = targ.parentNode;

				
				var positionX;
				var positionY = $(targ).position().top - options.yOffset;
				if(options.position === "right"){
					positionX = ($(targ).position().left + options.xOffset);
				} else {
					positionX = ($(targ).position().left - tipWidth - options.xOffset);
				}
				
				$("#" + options.useElement)
						.css("position","absolute")
						.css("top", positionY + "px")
						.css("left", positionX + "px")
						.css("display","none")
						.fadeIn("fast");	
				
			},
			function(){	
				$("#"  + options.useElement).hide();
				$(this).attr("title",title);
			});	
			$(this).mousemove(function(e){
				
				// MP: Using e.target instead of e.pageY/pageX because it doesnt work in modal windows
				var targ;
				if (!e) var e = window.event;
				if (e.target) targ = e.target;
				else if (e.srcElement) targ = e.srcElement;
				if (targ.nodeType == 3) // defeat Safari bug
					targ = targ.parentNode;
				
				var positionX;
				var positionY = $(targ).position().top - options.yOffset;
				if(options.position == "right"){
					positionX = ($(targ).position().left + options.xOffset);
				} else {
					positionX = ($(targ).position().left - tipWidth - options.xOffset);
				}
				
				$("#" + options.useElement)
					.css("top", positionY + "px")
					.css("left",positionX + "px");									
			});	
			if(options.clickRemove){
				$(this).mousedown(function(e){
					$("#" + options.useElement).hide();
					$(this).attr("title",title);
				});				
			}
		});
	  
	};

})(jQuery);

