$(document).ready(function(){
	$(".photosList img[class!='imageBackground']").each(imagesSizeSet);
	
	function imagesSizeSet() {
		var imageHeight = $(this).attr("height");
		var imageWidth = $(this).attr("width");
		var photosListHeight = $(".photosExplorer .photosList").height();
		var photosListWidth = $(".photosList").width();
		
		if (imageHeight>photosListHeight && imageWidth > photosListWidth) {
			$(this).attr("height",photosListHeight);
		} else if (imageHeight<=photosListHeight && imageWidth > photosListWidth) {
			$(this).attr("width",photosListWidth).attr("style","position: absolute; top: 50%; left: 0; margin: -"+imageHeight/2+"px 0 0");
		} else if (imageHeight > photosListHeight && imageWidth <= photosListWidth) {
			$(this).attr("height",photosListHeight);
		}
	}
	
	navigateThroughImages();
	
	function navigateThroughImages() {
		/* SOME ACTIONS IN THE BEGINNING */
		$(".photosList .imageFile:eq(0)").show();
		$(".photosList .imageDescription").fadeTo(0,0.8);
		$(".photosList .imageOverlay").fadeTo(0,0.7);
		$(".photosExplorer .navigateButton:eq(0)").addClass("currentImage");
		
		$(".photosExplorer .navigateButton").click(function(){
			$visible = $(".photosList .imageFile:visible");
			var previousPosition = $(".photosList .imageFile").index($visible);
			var currentPosition = $("a.navigateButton").index(this);
			if (currentPosition!=previousPosition) {
				showHideImages(previousPosition,currentPosition);
			}
		});
		
		$(".photosExplorer .prevButton").click(function(){
			$visible = $(".photosList .imageFile:visible");
			var previousPosition = $(".photosList .imageFile").index($visible);
			var currentPosition = previousPosition-1;
			if (currentPosition!=-1) {
				showHideImages(previousPosition,currentPosition);
			}
		});
		
		$(".photosExplorer .nextButton").click(function(){
			$visible = $(".photosList .imageFile:visible");
			var previousPosition = $(".photosList .imageFile").index($visible);
			var currentPosition = previousPosition+1;
			var maxPosition = $(".photosList .imageFile").length - 1;
			if (currentPosition<=maxPosition) {
				showHideImages(previousPosition,currentPosition);
			}
		});
		
		function showHideImages(previousPosition,currentPosition) {
			$(".photosList .imageFile:eq("+previousPosition+")").fadeTo(0, 0, function(){$(this).hide();});
			$(".photosList .imageFile:eq("+previousPosition+") .imageDescription").slideUp();
			
			$(".photosList .imageFile:eq("+currentPosition+")").fadeTo(0,0).show().fadeTo(0, 1, function(){
				$(".photosList .imageFile:eq("+currentPosition+") .imageDescription").slideDown();
			});
			
			$(".photosExplorer .navigate a").removeClass("currentImage");
			$(".photosExplorer .navigate .navigateButton:eq("+currentPosition+")").addClass("currentImage");
		}
	}

});
