//var images = [];
var last_index = 0;
var theTimer;
var running = false;

function showNextImage() {

    var img_index = last_index;

    /*
    if (category == "all")
    {
        // for the "home" category, show images in random order
        while (img_index == last_index) {
            img_index = Math.floor(Math.random() * images.length);
        }
    }
    else
    {
        // for a specific category, show images in sequential order
        img_index++;
        if (img_index >= images.length) {
            img_index = 0;
        }
    }
    */

    // show images in sequential order
    img_index++;
    if (img_index >= images.length) {
        img_index = 0;
    }

    var img_name = images[img_index];
    show(img_name);

    last_index = img_index;
}


function showPreviousImage() {
    var img_index = last_index;
    --img_index;
    if(img_index < 0) {
        img_index = images.length - 1;
    }
    var img_name = images[img_index];
    show(img_name);
    last_index = img_index;
}

function show(img_name)
{
    var bigImage = $("img[name='big']");
    if (!bigImage.ImageAnimating()) {
        showImage(img_name);
    }
}

function showImage(img_name)
{
    var bigImage = $("img[name='big']");
    bigImage.attr("alt", img_name);
    bigImage.ImageSwitch({
        NewImage:portfolioRoot + "/" + img_name,
        Speed: 2000
    });
}

 /*
function getImageList() {

    var d = new Date();

    var theUrl = appRoot + "portfolio/list/" + category;

   // alert("before getImageList XHR: " + theUrl);

    $.ajax(
        {
            type:"GET",
            url:theUrl,
            dataType: "json",
            success: onDataResult,
            error: onDataError,
            cache: false,
            timeout: 5000
        }
    );
   // alert("after getImageList XHR");

}

function onDataResult(data) {
    // alert("before gOnDataResult");

    var i;
    var j;
    var category;
    var img;
    for (i = 0; i < data.length; i++)
    {
        category = data[i];
        for (j = 0; j < category.images.length; j++)
        {
            img = category.images[j];
            images.push(category.name + "/" + img.url);
        }
    }


    start();

    enableButtonHandlers();

    // alert("after onDataResult");

}

function onDataError(textStatus, errorThrown) {

   // alert("before onDataError");

    var msg = "";
    if (textStatus)
    {
        if (textStatus == "timeout")
        {
            msg += "There was no response from the server (Timeout)";
        }
        else
        {
            msg += "There was an error communicating with the server";
        }
    }

    if (errorThrown)
    {
        msg += ": " + errorThrown;
    }

    alert(msg);
}
*/

function start() {
    // alert("before start");
    showNextImage();

    if(theTimer == null)
    {
        theTimer = $.timer(5000, function (timer) {
            showNextImage();
        });
    }
    else
    {
        theTimer.reset(5000);
    }

    $("#button_pause_start").attr("src", appRoot + "static/images/button_pause.png");
    running = true;

    //alert("after start");
}

function pause() {
    theTimer.stop();
    $("#button_pause_start").attr("src", appRoot + "static/images/button_play.png");    
    running = false;
}

function toggleSlideShow() {

    if(running)
    {
        pause();
    }
    else
    {
        start();
    }

}

function prev() {
   if(running) {
      pause();
   }
   showPreviousImage();
}

function next() {
   if(running) {
      pause();
   }
   showNextImage();
}

function enableButtonHandlers() {
 $("#button_prev").click(prev);
 $("#button_pause_start").click(toggleSlideShow);
 $("#button_next").click(next);    
}


/*
$(document).ready(function () {
    getImageList();
});
*/

jQuery(function () {
    //getImageList();

    start();
    enableButtonHandlers();
});