var checkspaces = true;

function checkSubmit(e) {
  var characterCode;

  if(e && e.which) {
    e = e;
    characterCode = e.which;
  }
  else{
    if (window.event) {
      e = event;
      characterCode = e.keyCode;
    }
  }
  if(characterCode == 13){
	if ($('what_autocomplete').style.display == 'none') get_posts_tags();
    return false
  }
  else{
    return true
  }
}

function getLastSpace(elem,car) {
    var pos = -1;
    for (var i = 0; i < elem.length; i++) {
      if (elem[i] == car) {
        pos = i;
      }
    }
    return pos;
}

function stripCounter(elem) {
  var newElem;
  var pos = elem.indexOf('(');
  if (pos >= 0) 
    newElem = elem.substr(0,pos-1);
  else
    newElem = elem;
  return newElem;
}

function hasSpaces(elem) {
  var count = elem.split(" ").length;
  if(!checkspaces) return false;
  if (count > 1)
    return true;
  return false
}

var Autocompleter;

Autocompleter.Local.prototype = Object.extend(new Autocompleter.Base(), {
  initialize: function(element, update, array, options) {
    this.baseInitialize(element, update, options);
    this.options.array = array;
  },

  getUpdatedChoices: function() {
    this.updateChoices(this.options.selector(this));
  },

  updateElement: function(selectedElement) {
    if (this.options.updateElement) {
      this.options.updateElement(selectedElement);
      return;
    }


    var value = Element.collectTextNodesIgnoreClass(selectedElement, 'informal');
    var lastTokenPos = this.findLastToken();
    if (lastTokenPos != -1) {
      var newValue = this.element.value.substr(0, lastTokenPos + 1);
      var whitespace = this.element.value.substr(lastTokenPos + 1).match(/^\s+/);
      if (whitespace)
        newValue += whitespace[0];
      this.element.value = newValue + value;
    } else {
      var oldValue = this.element.value;
      var pos = 0;
      pos = value.indexOf('(');
      value = value.slice(0,pos-1);
	
      if (hasSpaces(value)) {
        value = "\"" + value + "\""
      }
	
      pos = 0;
      if (oldValue.length > 0) {
        for( var i=oldValue.length-1;i>=0;i--) {
              if (oldValue.charAt(i) == ' ') {
                      pos = i;
                      break;
              }
        }
      }
      if (pos == 0) {
        this.element.value = value + ' ';
      } else {
        this.element.value = this.element.value.slice(0,pos) + ' ';
        this.element.value = this.element.value + value + ' ';
      }
    }
    this.element.focus();

    if (this.options.afterUpdateElement)
      this.options.afterUpdateElement(this.element, selectedElement);

	if($('formtaguser')){
		get_posts_tags();
	}
  },

  setOptions: function(options) {
    this.options = Object.extend({
      choices: 30,
      partialSearch: true,
      partialChars: 2,
      ignoreCase: true,
      fullSearch: false,
      selector: function(instance) {
        var ret       = []; // Beginning matches
        var partial   = []; // Inside matches
        var entry     = instance.getToken();
        var count     = 0;

        var lastSpace = getLastSpace(entry,' ');
  
        entry = entry.substr(lastSpace+1, entry.length);

        for (var i = 0; i < instance.options.array.length &&
          ret.length < instance.options.choices ; i++) {

          var elem = instance.options.array[i];
          var foundPos = instance.options.ignoreCase ?
            elem.toLowerCase().indexOf(entry.toLowerCase()) :
            elem.indexOf(entry);

          while (foundPos != -1) {
            if (foundPos == 0 && elem.length != entry.length) {
              ret.push("<li>" + elem.substr(0, entry.length) +
                elem.substr(entry.length) + "</li>");
              break;
            } else if (entry.length >= instance.options.partialChars &&
              instance.options.partialSearch && foundPos != -1) {
              if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos-1,1))) {
                partial.push("<li>" + elem.substr(0, foundPos) +
                  elem.substr(foundPos, entry.length) + elem.substr(
                  foundPos + entry.length) + "</li>");
                break;
              }
            }

            foundPos = instance.options.ignoreCase ?
              elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) :
              elem.indexOf(entry, foundPos + 1);

          }
        }
        if (partial.length)
          ret = ret.concat(partial.slice(0, instance.options.choices - ret.length))
        return "<ul>" + ret.join('') + "</ul>";
      }
    }, options || {});
  }
});

function get_posts_tags(){
	location.href = dest_location + $('what').value;
}
