$(function(){
	$("#photo_gallery_thumbnails li a").click(function(e){
		e.preventDefault();
		$.ajax({
			type:'GET',
			url: $(this).attr('href'),
			success:function(data) {
				$.openWindow(data);
				navigation();
			}
		});
		return false;
	});

});

function navigation(){
	$("#close").click(function(e) {
		e.preventDefault();
		closeWindowScreen();
	});
	$(".arrows").click(function(e) {
		e.preventDefault();
		var id = $(".big_img").find("img").attr("class").replace("detail_", "");
		var method = $(this).attr('href').replace("#", "");
		eval(method + "_photo("+id+")");
		return false;
	});
	
	document.onkeydown = function(e){
		keycode = (e==null) ? event.keyCode : e.which;
		var id = $(".big_img").find("img").attr("class").replace("detail_", "");
		if(keycode == 37){
			prev_photo(id);
		}else if(keycode == 39){
			next_photo(id);
		}else if(keycode == 27){
			closeWindowScreen();
		}
	};
}

function closeWindowScreen (){
	jQuery('#TB_window').remove();
	jQuery.screenClose();
}

function get_index(id){
	for (var i=0, obj; obj = photos[i]; i++) {
		if (obj.id == id) {
			return i
		}
	};
}

function parse_photos(){
	$("#photo_gallery_thumbnails li a").each(function(index) {
		var id = $(this).find("img").attr("class").replace("photo_", "");
		photos.push(id);
	});
}
function prev_photo(id){
	var index = get_index(id);
	index--;
	if (index < 0) {
		index = (photos.length-1);
	}
	show_photo(index);
}

function next_photo(id){
	var index = get_index(id);
	index++;
	if (index > (photos.length-1)) {
		index = 0;
	}
	show_photo(index);
}

function show_photo(i){
	var obj = photos[i];
	$(".big_img").find("img").removeClass().addClass("detail_"+obj.id).attr("src", obj.photo);
	$("#description h4").text(obj.name);
	$("#description #photo_body").html(obj.description);
}
