(function () {
    var guid = '50554E89-EA97-423A-B987-FADA6C9856A4',
        subsite = 'semestra-ch';
    document.write('<a id="' + guid + '"></a>');

    var container = document.getElementById(guid).parentNode;
    while (container.hasChildNodes()) {
        container.removeChild(container.firstChild);
    }

    var iframe = document.createElement('iframe');
    iframe.setAttribute('frameBorder', '0');
    iframe.style.width = '100%';
    iframe.style.height = '100%';
    container.appendChild(iframe);

    var reloadFrame = function () {
        var src = '//bookboon.com/external/' + subsite;

        if (location.hash && location.hash.match(/^#bookboon:/)) {
            src += '/' + location.hash.substring(10);
        }

        src += '#' + encodeURIComponent(location.href.substring(0, location.href.length - location.hash.length));

        iframe.src = src;
    }

    var hashChange = function () {
        //if (subsite === 'test') {
            location.reload(false);
        //}
        //else {
        //    reloadFrame();
        //}
    };

    if (window.onhashchange) {
        if (window.addEventListener) {
            window.addEventListener('hashchange', hashChange, false);
        } else if (window.attachEvent) {
            window.attachEvent('onhashchange', hashChange);
        }
    }
    else {
        var prevHash = location.hash;
        setInterval(function () {
            if (location.hash !== prevHash) {
                prevHash = location.hash;
                hashChange();
            }
        }, 50);
    }

    reloadFrame();
})();
