// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

Ajax.Responders.register({
  onCreate: function() {
    if($('busy') && Ajax.activeRequestCount>0)
      Effect.Appear('busy',{duration:0.5,queue:'end'});
  },
  onComplete: function() {
    if($('busy') && Ajax.activeRequestCount==0)
      Effect.Fade('busy',{duration:0.5,queue:'end'});
  }
});

function init(){
  document.getElementsByTagName("body")[0].appendChild(tooltip.tooltipDiv);
  
  document.getElementsByClassName("hover").each(function(element){
    Event.observe(element, "mouseover", function(e){
        var image_src = element.attributes['rel'].value;
        var name = element.firstChild.innerHTML;
        tooltip.setHTML("<img src=\"" + image_src + "\" alt=\"\" /><br />" +
                        name);
        tooltip.show();
      });
    Event.observe(element, "mouseout", function(){tooltip.hide();});
  });

/** The following code could be used to supress the links in the contact table
 *  on each profile page. I'm not sure if that is a good or bad idea. I 
 *  introduced these links to enable microformats
 * /
  document.getElementsByClassName("contact").each(function(contact_table){
    $A(contact_table.getElementsByTagName("a")).each(function(link) {
      var eventSwallow = function(e) {Event.stop(e);};
      Event.observe(link, "click", eventSwallow);
    });
  });
 /**/
}


var TooltipClass = Class.create();
TooltipClass.prototype = {
    isHidden: true,
    isVisible: false,
    tooltipDiv: null,

    initialize: function() {
      // we are a singleton
      if (window.tooltip) return window.tooltip;

      this.isHidden = true;
      this.gotPosition = false;
      this.isVisible = !this.isHidden;
      this.tooltipDiv = $(Builder.node("div", {className: "tooltip"}));

      window.tooltip = this;
    },

    updateToolTip: function(e){
	    // only update position, if tooltip is visible
      if (tooltip.isHidden) return;
      if (tooltip.gotPosition) return;
      tooltip.gotPosition = true;

      if (document.all) {
        x = e.clientX + document.documentElement.scrollLeft;
        y = e.clientY + document.documentElement.scrollTop;
      } else {
        x = e.pageX;
        y = e.pageY;
      }
      tooltip.tooltipDiv.style.left = (x + 20) + "px";
      tooltip.tooltipDiv.style.top  = (y + 20) + "px";
    },

    show: function(){
      tooltip.isVisible = true;
      tooltip.isHidden = !this.isVisible;
      tooltip.tooltipDiv.style.display = "block";
    },

    setHTML: function(data){
      tooltip.tooltipDiv.innerHTML = data;
    },

    hide: function(){
      tooltip.isVisible = false;
      tooltip.isHidden = !this.isVisible;
      tooltip.gotPosition = false;
      tooltip.tooltipDiv.style.display = "none";
    },

    getDiv: function() {
      return this.tooltipDiv;
    }

};
new TooltipClass();


// Now create the observer for the onmousemov event
Event.observe(document, "mousemove", tooltip.updateToolTip);
