﻿function swapImages() {

    var nextCell = $("#ImageSwapOrder li.next").attr('tag');

    var imageList = (nextCell == '#cell4') ? '#LargeImageList' : '#SmallImageList';

    var nextImageLi = $(imageList + ' li.next');

    var newImg = new Image();
    newImg.src = nextImageLi.attr('url');

    newImg.onload = (function () {
        var nextCellLi = $("#ImageSwapOrder li.next");
        var nextCell = nextCellLi.attr('tag');

        var activeImg = $(nextCell + ' img.active');

        activeImg.fadeOut(1200, function () { $(this).remove(); });
        $(this).fadeIn(1200);
        $(this).addClass('active');

        var nextNextImageLi = ($(imageList + ' li.next').next().length > 0) ? $(imageList + ' li.next').next() : $(imageList + ' li:first');
        nextImageLi.removeClass('next');
        nextNextImageLi.addClass('next');

        var nextNextCellLi = ($('#ImageSwapOrder li.next').next().length > 0) ? $('#ImageSwapOrder li.next').next() : $('#ImageSwapOrder li:first');
        nextCellLi.removeClass('next');
        nextNextCellLi.addClass('next');
    });

    $(nextCell + ' div').append(newImg);
}

$(document).ready(function () {
    // Run our swapImages() function every 5secs
    setInterval('swapImages()', 3000);
});

