/*
 * jQuery.tooltip
 * Copyright (c) 2009 Michal SImonfy
 */
    //#0071bc
(function($){
	$.fn.tooltip = function(options){
 	var	defaults = {  
      background: '#827964',color: '#fff',rounded: true,opaque: true
	 	 },
	settings = $.extend({},defaults,options);     
  var w = $(window);
 	this.each(function(){
 		$('#tooltip').each(function(){$(this).remove()});
		var $this = $(this);
	 	var title=this.title;
 		//if ($this.is('a') && $this.attr('title') != '') {
 		if ($this.attr('title') != '') {
			this.title = '';
 			$this.hover(function(e){
				$('<div id="tooltip" />')
				.appendTo('body')
				.hide()
		//		.html($.base64Decode(title))    
				.html(title)
				.css({
					backgroundColor: settings.background,
					color: settings.color,
					top: e.pageY + 15,
					left: e.pageX + 15
				})
				.fadeIn(100);
 				if (settings.rounded) {
					$('#tooltip').addClass('tooltip-rounded');
				}
				if (settings.opaque) {
					$('#tooltip').addClass('tooltip-opaque');
				}
			},
 			function() {
 				// mouseout
 				//$('#tooltip').remove();
 				$('#tooltip').each(function(){$(this).remove()});
 			});
	 	}
 		$this.mousemove(function(e){
 		 var max_height = (parseInt(e.pageY) + parseInt($("#tooltip").height())) - w.scrollTop();  
 		 var max_width  = (parseInt(e.pageX) + parseInt($("#tooltip").width())) + 25;
       
 	    if(max_height > w.height()){
        var actual_top = ((e.pageY) - (max_height - w.height()));   
      }
      else{    
        var actual_top = (e.pageY) +15;   
      }         
              
 	    if(max_width > w.width()){
        var actual_left = ((e.pageX) - (max_width - w.width()));   
      }
      else{    
        var actual_left = (e.pageX) +15;   
      }
      
 			$('#tooltip').css({
 				top: actual_top,
 				left: actual_left
 			})
		});
	});
	return this;
 	}
})(jQuery);
