﻿/* Made by erik.hennerfors@adeprimo.se */
(function($) {
    $.fn.wallpaperslider = function(options) {
        //Set #Background height
        $('#Background').height($('html').outerHeight(true));

        $(window).resize(function() {
            $('#Background').height($('html').outerHeight(true));
        });
        var rootElement = $(this);
        var colWallpapers = rootElement.find('div.wallpaper');
        var imgArr = [];
        $(colWallpapers).find('li').each(function() { imgArr.push($(this).text()) });
        var preloadArr = new Array();
        var i;
        /* preload images */
        for(i=0; i < imgArr.length; i++){
            preloadArr[i] = new Image();
            preloadArr[i].src = imgArr[i];
        }
        var currImg = 0;
        if(currImg == 0) {
            $('#Background').css('background','url(' + preloadArr[preloadArr.length-1].src +') no-repeat fixed center 96px');
        }
        var intID = setInterval(changeImg, 10000);
        /* image rotator */
        function changeImg(){
            if(preloadArr.length > 1) {
                $('#Background').animate({opacity: 0.5}, '2500', function() {
                    $(this).css('background','url(' + preloadArr[currImg++%preloadArr.length].src +') no-repeat fixed center 96px')
                        .animate({opacity: 1});
                });
                /*$('#Background').clone().insertBefore('#Background');
                $('#Background:first').addClass('New');
                $('#Background:last').addClass('Old');
                /*$('#Background.').fadeOut('fast', function() {
                    $('#Background.New').css('background','url(' + preloadArr[currImg++%preloadArr.length].src +') no-repeat fixed center 96px')
                        .fadeIn('fast', function() {});
                });*/
                
                //$('#Background.Old').remove();
            }
        }
    };
})(jQuery);
