var TooltipHandler = {
  _hoveredElem:null,
  _idCounter:0,
  tooltipHovered:false,
  register:function(elem, defer) {
      //console.debug("Registering element ", elem);
	  if(defer == null) defer = true;
      elem = $(elem);
      this._hoveredElem = elem;
      // dispatch tooltip text
      var tooltipcontainer =  $('#tooltipcontainer').hide();
      tooltipcontainer.attr('class','tooltipcontainer'); // reset class attr
      var cssClass = 'left';

      if(elem.hasClass('right')) cssClass = 'right';
      else if(elem.hasClass('bottom')) cssClass = 'bottom';
      tooltipcontainer.addClass(cssClass);
      
      if(elem.attr('title')!=null && elem.attr('title')!='') {
          tooltipcontainer.find('#tooltipcontent').html(elem.attr('title'));
          tooltipcontainer.css('width','auto');
          // todo find a good solution for title removal
         // tooltipcontainer.addClass("fromtitle");
          //elem.attr('title',null);
      } else {
          var contentholder = elem.next(":first.tooltip");
          if(contentholder.length>0 && $.trim(contentholder.html()).length>0) {
              tooltipcontainer.css('width',contentholder.css('width')); // adopt width attribute
              tooltipcontainer.find('#tooltipcontent').html(contentholder.html());
              tooltipcontainer.find('.hideLink').css('cursor','pointer').bind("click", function() {TooltipHandler.hideTooltip(true);});
        }
      }
      

      // get or create id for hovered element
      var elemId = elem.attr('id');
      if(elemId == null || elemId.length == 0) {
          elemId = 'tooltippeditem-'+(++this._idCounter);
          elem.attr('id', elemId);
      }
      if(defer) window.setTimeout('TooltipHandler._deferred("'+elemId+'")', 1000);
      else this._deferred(elemId);
  },
  deregister:function(elem) {
      //console.debug("De-registering element ", elem);
      //elem = $(elem);
      this._hoveredElem = null;
      window.setTimeout('TooltipHandler.hideTooltip();',1000);
     return;
  },
  hideTooltip:function(force) {
      if(force == undefined) force = false;
      if(!force && (this.tooltipHovered || this._hoveredElem != null)) return;
      //console.debug("Hiding tooltip");
      // hide tooltip and remove text
      var tooltipcontainer = $('#tooltipcontainer');
      var tooltipcontent = tooltipcontainer.find('#tooltipcontent');
      tooltipcontainer.hide();
//      if(tooltipcontainer.is('.fromtitle')) {
//         elem.attr('title') = tooltipcontent.html();
//      }
      tooltipcontent.html('');
  },
  _deferred:function(elemId) {
    // show tooltip if contains text
    if(this._hoveredElem != null && this._hoveredElem.attr('id') == elemId && $('#tooltipcontent').html() != null && $('#tooltipcontent').html().length > 0) {
      //console.debug("Showing tooltip for ", this._hoveredElem);
      var tooltipcontainer = $('#tooltipcontainer');
      this.alignTooltipToElement(tooltipcontainer, elemId);
      tooltipcontainer.show();
    }
  },

   alignTooltipToElement:function(tooltip, elemId) {
    var elem = $('#'+elemId);
    var elemoff = elem.offset({scroll:false});
    
    if(tooltip.hasClass('left')) {
       tooltip.css('left',elemoff.left-tooltip.width()+1);
    } else if(tooltip.hasClass('bottom')) {
       tooltip.css('left',elemoff.left);
    } else {
      tooltip.css('left',elemoff.left+elem.outerWidth()-1);
    }

    if(tooltip.hasClass('bottom')) {
      tooltip.css('top',elemoff.top + elem.height() );
      } else {
        //if(tooltip.height() < elem.height()) {
            tooltip.css('top',elemoff.top);
        //} else {
        //    tooltip.css('top',elemoff.top-12+(elem.height()/2));
        //}
      }
    },
  initTooltips:function() {
    $(".tooltipped").each(function(){
        this.onmouseover = function() {
        	TooltipHandler.register(this);
        } ;
        this.onmouseout = function() {
            TooltipHandler.deregister(this);
        } ;
    });
  $(".clicktipped").each(function(){
        this.onclick = function() {TooltipHandler.register(this, false);} ;
    }).css('cursor','pointer');  
  $("#tooltipcontainer").each(function(){
        this.onmouseover = function() {
            TooltipHandler.tooltipHovered = true;
        } ;
        this.onmouseout = function() {
            TooltipHandler.tooltipHovered = false;
            window.setTimeout('TooltipHandler.hideTooltip();',500)
        } 
  });
  }
};

// initialize all tooltipped items + tooltip container
$(document).ready(function() {
  $("body").append('<div id="tooltipcontainer" style="position:absolute;display:none;" class="tooltipcontainer"><div class="deco">&nbsp;</div><div class="outer"><div class="inner" id="tooltipcontent"></div></div></div>');
  TooltipHandler.initTooltips();  
});
