//style-my-tootltips by malihu (http://manos.malihu.gr)
//plugin home http://manos.malihu.gr/style-my-tooltips-jquery-plugin
(function($){  
 $.fn.style_my_tooltips = function(options) {  
	var defaults = {  
		tip_follows_cursor: "on", 
		tip_delay_time: 1000
	};
	var options = $.extend(defaults, options);
	
    return this.each(function() {
    	var smtTip = null;
    	var state = 0;
    	
    	if (!this.title) return;

		$(this).hover(function(e) {
			//e.stopImmediatePropagation();
			//e.preventDefault();
			//e.stopPropagation();
			
			if (!smtTip) createTooltip();
			
			var $this=$(this);

			if (!state) {
				$this.data("smtTitle",$this.attr("title"));
				$this.attr("title","");
				var theTitle=$this.data("smtTitle");
				smtTip.empty().append(theTitle);
				showTooltip();
			}
			
			if(options.tip_follows_cursor=="off"){
				smtMouseMove(e);
			} else {
				$(document).bind("mousemove", function(event){
					smtMouseMove(event); 
				});
			}
		}, function() { 
			hideTooltip();
			
			var $this=$(this);

			if(options.tip_follows_cursor!="off"){
				$(document).unbind("mousemove");
			}
			
			$this.attr("title",$this.data("smtTitle"));
		});
    	
    	function createTooltip(){
	    	var idT = 'tooltip'+Math.random();
	    	$("body").append("<div id=\""+idT+"\"></div>");
			smtTip = $('div[id='+idT+']');
	    	smtTip.addClass('myTooltip').hide();
	    	hideTooltip();
    	}
    	
    	function showTooltip(){
    		if (!state)	smtTip.removeClass('myTooltipHide').addClass('myTooltipShow');
    		state = 1;
    	}
    	
    	function hideTooltip(){
    		if (state) smtTip.removeClass('myTooltipShow').addClass('myTooltipHide');
    		state = 0;
    	}
    	
		function smtMouseMove(e){
			smtMouseCoordsX=e.pageX;
			smtMouseCoordsY=e.pageY;
			smtTipPosition();
		}
		
		function smtTipPosition(){
			var cursor_tip_margin_x=0; //horizontal space between the cursor and tooltip
			var cursor_tip_margin_y=24; //vertical space between the cursor and tooltip
			
			var leftOffset=smtMouseCoordsX+cursor_tip_margin_x+$(smtTip).outerWidth();
			var topOffset=smtMouseCoordsY+cursor_tip_margin_y+$(smtTip).outerHeight();
			if(leftOffset<=$(window).width()){
				smtTip.css("left",smtMouseCoordsX+cursor_tip_margin_x);
			} else {
				var thePosX=smtMouseCoordsX-(cursor_tip_margin_x)-$(smtTip).width();
				smtTip.css("left",thePosX);
			}
			if(topOffset<=$(window).height()){
				smtTip.css("top",smtMouseCoordsY+cursor_tip_margin_y);
			} else {
				var thePosY=smtMouseCoordsY-(cursor_tip_margin_y)-$(smtTip).height();
				smtTip.css("top",thePosY);
			}
		}
		
		function smtTip_fadeIn(){
			smtTip.fadeTo("fast",1,function(){clearInterval(smtTip_delay);});
		}
	});  
 };  
})(jQuery);  
