
function onPhotoPopupContentLoaded(parameters){
  var photoId = parameters.photoId;
  var photoSize = parameters.photoSize;
  var photoImageUrl = parameters.photoImageUrl;

  var photoImageObjId = "photoImage_" + photoId + "_" + photoSize;
	var photo = document.getElementById(photoImageObjId);
	setOpacity("popup", 0);
	showPopup(); // IE7 needs to have the im displayed to get its size...so showPopup() is called before
	adaptPopupSizeToPhotoSize(popup, photo);
	center(popup);
	setOpacity("popup", 1);
	setOpacity(photoImageObjId, 0);
	
	photo.onload = function() {
	    if(isPopupOpen()) { 
	    	hideLoadingProgress();
	        setOpacity(photoImageObjId, 0);
	        fadeIn(photoImageObjId);
	        this.onload = null;
	        photoImageObjId=null;
		}
		// if the popup is not open, the user probably closed it before the image was loaded : we discard the downloaded image.
	  };
	 showLoadingProgress();
	 photo.src = photoImageUrl;
}

function adaptPopupSizeToPhotoSize(popup, photo) {
	var imgWith = photo.width ? photo.width : photo.offsetWidth;
	var imgHeight = photo.height ? photo.height : photo.offsetHeight;
	var newPopupWidth = photo.width + 320;
	var newPopupHeight = photo.height + 150;
	var popup = getPopupObject();
	popup.style.width = newPopupWidth + "px";
	popup.style.height = newPopupHeight+ "px";
}

function onPhotoPopupContentBufferLoaded(parameters){
	var popup = getPopupObject();
	var popupContentBuffer = document.getElementById("popupContentBuffer");
	var photo;
	var childrenImg = popupContentBuffer.getElementsByTagName("IMG");
	for(var i=0; i<childrenImg.length; i++) {
		var img = childrenImg[i];
		if(img.id && img.id.indexOf("photoImage_") >= 0) {
			photo = img;
			break;
		}
	}
	var imageUrlSaved = photo.src;
	photo.onload = function() {
		this.onload = function() {
			setOpacity("popup", 0);
			var popup = getPopupObject();
			var popupContent= document.getElementById("popupContent");
			var popupContentBuffer = document.getElementById("popupContentBuffer");
			popupContent.innerHTML = popupContentBuffer.innerHTML;
			popupContentBuffer.innerHTML = "";

			adaptPopupSizeToPhotoSize(popup, photo);
			center(popup);
			setTimeout("fadeIn('popup')", 100); // without this Delay FF3 does not fadeIn properly... (why??)
			this.onload = null;
			
			var popupPagerForm = document.popupPagerForm;
			if("true" == popupPagerForm.slideshow.value) {
				var slideshowLink = document.getElementById("slideshowLink");
				if(slideshowLink) {
					var url = slideshowLink.href;
					var command = url.substring(11).replace("%20", " "); // url is something like that : "javascript: ...", javascript command starts at index 11
					if(isPopupOpen()) {
						eval(command);
					}
				}
			}
		}
		photo.src = imageUrlSaved;
	}
	emptyPhotoImage(photo.id);
}

function viewPhoto(photoId, staging, offset, photoSize, photoImageUrl, photoWidth, photoHeight) {
  var popupForm = document.popupForm;
  popupForm.photoid.value = photoId;
  popupForm.staging.value = staging;
  popupForm.offset.value = offset;
  popupForm.photosize.value = photoSize;
  
  var callback = onPhotoPopupContentLoaded;
  
  var callbackParameters = new Object();
  callbackParameters.photoId = photoId;
  callbackParameters.staging = staging;
  callbackParameters.photoSize = photoSize;
  callbackParameters.photoImageUrl = photoImageUrl;
  openPopup("gallery", popupForm, null, null, false, callback, callbackParameters);
 }

function gotoPage(page) {
	var form = document.pagerForm 
	form.pagenumber.value = page;
	form.submit();
}
 
