var isBeingClicked = false;

$(document).ready(function()
{    
    slideShow();
});
 
function slideShow() 
{
  var imageCount = 1;

    //append a LI item to the UL list for displaying caption
    $('ul.slideshow').append('<li id="slideshow-caption" class="caption"><div class="slideshow-caption-container"><h3></h3><p></p></div></li>');
 
    //Set the opacity of all images to 0
    $('ul.slideshow li').css({opacity: 0.0});
     
    //Get the first image and display it (set it to full opacity)
    $('ul.slideshow li:first').css({opacity: 1.0});
     
    //Get the caption of the first image from REL attribute and display it
    $('#slideshow-caption h3').html($('ul.slideshow a:first').find('img').attr('title'));
    $('#slideshow-caption p').html($('ul.slideshow a:first').find('img').attr('alt'));
         
    //Display the caption
    $('#slideshow-caption').css({opacity: 0.8, bottom:0});
  
  /*
  $('ul.slideshow li').each(function(index) 
  {
    // Dont display the last <li> which is used for the slideshow caption
    if (imageCount != $('ul.slideshow li').length)
    {
      // Add the actual link
      $('.slider').append
      (
        '<div class="number_button">' +
          '<a href="javascript:void" id="number_button_' + imageCount + '" name="' + imageCount + '">' + imageCount + '</a>' +
        '</div>'
      );
      
      $('a#number_button_' + imageCount).click(function()
      {
        gallery($(this).attr('name'));
      });
      
      imageCount++;
    }
  });
  */
  
  $('a.next_item').click(function()
  {
    if (!isBeingClicked)
    {
      gallery("next");
    }
  });

  $('a.previous_item').click(function()
  {
    if (!isBeingClicked)
    {
      gallery("previous");
    }
  });    
}
 
function gallery(direction) 
{ 
  isBeingClicked = true;
    //if no IMGs have the show class, grab the first image
    var current = ($('ul.slideshow li.show')?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));
  
  // variable holding the next element to show
  var next;
  
  if (direction == "next")
  {
    if (current.next().length)
    {
      if (current.next().attr('id') == 'slideshow-caption')
      {
        next = $('ul.slideshow li:first');
      }
      else
      {
        next = current.next();
      }
    }
    else
    {
      next = $('ul.slideshow li:first');
    }
  }
  else if (direction == "previous")
  {
    if (current.prev().length)
    {
      next = current.prev();
    }
    else
    {
      next = $('ul.slideshow li:last').prev();
    }
  }
  // Must be coming from a number link
  //else
  //{
    //alert(test);
    // If the index exists in the UL
    //var mew = $('ul.slideshow li').get(direction-1);
    //alert(mew);
   // if (mew.length)
    //{
     // alert("WORKS.");
   // }
  //}
        
    //Get next image caption
    var title = next.find('img').attr('title'); 
    var desc = next.find('img').attr('alt');    
         
    //Set the fade in effect for the next image, show class has higher z-index
    next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
     
    //Hide the caption first, and then set and display the caption
    $('#slideshow-caption').slideToggle(300, function () 
  { 
        $('#slideshow-caption h3').html(title); 
        $('#slideshow-caption p').html(desc); 
        $('#slideshow-caption').slideToggle(500); 
    }); 
 
    //Hide the current image
    current.animate({opacity: 0.0}, 1000, function()
  {
    current.removeClass('show');
    isBeingClicked = false;
  });
}
