/*
TODO:
  - Limit the size to a certain number of elements
  - addEventListener("click", ... , false);
  - addEventListener("paste", ... , false);
  - Make this a general purpose hint-System with left-context-suport
  - Die Funktion, wann was angezeigt werden soll, sollte extern
    definiert sein (der Kontext / changed)
  - Die Werteliste sollte weitere Attribute enthalten, wie title und class
*/

var Hint = function (param) {
  var foundryRegex = new RegExp("(?:^|[^a-zA-Z0-9])([-a-zA-Z0-9]+?)\/(?:([^=]+?)=)?$");

  var search =   document.getElementById(param["ref"]);
  var qlField =  document.getElementById(param["qlRef"]);
  var mirror =   document.createElement("div");
  var hint =     document.createElement("ul");
  var hintSize = param["hintSize"] ? param["hintSize"] : 10;
  var hints =    param["hints"];
  var that = this;
  var ql;

  // Build the mirror element
  // <div id="searchMirror"><span></span><ul></ul></div>
  mirror.setAttribute("id", "searchMirror");
  mirror.appendChild(document.createElement("span"));
  mirror.appendChild(hint);
  document.getElementsByTagName("body")[0].appendChild(mirror);
  // Default active state
  this.active = -2;

  // Show hint table
  this.show = function (topic) {
    if (!hints[topic])
      return;
    this.hide();
    this.active = -1;
    this.list(hints[topic]);
    var searchRight = search.getBoundingClientRect().right;
    var infoRight = hint.getBoundingClientRect().right;
    if (infoRight > searchRight) {
      hint.style.marginLeft = '-' + (infoRight - searchRight) + 'px';
    };
    hint.style.opacity = 1;
  };

  // Initialize the mirror element
  function init () {
    // Copy input style
    var searchRect = search.getBoundingClientRect();
    var searchStyle = window.getComputedStyle(search, null);

    with (mirror.style) {
      left = searchRect.left + "px";
      top = searchRect.bottom + "px";
      borderLeftColor = "transparent";
      paddingLeft     = searchStyle.getPropertyValue("padding-left");
      marginLeft      = searchStyle.getPropertyValue("margin-left");
      borderLeftWidth = searchStyle.getPropertyValue("border-left-width");
      borderLeftStyle = searchStyle.getPropertyValue("border-left-style");
      fontSize        = searchStyle.getPropertyValue("font-size");
      fontFamily      = searchStyle.getPropertyValue("font-family");
    };

    qlSelect();
  };

  // Hide hint table
  this.hide = function () {
    if (this.active === -2)
      return;
    this.active = -2;
    hint.style.opacity = 0;
    hint.style.marginLeft = 0;

    // Remove all children
    var lis = hint.childNodes;
    for (var li = lis.length - 1; li >= 0; li--) {
      hint.removeChild(lis[li])
    };
  };

  // List elements in the hint table
  this.list = function (hintList) {
    var li, title;
    var arrayType = hintList instanceof Array;
    for (var i in hintList) {
      // Create list items
      li = document.createElement("li");
      li.setAttribute("data-action", arrayType ? hintList[i] : hintList[i][0]);
      title = document.createElement("strong");
      title.appendChild(document.createTextNode(arrayType ? hintList[i] : i));
      li.appendChild(title);
      hint.appendChild(li);

      // Include descriptions
      if (!arrayType && hintList[i][1]) {
        var desc = document.createElement("span");
        desc.appendChild(document.createTextNode(hintList[i][1]));
        li.appendChild(desc);
      };
    };
  };

  // Choose next item in list
  this.next = function () {
    if (this.active === -2)
      return false;

    var lis = hint.getElementsByTagName("li");
    if (this.active === -1) {
      lis[0].setAttribute("class", "active");
      this.active = 0;
    }
    else if (this.active === lis.length - 1) {
      lis[this.active].removeAttribute("class");
      lis[0].setAttribute("class", "active");
      this.active = 0;
    }
    else {
      lis[this.active].removeAttribute("class");
      lis[++this.active].setAttribute("class", "active");
    };

    return true;
  };

  // Choose previous item in list
  this.previous = function () {
    if (this.active === -2)
      return;

    var lis = hint.getElementsByTagName("li");
    if (this.active === -1) {
      this.active = lis.length - 1;
      lis[this.active].setAttribute("class", "active");
    }
    else if (this.active === 0) {
      lis[0].removeAttribute("class");
      this.active = lis.length - 1;
      lis[this.active].setAttribute("class", "active");
    }
    else {
      lis[this.active].removeAttribute("class");
      lis[--this.active].setAttribute("class", "active");
    };
  };

  // Choose item from list
  this.choose = function () {
    if (this.active < 0)
      return;

    var action = hint.getElementsByTagName("li")[this.active].getAttribute("data-action");

    var value = search.value;
    var start = search.selectionStart;
    var begin = value.substring(0, start);
    var end = value.substring(start, value.length);
    search.value = begin + action + end;
    search.selectionStart = (begin + action).length
    search.selectionEnd = search.selectionStart;

    this.hide();

    // Check for new changes
    mirror.firstChild.textContent = begin + action;

    if (foundryRegex.exec(begin + action))
      this.show(RegExp.$1 + (RegExp.$2 ? '/' + RegExp.$2 : ''));

    return true;
  };

  function changed (e) {
    var el = e.target;
    if (e.key === '/' || e.key === '=') {
      var start = el.selectionStart;
      mirror.firstChild.textContent = el.value.substring(0, start);
      var sub = el.value.substring(start - 128 >= 0 ? start - 128 : 0, start);

      if (foundryRegex.exec(sub))
	that.show(RegExp.$1 + (RegExp.$2 ? '/' + RegExp.$2 : ''));
    }
    else if (e.key === '>') {
      that.hide();
    }
    else if (ql === 'poliqarp' && (e.key === '[' || e.key === '<')) {
      mirror.firstChild.textContent = el.value.substring(0, el.selectionStart);
      that.show("-foundries");
    }
    else if (e.key !== 'Shift' &&
             e.key !== 'Up'    &&
             e.key !== 'Down'  &&
             e.key !== 'Enter' &&
	     e.key !== 'Alt'   &&
	     e.key !== 'AltGraph' &&
	     e.key !== 'CapsLock') {
      that.hide();
    };
  };

  // Select item from suggestion list
  function select (e) {
    if (that.active === -2)
      return;
    if (e.key === 'Down') {
      return that.next();
    }
    else if (e.key === 'Up') {
      return that.previous();
    }
    else if (e.key === 'Enter' && that.choose()) {
      e.stopPropagation();
      e.preventDefault();
      return false;
    }
    else if (e.key !== 'Shift' &&
	     e.key !== 'Alt' &&
	     e.key !== 'AltGraph' &&
	     e.key !== 'CapsLock') {
      that.hide();
    };
    return true;
  };

  function qlSelect () {
    var nodes = qlField.childNodes;
    for (var i = 0; i < nodes.length; i++) {
      if (nodes[i].selected) {
        ql = nodes[i].value;
        break;
      };
    };
  };

  // Initialize style
  init();
  window.onresize = init;
  search.addEventListener("keyup",   changed,  false);
  search.addEventListener("keydown", select,   false);
  qlField.addEventListener("change", qlSelect, false);
};
