$(function() {
  slideshow = $('div.slideshow');
  positions = [];
  position = 0;
  
  slideshow.find('li').each(function(index) {
    positions.push(slideshow.find('li').width() * index * -1);
  });
  
  slideshow.find('ul').css('width', slideshow.find('li').length * slideshow.find('li').width());

/*  slideshow.append('<a href="#" class="previous">&lt; Previous</a><a href="#" class="next">Next &gt;</a>'); */

  $('a.previous').bind('click', function(event) {
    event.preventDefault();
    if(position > 0) {
      position--;
    }
    else {
      position = positions.length - 1;
    }
    
    moveSlideShow(slideshow, position);
  });

  $('a.next').bind('click', function(event) {
    event.preventDefault();
    if(position < positions.length - 1) {
      position++;
    }
    else {
      position = 0;
    }
    
    moveSlideShow(slideshow, position);
  });
  
/*  slideshow.bind('mouseenter', stopTimer);
  slideshow.bind('mouseleave', startTimer);*/
  
  startTimer();
  
});

function startTimer() {
  slideshow.everyTime(5000, function() {
//    if(position < positions.length - 1) {
      position++;
//    }
/*    else {
      position = 0;
    }*/
    
    moveSlideShow(slideshow, position);
  });
}

function stopTimer() {
  slideshow.stopTime();
}

function moveSlideShow(slideshow, position) {
  if(position == 0){
	  slideshow.find('ul').css("display", "none" );
	  slideshow.find('ul').css("left", "0px" );
	  slideshow.find('ul').fadeIn("slow");
	}
  else {
  
  slideshow.find('ul').animate({
    left:positions[Math.abs(position)]
  }, 500);
  }
}



