
var Naturligvis = {

  fx : null,
  xhr : null,

  init: function () {
    $$('#nav a').each(function (item, index, array) {
      item.addEvent('click', Naturligvis.click);
    });

    Naturligvis.xhr = new Request.HTML({
      onSuccess: function(html) {
        //Clear the text currently inside the results div.
        $('main').set('text', '');
        //Inject the new DOM elements into the results div.
        $('main').adopt(html);

        Naturligvis.fx.onComplete = function () {};
        Naturligvis.fx.slideIn();
      },
      //Our request will most likely succeed, but just in case, we'll add an
      //onFailure method which will let the user know what happened.
      onFailure: function() {
        $('main').set('text', 'Ukjent side.');

        Naturligvis.fx.onComplete = function () {};
        Naturligvis.fx.slideIn();
      },
      noCache : true
    });

    Naturligvis.fx = new Fx.Slide('main');
    Naturligvis.fx.hide();

    var uri = new URI(location.href);
    Naturligvis.showPage(uri.get('fragment'));
  },

  click: function(e) {
    this.blur();

    var uri = new URI(this.href);

    Naturligvis.showPage(uri.get('fragment'));
  },

  showPage: function (page) {

    if (!page) {
      page = 'naturligvis';
    }

    Naturligvis.fx.onComplete = function() {
      Naturligvis.xhr.get(page+'.html');
    }
    Naturligvis.fx.slideOut();
  }
}

window.addEvent('domready', Naturligvis.init);