// JavaScript Document

$(function(){
		   
	var currentIndex = 0;
	var screenshots = $('.screenshot');
	var fadeSpeed = 500;
	
	$.each(screenshots, function(i, screenshot) {
		$(screenshot).hide();
	});
	$(screenshots[0]).show();
	updateGalleryLocationText(1);

	function updateGalleryLocationText(position){
		$('#galleryLocationText').html('&nbsp;&nbsp;&nbsp;'+position+'&nbsp;/&nbsp;'+screenshots.length+'&nbsp;&nbsp;&nbsp;');
	}
	
	$('.screenshotClick').click(function(event){
		if(screenshots.length!=1) { goNext(event); }
	});
	$('#prev').click(function(event){ goPrev(event) });
	$('#next').click(function(event){ goNext(event) });
	
	function goPrev(event){
		event.preventDefault();
		var oldIndex = currentIndex;
		if(currentIndex==0){ 
			currentIndex=screenshots.length-1; 
		}else{
			currentIndex = currentIndex - 1;
		}
		$(screenshots[currentIndex]).fadeIn(fadeSpeed, function() { $(screenshots[oldIndex]).fadeOut(fadeSpeed) });
		updateGalleryLocationText(currentIndex+1);
	};

	function goNext(event){
		event.preventDefault();
		var oldIndex = currentIndex;
		if(screenshots.length==currentIndex+1){ 
			currentIndex=0; 
		}else{
			currentIndex = currentIndex + 1;
		}
		$(screenshots[currentIndex]).fadeIn(fadeSpeed, function() { $(screenshots[oldIndex]).fadeOut(fadeSpeed) });
		updateGalleryLocationText(currentIndex+1);
	}
	
});
