﻿//slides must be inside a div named photoShuffle
//slides must be named pic-x where x starts at 1 and goes by 1
//shuffle is started by calling InitShuffle

var movingImage;
var widestPoint=0;
var numImages;
var imageNum = 1;

function InitShuffle() {
    numImages = $("div.#photoShuffle > img").length;

    //init z-index's
    $('#photoShuffle > img').each(function(index, image) {
        regexp = image.id.match(/\-(\d+)$/);
        zOrder = regexp[1];

        $(image).css("z-index", zOrder);
        //set the widestpoint with the widest image
        if ($(image).width() > widestPoint) {
            widestPoint = $(image).width();
        }
    });

    ShufflePhoto();
    window.setInterval(ShufflePhoto, 5000);
}

function ShufflePhoto() {
    var photo = "#photoShuffle > img.#pic-" + imageNum.toString();
    var image = $(photo);
    var randomVertical = Math.random() * 100 - 50;

    imageNum++;

    if (imageNum > numImages) {
        imageNum = 1;
    }

    if (image.css('z-index') == numImages) {
        movingImage = image;

        // move out
        image.animate({ left: "+=" + widestPoint, top: "+=" + randomVertical }, 300, null, ResetZIndex).animate({ left: "-=" + widestPoint, top: "-=" + randomVertical });
    }

}

function ResetZIndex() {
    movingID = movingImage.attr("id");

    // set z-index of moving image to bottom
    movingImage.css('z-index', 1);

    // move others up
    for (i = 0; i < numImages; ++i) {
        image = $('#photoShuffle > img.#pic-' + (i + 1));

        if (image.attr("id") != movingID) {
            image.css('z-index', parseInt(image.css('z-index')) + 1);

            if (parseInt(image.css('z-index')) == numImages) {
                regexp = image[0].id.match(/\-(\d+)$/);
                index = regexp[1];
            }
        }
    }
}

