$(document).ready(function() {

	$("#portfolio_left_photos_navigate_prev").addClass("hidden");	
	var items = $(".thumbnail_link");
	var aantal = items.length;
	if(aantal < thumbs){
		$("#portfolio_left_photos_navigate_next").addClass("hidden");	
	}
	
	

	$("#portfolio_category_id").change(function(){
		var id = $(this).val();
		$.get("/ajax/getThumbs.php?id=" + id, "", function(data) {
			$("#portfolio_left_photos_inner").html(data);
			var items = $(".thumbnail_link");
			var aantal = items.length;
			
			items.eq(0).trigger("click");
										
			$("#portfolio_left_photos_navigate").attr('class', '0');
			$("#portfolio_left_photos_navigate_next a").attr('title', '16');
			$("#portfolio_left_photos_navigate_prev a").attr('title', '0');
			
								
			if($("#portfolio_left_photos_navigate_next").hasClass("hidden") && aantal == thumbs){				
				$("#portfolio_left_photos_navigate_next").removeClass("hidden");				
			}
			else if(!$("#portfolio_left_photos_navigate_next").hasClass("hidden") && aantal < thumbs){
				$("#portfolio_left_photos_navigate_next").addClass("hidden");				
			}			
			if(!$("#portfolio_left_photos_navigate_prev").hasClass("hidden")){
				$("#portfolio_left_photos_navigate_prev").addClass("hidden");		
			}	
		}, "html" );	
		
		current = 0;
		intervalId = setInterval( "slideSwitch()", 200 );
	
	});
	
	$("#portfolio_left_photos_navigate a").live("click", function(){
		var id = $("#portfolio_category_id").val();
		var offset = $("#portfolio_left_photos_navigate").attr('class');	
		var next = ($("#portfolio_left_photos_navigate_next a").html() == $(this).html()) ? thumbs : -thumbs;		
		offset = parseInt(offset);
		var offset_new  = offset + next;
		$.get("/ajax/getThumbs.php?id=" + id + "&offset=" + offset_new, "", function(data) {
		
			$("#portfolio_left_photos_inner").html(data);
			
			var items = $(".thumbnail_link");
			var aantal = items.length;			
			
			offset = parseInt(offset);
			
			var offset_next = offset_new + thumbs;
			var offset_prev = (offset_new - thumbs) > 0 ? (offset_new - thumbs) : 0;			
			
			//alert("Nieuwe offset:" + offset_new + " Offset next: " + offset_next + " Offset prev " + offset_prev);
			
			$("#portfolio_left_photos_navigate").attr('class', offset_new);
			
			$("#portfolio_left_photos_navigate_next a").attr('title', offset_next);			
			$("#portfolio_left_photos_navigate_prev a").attr('title', offset_prev);
			
			
					
			
			if($("#portfolio_left_photos_navigate_next").hasClass("hidden") && aantal == thumbs){				
				$("#portfolio_left_photos_navigate_next").removeClass("hidden");				
			}
			else if(!$("#portfolio_left_photos_navigate_next").hasClass("hidden") && aantal < thumbs){
				$("#portfolio_left_photos_navigate_next").addClass("hidden");				
			}
			
			if($("#portfolio_left_photos_navigate_prev").hasClass("hidden") && offset_prev >= 0){
				$("#portfolio_left_photos_navigate_prev").removeClass("hidden");		
			}
			else if(!$("#portfolio_left_photos_navigate_prev").hasClass("hidden") && (offset_prev < 0 || offset_next == 16)){
				$("#portfolio_left_photos_navigate_prev").addClass("hidden");	
			}		
			
		}, "html" );	
	});
	
	  intervalId = setInterval( "slideSwitch()", 200 );
	  $('#portfolio_right_image_large').fadeIn(800);
	  $("a.fancybox").fancybox({
		'overlayOpacity':	0.7
	 });

	$(".thumbnail_link").live("click", function(){
		var id = $(this).attr('title');		
		 $.getJSON("/ajax/getGalleryItem.php?id=" + id,
		function(data){		  
		  $('#portfolio_right_image_large').attr('src', data['location']);
		  $('#portfolio_right_image_large').parent().attr('href', data['location_link']);
		  $('#portfolio_right_image_large').css("display", "none");
		  $('#portfolio_right_image_large').fadeIn(800);
		  $("a.fancybox").fancybox({
		'overlayOpacity':	0.7
		 });
		  $('#portfolio_right_description').html(data['description']);
		});
	});
});


var intervalId = 0;
var thumbs = 16;
var current = 0;

function slideSwitch() {    
    $('.portfolio_inner:hidden:first').fadeIn(800).css("display", "table-cell");
    current++;
    if(current >= thumbs){
	//clearInterval ( intervalId );
    }
}


