/*************************************************************
 
   vvTooltip v1.4
   
   Created by VividVisions 
   (www.vividvisions.at)
    
*************************************************************/

function vvTooltip (entries)  {
  // Configuration
  this.configuration = {
    defaultLanguage: "de",
    CSSClass: "glossary",
    divID: "glossary",
    path: "",
    appendAnchor: true
  };
  
  // Internal
  this.entries = entries || {};
  this.activeEntry = null;

  // Events
  this.eventMouseOver  = this.mouseOver.bindAsEventListener(this);
  this.eventMouseOut   = this.mouseOut.bindAsEventListener(this);
  this.eventMouseMove  = this.mouseMove.bindAsEventListener(this);
  this.eventMouseClick = this.mouseClick.bindAsEventListener(this);
}

vvTooltip.prototype.add = function (language, key, description, url) {
  if (this.entries[language]) {
    this.entries[language][key] = {
      description: description,
      url: url
    };
  } else {
    this.entries[language] = new Object();
    this.entries[language][key] = {
      description: description,
      url: url
    };
  } 
}

vvTooltip.prototype.mouseOver = function (event) {
  if (this.activeEntry == null) {
    var language = Event.element(event).getAttribute("lang") || this.configuration.defaultLanguage;
    var key = Event.element(event).innerHTML;
    var entry = this.getEntry(language, key);
    
    var str = this.entries[language]["_template"] || this.entries[this.configuration.defaultLanguage]["_template"];
    str = str.replace(/\$key/g, entry.key).replace(/\$description/g, entry.description).replace(/\$url/g, entry.url);

    // Preset position for Firefox
    Element.setStyle(this.configuration.divID, {left:Event.pointerX(event) + 10 + "px"});
    Element.setStyle(this.configuration.divID, {top:Event.pointerY(event) + 10 + "px"});

    Element.update(this.configuration.divID, str);
    Element.show(this.configuration.divID);

    this.activeEntry = entry;
  }
}

vvTooltip.prototype.mouseMove = function (event) {
  var screenWidth = Element.getDimensions(document.getElementsByTagName("body")[0]).width;
  var divWidth = Element.getDimensions(this.configuration.divID).width;
  var x = Event.pointerX(event) + 10;
  
  if ((x + divWidth) > screenWidth)
    x = x - ((x+divWidth) - screenWidth) - 10;
  
  Element.setStyle(this.configuration.divID, {left:x + "px"});
  Element.setStyle(this.configuration.divID, {top:Event.pointerY(event) + 10 + "px"});
}

vvTooltip.prototype.mouseClick = function (event) {
  if (this.activeEntry.url)
    location.href = this.configuration.path + this.activeEntry.url + ((this.configuration.appendAnchor) ? "#" + this.activeEntry.key : "");
}

vvTooltip.prototype.mouseOut = function (event) {
  this.activeEntry = null;
  Element.hide(this.configuration.divID);
}

vvTooltip.prototype.getEntry = function (language, key) {
  var ent;
  
  if (!language in this.entries) {
    ent = "Language '"+ language + "' not found in glossary!";
  }
  else
  {
    var entry = null;

    if (key in this.entries[language])
      entry = this.entries[language][key];
    else if (this.entries[language][key.replace(/\s/g,"_")])
      entry = this.entries[language][key.replace(/\s/g,"_")];
    else if (this.configuration.defaultLanguage != language && this.entries[this.configuration.defaultLanguage][key])
      entry = this.entries[this.configuration.defaultLanguage][key];

    if (entry == null)
      ent = {key: "Error", description: "No entry with key '" + key + "' found!"};
    else if (entry.alias != null)
      ent = this.getEntry(language, entry.alias);
    else {
      entry.key = key;
      ent = entry;
    }
  }
  
  return ent;
}

vvTooltip.prototype.load = function (language) {
  this.checkRequirements();
  
  // Add a <div> to <body>
  var glossary_div = document.createElement("div");
  glossary_div.setAttribute("id",this.configuration.divID);
  glossary_div.setAttribute("style","display: none;");
  var body = document.getElementsByTagName("body")[0];
  body.appendChild(glossary_div);
  Element.hide(this.configuration.divID);
  
  
  // Apply event call-backs to all glossary items
  var elements = document.getElementsByClassName(this.configuration.CSSClass);
  for (var i = 0; i < elements.length; i++) {
    var el = elements[i];
    
    Event.observe(el, 'mouseover', this.eventMouseOver);
    Event.observe(el, 'mousemove', this.eventMouseMove);
    Event.observe(el, 'mouseout',  this.eventMouseOut);
    Event.observe(el, 'click',     this.eventMouseClick);
  }
}

vvTooltip.prototype.checkRequirements = function () {
  if((typeof Prototype == 'undefined') || 
    (typeof Element == 'undefined') || 
    (typeof Element.Methods == 'undefined'))
      throw("vvTooltip requires the Prototype JavaScript framework");
}