/*******************************************************************************
 jquery.mb.components
 Copyright (c) 2001-2010. Matteo Bicocchi (Pupunzi); Open lab srl, Firenze - Italy
 email: info@pupunzi.com
 site: http://pupunzi.com

 Licences: MIT, GPL
 http://www.opensource.org/licenses/mit-license.php
 http://www.gnu.org/licenses/gpl.html
 ******************************************************************************/

/*
 * Name:jquery.mb.tooltip
 * Version: 1.6
 * dependencies: jquery.timers.js, jquery.dropshadow.js
*/

(function($){
  jQuery.fn.mbTooltip = function (options){
    return this.each (function () {
      this.options = {
        live:true,
        opacity:.9,
        wait:2000,
        timePerWord:70,
        cssClass:"default",
        hasArrow:true,
        imgPath:"images/",
        hasShadow:true,
        shadowColor:"black",
        shadowLeft:1,
        shadowTop:1,
        fade:200
      };
      $.extend(this.options, options);
      if (this.options.live) $("[title]", this).live("mouseover",function(){$(this).mbTooltip(options);});
      if(!$(this).is("[title]")) return;
			ttEl=$(this);
      var wait=this.options.wait;
      var hasShadow=this.options.hasShadow;
      var fade=this.options.fade;
      var myOptions=this.options;
      $(ttEl).each(function(){
        $(this).attr("tooltip", $(this).attr("title"));
        $(this).removeAttr("title");
        $(this).attr("tooltipEnable","true");
        var theEl=$(this);
        var ttCont= theEl.attr("tooltip");
        var hover=$.browser.msie?"mouseenter":"mouseover";
        $(this).bind(hover,function(e){
          $(document).mb_getXY();
          $(this).one("mouseout",function(){
            $(this).stopTime();
            $(this).deleteTooltip(hasShadow,fade);
          }).one("click",function(){
            $(this).stopTime();
            $(this).deleteTooltip(hasShadow,fade);
          });
          $(this).oneTime(wait, function() {
            if ($(this).attr("tooltipEnable")=="true")
              $(this).buildTooltip(ttCont,myOptions,e);
          });
        });
      });
    });
  };

  var mbX = 0;
  var mbY = 0;
	var n_border = 0;
	var o_arrow;
  $.fn.extend({
    mb_getXY:function(){
      $(document).bind("mousemove", function(e) {
        mbX = e.pageX;
        mbY = e.pageY;
      });
      return {x:mbX,y:mbY};
    },
    buildTooltip: function(cont,options){
      this.options={};
      $.extend (this.options, options);
      var parent=$(this);
      $("body").append("<div id='tooltip'></div>");
      var imgUrl=this.options.imgPath+"tooltip_arrows.png";
      $("#tooltip").html(cont);
      $("#tooltip").addClass(this.options.cssClass);
      if (this.options.hasArrow){
        $("#tooltip").prepend("<div id='ttimg' style='background-image:url(\""+imgUrl+"\")'></div>");
				o_arrow=$("#ttimg");
        o_arrow.css({
          position:"absolute",
          opacity:.5
        });
        o_arrow.addClass("top");
      }
      $("#tooltip").css({
        position:"absolute",
        top:$(document).mb_getXY().y,
        left:$(document).mb_getXY().x,
        opacity:0
      });
			n_border=parseInt($("#tooltip").css("border-top-width"));
			n_margin=parseInt($("#tooltip").css("margin-top"));
      $("#tooltip").findBestPos(parent,this.options.imgPath,this.options.anchor);
      $(document).unbind("mousemove");
      if (this.options.hasShadow) {
        $("#tooltip").dropShadow({left: this.options.shadowLeft, top: this.options.shadowTop, blur: 2, opacity: 0.3, color:this.options.shadowColor});
        $(".dropShadow").css("display","none");
        $(".dropShadow").fadeIn(this.options.mb_fade);
      }
      $("#tooltip").fadeTo(this.options.mb_fade,this.options.opacity,function(){});
      var timetoshow=3000+cont.length*this.options.timePerWord;
      var hasShadow=this.options.hasShadow;
      var fade=this.options.mb_fade;
      $(this).oneTime(timetoshow,function(){$(this).deleteTooltip(hasShadow,fade);});
    },
    deleteTooltip: function(hasShadow,fade){
      var sel=hasShadow?"#tooltip,.dropShadow":"#tooltip";
      $(sel).fadeOut(fade,function(){$(sel).remove();});
    },
    findBestPos:function(parent,imgPath,anchor){
      var theEl=$(this);
      var ww=$(window).width()+$(window).scrollLeft();
      var wh=$(window).height()+$(window).scrollTop();
      var w=theEl.outerWidth();
			var h=theEl.outerHeight(true);
			var ot=theEl.offset().top;
			var ol=theEl.offset().left;
			// check if tip overlaps bottom
			if((ot+h)>wh){
				t=ot-theEl.outerHeight()-20;
				t=t<0?0:t;
				b_bottom=false;
			}else{
				t=ot;
				b_bottom=true;
			}
			if((ol+w)>ww-10){
				l=ol-w;
				l=l<0?0:l;
				b_right=false;
			}else{
				l=ol;
				b_right=true;
			}
      theEl.css({top:t, left:l});
			w=theEl.outerWidth();			
      if (b_bottom){
				if(b_right){
					$("#ttimg").css({top:"0", left:"0"});
					$("#ttimg").css({"background-position":"0 0"});
				}else{
					$("#ttimg").css({top:"0", left:(w-$("#ttimg").outerWidth()-n_border*2)+"px"});
					$("#ttimg").css({"background-position":"-12px 0"});
				}
      }else{
				if(b_right){
					$("#ttimg").css({top:(h-$("#ttimg").outerHeight()-n_border*2-n_margin*2)+"px", left:"0"});
					$("#ttimg").css({"background-position":"-36px 0"});
				}else{
					$("#ttimg").css({top:(h-$("#ttimg").outerHeight()-n_border*2-n_margin*2)+"px", left:(w-$("#ttimg").outerWidth()-n_border*2)+"px"});
					$("#ttimg").css({"background-position":"-24px 0"});
				}			
			}
    },
    disableTooltip:function(){
      $(this).find("[tooltip]").attr("tooltipEnable","false");
    },
    enableTooltip:function(){
      $(this).find("[tooltip]").attr("tooltipEnable","true");
    }
  });

  $(function(){
    //due to a problem of getter/setter for select
    $("select[title]").each(function(){
      var selectSpan=$("<span></span>");
      selectSpan.attr("title",$(this).attr("title"));
      $(this).wrapAll(selectSpan);
      $(this).removeAttr("title");
    });
  });

})(jQuery);
