(function(B){var A=1;B.fn.dropShadow=function(C){var D=B.extend({left:4,top:4,blur:2,opacity:0.5,color:"black",swap:false},C);var E=B([]);this.not(".dropShadow").each(function(){var N=B(this);var J=[];var G=(D.blur<=0)?0:D.blur;var M=(G==0)?D.opacity:D.opacity/(G*8);var O=(D.swap)?A:A+1;var F=(D.swap)?A+1:A;var P;if(this.id){P=this.id+"_dropShadow"}else{P="ds"+(1+Math.floor(9999*Math.random()))}B.data(this,"shadowId",P);B.data(this,"shadowOptions",C);N.attr("shadowId",P).css("zIndex",O);if(N.css("position")!="absolute"){N.css({position:"relative",zoom:1})}bgColor=N.css("backgroundColor");if(bgColor=="rgba(0, 0, 0, 0)"){bgColor="transparent"}if(bgColor!="transparent"||N.css("backgroundImage")!="none"||this.nodeName=="SELECT"||this.nodeName=="INPUT"||this.nodeName=="TEXTAREA"){J[0]=B("<div></div>").css("background",D.color)}else{J[0]=N.clone().removeAttr("id").removeAttr("name").removeAttr("shadowId").css("color",D.color)}J[0].addClass("dropShadow").css({height:N.outerHeight(),left:G,opacity:M,position:"absolute",top:G,width:N.outerWidth(),zIndex:F});var L=(8*G)+1;for(K=1;K<L;K++){J[K]=J[0].clone()}var K=1;var I=G;while(I>0){J[K].css({left:I*2,top:0});J[K+1].css({left:I*4,top:I*2});J[K+2].css({left:I*2,top:I*4});J[K+3].css({left:0,top:I*2});J[K+4].css({left:I*3,top:I});J[K+5].css({left:I*3,top:I*3});J[K+6].css({left:I,top:I*3});J[K+7].css({left:I,top:I});K+=8;I--}var H=B("<div></div>").attr("id",P).addClass("dropShadow").css({left:N.position().left+D.left-G,marginTop:N.css("marginTop"),marginRight:N.css("marginRight"),marginBottom:N.css("marginBottom"),marginLeft:N.css("marginLeft"),position:"absolute",top:N.position().top+D.top-G,zIndex:F});for(K=0;K<L;K++){H.append(J[K])}N.after(H);E=E.add(H);B(window).resize(function(){try{H.css({left:N.position().left+D.left-G,top:N.position().top+D.top-G})}catch(Q){}});A+=2});return this.pushStack(E)};B.fn.redrawShadow=function(){this.removeShadow();return this.each(function(){var C=B.data(this,"shadowOptions");B(this).dropShadow(C)})};B.fn.removeShadow=function(){return this.each(function(){var C=B(this).shadowId();B("div#"+C).remove()})};B.fn.shadowId=function(){return B.data(this[0],"shadowId")};B(function(){var C="<style type='text/css' media='print'>";C+=".dropShadow{visibility:hidden;}</style>";B("head").append(C)})})(jQuery);

//script for tooltip
jQuery.fn.mbTooltip = function (options){
  return this.each (function () {
    this.options = {
      opacity : .9,
      wait:2000,
      timePerLetter:70,
      cssClass:"default",
      hasArrow:true,
      imgPath:"images/",
      hasShadow:true,
      shadowColor:"black",
      shadowLeft:1,
      ancor:"mouse", //"parent",
      shadowTop:1,
      mbFade:200
    };
    $.extend (this.options, options);
    var ttEl=$(this).is("[tooltip]")? $(this): $(this).find("[tooltip]");
    var wait=this.options.wait;
    var hasShadow=this.options.hasShadow;
    var mbFade=this.options.mbFade;
    var myOptions=this.options;
    $(ttEl).each(function(){
      this.tt=true;
      var theEl=$(this);
      var ttCont= theEl.attr("tooltip");
      var hover=$.browser.msie?"mouseenter":"mouseover";
      $(this).bind(hover,function(e){
        if (myOptions.ancor=="mouse") $(this).getXY();
        $(this).one("mouseout",function(){
          if (myOptions.ancor=="mouse") $(this).unbind("mousemove",$(this).getXY());
          $(this).stopTime();
          $(this).deleteTooltip(hasShadow,mbFade);
        }).one("click",function(){
          $(this).stopTime();
          $(this).deleteTooltip(hasShadow,mbFade);
        });
        $(this).oneTime(wait, function() {
          if (this.tt)
            $(this).buildTooltip(ttCont,myOptions,e);
        });
      });
    });
  });
};


