// Gallery JavaScript

/**
 * get previous/next image information
 * the image itself is loaded one step later
 */
function getGalleryImage(direction) {
	galleryID = $("#gallerySelect option:selected").val();
	imageID = $("#galleryImageId").val();
	
	$.ajax({
	   type: "POST",
	   url: "ajaxServer.php",
	   dataType: "json",
	   data: "action=getGalleryImage&imageID="+imageID+"&direction="+direction,
	   success: getGalleryImageServer
	 });
	
	return false;
}

function getGalleryImageServer(response) {
	if (response.fehler.length == 0) {
		// update texts
		$("#galleryImageId").val(response.imageID);
		$("#galleryAnzahl").html(response.anzahlText);
		
		// switch to loading animation
		$("#galleryImage img").hide();
		$("#galleryLoadingImg").show();
		
		// update download links
		$("div.gallery.download a.single").attr("href", "/galleryDownload.php?action=getImg&imgID=" + response.imageID +  "&galleryID=" + response.galleryID);
		$("div.gallery.download a.gallery").attr("href", "/galleryDownload.php?action=getGallery&galleryID=" + response.galleryID);
		
		// when image is ready in temporary image holder -> replace real image
		imgPreloader = new Image();
		imgPreloader.onload = function() {
			imgPreloader.onerror = null;
			imgPreloader.onload = null;
			$("#galleryImage").html('<img src="' + response.imageSrc + '" />');
			var w = $("#galleryImage img").width();
			var h = $("#galleryImage img").height();
			if((w/h) > (780/460)) {
				$("#galleryImage img").width("780");
			} else {
				$("#galleryImage img").height("460");
			}
			$("#galleryLoadingImg").hide();
			$("#galleryImage img").show();
		};		
		
		// start loading image in a temporary image holder
		imgPreloader.src = response.imageSrc;
	}					
}

function getGalleryImageDiashow() {
	getGalleryImage('next');	
}

function galleryStartStopDiashow() {
	if (galleryDiashowActive == false) {
		galleryDiashowInterval = window.setInterval("getGalleryImageDiashow()", 3000);
		galleryDiashowActive = true;		
	}
	else {
		window.clearInterval(galleryDiashowInterval);	
		galleryDiashowActive = false;
	}	
}
