// JavaScript Document
//
(function($) {
//
var error = true;

// plugin definition
$.fn.tooltip = function(options) {
	$this = $(this);
	var message = '';
	$.fn.tooltip.defaults  = $.extend({}, $.fn.tooltip.defaults, options);
	
	$this.each(function(){
	  /*$(this).bind('mouseover',function(){
	    $.fn.tooltip.build($(this).attr("title"));
		$.fn.tooltip.pos(this);
		$("#tooltip").fadeIn();
		$(this).bind('mouseout',function(){$("#tooltip").remove();})
	  })*/
		
		   timer = 0;
		   $(this).mouseover(function(){
			  
			  $("#tooltip").remove();
			  clearTimeout(timer)
			  $.fn.tooltip.build(
				$(this).next("span").html()
			  );
			  $.fn.tooltip.pos(this);
			  $("#tooltip").fadeIn();
			  
			});
			$(this).mouseout(function(){
			  if(!$.fn.tooltip.defaults.clickable)//NON CLICKABLE
				  $("#tooltip").remove();
				else{
				  var a = this;
				  clearTimeout(timer)
				  if($("#tooltip").is(":visible"))timer = setTimeout(function(){$("#tooltip").remove();},500); 
				  $("#tooltip").bind('mouseover',function(){clearTimeout(timer);})
				  $("#tooltip").bind('mouseout',function(){clearTimeout(timer);timer = setTimeout(function(){$("#tooltip").remove();},800)})
				  //}).filter(a );
				      
				  
				}
						  
		   });
		  
		
	})//EOF Bind
};
//
$.fn.tooltip.defaults = {
  rightM : 10,
  topM : 10,
  pos : "top-right", //could be top,bottom,top-right,top-left
  fix : true,
  clickable : true,
  timeout : null 
}

//BUILD
$.fn.tooltip.build = function(content){
  $("body").append('<div id="tooltip">' + content + '</div>');
  $("#tooltip").css({'position':'absolute'})
  $("#tooltip").addClass('tooltip')
}
$.fn.tooltip.remove = function(){
  //$("#tooltip").remove();
}
$.fn.tooltip.pos = function(el){
  var offset = $(el).offset();
  var l = offset.left + $(el).width()+ $.fn.tooltip.defaults.rightM;
  var t = offset.top -$("#tooltip").innerHeight()-$.fn.tooltip.defaults.topM;
  if(t<0)t = $("#tooltip").height()+$.fn.tooltip.defaults.topM;
  var tip = $("#tooltip");
  switch($.fn.tooltip.defaults.pos){
    case "top-right":tip.css({"left":l+"px","top":t}); break;
    case "top":
	  t = offset.top - $("#tooltip").innerHeight();
	  if(t<0)t = $("#tooltip").height()+$.fn.tooltip.defaults.topM;
	  tip.css({"left":offset.left,"top":+t}); 
	break;
  }
}
//
})(jQuery);


