var pageLoader = new Class({
    Implements: Options,
    options: {
        target: $empty,
        pages: $empty,
        eval: true,
        observe: false,
        applyClass: false,
        filePath: $empty,
        interval: 100
    },

    vars: {
        location: window.location.protocol + '//' + 
                  window.location.hostname +
                  window.location.pathname,

        hash:     window.location.hash
    },
    objects: {
        timer: {}
    },

    initialize: function(options)
    {
        this.setOptions(options);
        timer = function() {
            this.checkForNewHashTimer()
        }.bind(this).periodical(
            this.options.interval);

        if(true  == this.options.observe && false != $chk(window.location.hash))
            return this.observe();

        return this.loadPage('_default');
    },
    observe: function()
    {
        this.loadPage(
            window.location.hash.replace('#/', '')
        );
    },
    checkForNewHashTimer: function()
    {
        if(this.vars.hash != window.location.hash) {
            this.loadPage(
                window.location.hash.replace('#/', '')
            );
            this.vars.hash = window.location.hash;
        }
        return this;
    },
    loadPage: function(page) {
        file = eval('this.options.pages.' + page);
        if(!file || file == 'undefined' || file == null)
            file = this.options.pages._default;

        if (this.options.filePath == $empty)
            throw("Missing Filepath for AJAX Request");

        if (this.options.target == $empty)
            throw("Missing Target Element for AJAX Request"); 

        uri = this.vars.location + this.options.filePath + file;

        this.options.target.set('load', {
            method: 'get',
            evalScripts: this.options.eval,
            onComplete: function() {
                if(this.options.applyClass)
                    this.options.target.removeClass('loading');
            }.bind(this),
            onRequest: function() {
                if(this.options.applyClass)
                    this.options.target.addClass('loading');
            }.bind(this)
        });
        this.options.target.load(uri);

        return this;
    }
});