function gotoPhoto(offset, photoSize) {
	if (isPopupOpen()) {
	  var popupPagerForm = document.popupPagerForm;
	  if(popupPagerForm) {
		  popupPagerForm.offset.value = offset;
		  popupPagerForm.photosize.value = photoSize;
		  
		  var callback = onPhotoPopupContentBufferLoaded;  
		  var callbackParameters = null;
		  
		  ajaxFormSubmit(popupPagerForm, "popupContentBuffer", callback, callbackParameters);
	  }
	}
 }

 function showLoadingProgress() {
//	document.getElementById("galleryPopupPhoto").style.display="none";
//	document.getElementById("galleryPopupPhotoLoading").style.display="block";
 }
 
 function hideLoadingProgress() {
//	document.getElementById("galleryPopupPhotoLoading").style.display="none";
//	document.getElementById("galleryPopupPhoto").style.display="block";
 }
 
 function showProcessingIcon(photoId) {
	 var obj = document.getElementById("photoProcessing_"+photoId); 
	 if(obj) {
		 obj.style.display = "block";
	 }
 }
 function hideProcessingIcon(photoId) {
	 var obj = document.getElementById("photoProcessing_"+photoId); 
	 if(obj) {
		 obj.style.display = "none";
	 }
 }
 
 function emptyPhotoImage(imageObjId) {
	 document.getElementById(imageObjId).src = "/images/empty.gif";
 }
 
 function onPhotoRotated(newOrientation) {
	closePopup()
	window.location.reload();
 }
 
 function rotatePhoto(photoId, size, staging, admin, clockwise, actualOrientation) {
	 document.getElementById("rotateButtons_" + photoId).style.display = "none";
	 var rotateForm = document.rotateForm;
	 rotateForm.photoid.value = photoId;
	 rotateForm.staging.value = staging;
	 rotateForm.admin.value = admin;
	 rotateForm.clockwise.value = clockwise;
	 
	 var imageObjId = "photoImage_" + photoId + "_" + size;
	 
	 emptyPhotoImage(imageObjId);
	 showProcessingIcon(photoId);
	 
	 var resultDivId;
	 var callback=null;
	 if(admin) {
		 resultDivId = "photoBuffer_" + photoId;
	 } else {
		 resultDivId = "popupContentBuffer";
		 callback = onPhotoRotated;
	 }
	 ajaxFormSubmit(rotateForm, resultDivId, callback, null);	 
 }
 
 function confirmDeletePhoto(photoId) {
	 document.getElementById("confirmDelete_" + photoId).style.display="block";
 }
 function cancelDeletePhoto(photoId) {
	 document.getElementById("confirmDelete_" + photoId).style.display="none";
 }
 function doDeletePhoto(photoId, staging) {
	 var deleteForm = document.deleteForm;
	 deleteForm.photoid.value = photoId;
	 deleteForm.staging.value = staging;
	 ajaxFormSubmit("deleteForm", null, onPhotoDeleted, null);	 
 }
 function onPhotoDeleted(){
	 window.location.reload();
 }
 
function acceptRefusePhoto(photoStageId){
    var field = document.getElementById("release_" + photoStageId);
	var refusedBanner = document.getElementById("refused_" + photoStageId)
	
	if(field.value == "Y") {
		field.value = "N";
		refusedBanner.style.display = "block"; 
	} else {
		field.value = "Y";
		refusedBanner.style.display = "none"; 
	}
}

function editDescription(photoId) {
	document.getElementById("descriptionLink_" + photoId).style.display = "none";
	document.getElementById("description_" + photoId).style.display = "block";
	document.getElementById("descriptionField_" + photoId).focus();
}
function saveDescription(photoId, isInGalleryAdminScreen) {
	if(isInGalleryAdminScreen) {
		// don't save immediately, it will be done when the admin Submits the whole page
		document.getElementById("descriptionText_" + photoId).innerHTML = "";
		var newDescription = document.getElementById("descriptionField_" + photoId).value;
		document.getElementById("originalDescriptionField_" + photoId).value = newDescription;
		var newDescriptionText = document.createTextNode(newDescription);
		document.getElementById("descriptionText_" + photoId).appendChild(newDescriptionText);
		
		document.getElementById("description_" + photoId).style.display = "none";
		document.getElementById("descriptionLink_" + photoId).style.display = "block";
	} else {
		// Save immediately the description using Ajax
		var descriptionForm = document.descriptionForm;
		var newDescription = document.getElementById("descriptionField_" + photoId).value;
		descriptionForm.description.value = newDescription;
		var callback = closePopup;
		
		ajaxFormSubmit(descriptionForm, "popupContentBuffer", callback, null);
	}
}
function cancelDescription(photoId) {
	document.getElementById("description_" + photoId).style.display = "none";
	document.getElementById("descriptionField_" + photoId).value = document.getElementById("originalDescriptionField_" + photoId).value;
	document.getElementById("descriptionLink_" + photoId).style.display = "block";
}

function startSlideshow(offset, size) {
	document.getElementById("slideshowLink").style.display = "none"; // to avoid double clicks : 2 slidesows in the same time !
	setOpacity("popup", 0);
	var popupPagerForm = document.popupPagerForm;
	popupPagerForm.slideshow.value = "true";
	gotoPhoto(offset, size);
}
function continueSlideshow(offset, size) {
	setTimeout("if(isPopupOpen() && document.popupPagerForm && document.popupPagerForm.slideshow && document.popupPagerForm.slideshow.value=='true') gotoPhoto('" + offset + "', '" + size + "')", 3000);
}

