// ---------------------------------------------------------------------------
// (c) 2005 KystAtlas, Hans Martin Mohn
// ---------------------------------------------------------------------------
//
// Tool to facilitate clicking on areas of an imagemap.
//
// Dependencies:
//
//    gtTool.js
//

gtToolImageMap.prototype = new gtTool();  // Sets gtToolImageMap to be a subclass of gtTool!
gtToolImageMap.prototype.constructor = gtTool;
gtToolImageMap.superclass = gtTool.prototype;

//
// Tool to enable panning the map
//
// Dependencies:
//
// gtTool.js
//
function gtToolImageMap()
{
  // Member variables
  this.m_map = null;
  this.m_event_handler = new gtEventHandler();
} // end gtToolImageMap() constructor



// Methods
gtToolImageMap.prototype.select = function(map)
{
  this.m_map = map;
  this.m_map.setCursor("");
} // end select()


gtToolImageMap.prototype.buttonDown = function(evt)
{
  gtCancelBubble(evt);
//  alert("buttonDown: " + evt.gtElement.tagName);
  if (evt.gtElement.tagName != "AREA")
    return gtPreventDefault(evt);

  this.m_button_down = true;
  return gtPreventDefault(evt);
} // end buttonDown()



gtToolImageMap.prototype.buttonUp = function(evt)
{
//  alert("buttonUp: " + evt.gtElement.tagName);
  gtCancelBubble(evt);
  if (evt.gtElement.tagName != "AREA")
    return gtPreventDefault(evt);
    
  if (this.m_button_down)
  {
    this.m_button_down = false;
    // The href on the area objects has to be prefixed with "javascript:"
    try
    {
      var s = decodeURI(evt.gtElement.href).split(/[:|]/g);
    }
    catch (e)
    {
      // Depending on the browser's character set setting????, URI decoding sometimes doesn't work.
      // At least this happens on Safari running on Mac with default settings.
      // Manually replace! (this file must be saved as UTF-8!)
      var h = evt.gtElement.href;
      h = h.replace(/\%E6/g, "æ");
      h = h.replace(/\%F8/g, "ø");
      h = h.replace(/\%E5/g, "å");
      h = h.replace(/\%E4/g, "ä");
      h = h.replace(/\%F6/g, "ö");
      h = h.replace(/\%C6/g, "Æ");
      h = h.replace(/\%D8/g, "Ø");
      h = h.replace(/\%C5/g, "Å");
      h = h.replace(/\%C4/g, "Ä");
      h = h.replace(/\%D6/g, "Ö");
      var s = decodeURI(h).split(/[:,]/g);
    }
    var javascript = s.shift();
    this.m_event_handler.raise(s);
  }
  return gtPreventDefault(evt);
}; // end buttonUp()



gtToolImageMap.prototype.click = function(evt)
{
//  alert("click");
  gtCancelBubble(evt);
  return gtPreventDefault(evt);
}; // end click()



// This tool does not depend on the current selection!
this.canHandleSelection = function (selection) { return true; }

