function showAllEmbassy() {
	$("#newEmbassyList").remove();
	$("#embassy").show();
}

$(document).ready(function(){
	
	// ambasady
	
		$("#embassyForm").submit(function() {
			$("#newEmbassyList").remove();
			
			var toshowId = $("#embassySelect").val();
			
			var newListDiv = $("<div>");
			newListDiv.attr("id", "newEmbassyList");
			var newList = $("<ul>");
			newList.addClass("embassyList");
			newListDiv.append(newList);
			
			var showAllLink = $('<a href="#">pokaż pełną listę</a>');
			showAllLink.click(function () { 
				showAllEmbassy();
				return false;
			});
			
			newListDiv.append(showAllLink);
			
			if (toshowId == 'inne') {
				var toshow = $("#embassy li:not([id])").clone(true);
				newList.append(toshow);
			}
			else {
				var toshow = $("#" + toshowId + "").clone(true).removeAttr("id");
				newList.append(toshow);
			}
			
			$("#embassy").before(newListDiv);
			$("#embassy").hide();
			
			return false;
		});
		
		$("#embassyChars a").click(function() {
			showAllEmbassy();
		});
	
	
	// galeria
	
		var gallLen = $("#gallList li").length;
		var imLen = $('#gallList div').length;
		var pgLen = $('.galleryList div').length;
		
		function loadPhoto() {
			var gallIndex = $("#gallList li").index($("#gallList .active")[0]) + 1;
			$("#gallCount").text(gallIndex + " / " + gallLen);
			$("#gallDesc").text($("#gallList .active img").eq(0).attr("title"));
			$("#gallPhoto").empty();
			$("#gallPhoto").append('<img src="' + $("#gallList .active a").eq(0).attr("href") + '" />');
				if ($("#gallList .active").length > 0) {
					var photoHeight = /^height:(\d+)/i.exec($("#gallList .active a").eq(0).attr("title"))[1];
					$("#gallPhoto").height(photoHeight+"px");
				}

			// przesun strone ze zdjeciami gdy potrzeba
			var id = $("#gallList .active").parent().attr("id");
			$('#gallList div').hide();
			$('div#'+id).show();

			moveImage();
			}

		function moveImage() {
			var pageIndex = $('#gallList div').index($('#gallList div:visible')) + 1;
			$("#imCount").text(pageIndex + " / " + imLen);
			}

		loadPhoto();
		
		$("#gallList a").click(function() {
			$("#gallList li").removeClass();
			$(this).parent().addClass("active");
			loadPhoto();
			return false;
		});
		
		// nastepny/poprzedni obrazek
		$("#gallNav .prev").click(function() {
			var prevIndex = $("#gallList li").index($("#gallList .active")[0]) - 1;
			if (prevIndex >= 0) {
				$("#gallList li").removeClass();
				$("#gallList li").eq(prevIndex).addClass("active");
				loadPhoto();
			}
			return false;
		});
		
		$("#gallNav .next").click(function() {
			var nextIndex = $("#gallList li").index($("#gallList .active")[0]) + 1;
			if (nextIndex < gallLen) {
				$("#gallList li").removeClass();
				$("#gallList li").eq(nextIndex).addClass("active");
				loadPhoto();
			}
			return false;
		});

		// nastepna/poprzednia strona
		$("#imNav .im_prev").click(function() {
			var prevImage = $('#gallList div').index($('#gallList div:visible')) - 1;
			if (prevImage >= 0) {
				$('#gallList div:visible').hide();
				$('#gallList div').eq(prevImage).show();
				moveImage();
			}
			return false;
		});
		
		$("#imNav .im_next").click(function() {
			var nextImage = $('#gallList div').index($('#gallList div:visible')) + 1;
			if (nextImage < imLen) {
				$('#gallList div:visible').hide();
				$('#gallList div').eq(nextImage).show();
				moveImage();
			}
			return false;
		});

		// nastepna/poprzednia galeria
		$("#pgNav .pg_prev").click(function() {
			var prevPage = $('.galleryList div').index($('.galleryList div:visible')) - 1;
			if (prevPage >= 0) {
				$('.galleryList div:visible').hide();
				$('.galleryList div').eq(prevPage).show();
			}
			return false;
		});
		
		$("#pgNav .pg_next").click(function() {
			var nextPage = $('.galleryList div').index($('.galleryList div:visible')) + 1;
			if (nextPage < pgLen) {
				$('.galleryList div:visible').hide();
				$('.galleryList div').eq(nextPage).show();
			}
			return false;
		});
});
