var settingsToggle = document.getElementById('settings-toggle');
var filterElement = document.getElementById('filter');
var galleryElement = document.getElementById('pswp');
var idElement = document.getElementById('id');
var rtl = document.getElementById('rtl').checked;
var submitElement = document.getElementById('submit');
var messengerElement = document.getElementById('messenger');
var gallery;
var index;

function handleItems() {
  messengerElement.innerHTML = '';

  if (this.status >= 500) {
    document.title = 'aww jeez';
    messengerElement.innerHTML = "sorry, something bad happened<br/><br/>the server has probably printed or logged more information<br/><br/>it'd be very nice if you could <a href='https://github.com/tinruufu/friction/issues'>file a bug</a> about this";
  }

  var resp = JSON.parse(this.responseText);

  if (this.status != 200) {
    messengerElement.innerHTML = resp.message;
    document.title = 'sorry :<';
    showSettings();
    return;
  }

  if (!idElement.value) {
    window.history.replaceState({}, resp.title, window.location.pathname + '?id=' + resp.id + '&' + window.location.search.replace(/^\?/, ''));
  }

  if (index === undefined) {
    if (rtl) {
      resp.photoswipe.reverse();
      index = resp.photoswipe.length - 1;
    } else {
      index = 0;
    }
  }

  document.title = resp.title;
  gallery = new PhotoSwipe(
      galleryElement,
      PhotoSwipeUI_Default,
      resp.photoswipe,
      {
        spacing: 0,
        history: false,
        escKey: true,
        barsSize: {top: 0, bottom: 0},
        index: index
      }
  );

  function updateIndex() {
    index = gallery.getCurrentIndex();
  }

  gallery.listen('beforeChange', updateIndex);
  gallery.listen('destroy', function() {submitForm();});
  window.addEventListener('keypress', function(e) {
    if (e.keyCode == 13) submitForm();
  });
  gallery.init();
}

// *some* browsers won't let us submit the form unless we change something, so,,
function changeForm() {
  filterElement.value += ' ';
}

function submitForm() {
  changeForm();
  submitElement.click();
}

function getItems() {
  var url;

  if (idElement.value) url = '/items?id=' + encodeURIComponent(idElement.value);
  else url = '/items?f=' + encodeURIComponent(filterElement.value);

  var itemsRequest = new XMLHttpRequest();
  document.title = 'loading';
  itemsRequest.addEventListener('load', handleItems);
  itemsRequest.open('GET', url);
  itemsRequest.send();
}

function toggleSettings() {
  document.body.classList.toggle('settings-shown');
}

function showSettings() {
  if (!document.body.classList.contains('settings-shown')) toggleSettings();
  filterElement.focus();
}

window.addEventListener('keyup', function(e) {
  if (e.keyCode == 79) showSettings();
});

window.addEventListener('pageshow', function() {
  if ((gallery !== undefined) && !(galleryElement.classList.contains('pwsp--open'))) {
    getItems();
  }
});

settingsToggle.addEventListener('click', function(e) {
  toggleSettings();
  e.preventDefault();
});

setTimeout(function() {
  settingsToggle.classList.remove('just-loaded');
}, 1000);

getItems();

@@ -600,16 +600,70 @@
 r/%3E%3Cbr/%3E
+you might have a bad archive in your library%3Cbr/%3E%3Cbr/%3E
 the serv
@@ -722,16 +722,60 @@
 r/%3E%3Cbr/%3E
+if the archive that caused this seems fine, 
 it'd be 
@@ -876,16 +876,73 @@
 out this
+%3Cbr/%3E%3Cbr/%3E%3Ca href=.%3Ei don't care, just load something%3C/a%3E
 %22;%0A  %7D%0A%0A

improve error messaging