$(document).ready(function () {


    // --[BACKGROUND IMAGES]-------------------------------------------
    // Build array with background images
    var lastImg = 8; // number of background images
    var bgArray = new Array();
    for (i = 0; i <= lastImg; i++) {
        bgArray[i] = 'bg-' + i + '.jpg';
    } //end:for

    // Get and set background of site
    getRandomImg(bgArray);
    // Get new background every x seconds
    window.setInterval(function () {
        if (currentMenu != 'play') getRandomImg(bgArray);
    }, 7000);
    //$('#nav a').click(function () { getRandomImg(bgArray); }); // onclick get new background

    // Resize 'background' on scroll
    $(window).resize(function () { resizeBg(); });
    // ----------------------------------------------------------------



    // --[DYNAMIC CONTENT]---------------------------------------------
    // START Load dynamic content from html from external file
    var menuitem = "#nav ul li a";   // menuitems
    var content = ".info-block";    // content block
    var currentMenu; // global var for current menuitem

    $('#nav a').hover( // On menu hover, remove active
        function () { $('#nav a.active').addClass('deactive').removeClass('active'); }, // in: disable active
        function () { $('#nav a.deactive').addClass('active').removeClass('deactive'); } // out: enable active
    );

    $('.info-block').slideUp(0);    // 'Hide' slider @ page load
    $('#movie').hide();             // Hide movie on page load
    //$(content).load("external.html #company"); // Load default content on page load

    // Manage click events
    $(menuitem).click(function () {
        // move active class on menu
        var thisItem = menuitem + '#' + this.id;
        $(menuitem).removeClass('deactive');
        $(thisItem).addClass('active');

        // load new content with animation
        var loadContent = "external.html #" + this.id;
        $(content).slideUp(900, 'easeInQuart', function () {
            if (thisItem != (menuitem + '#play')) {
                $('#background-img').fadeTo('slow', 1);
                $('#movie').hide(); // if visible, hide movie
                $(content).load(loadContent, function () {
                    $(content).slideDown(900, 'easeOutQuart');
                });
                currentMenu = 'normal';
            } else {
                $('#background-img').fadeTo('slow', 0.15);
                $('#movie').show();
                $('#container').fadeOut();
                currentMenu = 'play';
            }
        });

    }); // end: dynamic content
    // ----------------------------------------------------------------



    // --[MOVIEPLAYER]-------------------------------------------------
    // START button
    $("#play").click(function () {
        jwplayer("movie-01").play(true);
        jwplayer("movie-01").setVolume(100);
    });
    // ----------------------------------------------------------------

});  // end:onload



// FUNCTION resize 'background' img en set scroll
function resizeBg() {
    var widthWindow = $(window).width()
    var widthBg = $('#background-img').width();

    var heightWindow = $(window).height()
    var heightBg = $('#background-img').height();

    if (heightWindow > heightBg) {
        $('#background-img').removeClass();
        $('#background-img').addClass('bgheight');
    }
    if (widthWindow > widthBg) {
        $('#background-img').removeClass();
        $('#background-img').addClass('bgwidth');
    }
}// END resize 'background' img



// FUNCTION getRandomImg: get and set random background image
var randLast; // global var for saving last used background
function getRandomImg(randArray) {
    do { 
        randNr = Math.floor(Math.random() * randArray.length); // select random image
    } while (randNr == randLast); // loop if image is the same as last
    //console.log('nr: '+randNr+' last: '+randLast);
    randLast = randNr; // set new last used image
    $('<img style="display:none;" />') // preload image and set src when fully loaded (load in background)
        .attr('src', 'Images/Visuals/' + randArray[randNr])
        .load(function () {
		    $('#background-img').attr("src", 'Images/Visuals/' + randArray[randNr]); // set new source of image
		    resizeBg(); // Fit background to screen
		});
} //end:getRandomImg
	
	

