var Dispatcher = function (page) {
    var dispatch = function (event) {
        
        if (!window.location.hash) {
            window.location.hash = "#"+page.startpage;
            return;
        }
        
        var hash = window.location.hash,
            regEx = /^(?:#([A-Za-z]+))((?:(?:!|&)(?:(?:[A-Za-z]+)=(?:[A-Za-z0-9]+)))*)$/,
            pattern = regEx.exec(hash),
            params = (pattern[2].length > 0) ? pattern[2].substring(1).split("&") : "",
            fnct = pattern[1],
            param = { };
        


        // extract parameters from params string example !first=abc&second=def...
        for (p in params) {
            var tmp =  [],
                tmp = params[p].split("=");                  
                param[tmp[0]] = tmp[1];
            }
                
        // set active link inactive and new active link active
        $("#submenu a[class=activeLink]").removeClass("activeLink");
        $("#submenu a[href="+hash+"]").addClass("activeLink");
                
        // if a fnct is definded call that function from the pages object and
        // deliver the params specified in the address hash
        // if no fnct is specified in the hash call 'startpage' as start page
        page[fnct](param);
                       
        // firefox demands that
        return true;
               
    };

    var register = function () {
        window.addEventListener(
            'hashchange',
            dispatch(),
            true);
     };
     
     var unregister = function () {
        window.removeEventListener(
            'hashchange',
            dispatch(),
            true);
     };

    return {
        start : function () {
            register();
        },
        stop : function () {
            unregister();
        },
        dispatch : function () {
            dispatch();
        }
    };
};


