/**
 * $Workfile: jquery.tooltip.js $
 * $Revision: 8 $
 * $Modtime: 21/06/10 12:08 $
 * $Author: Aamir.afridi $
 *
 * jQuery simpleTooltip plugin 
 *
 * @author Marius ILIE (http://dev.mariusilie.net) / Improved by Aamir Afridi (http://www.aamirafridi.com - info@aamirafridi.com)
 * @date 2009-08-06
 * @example $("link").simpletooltip({html : 'My Tooltip', mouseOffsetX : 10 , mouseOffsetY : 12})
 * @example $("link").simpletooltip({ opacity : 0.8, css : { 'padding' : '8px' , 'border' : '2px solid #ccc' } })
 * @desc create a tooltip effect for any html element's title attribute or any text provided
 *
 * @name simpletooltip
 * @type jQuery
 * @param String - html - custom text which will overwrite the text in element's title attribute
 * @param String - errorText - text which will appear if there is no title attribute found or html provide to the plugin otherwise leaving this empty will diable the plugin
 * @param Number - cornerRadius - the radius of the round corner of the tooltip. Work only on Firefox 3+ and Webkit-base browsers
 * @param String - cssClass - additional css class
 * @param Number - opacity - the transparency of the tooltip. From 0.0 to 1. Should work on IE but haven't tested
 * @param String / Number - fadeSpeed - the animation speed of the tooptip to fadeIn
 * @param Number  - mouseOffsetX - the position of the tooltip on x-axis
 * @param Number  - mouseOffsetY - the position of the tooltip on y-axis
 * @param Array  - css - css properties will be applied on the tooltip
 * @cat Plugin
 */
 

(function($){ $.fn.simpletooltip = function(options){
	var	 o = $.extend({}, $.fn.simpletooltip.defaults, options);
	//return this;
	return this.each(function() {					  
		var text = ($(this).attr("title")!='') ? $(this).attr("title") : o.errorText;
		if(o.html=='' && (text=='' || text=='undefined')) return;
		$(this).hover(
			function(e)
			{
				$(this).attr('title','');
				$('.simpleToolTip').remove();//Remove any previous tooltips
				var tipX = e.pageX ,
					tipY = e.pageY ,
					 $st = $('<div></div>')
						.addClass(o.cssClass+' simpleToolTip')
						.appendTo('body')
						.html(($.trim(o.html)!='') ? o.html : text)
						.fadeTo(o.fadeSpeed,o.opacity)
						.attr('style','z-index:10000000 !important;')
						.css({
						   'position'				: 'absolute',
						   'display'				: 'none',
						   '-moz-border-radius'		: o.cornerRadius,
						   '-webkit-border-radius'	: o.cornerRadius,
						   'left'					: tipX,
						   'top'					: tipY,
						   'width'					: o.width
					});
					if(o.css!='') $st.css(o.css);
					
			},
			function(e)
			{
				$('.simpleToolTip').remove();
				$(this).attr("title", text);
			}
		)
		.click(function(){
			$('.simpleToolTip').remove();
		})
		.mousemove(function(e){
			//if($('.simpleToolTip').hasClass('toolTipMoved')) return;
			var tipX = e.pageX + o.mouseOffsetX - parseInt($('body').offset().left,10),
				tipY = e.pageY + o.mouseOffsetY,
				tipWidth = $('.simpleToolTip').outerWidth(true),
				tipHeight = $('.simpleToolTip').outerHeight(true);
			if(tipX + tipWidth > $(window).scrollLeft() + $(window).width()) tipX = e.pageX - tipWidth;
			if($(window).height()+$(window).scrollTop() < tipY + tipHeight) tipY = e.pageY - tipHeight;
			$('.simpleToolTip').addClass('toolTipMoved').css({'left':tipX,'top':tipY}).fadeIn(o.fadeSpeed);
		});
	 });
	}
	
	$.fn.simpletooltip.defaults = {
		html		: '',
		errorText	: '',//'<b>Error:</b><br />No title attribute found<br />And no Html provide to the plugin.', //Leave it empty which will show nothing
		cornerRadius: 5, 	
		cssClass	: 'ui-state-default',
		opacity		: 0.94,
		fadeSpeed	: 'fast',
		mouseOffsetX: 5,
		mouseOffsetY: 5,
		width		: 150,
		css			: {
						'padding'	: 4,
						//'background': '#F2F3F5',
						//'border'	: '1px solid #A6A7AB',
						'font-size'	: '12px',
						'font-weight': 'normal'
					  }
	};
	
})(jQuery);
