$(document).ready(function() {
  var slideshowindex = new Array();
  slideshowindex['raisetheodds_full'] = 0;
  slideshowindex['ikeahomepage_full'] = 0;
  slideshowindex['saf_full'] = 0;
  slideshowindex['parfois_full'] = 0;
  slideshowindex['cloudania_full'] = 0;
  slideshowindex['gcl_full'] = 0;
  slideshowindex['theideaswap_full'] = 0;
  slideshowindex['tallennus_full'] = 0;
  
  $('#menu a').click(function() {
     $('#menu a').removeClass('active');
    $(this).addClass('active');
		var clicked_id = $(this).attr('id');
    scrollTo("#" + clicked_id + "_full");
		return false;
	});
	
	$('#categories span').click(function(){
	  $('#categories span').removeClass('active');
	  $(this).addClass('active');
	  if($(this).attr('id') != 'all') {
	    var categorie = $(this).attr('id');
  	  $('.thumb').each(function(index) {
        if($(this).hasClass('faded') && $(this).hasClass(categorie + '_thumb')) {
          $(this).fadeTo(500, 1);
          $(this).removeClass('faded');
        }
        else if(!$(this).hasClass('faded') && !$(this).hasClass(categorie + '_thumb')) {
          $(this).fadeTo(500, 0.3);
          $(this).addClass('faded');
        }
      });
    }
	});
	
	$('.next').click(function(){
	  var parent_id = $(this).parent().parent().attr('id');
	  var number_slides = $('#' + parent_id + ' .slide').length;
	  var clicked_button = $(this);
    $('#' + parent_id + ' .previous').removeClass('disabled');
	  $('#' + parent_id + ' .slide').each(function(index) {
	    if(!$(this).hasClass('hidden') && slideshowindex[parent_id] < number_slides - 1) {
	      $(this).fadeOut(250, function(){
	        $(this).addClass('hidden');
	        $(this).next().fadeIn(250).removeClass('hidden');
          slideshowindex[parent_id] = slideshowindex[parent_id] + 1;
          if(slideshowindex[parent_id] >= number_slides - 1) {
            clicked_button.addClass('disabled');
          }
          else {
            clicked_button.removeClass('disabled');
          }
	      });
	    }
	  });
	});
	
	$('.previous').click(function(){
	  var parent_id = $(this).parent().parent().attr('id');
	  var number_slides = $('#' + parent_id + ' .slide').length;
	  var clicked_button = $(this);
    $('#' + parent_id + ' .next').removeClass('disabled');
	  $('#' + parent_id + ' .slide').each(function(index) {
	    if(!$(this).hasClass('hidden') && slideshowindex[parent_id] > 0) {
	      $(this).fadeOut(250, function(){
	        $(this).addClass('hidden');
	        $(this).prev().fadeIn(250).removeClass('hidden');
          slideshowindex[parent_id] = slideshowindex[parent_id] - 1;
          if(slideshowindex[parent_id] <= 0) {
            clicked_button.addClass('disabled');
          }
          else {
            clicked_button.removeClass('disabled');
          }
	      });
	    }
	  });
	});
	
	$('#all').click(function(){
	  $('.faded').fadeTo(500, 1);
    $('.faded').removeClass('faded');
	});
	$('.thumb').click(function(){
	  $('.work_single').hide();
	  var work_id = '#' + $(this).attr('id') + '_full';
	  $(work_id).show().addClass('visible');
  	scrollTo(work_id)
	})
	$('.thumb, .website_link').mouseenter(function(){
	  $(this).children('p').show();
	});
	$('.thumb, .website_link').mouseleave(function(){
    $(this).children('p').hide();
   });
   
   
   function scrollTo(id_to_scroll) {
     targetOffset = $(id_to_scroll).offset().top - 29;
     $("html,body").animate({scrollTop: targetOffset},500);
   }
});