$(document).ready(slideShow_initialize);

var slideShow_current = 0;
var slideShow_elements = [];
var slideShow_timeOut;
var slideShow_inTimeOut;
var slideShow_interval = 11000;
var slideShow_animationWorking = false;

function slideShow_initialize()
{
    var elements = $(".slideShow");
    
    for (i=0; i<elements.length; i++)
    {
        $(elements[i]).attr("id", i+"_slideShow");
        slideShow_elements[i] = i+"_slideShow";
    }
    
    $(".slideShowNext").click(slideShow_next).css("cursor", "pointer");
    $(".slideShowPrev").click(slideShow_previous).css("cursor", "pointer");    
    $(".slideShowPause").click(slideShow_timeOutPause).css("cursor", "pointer");
    $(".slideShowPlay").click(slideShow_timeOutPause).css("cursor", "pointer");    
    
    slideShow_start();
    slideShow_timeOutStart();
}

function slideShow_currentIndex()
{
    return slideShow_elements[slideShow_current];    
}

function slideShow_nextIndex()
{
    if ((slideShow_current+1)<slideShow_elements.length)
    {
        slideShow_current = slideShow_current + 1;
    }else
    {
        slideShow_current = 0;
    }
    
    return slideShow_elements[slideShow_current];
}

function slideShow_prevIndex()
{
    if ((slideShow_current-1)>=0)
    {
        slideShow_current = slideShow_current - 1;
    }else
    {
        slideShow_current = slideShow_elements.length-1;
    }
    
    return slideShow_elements[slideShow_current];
}

function slideShow_timeOutStart()
{
    slideShow_timeOutStop();
    slideShow_inTimeOut=true;
    slideShow_timeOut = setTimeout(function () {
                                                slideShow_next();
                                                slideShow_timeOutStart();
                                                }, slideShow_interval);
}

function slideShow_timeOutReset()
{
    if (slideShow_inTimeOut)
    {
        slideShow_timeOutStart();
    }
}

function slideShow_timeOutStop()
{
    clearTimeout(slideShow_timeOut);
}

function slideShow_timeOutPause()
{
    if (slideShow_inTimeOut)
    {
        $(".slideShowPlay").show();
        $(".slideShowPause").hide();
        slideShow_timeOutStop();
        slideShow_inTimeOut = false;
    }
    else
    {
        $(".slideShowPause").show();          
        $(".slideShowPlay").hide();      
        slideShow_next();
        slideShow_timeOutStart();     
    }
}

function slideShow_next()
{
    if (!slideShow_animationWorking) {
        slideShow_animationWorking = true;
        $("#"+slideShow_currentIndex()).fadeOut("slow", function () {
                                                                    slideShow_timeOutReset();
                                                                    $("#"+slideShow_nextIndex()).fadeIn("slow", function () {
                                                                                                                            slideShow_animationWorking=false;
                                                                                                                            });
                                                                    });
    }
}

function slideShow_previous()
{
    if (!slideShow_animationWorking) {
        slideShow_animationWorking = true;        
        $("#"+slideShow_currentIndex()).fadeOut("slow", function () {
                                                                    slideShow_timeOutReset();
                                                                    $("#"+slideShow_prevIndex()).fadeIn("slow", function () {
                                                                                                                            slideShow_animationWorking=false;
                                                                                                                            });
                                                                    });
    }
}

function slideShow_start()
{
    $("#"+slideShow_currentIndex()).show();
}