$(function () {
    var rpicbottom = $("#randompic");
    //shuffles list in-place
    function shuffle(list) {
        var i, j, t;
        for (i = 1; i < list.length; i++) {
            j = Math.floor(Math.random() * (1 + i)); // choose j in [0..i]
            if (j != i) {
                t = list[i]; // swap list[i] and list[j]
                list[i] = list[j];
                list[j] = t;
            }
        }
    }

    if (rpicbottom.length) {
        var rpics = RANDOMPICS;
        shuffle(rpics);
        rpicbottom.attr('src', rpics[0]);
        var rpictop = $("<img id='randompictop' />")
            .insertAfter(rpicbottom)
            .css('position', 'absolute')
            .css(rpicbottom.position())
            .attr('src', rpicbottom.attr('src'))
            .hide(0);
        
        
        var precachedImages = [];
        function precacheImages(images) {
            $.each(images, function(i) {
                precachedImages[i] = new Image();
                precachedImages[i].src = this;
            });
        }
        precacheImages(rpics);
        
        var curpos = 0;
        function next() {
            curpos += 1;
            var nextpic = rpics[curpos % rpics.length];
            rpictop.hide(0);
            rpictop.attr('src', nextpic);
            rpictop.fadeIn(2000, function() {
                rpicbottom.hide(0).detach().insertAfter(rpictop);
                var _rpictop = rpictop;
                rpictop = rpicbottom;
                rpicbottom = _rpictop;
            });
        }
        setTimeout(function () {
            next();
            setInterval(next, 9000);    
        }, 7000);
    
        $(".adminbox-category a").overlayDialog({
            width: 800,
            height: 310
        });
    
        $(".adminbox-prices a").overlayDialog({
            width: 500,
            height: 310
        });
            
        
    }      
});
