/* DO NOT MODIFY. This file was compiled Mon, 20 Jun 2011 08:10:06 GMT from
 * /Users/philnash/projects/philnash2/app/coffeescripts/app.coffee
 */

(function() {
  window.philHistory = {
    createClassName: function(path) {
      return path.split(/[\/\?]/)[1];
    },
    createTitle: function(target) {
      if (target.getAttribute('href') === '/') {
        return 'Phil Nash';
      } else {
        return target.getAttribute('title') + ' - Phil Nash';
      }
    },
    popState: function(event) {
      var state;
      if (event.state) {
        philHistory.loadContent(event.state, true);
      } else {
        state = {
          title: document.title,
          url: window.location.pathname,
          bodyClass: philHistory.createClassName(window.location.pathname)
        };
        window.history.replaceState(state, state.title, state.url);
      }
    },
    loadContent: function(state, skipHistory) {
      var main, xhr;
      if (skipHistory == null) {
        skipHistory = false;
      }
      main = document.getElementById('main');
      main.setAttribute('class', 'loading');
      xhr = new XMLHttpRequest;
      xhr.open('get', state.url);
      xhr.addEventListener('readystatechange', (function() {
        if (xhr.readyState === 4) {
          main.innerHTML = xhr.responseText;
          document.body.setAttribute('class', state.bodyClass);
          document.title = state.title;
          document.body.addEventListener('click', philHistory.handleClick, false);
          document.body.removeEventListener('click', philHistory.cancelClick, false);
          main.setAttribute('class', '');
        }
      }), false);
      xhr.setRequestHeader('X_REQUESTED_WITH', 'XMLHttpRequest');
      xhr.send(null);
      if (!skipHistory) {
        window.history.pushState(state, state.title, state.url);
      }
    },
    handleClick: function(event) {
      var state, target;
      target = event.target;
      if (target.nodeName === "A" && !target.getAttribute('href').match(/^http/)) {
        document.body.removeEventListener('click', philHistory.handleClick, false);
        document.body.addEventListener('click', philHistory.cancelClick, false);
        state = {
          bodyClass: philHistory.createClassName(target.getAttribute('href')),
          title: philHistory.createTitle(target),
          url: target.getAttribute('href')
        };
        event.preventDefault();
        philHistory.loadContent(state);
      }
    },
    cancelClick: function(event) {
      var target;
      target = event.target;
      if (target.nodeName === "A" && !target.getAttribute('href').match(/^http/)) {
        event.preventDefault();
      }
    },
    init: function() {
      if (window.history.pushState && XMLHttpRequest) {
        document.body.addEventListener('click', philHistory.handleClick, false);
        window.addEventListener('popstate', philHistory.popState, false);
      }
    }
  };
  window.addEventListener('DOMContentLoaded', philHistory.init, false);
}).call(this);

