$(document).ready(function(){

  var gallery = $('#project-gallery');
  var previous = $('.gallery-previous');
  var next = $('.gallery-next');
  var currentCount = $('#current-image-count');
  var allowClick = true;

  $('.link').css('opacity','1'); // changed to 1 from 0

//  previous.addClass('hide');

  var gallerySize = 0;
  $('.gallery-image').each(function(){
    gallerySize = gallerySize + 1;
  })
  if(gallerySize < 10){
    gallerySize = '0' + gallerySize;
  }

  $('#image-count').html(gallerySize);

  var maximumOffset = (gallerySize * 678) - 678;
  if(gallerySize < 2){
    next.addClass('hide');
  }

  $('#project-gallery-wrapper').hover(function(){
    $(this).find('.link').each(function(){
      var linkCls = $(this).attr('class');
      if (linkCls.toLowerCase().indexOf('hide') == -1){
        $(this).stop().animate({ opacity: 1 }, 300);
      }
    })
  },function(){
    $(this).find('.link').each(function(){
      $(this).stop().animate({ opacity: 1 }, 300); // changed to 1 from 0
    })
  })


  /*
  // sliding
  next.click(function(){
    if(allowClick == true){
      allowClick = false;
      var nextImagePosition = gallery.css('left').replace('px','') - 678;
      var newCount = parseInt(currentCount.html()) + 1;
      if(newCount < 10){
        newCount = '0' + newCount;
      }
      currentCount.html(newCount);
      gallery.stop().animate({ left: nextImagePosition }, 300, function(){
        gallery.css('left',nextImagePosition);
        previous.removeClass('hide').animate({ opacity: 1 }, 300);
        if(nextImagePosition + maximumOffset == 0){
          next.addClass('hide').css('opacity','0');
        }
        allowClick = true;
      })
    }
  })

  previous.click(function(){
    if(allowClick == true){
      allowClick = false;
      var previousImagePosition = parseInt(gallery.css('left').replace('px','')) + 678;
      if(previousImagePosition > 0) return false;
      var newCount = parseInt(currentCount.html()) - 1;
      if(newCount < 10){
        newCount = '0' + newCount;
      }
      currentCount.html(newCount);
      gallery.stop().animate({ left: previousImagePosition }, 300, function(){
        gallery.css('left',previousImagePosition);
        next.removeClass('hide').animate({ opacity: 1 }, 300);
        if(previousImagePosition == 0){
          previous.addClass('hide').css('opacity','0');
        }
        allowClick = true;
      })
    }
  })
  */

  //  fading
  $('.gallery-image').css('opacity','0');
  $('.gallery-image:first-child').css('opacity','1').addClass('first active');
  $('.gallery-image:last-child').addClass('last');

  next.click(function(){
    if(allowClick == true){
      if(!$('#project-gallery .active').hasClass('last')){
        allowClick = false;
        var newCount = parseInt(currentCount.html(),10) + 1;
        if(newCount < 10){
          newCount = '0' + newCount;
        }
        currentCount.html(newCount);
        $('#project-gallery .active').removeClass('active').animate({ opacity: 0 }, 400).next().animate({ opacity: 1}, 400, allowGallery).addClass('active');
//        previous.removeClass('hide').animate({ opacity: 1 }, 300);
//        if($('#project-gallery .active').hasClass('last')){
//          next.addClass('hide').css('opacity','0');
//        }
      }
    }
  })

  previous.click(function(){
    if(allowClick == true){
      if(!$('#project-gallery .active').hasClass('first')){
        allowClick = false;
        var newCount = parseInt(currentCount.html(),10) - 1;
        if(newCount < 10){
          newCount = '0' + newCount;
        }
        currentCount.html(newCount);
        $('#project-gallery .active').removeClass('active').animate({ opacity: 0 }, 400).prev().animate({ opacity: 1}, 400, allowGallery).addClass('active');
//        next.removeClass('hide').animate({ opacity: 1 }, 300);
//        if($('#project-gallery .active').hasClass('first')){
//          previous.addClass('hide').css('opacity','0');
//        }
      }
    }
  })

  var allowGallery = function(){
    allowClick = true;
  }

})