var X = 0;
var Y = 0;

$.fn.extend({
  getXY:function(){
    $(this).bind("mousemove", function(e) {
      X = e.pageX;
      Y = e.pageY;
    });
  },

  buildTooltip: function(cont,options,e){
    this.options={};
    $.extend (this.options, options);
    var parent=$(this);
    $("body").append("<div id='tooltip'></div>");
    var imgUrl=this.options.imgPath+"up.png";
    $("#tooltip").html(cont);
    $("#tooltip").addClass(this.options.cssClass)
    if (this.options.hasArrow){
      $("#tooltip").prepend("<img id='ttimg' src='"+imgUrl+"'>");
      $("#ttimg").css({
        position:"absolute",
        opacity:.5
      })
      $("#ttimg").addClass("top");
    }
    $("#tooltip").css({
      position:"absolute",
      top:  this.options.ancor=="mouse"?Y+7:parent.offset().top+(parent.outerHeight()),
      left:this.options.ancor=="mouse"?X+7:parent.offset().left,
      opacity:0
    })
    $("#tooltip").findBestPos(parent,this.options.imgPath,this.options.ancor);
    if (this.options.ancor=="mouse") $(this).unbind("mousemove");
    if (this.options.hasShadow) {
      $("#tooltip").dropShadow({left: 1, top: 1, blur: 2, opacity: 0.3, color:this.options.shadowColor});
      $(".dropShadow").css("display","none")
      $(".dropShadow").fadeIn(this.options.mbFade);
    }
    $("#tooltip").fadeTo(this.options.mbFade,this.options.opacity,function(){});
    var timetoshow=3000+cont.length*this.options.timePerLetter;
    var hasShadow=this.options.hasShadow;
    var mbFade=this.options.mbFade;
    $(this).oneTime(timetoshow,function(){$(this).deleteTooltip(hasShadow,mbFade);});
  },
  deleteTooltip: function(hasShadow,mbFade){
    var sel=hasShadow?"#tooltip,.dropShadow":"#tooltip";
    $(sel).fadeOut(mbFade,function(){$(sel).remove();});
  },
  findBestPos:function(parent,imgPath,ancor){
    var theEl=$(this);
    var ww= $(window).width()+$(window).scrollLeft();
    var wh= $(window).height()+$(window).scrollTop();
    var w=theEl.outerWidth();
    theEl.css({width:w});
    var t=((theEl.offset().top+theEl.outerHeight(true))>wh)? theEl.offset().top-(ancor!="mouse"? parent.outerHeight():0)-theEl.outerHeight()-20 : theEl.offset().top;
    t=t<0?0:t;
    var l=((theEl.offset().left+w)>ww-5) ? theEl.offset().left-(w-(ancor!="mouse"?parent.outerWidth():0)) : theEl.offset().left;
    l=l<0?0:l;
    if (theEl.offset().top+theEl.outerHeight(true)>wh){
      $("#ttimg").attr("src",imgPath+"bottom.png");
      $("#ttimg").removeClass("top").addClass("bottom");
    }
    theEl.css({width:w, top:t, left:l});
  },
  disableTooltip:function(){
    $(this).find("[tooltip]").each(function(){this.tt = false;});
  },
  enableTooltip:function(){
    $(this).find("[tooltip]").each(function(){this.tt = true;});
  }
});


$(function(){
  //due to a problem of getter/setter for select
  $("select[toolTip]").each (function(){
    var selectSpan=$("<span></span>");
    selectSpan.attr("toolTip",$(this).attr("toolTip"));
    $(this).removeAttr("toolTip");
    $(this).wrapAll(selectSpan);
  });
  $("[tooltip]").livequery(function(){
    $(this).mbTooltip({
      opacity : .8,       //opacity
      wait:800,           //before show
      cssClass:"default",  // default = default
      imgPath: contextPath +"/commons/js/tooltip/images/", //used only if hasArrow
      timePerLetter:90,      //time to show in milliseconds per letter
      hasArrow:false,
      hasShadow:true,
      ancor:"mouse", // or parent
      color:"white",
      shadowColor:"black",
      mbFade:500
    });
  });
});
