/**
 * Class to define what current page is. 
 * It accepts set of rules (plain js functions) which instruct him what unique detail any interested page have
 */
var PageFinder = new Class({
    Implements : [Options, Events],
    options : {
        pathname : location.pathname,
        hash : location.hash,
        rules : $H({})
    }, 
    initialize : function (options) {
        if ($type(options) === 'array') { // user passed in set of rules
            this.setOptions();
            this.addRules(options);
        } else {
            this.setOptions(options);
        }
    },
    /**
     * Adds rule to the PageFinder.
     * @method setOption
     * @param rule {Object} Hash which contains two properties - pagename and function which shold be called to define,
     * is current page, for example:
     * <code>{ pageName: 'splash', fn : function (pathname, hash) { return pathname.replace(/\/g,'').toLowerCase() === 'splash'; }}</code>
     */
    addRule : function (rule) {
        this.options.rules.set(rule.pageName, rule.fn);
    },
    /**
     * Same as above, but accept more then one rule
     * @method setOptions
     * @param rules {Array} array of rules
     */
    addRules : function (rules) {
        rules.each(this.addRule, this);
    },
    /**
     * Return name of the current page (name based on the pageName property of the rule passed in)
     * The name of this method inspired by greate *nix command ;)
     * @method whereAmI
     * @returns {String} name of the page or null if you don't provide appropriate rule
     */
    whereAmI : function () {
        var r = this.options.rules; // to make things little faster, I bind value to the separate variable
        
        this.update();
        
        return (r.filter(function (fn, pageName) {
            return fn(this.options.pathname, this.options.hash);
        }, this)).getKeys()[0] || null;
    },
    
    /**
     * Update stored hash
     * @method update
     */
    update : function () {
        this.options.hash = location.hash;
    }
});



var G = Allthat.namespace('Allthat.GUI');

G.pageFinder = new PageFinder([
    { 
        pageName: 'splash',
        fn : function (pathname, hash) {
            return location.pathname.replace(/\//g,'').toLowerCase() === 'splash';
        }
    }
]);