$(document).ready(function() {
	$("#photo").parent().attr("title", descr['DSCF0019.JPG']);

	$(".placeholder > a > img").click(function(e) {
		e.preventDefault();
		var id = $($(this).parent().find(".img")[0]).attr("id").substring(5);
		selected = parseInt(id) + start;
		if (selected >= photos.length) {
			selected -= photos.length;
		}
		var img = $(this).parent().find(".img").attr("src").substring(19);
		clearInterval(inter);
		$("#photo").fadeOut(1000, function() {
			$("#photo").parent().attr("title", descr[photos[selected]]);
			$("#photo").attr("src", "images/home/middle/" + img);
			$("#photo").parent().attr("href", "images/home/middle/" + img);
			$("#photo").fadeIn();
			inter = setInterval("changePhoto()", 5000);
		});
	});
	
	$("#left").click(function(e) {
		e.preventDefault();
		
	});
	
	$("#btnRight").click(function(e) {
		e.preventDefault();
		if (start == photos.length - 1) {
			start = 0;
		} else {
			start++;	
		}
		showThumbs();
	});
	
	$("#btnLeft").click(function(e) {
		e.preventDefault();
		if (start == 0) {
			start = photos.length - 1;
		} else {
			start--;
		}
		showThumbs();
	});
	
	inter = setInterval("changePhoto()", 5000);
	
	for (var i = 1; i < photos.length; i++) {
		$("#photos").append("<a style=\"display: none;\" href=\"images/home/middle/"+photos[i]+"\" title=\""+descr[photos[i]]+"\"><img src=\"images/home/middle/"+photos[i]+"\" /></a>");
	}
	
	$("#photos > a").lightBox();
});

function changePhoto() {
	selected++;
	if (selected == photos.length) {
		selected -= photos.length;	
	}
	$("#photo").fadeOut(1000, function() {
		//alert(descr[photos[selected]]);
		$("#photo").parent().attr("title", descr[photos[selected]]);
		$("#photo").attr("src", "images/home/middle/" + photos[selected]);
		$("#photo").parent().attr("href", "images/home/middle/" + photos[selected]);
		$("#photo").fadeIn();
	});
	if (selected + 1 == photos.length) {
		//$("<img>").attr("src", "images/home/middle/" + photos[selected]);
	} else {
		//$("<img>").attr("src", "images/home/middle/" + photos[selected+1]);
	}
}

function showThumbs() {
	for (var i = 0; i < 5; i++) {
		var j = i+start;
		if (j >= photos.length) {
			j -= photos.length;	
		}
		
		var s = "images/home/thumbs/" + photos[j];
		$("#thumb" + i).attr("src", s);
	}
}
