function intersect(a, b) {
  // Assumes a and b have unique elements.
  a = sorted(a);
  b = sorted(b);
  var result = new Array();
  while (a.length > 0 && b.length > 0)
    if (a[0] < b[0])
      a.shift();
    else if (a[0] > b[0])
      b.shift();
    else {
      result.push(a.shift());
      b.shift();
    }

  return result;
}

function sorted(a) {
  return a.slice(0).sort(function(x, y){x < y ? 1 : -1});
}

function union(a, b) {
  // Assumes a and b have unique elements.
  // Slice to make sure we have a copy.
  a = sorted(a);
  b = sorted(b);

  var result = [];
  while ((a.length > 0) && (b.length > 0))
    if (a[0] < b[0])
      result.push(a.shift());
    else if (a[0] > b[0])
      result.push(b.shift());
    else {
      result.push(a.shift());
      b.shift();
    }

  // Now clean up any stragglers.
  while (a.length > 0) result.push(a.shift());
  while (b.length > 0) result.push(b.shift());

  return result;
}

function init() {
  searchField = document.getElementById('search_text');
  items_shown = document.getElementById('items_shown');
  posts = {};
  for (var i = 0; i < num_items; i++)
    posts[i] = document.getElementById("entry_" + ids[i]);

  activatePlaceholders();
}
window.onload = init;

function activatePlaceholders() {
  // Taken from http://www.beyondstandards.com/archives/input-placeholders/.
  var detect = navigator.userAgent.toLowerCase(); 
  if (detect.indexOf("safari") > 0) return false;
  var inputs = document.getElementsByTagName("input");
  for (var i=0;i<inputs.length;i++) {
    if (inputs[i].getAttribute("type") == "text") {
      if (inputs[i].getAttribute("placeholder") && inputs[i].getAttribute("placeholder").length > 0) {
        inputs[i].value = inputs[i].getAttribute("placeholder");
        inputs[i].onclick = function() {
          if (this.value == this.getAttribute("placeholder")) {
            this.value = "";
          }
          return false;
        }
        inputs[i].onblur = function() {
          if (this.value.length < 1) {
            this.value = this.getAttribute("placeholder");
          }
        }
      }
    }
  }
}


function changeVal() {
  var matches = getMatches(searchField.value);
  if (matches.length == num_items)
    items_shown.innerHTML = "all";
  else
    items_shown.innerHTML = "" + matches.length;

  var states = [];
  for (var i = 0; i < num_items; i++)
    states.push(false);

  for (var i in matches)
    states[matches[i]] = true;

  for (var i = 0; i < num_items; i++)
    if (states[i]) {
      posts[i].style['display'] = 'block';
    } else {
      posts[i].style['display'] = 'none';
    }
}

function getPrefixEntries(prefix) {
  var results = [];
  for (var k in terms)
    if (k.match(prefix)) {
      results = union(results, terms[k]);
    }
  
  return results;
}

function getMatches(s) {
  var words = s.toLowerCase().split(' ');
  var possibles = [];
  for (var i = 0; i < num_items; i++)
    possibles.push(i);
  for (var i in words) {
    x = words[i];
    if (x.length < 3)
      continue;
    if (x != "") {
      wordTerms = getPrefixEntries(x);
      possibles = intersect(possibles, wordTerms);
    }
  }
  return possibles;
}
