(function() {
    var d = document,
        dl = d.location,
        sts = d.getElementsByTagName('script'),
        st = sts[sts.length-1],
        da = 'data-csr-',
        ieLt8 = document.getElementsByTagName("html")[0].className.indexOf("ielt8") !== -1,
        config;

    //Get the hash
    function getHash(){
        var a = String(dl.href),
            b = a.indexOf('#!');

        if (b !== -1){
            return a.substr(b+2).replace('%3F','?');
        }

        return false;
    }

    config = {
        contextpath: st.getAttribute(da + 'contextpath').replace(/\/$/, ''), //Strip slash at end
        enabled: st.getAttribute(da + 'enabled') === 'true' && !ieLt8,
        rendered: st.getAttribute(da + 'rendered') || '/',
        hash: getHash()
    };

    //If enabled and not supporting HTML5 History API we need to check the hash!
    if (config.enabled && !Modernizr.history) {
        if( config.rendered && config.rendered != '/' ){
            //Only if the page is not already the main-page we need to redirect with the hash
            dl.replace(config.contextpath + '/#!' + config.rendered);
        } else if(config.hash) {
            //Show the loading message because we have a hashbang and we need additinal loading
            var elm = document.getElementById('container-status');
            elm.style.display = 'block';
        }
    } else if (config.hash) {
        //If we are disabled or have HTML5 History API support we need to remove the hash if it was there!
        dl.replace(config.contextpath + config.hash);
    }
})();
