/*
 * (c) 2011 David Krause, Interaktive Gestaltung
 * http://www.interaktive-gestaltung.de
 */

/**
 * Class for handling the display of the slideshow.
 *
 * @param photoSelector
 */
function Slideshow(photoSelector)
{
    var photoSelector = photoSelector;

    var photos; // contains all photo objects
    var photosNum; // Number of photos in the slideshow
    var addedImagesCount; // counts the added images
    var firstRun = true; // first run of slideshow
    var timerId; // stores the timer

    var currentPhotoId; // Id of currently displayed Photo

    var playOrder = new Array(); // Describing the order, in which the images are displayed.

    /**
     * Inits the slideshow
     * @param numOfPhotos Number of photos
     */
    this.init = function(numOfPhotos)
    {
        photosNum = numOfPhotos;
        addedImagesCount = 0;
        photos = $(photoSelector);

        /*
            Initializing the playOrder. The first run is using a straight order from 0 to X,
            to stay synchronized with the preloading of the view.
            It's still random, since the urls have been shuffled in the view before.
         */
        for (var i = 0; i < photosNum; i++ )
        {
            playOrder[i] = i;
        }
    }

    /**
     * Exits the slideshow.
     * Clears the timer.
     */
    this.exit = function ()
    {
        clearTimeout(timerId);
    }

    /**
     * Adds a new image.
     * The image is added to the background of the already existant photo div.
     * The function is called from the contentView.
     *
     * @param img The image to be added.
     */
    this.addImage = function (img)
    {
        $(photos[addedImagesCount]).css('background-image', 'url(' + img.src + ')');
        addedImagesCount ++;
        if (addedImagesCount == 1) showPhoto(0, true);
    }

    /**
     * Hides all photos.
     */
    var hideAllPhotos = function()
    {
        photos.hide();
    }

    /**
     * Shows a photo.
     *
     * @param id The id of the photo.
     * @param doFade If true, photo fades in.
     */
    var showPhoto = function(id, doFade)
    {
        trace("slideshow showPhoto " + id);

        hideAllPhotos();
        var playNr = playOrder[id];
        var photo = $(photos[playNr]);
        photo.show();
        if (doFade)
        {
            photo.css({ opacity: 0 });
            photo.fadeTo(250, 1, 'swing');
        }
        currentPhotoId = id;

        timerId = setTimeout(waitTillNextPhotoAdded, 1000);
    }

    /**
     * Checks if the next photo to be displayed is already added (to the div bgs).
     * If not, waits till the photo is loaded and then displays the foto.
     */
    var waitTillNextPhotoAdded = function ()
    {

        if (!firstRun || addedImagesCount == photosNum || addedImagesCount - 1 > currentPhotoId)
        {
            // image has already been added
            showNextPhoto();
        } else
        {
            // recheck till image is loaded
            trace("recheck...");
            setTimeout(waitTillNextPhotoAdded, 10);
        }
    }

    /**
     * Shows the next photo.
     */
    var showNextPhoto = function()
    {
        var nextPhotoId = currentPhotoId + 1;
        if (nextPhotoId < addedImagesCount)
        {
            showPhoto(nextPhotoId);
        }
        else
        {
            restartSlideshow();
        }
    }

    /**
     * Restarts the slideshow with a different order.
     *
     */
    var restartSlideshow = function ()
    {
        playOrder.shuffle();
        firstRun = false;
        showPhoto(0);
    }



}

