$(document).ready(function() {
    PageNavigator.init();
    MuscomQuiz.init();


    fixFonts();

    var flashvars = {};
    flashvars.xmlSource = "/media/flash/xml/config.xml";
    var params = {};
    params.allowscriptaccess = "always";
    params.allowfullscreen = "true";
    params.wmode = "transparent";
    var attributes = {};


    SWFAddress.onChange = function() {

        var url = this.getBaseURL() + "/#" + this.getValue();
        if (!MuscomQuiz.answer(url)) {
            PageNavigator.loadPage(url);
        }

        var segments = this.getValue().split("/")
        if (segments[1] == "tijdlijn" || segments[1] == "ontwerpers" || segments[1] == "ontwerpen" || segments[1] == "emissie" || segments[1] == "album") {
            if ($('#flashContent p').length > 0) {
                swfobject.embedSWF("/media/flash/Timeline.swf", "flashContent", "100%", "100%", "9.0.0", "/media/flash/expressInstall.swf", flashvars, params, attributes);
            }
        }
    }

    // capture enter events in extended search
    if ($("#search_container").length > 0) {
        window.onkeypress = globalKeyPressed;
    }

    postProcess();
});

function globalKeyPressed(event) {
    if (currentForm && event.keyCode == 13) {
        currentForm.submit();
    }
}

function fixFonts() {
    /**
     Cufon.replace('h2');
     Cufon.replace('h3');
     Cufon.replace('h4');
     Cufon.replace('h5 a', {
     textShadow: '#bbb 0px 0px, #bbb 1px 1px',
     hover: true
     });
     Cufon.replace('p');
     */
}

function postProcess() {
    $('.chapter_description').draggable({
        containment: 'parent',
        handle: 'div.headerbar',
        cursor: 'move'
    });

    $('.zoomer').draggable({
        containment: 'parent',
        cursor: 'move',
        drag: function(event, ui) {
            var zoomer = $(event.target);
            var offsetLeft = zoomer.css('left').replace('px', '');
            var offsetTop = zoomer.css('top').replace('px', '');
            var percTop = (offsetTop / (100 - zoomer.height())) * 100;
            var percLeft = (offsetLeft / (165 - zoomer.width())) * 100;
            $('.zoomedimage').css('background-position', percLeft + '% ' + percTop + '%');
        }
    });
}

