var Layout = {
    live_path_regex: { 'loading': [], 'success': [] },

    init: function() {
        jQuery(function($) {
            var hash = window.location.hash;
            var needs_default_hash = !hash || (hash && hash == '#/');
            if (needs_default_hash) {
                window.location.hash = '#/forside/2/200/208';
            }

            window.currentHash = window.location.hash;
            Layout.handlePageLoad();
            Layout.addObservers();
            Layout.makeBackFowardButtonsWork();
        });
    },

    addObservers: function() {
        Layout.observeLinks();
        Layout.observeHashChange();
    },

    observeLinks: function() {
        $("a[href^='#/']").live('click', function(event) {
            var ctrl_cmd = event.ctrlKey || event.metaKey;
            if (!ctrl_cmd) {
                event.preventDefault();
                var $link = $(this);
                Layout.updateHashWithoutLoad($link.attr('href'));
                $(document).trigger('hashchange');
            }
        });
    },

    observeHashChange: function() {
        $(document).bind('hashchange', Layout.reload);
    },

    updateHashWithoutLoad: function(location) {
        window.currentHash = window.location.hash = location;
    },

    makeBackFowardButtonsWork: function() {
        setInterval(function() {
            var hash_is_new = window.location.hash && window.currentHash != window.location.hash;

            if (hash_is_new) {
                window.currentHash = window.location.hash;
                Layout.handlePageLoad();
            }
        }, 300);
    },

    load: function(path, options) {
        path = path.replace(/^#/, '');
        path = 'RESTCMSService.svc' + path;
        $.ajax({
            url: path,
            dataType: 'json',
            success: function(json) {
                Layout.onSuccess(json);
                if (options && options.success) {
                    options.success();
                }
            },
            complete: function() {
                if (options && options.complete) {
                    options.complete();
                }
            }
        });
    },

    reload: function(options) {
        Layout.load(window.location.hash, options);
    },

    onSuccess: function(json) {
        Layout.applyJSON(json);
        $(document).trigger('layout:success');
    },

    applyJSON: function(json) {
        $.each(json, function(i, v) {
            switch (v.action) {
                case 'append': $(v.select).append(v.data); break;
                case 'replace': $(v.select).html(v.data); break;
                case 'prepend': $(v.select).prepend(v.data); break;
                case 'function': parent.window[v.select].apply(this, Array.prototype.slice.call(v.args)); break;
            }
        });
    },

    handlePageLoad: function() {
        Layout.load(window.location.hash);
    }
};

Layout.init();
