var MEOWTO = "__MEOWTO__";  // LocalStorage goes here
var aliases = {};

(function() {
  // Read aliases from local storage, if there are any.
  if (!localStorage[MEOWTO])
    updateLocalStorage();
  else
    aliases = JSON.parse(localStorage[MEOWTO]);

  var query = window.location.search.substring(1);

  switch(query) {
    case '':
      show('setup');
      showList();
      show('help');
      break;
    case 'setup':
      show('setup');
      show('help');
    case 'list':
      showList();
      break;
    case 'help':
    case 'halp':
      show('help');
      break;
    default:
      if (query.indexOf('add') == 0)
        addAlias(query);
      else if (query.indexOf('nuke') == 0)
        nukeAlias(query);
      else
        redirectTo(query);
}
})();

function addAlias(query) {
  var cleanQuery = queryWithoutCommand(query, 'add');
  if (cleanQuery == '') {
    show('error');
    show('help');
    return;
  }

  var parsedQuery = cleanQuery.split('->');

  if (parsedQuery.length != 2) {
    show('error');
    show('help');
    return;
  }

  var name = parsedQuery[0].trim();
  var url = parsedQuery[1].trim();

  // If there wasn't an http://, add one.
  if (url.indexOf('http') == -1)
    url = "http://" + url;

  aliases[name] = url;
  updateLocalStorage();
  showList();
}

function nukeAlias(query) {
  var cleanQuery = queryWithoutCommand(query, 'nuke');
  if (cleanQuery == '') {
    show('error');
    show('help');
    return;
  }

  delete aliases[cleanQuery];
  updateLocalStorage();
  showList()
}

function redirectTo(query) {
  var url = localStorage[query];
  if (url && url != '')
    window.location.replace(url);
  else
    show('add');
}

function queryWithoutCommand(query, command) {
  var cleanQuery = decodeURIComponent(query.trim());
  // You might have +s. You shouldn't.
  cleanQuery = cleanQuery.replace(/\+/g, ' ');

  if (cleanQuery.indexOf(command) != 0)
    return ''
  return cleanQuery.substring(command.length).trim();
}

function showList() {
  document.getElementById('list-box').hidden = false;
  if (Object.keys(aliases).length == 0) {
    document.getElementById('no-items').hidden = false;
    return;
  }

  var ul = document.getElementById('list')

  for (var name in aliases) {
    var li = document.createElement('li');

    var span = document.createElement('span');
    span.className = "meow";
    span.innerText = name;

    var a = document.createElement('a');
    a.innerText = aliases[name];
    a.href = aliases[name];
    a.target = '_blank';

    var del = document.createElement('a');
    del.className = "meow";
    del.innerText = "x";
    del.href = "?nuke " + name;

    li.appendChild(span);
    li.appendChild(a);
    li.appendChild(del);

    ul.appendChild(li);
  }
}

function updateLocalStorage() {
  localStorage[MEOWTO] = JSON.stringify(aliases);
}

function show(what) {
  document.getElementById(what + '-box').hidden = false;
}

@@ -572,24 +572,116 @@
      break;%0A
+    case 'nukeall':%0A      aliases = %7B%7D;%0A      updateLocalStorage();%0A      redirectToList();%0A
     default:
@@ -681,16 +681,16 @@
 efault:%0A
-
       if
@@ -1313,17 +1313,17 @@
 l = 
-%22
+'
 http://
-%22
+'
  + u
@@ -1372,28 +1372,34 @@
 torage();%0A  
-show
+redirectTo
 List();%0A%7D%0A%0Af
@@ -1616,20 +1616,26 @@
 ge();%0A  
-show
+redirectTo
 List()%0A%7D
@@ -2077,24 +2077,92 @@
 .trim();%0A%7D%0A%0A
+function redirectToList() %7B%0A  window.location.replace('?list');  %0A%7D%0A
 function sho
@@ -2522,30 +2522,30 @@
 className = 
-%22
+'
 meow
-%22
+'
 ;%0A    span.i
@@ -2757,14 +2757,14 @@
 e = 
-%22
+'
 meow
-%22
+'
 ;%0A  
@@ -2785,11 +2785,11 @@
 t = 
-%22x%22
+'x'
 ;%0A  
@@ -2805,16 +2805,16 @@
 f = 
-%22
+'
 ?nuke 
-%22
+'
  + n
@@ -2918,19 +2918,397 @@
 ld(li);%0A
-
   %7D
+%0A%0A  var li = document.createElement('li');%0A  var span = document.createElement('span');%0A  span.className = 'red';%0A  span.innerText = ' %5Bfor reals. no confirmation%5D';%0A  var a = document.createElement('a');%0A  a.className = 'meow';%0A  a.innerText = 'delete all aliases';%0A  a.href = '?nukeall';%0A  a.target = '_blank';%0A  a.appendChild(span);%0A  li.appendChild(a);%0A  ul.appendChild(li);
 %0A%7D%0A%0Afunc

add a nukeall command