$(document).ready(function(){
  
  
  //Home slide show
  var numMainImages = $('#home-slideshow .images img').length;
  $('#home-slideshow .caption').not(':first').hide();
  $('#home-slideshow .caption:first').css({opacity:0}).delay(1000).animate({opacity: '0.6'}, 500);
  var slideTimerId = 0;
  if(numMainImages > 1){
    var slideTimerId = setInterval(nextImage, 6000);
  }
  
  function nextImage(){
    numMainImages --;
    if(numMainImages > 0){
      var currCaption = $('#home-slideshow .caption').not(':hidden');
      var nextCaption = $('#home-slideshow .caption').not(':hidden').next('.caption');
      $('#home-slideshow .caption').not(':hidden').animate({opacity: '0'}, 100);
      $('#home-slideshow .images').animate({marginLeft: '-=870'}, 500, function(){
        currCaption.hide();
        nextCaption.show().delay(500).animate({opacity: '0.6'}, 500);
      });
    } else {
      clearTimeout(slideTimerId);
    }
  }
  
  
  
  //Food slide show
  var numFoodImages = $('#home-food .photo .image-frame').length;
  var currFoodImage = 1;
  //var foodImageOffset = $('#home-food .photo .image-frame:first').width() + 90;
  var foodImageOffset = 343 + 90;
  checkbuttons();
  $('#home-food .description').not(':first').css('opacity', 0);
  $('#home-food .button-left').click(function(){
    if(currFoodImage != 1){
      $('#home-food .description:nth-child(' + (currFoodImage - 1) + ')').animate({opacity: 1}, 300);
      $('#home-food .description:nth-child(' + currFoodImage + ')').animate({opacity: 0}, 300);
      $('#home-food .photo .wrapper').animate({marginLeft: '+=' + foodImageOffset}, 300);
      currFoodImage--;
      checkbuttons();
    } 
  });
  $('#home-food .button-right').click(function(){
    if(currFoodImage != numFoodImages){
      $('#home-food .description:nth-child(' + (currFoodImage + 1) + ')').animate({opacity: 1}, 300);
      $('#home-food .description:nth-child(' + currFoodImage + ')').animate({opacity: 0}, 300);
      $('#home-food .photo .wrapper').animate({marginLeft: '-=' + foodImageOffset}, 300);
      currFoodImage++;
      checkbuttons();
    } 
  });
  
  function checkbuttons(){
    if(currFoodImage != numFoodImages && currFoodImage != 1){
      $('#home-food .button-left').removeClass('button-left-disabled');
      $('#home-food .button-right').removeClass('button-right-disabled');
    } else if(currFoodImage == numFoodImages){
      $('#home-food .button-right').addClass('button-right-disabled');
    } else if(currFoodImage == 1){
      $('#home-food .button-left').addClass('button-left-disabled');
    }
  }
  

});


