var currentProject = null;
$(document).ready(function(){
	$('div.project h2 a').click(function(){
		var project = $(this).parent().parent().find('div.pictures');
		if ( currentProject == $('a').index(this) ) 
		{
			project.slideUp('slow');
			currentProject = null;
		}
		else 
		{
			//$('div.project div.pictures').slideUp();
			project.slideDown('slow');
			currentProject = $('a').index(this);
		}
	});
	$('.imageLinkCloseProject').click(function(){
		var project = $(this).parent().parent().parent().find('div.pictures');
		project.slideUp('slow');
		currentProject = null;
	});
	$('.contentCloseProject').click(function(){
		var project = $(this).parent().parent().find('div.pictures');
		project.slideUp('slow');
		currentProject = null;
	});
	$('.photoViewerPrev').click(function(){
		var photoViewerActive = $(this).parent().parent().find('.photoViewerActive');
		var prev = photoViewerActive.index() != 0 ? photoViewerActive.index() - 1 : ( $(this).parent().parent().find('.photoViewer a').length - 1 );
		var link = $(this);
		photoViewerActive.removeClass('photoViewerActive').fadeOut(400, function() {
			link.parent().parent().find('.photoViewer a:eq(' + prev + ')').fadeIn().addClass('photoViewerActive');
			var pictureTitle = link.parent().parent().find('.photoViewer a.photoViewerActive img').attr('alt');
			link.parent().parent().find('.photoTitle').text(pictureTitle);
			link.parent().find('.photoViewerActivePhotoNumber').text( (prev + 1) );
		});
	});
	$('.photoViewerNext').click(function(){
		var photoViewerActive = $(this).parent().parent().find('.photoViewerActive');
		var next = photoViewerActive.index() != ( $(this).parent().parent().find('.photoViewer a').length - 1 ) ? photoViewerActive.index() + 1 : 0;
		var link = $(this);
		photoViewerActive.removeClass('photoViewerActive').fadeOut(400, function() {
			link.parent().parent().find('.photoViewer a:eq(' + next + ')').fadeIn().addClass('photoViewerActive');
			var pictureTitle = link.parent().parent().find('.photoViewer a.photoViewerActive img').attr('alt');
			link.parent().parent().find('.photoTitle').text(pictureTitle);
			link.parent().find('.photoViewerActivePhotoNumber').text( (next + 1) );
		});
	});
	$('.photoNext').click(function() {
		var photoViewerActive = $(this).parent().find('.photoViewerActive');
		var next = photoViewerActive.index() != ( $(this).parent().find('img').length - 1 ) ? photoViewerActive.index() + 1 : 0;
		var link = $(this);
		photoViewerActive.removeClass('photoViewerActive').fadeOut(400, function() {
			link.parent().find('a:eq(' + next + ')').fadeIn().addClass('photoViewerActive');
			var pictureTitle = link.parent().find('a.photoViewerActive img').attr('alt');
			link.parent().parent().find('.photoTitle').text(pictureTitle);
			link.parent().parent().find('.photoViewerActivePhotoNumber').text( (next + 1) );
		});
	});
});
