$(window).load(function() {
    var theWindow    = $(window),
    $bg              = $("#bg"),
    aspectRatio      = $bg.width() / $bg.height();

    function resizeBg() {
        if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
            $bg.removeClass().addClass('bgheight');
        } else {
            $bg.removeClass().addClass('bgwidth');
        }

    }

    theWindow.resize(function() {
        resizeBg();
    }).trigger("resize");
});

var galleries = new Array();
galleries['evenementen'] = 15;
galleries['portretten'] = 9;
galleries['reclame'] = 6;
galleries['stedelijk'] = 8;
galleries['zakelijk'] = 4;
galleries['anders'] = 7;

galleries['ambitieuzevrouwen'] = 5;
galleries['fabriek036'] = 6;
galleries['lafemme'] = 10;
galleries['marilyn'] = 15;
galleries['tenali'] = 11;

var currentImageIndex = 0;  // current image index within the gallery
var totalNrImages = 2;      // total number of images in the gallery
var containerMinimized = false;
var currentGallery = 'evenementen';

function forward() {
    if (currentImageIndex < galleries[currentGallery]) {
        currentImageIndex++;
        $("#bg").fadeOut(500, function() {
            $(this).attr("src", "./images/gallery/" + currentGallery + "/monavid_" + currentImageIndex + ".jpg");
            $(this).fadeIn(500);
        });
    } else {
        currentImageIndex = 1;
        $("#bg").fadeOut(500, function() {
            $(this).attr("src", "./images/gallery/" + currentGallery + "/monavid_" + currentImageIndex + ".jpg");
            $(this).fadeIn(500);
        });
    }
    console.log(currentImageIndex);
    return true;
}

function backward() {
    if (currentImageIndex > 1) {
        currentImageIndex--;
        $("#bg").fadeOut(500, function() {
            $(this).attr("src", "./images/gallery/" + currentGallery + "/monavid_" + currentImageIndex + ".jpg");
            $(this).fadeIn(500);
        });
    } else {
        currentImageIndex = galleries[currentGallery];
        $("#bg").fadeOut(500, function() {
            $(this).attr("src", "./images/gallery/" + currentGallery + "/monavid_" + currentImageIndex + ".jpg");
            $(this).fadeIn(500);
        });
    }
    console.log(currentImageIndex);
    return true;
}

function toggleContainer() {
    if (!containerMinimized) {
        $("#container").slideUp(500, function(){
            $("#header").slideDown(500, function() {
                containerMinimized = true;
                currentImageIndex = 1;
                $("#bg").fadeOut(500, function() {
                    $(this).attr("src", "./images/gallery/" + currentGallery + "/monavid_" + currentImageIndex + ".jpg");
                    $(this).fadeIn(500);
                });

            });
        });
    } else {
        $("#header").slideUp(500, function() {
            $("#container").slideDown(500, function(){
                containerMinimized = false;
            });
        });
    }
}
