//
//	tootip jquery plugin
//	(c) revolweb 2011
//	@ picios
//
(function($){
 
    $.fn.extend({
         
        //pass the options variable to the function
        rwTooltip: function(options) {
 
            //Set the default values, use comma to separate the settings, example:
            var defaults = {
                showDelay: 300,
                offsetX : 10,
                offsetY : 10
            }
                 
            var options =  $.extend(defaults, options);
            
            var tip = null;
			$("body").prepend("<div id=\"rwTooltip\"></div>");
			var tooltip = $("#rwTooltip");
 
            return this.each(function() {
                var o = options;
                var obj = $(this);
                
                obj.hover(
                
					function() {
						$(this).children().attr("title","");
						
						tip = $(this).find('.tip:first-child');	
						
						if (tip == undefined || tip.html() == null) {
							tip = $(this).next(".tip");
						}
						
						if (tip != undefined && tip.html() != null) {
							tooltip.html(tip.html());
							_ticking = setTimeout(function() {	tooltip.show() }, o.showDelay);
						}

					}, function() {
						clearTimeout(_ticking);
						tooltip.hide(); //Hide tooltip
				}).mousemove(function(e) {
					
					if (tip == undefined || tip == null) {
						return false;
					}
					
					_left = e.pageX+o.offsetX;
					_top = e.pageY+o.offsetY;
					
					_controllX = $(window).width() - e.clientX - tip.width();
					_controllY = $(window).height() - e.clientY - tip.height();

					if (_controllY<0) {
						_top = _top - tip.height() - 3*o.offsetY;
					}
					
					if (_controllX<0) {
						_left = _left - tip.width() - 3*o.offsetX;
					}

					tooltip.css({  
						left: _left,
						top: _top
						});
				}).mouseout(function(e) {
					//clearTimeout(_ticking);
					tooltip.hide();
				});
            });
        }
    });
     
})(jQuery);

