// PotoSIG -- SIG extensions for Rails. Written by Javier Goizueta
// IvyGIS --- Mapserver and PostGIS on Rails.  Written by Robert S. Thau.
// Copyright 2006, Japan Spatial Information Technology, Inc.
//
// Distributed without warranty under the terms of the GNU General Public
// License, version 2.  

var PotoTooltips = {

  disabled_over: [],

  disable_over: function(elt) {
    this.hide(elt);
    this.disabled_over.push(elt);
  },

  enable_over: function(elt) {
    for (var i = 0; i < this.disabled_over.length; ++i)
      if (this.disabled_over[i] == elt) {
        this.disabled_over.splice(i,1);
        --i;
      }
  },

  show: function (elt, evt, type, tooltip) {

    var tooltip_div = $("poto_tooltip_elt");

    if (tooltip_div == null) {
      tooltip_div = document.createElement("div");
      tooltip_div.id = "poto_tooltip_elt";
      tooltip_div.className = "hidden tooltip";
      document.body.appendChild (tooltip_div);
    }

    if (PotoTooltips.showing_for_elt != elt) 
    {
      for (var i = 0; i < this.disabled_over.length; ++i)
        if (this.disabled_over[i] == elt)
          return;

      while (tooltip_div.firstChild)
        tooltip_div.removeChild(tooltip_div.firstChild);

      var tooltip_span = document.createElement("span");
      tooltip_span.className = type;

      tooltip_div.appendChild(tooltip_span);
      tooltip_span.appendChild(document.createTextNode (tooltip));

      PotoTooltips.showing_for_elt = elt;
    }

    var ptrx, ptry;

    if (evt != null) {
      ptrx = Event.pointerX(evt); 
      ptry = Event.pointerY(evt);
    }
    else {
      var pos = Position.page(elt);
      ptrx = pos[0] + elt.offsetWidth/2; 
      ptry = pos[1] + elt.offsetHeight/2;
    }

    tooltip_div.style.left = ptrx + 20 + "px";
    tooltip_div.style.top  = ptry + "px";

    // Do this *last*; current location and contents get set up first.

    tooltip_div.className = "tooltip";
  },

  hide: function (elt) {
    if (elt == PotoTooltips.showing_for_elt) {
      var tooltip_div = $("poto_tooltip_elt");
      tooltip_div.className = "hidden_tooltip";
      PotoTooltips.showing_for_elt = null;
    }
  }
};

Event.observe (document, "load", function() { PotoTooltips.initialize(); });
