AMOUNT_BACKGROUNDS = 3;
URL_PAGE_PARAM = window.location.href.split('?page=');

function backgroundResize(img, ratio) {
    var windowRatio = $(window).width() / $(window).height();

    if (ratio > windowRatio) {
        img.height($(window).height());
        img.width('');
    } else {
        img.width($(window).width());
        img.height('');
    }
}

function setIframeSrc(newSrc) {
    $('#main-frame').attr('src', newSrc);
}

function cycleBackground() {
    var background = $("#background-photo");
    var nextIndex = parseInt(background.attr('src').match(/\d/)[0]) + 1;
    if (nextIndex == AMOUNT_BACKGROUNDS) {
        nextIndex = 0;
    }
    background.attr({
        src: background.attr('src').replace(/\d/, nextIndex),
        alt: background.attr('alt').replace(/\d/, nextIndex)
    });
}

$(function(){

    URL_PAGE_PARAM.length > 0 ? setIframeSrc(URL_PAGE_PARAM[1]) : '';

    var background = $("#background-photo");
    var imageRatio = background.width() / background.height();
    backgroundResize(background, imageRatio);
    $(window).bind("resize", function() {
        backgroundResize(background, imageRatio);
    });

});
