
  currentImageIndex = 0;
  intervalId = undefined;
  
  window.stopAnimation = false;

  function slideSwitch(nextIndex) {
 
    clearInterval(intervalId);

    var active = $('#image-' + currentImageIndex);
    var length = $('#slideshow img').size();

    if ( active.length == 0 ) active = $('#slideshow img:last');

    if(nextIndex == undefined)
      nextIndex = parseInt(currentImageIndex) + 1;
    else
      stopAnimation = true;
   
    if(nextIndex > (length-1))
      nextIndex = 0;
    if(nextIndex < 0)
      nextIndex = length-1;
    
    currentImageIndex = nextIndex;

    var next  = $('#image-' + nextIndex);
            
    active.addClass('last-active');

    next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            active.removeClass('active last-active');
        });

    if(!stopAnimation)
      intervalId = setInterval( "slideSwitch()", 5000 );

    $('#slideshow-container .item').removeClass('selected');
    $('#slideshow-container #item-' + nextIndex).addClass('selected');
  }
  
  $(document).ready ( function() {
    
    intervalId = setInterval( "slideSwitch()", 5000 );

    $('#slideshow-container .item').click(function() {
      console.log("Clicked");
      var thisId = $(this).attr('id');
      thisId = thisId.substr(thisId.lastIndexOf('-')+1);
      console.log("calling with id " + thisId);
      slideSwitch(thisId);
    })

  });// ActionScript Communications Document
