(function ($) {

    var backgroundImages = [
        'theme_1/bg.jpg',
        'theme_2/bg.jpg',
        'theme_3/bg.jpg',
        'theme_4/bg.jpg',
        'theme_5/bg.jpg',
        'theme_6/bg.jpg',
        'theme_7/bg.jpg',
        'theme_8/bg.jpg',
        'theme_9/bg.jpg',
        'theme_10/bg.jpg',
        'theme_11/bg.jpg'
    ];


    var imagePrefix = 'images/base/theme/';

    $(document).ready(function () {
        var iid = $.cookie('iid');
        var selectedImage = backgroundImages[iid];
        if (selectedImage !== undefined) {
            setBackgrounds(selectedImage);
        } else {
            var index = getRandomArbitrary(backgroundImages.length);
            selectedImage = backgroundImages[index];
            setBackgrounds(selectedImage);
            $.cookie("iid", index,{ path: '/' });
        }
    });

    function getRandomArbitrary(max) {
        return Math.floor(Math.random() * max);
    };

    function setBackgrounds(imageName) {
        setBackgroundFor('header', imageName);
        setBackgroundFor('.grayPage', imageName);
        setBackgroundFor('.login_bg', imageName);
        setBackgroundFor('.login_bg', imageName);
        setBackgroundFor('#bg-static', imageName);
        setBackgroundFor('#bg', imageName);
    };

    function setBackgroundFor(elementId, imageName) {
        $(elementId).css('background-image', 'url(\'' + appContext + imagePrefix + imageName + '\')')
    }

})(jQuery);
