$('html').addClass('js');

$(function(){
  
  //************************ Image gallery ***/
  slide = $('#img-gallery li').outerWidth(true);
  slideContainer = $('#img-gallery ul');
  
  $('#img-gallery ul').css({'width':slide*$('#img-gallery li').length});
  
  $('#img-gallery li:first-child').addClass('active first-visible');
  $('#img-gallery li:nth-child(10)').addClass('last-visible');
  $('#img-gallery li a').click(function(){
    $(this).parent().addClass('active').siblings().removeClass('active');
    updateHolder();
    return false;
  });
  $('#img-holder').after('<p id="img-description">'+$('#img-gallery .active img').attr('alt')+'</p>');
  $('#img-gallery .controls a').click(function(){ sliderControls( $(this).attr('class') ) });
  
  function updateHolder() {
    var imgLink = $('#img-gallery .active a');
    var imgBig = $('#img-holder img');
    $('#img-holder').addClass('loading');
    if ( imgBig.attr('src') !== imgLink ) {  
      $('#img-description').animate({ opacity: 0 });
      imgBig.animate({ opacity: 0 }, function(){
        var img = new Image();
        $(img).load(function(){   
          $(this).css({opacity: 0});
          $('#img-holder').removeClass('loading').html(this);
          $(this).animate({opacity: 1});
          $('#img-description').html($('#img-gallery .active img').attr('alt')).animate({opacity: 1});
        }).error(function(){}).attr({src: imgLink.attr('href'), alt: imgLink.children().attr('alt') });
      });
    }
  }
  function sliderControls(sliderControl){
    if (sliderControl === 'next') {
      $('#img-gallery .active').removeClass('active').next().addClass('active');
      if ( $('#img-gallery .active').prev().hasClass('last-visible') ) {
        $('#img-gallery .last-visible').removeClass('last-visible').next().addClass('last-visible');
        $('#img-gallery .first-visible').removeClass('first-visible').next().addClass('first-visible');
        slideContainer.animate({ marginLeft: -(slide-parseInt(slideContainer.css('margin-left'))) });
      } else if ( $('#img-gallery .active').prevAll().length === 0 ) {
        $('#img-gallery li:first-child').addClass('active first-visible').siblings().removeClass('active first-visible');
        $('#img-gallery li:nth-child(10)').addClass('last-visible').siblings().removeClass('last-visible');
        slideContainer.animate({ marginLeft: 0 });
      }
      updateHolder();
    } else if (sliderControl === 'prev') {
      $('#img-gallery .active').removeClass('active').prev().addClass('active');
      if ( $('#img-gallery .active').next().hasClass('first-visible') ) {
        $('#img-gallery .first-visible').removeClass('first-visible').prev().addClass('first-visible');
        $('#img-gallery .last-visible').removeClass('last-visible').prev().addClass('last-visible');
        slideContainer.animate({ marginLeft: parseInt(slideContainer.css('margin-left'))+slide });
      } else if ( $('#img-gallery .active').nextAll().length === 0 ) {
        $('#img-gallery li:last-child').addClass('active last-visible').siblings().removeClass('active last-visible');
        $('#img-gallery li:eq('+($('#img-gallery li').prevAll().length-(10-1))+')').addClass('first-visible').siblings().removeClass('first-visible');
        slideContainer.animate({ marginLeft: -(slide*($('#img-gallery li:last-child').prevAll().length-(10-1))) });
      }
      updateHolder();
    }
  }
  
});