Tooltip = {
  tooltips: [],

  /**
   * Hack to overcome the fact that prototip will not hide the tip if a click 
   * is performed on the tip element.
   * 
   * @param {Event} event The event object. */
  hideTooltips: function(){
    var len = Tooltip.tooltips.length;
    
    for (var i = 0; i < len; i++) {
      var tip = Tooltip.tooltips[i];

      if (!tip.options.closeButton) {
        tip.wrapper.style.display = "none";
      }
    }
  },
  
  addInfo: function(element, content){
    element = $(element);
    var tooltip = element.tooltip;
    var list = element.tooltip.tip.childNodes;
    var len = list.length;
    
    for (var i = 0; i < len; i++) {
      var child = list[i];
      
      if (child.className == "tip_info") {
        child.innerHTML = content;
        break;
      }
    }
  },
  
  /**
   * Standard way of creating a tooltip in the application.
   * The tooltip shows when you hover over the element that owns the tooltip.
   *
   * There are default options for:
   *    * className
   *    * delay
   *    * effect
   * 
   * @param {Element} element The element that owns the tooltip.
   * @param {String} content The content to show in the tooltip.
   * @param {Hash} options Additional options accepted by prototip. */
  basic: function(element, content, options){
    var el = $(element);
    options = options || {};
    //if (options.hideOn == undefined) options.hideOn = { element: window, event: "click" };
    //if (options.delay == undefined) options.delay = 0;
    if (options.border == undefined) options.border = 3;
    if (options.radius == undefined) options.radius = 3;
    if (options.hook == undefined) options.hook = { target: "topMiddle", tip: "bottomMiddle" };
    if (options.offset == undefined) options.offset = { x: 0, y: -5 };
    if (options.viewport == undefined) options.viewport = true;
    if (options.style == undefined) options.style = "protogrey";
    if (options.width == undefined) options.width = "auto";

    content = content + "<div class='tip_info'></div>";
    var tip = new Tip(el, content, options);
    el.tooltip = tip;
    tip.trigger = el;
    this.tooltips.push(tip);
  },
  
  contactRolodex: function(element, content, options){
    options = options || {};
    // if (options.title == undefined) options.title = "This is a Test title"; // Adds a Title Bar with text
    if (options.border == undefined) options.border = 0;
    if (options.radius == undefined) options.radius = 0;
    //if (options.stem == undefined) options.stem = "rightBottom";
    if (options.hook == undefined) options.hook = { target: "topLeft", tip: "topLeft" };
  	if (options.offset == undefined) options.offset = { x: 2, y: 2 };
    if (options.delay == undefined) options.delay= .01;
    this.basic(element, content, options);
  },
  
  toolBar: function(element, content, options){
    options = options || {};
    // if (options.title == undefined) options.title = "This is a Test title"; // Adds a Title Bar with text
    if (options.border == undefined) options.border = 0;
    if (options.radius == undefined) options.radius = 0;
    //if (options.stem == undefined) options.stem = "rightBottom";
    if (options.hook == undefined) options.hook = { target: "topLeft", tip: "topLeft" };
  	if (options.offset == undefined) options.offset = { x: 0, y: 2 };
    if (options.delay == undefined) options.delay= .01;
    this.basic(element, content, options);
  },
  
  /**
   * DOM event handler to update tooltip contents on the fly.
   * Handles updating the tip content correctly because prototip doesn't actually 
   * inject HTML into the tip until the trigger is invoked. */
  updateTip: function(event) {
    event = event || window.event;
    Event.extend(event);
    var el = event.element();
    
    if (el.tooltip && el.tooltipHTML) {
      if (el.tooltip.tip.innerHTML != el.tooltipHTML) {
        setTimeout("var el = $('" + el.id  + "'); el.tooltip.tip.innerHTML = el.tooltipHTML;", 100);
      }
    }
  }
}


Element.observe(document.documentElement, "mouseup", Tooltip.hideTooltips, true);
