	function updatePhotoName(photo) {
		var div = $(photo);
		var photocounter = div.getElementsByTagName("input")[0].value;
		var photoId = div.getElementsByTagName("input")[1].value;
		
		var photoNameBox = $("photo" + photocounter + "_name");
		var photoName = photoNameBox.value;
		
		if (photoName != "") {
			photoNameBox.className = photoNameBox.className.replace(/empty/, "");
			photoNameBox.className += ' loading';
						
			var ajax = new Request({url: "galleries/handlers/multimedia_edit.php?id=" + photoId + "&action=edit&type=name&value=" + photoName, method: "get",
				onSuccess: function() {
					photoNameBox.className = photoNameBox.className.replace(/loading/, "");
				},
				onFailure: function() {
					photoNameBox.className = photoNameBox.className.replace(/loading/, "");				
				}
			});
			
			ajax.send();
		} else {
			alert(photoNameEmpty);
			photoNameBox.className += ' empty';
		}
	}
	
	function updatePhotoCollection(photo) {
		var div = $(photo);
		var photocounter = div.getElementsByTagName("input")[0].value;
		var photoId = div.getElementsByTagName("input")[1].value;
		
		var collectionList = $("photo" + photocounter + "_collection");
		var newCollection = collectionList.options[collectionList.selectedIndex].value;
		if (newCollection != currentCollection) {
			var ajax = new Request({url: "galleries/handlers/multimedia_edit.php?id=" + photoId + "&action=edit&type=collection&value=" + newCollection, method: "get"});
			ajax.send();
			removePhotoFromList(photo);
		}
	}
	
	function updatePhotoOrder(photo, way) {
		var div = $(photo);
		var photocounter = div.getElementsByTagName("input")[0].value;
		var photoId = div.getElementsByTagName("input")[1].value;
		
		var allPhotos = $("collectioneditphotos").getElementsByTagName("div");
		var photoNumber = 0;
		var numberDifference = div.getElementsByTagName("div").length;
		
		for (var i = 0; i < allPhotos.length; i++) {
			if (allPhotos[i] == div) {
				photoNumber = i;
			}
		}
		
		if (((way == "up") && (photoNumber != 0)) || ((way == "down") && (photoNumber != allPhotos.length - 1 - numberDifference))) {	
			if (way == "up")
				var otherPhoto = allPhotos[photoNumber - 1 - numberDifference];
			else if (way == "down")
				var otherPhoto = allPhotos[photoNumber + 1 + numberDifference];
			var thisPhoto = div;
			
			var theurl = "galleries/handlers/multimedia_edit.php?id=" + photoId + "&action=edit&type=order&value=" + otherPhoto.getElementsByTagName("input")[1].value;
			changePhotoOrder(theurl);
			
			if ((otherPhoto.getSize) && (thisPhoto.getSize)) {
				var thisPhotoSize = thisPhoto.getSize();
				var thisPhotoHeight = parseInt(thisPhotoSize.y);
				var thisPhotoTop = parseInt(thisPhoto.style.top.replace("px", ""));
				var thisPhotoMargin = parseInt(thisPhoto.getStyle("marginTop").replace("px", "")) + parseInt(thisPhoto.getStyle("marginBottom").replace("px", ""));
				thisPhotoHeight = thisPhotoHeight + thisPhotoMargin;
			
				var otherPhotoSize = otherPhoto.getSize();
				var otherPhotoHeight = otherPhotoSize.y;
				var otherPhotoTop = parseInt(otherPhoto.style.top.replace("px", ""));
				var otherPhotoMargin = parseInt(otherPhoto.getStyle("marginTop").replace("px", "")) + parseInt(otherPhoto.getStyle("marginBottom").replace("px", ""));
				otherPhotoHeight = otherPhotoHeight + otherPhotoMargin;
			
				if (way == "up") {
					var thisPhotoNewTop = thisPhotoTop - otherPhotoHeight;
					var otherPhotoNewTop = otherPhotoTop + thisPhotoHeight;
				} else if (way == "down") {
					var thisPhotoNewTop = thisPhotoTop + otherPhotoHeight;
					var otherPhotoNewTop = otherPhotoTop - thisPhotoHeight; 
				}
			
				var thisFx = new Fx.Morph(thisPhoto, {duration: 1000});
				var otherFx = new Fx.Morph(otherPhoto, {duration: 1200});
			
				thisFx.start({
					'top': thisPhotoNewTop
				});
			
				otherFx.start({
					'top': otherPhotoNewTop
				}).chain(function() {
					switchPhotos(thisPhoto, otherPhoto);
				});
			} else {
				switchPhotos(thisPhoto, otherPhoto);
			}
		}
	}
	
	function switchPhotos(thisPhoto, otherPhoto) {
		var photocounter = thisPhoto.getElementsByTagName("input")[0].value;
		
		thisPhoto.style.top = "0px";
		otherPhoto.style.top = "0px";
				
		thisPhotoId = thisPhoto.getElementsByTagName("input")[1].value;
		otherPhotoId = otherPhoto.getElementsByTagName("input")[1].value;
				
		thisPhoto.appendChild(otherPhoto.getElementsByTagName("div")[0]);
		var thisPhotoDivs = thisPhoto.getElementsByTagName("div");
		otherPhoto.appendChild(thisPhotoDivs[0]);
				
		thisPhoto.setAttribute("id", "photo" + thisPhoto.getElementsByTagName("input")[0].value);
		otherPhoto.setAttribute("id", "photo" + photocounter);
				
		thisPhoto.getElementsByTagName("input")[1].value = thisPhotoId;
		otherPhoto.getElementsByTagName("input")[1].value = otherPhotoId;
				
		thisPhoto.style.top = "0px";
		otherPhoto.style.top = "0px";
				
		fixAllPhotos();
	}
	
	function fixAllPhotos() {
		var allPhotos = $("collectioneditphotos").getElementsByTagName("div");
		for (var i = 0; i < allPhotos.length; i++) {
			allPhotos[i].style.top = "0px";
		}
	}
	
	var photoOrderUrls = new Array();
	
	function changePhotoOrder(url) {
		photoOrderUrls[photoOrderUrls.length] = url;
		if (photoOrderUrls.length == 1)
			execPhotoOrderUrls();
	}
	
	function execPhotoOrderUrls() {
		if (photoOrderUrls.length > 0) {
			var ajax = new Request({url: photoOrderUrls[0], method: 'post',
				onSuccess: function() { updatePhotoOrderUrls(); },
				onFailure: function() { updatePhotoOrderUrls(); }
			});
			ajax.send();
		}
	}
	
	function updatePhotoOrderUrls() {
		var tempArray = photoOrderUrls;
		photoOrderUrls = new Array();
		for (var i = 0; i < tempArray.length; i++) {
			if (i != 0) {
				var photoUrl = tempArray[i];
				photoOrderUrls[i - 1] = photoUrl;
			}
		}
		execPhotoOrderUrls();
	}
	
	function deletePhoto(photo) {
		var div = $(photo);
		var photocounter = div.getElementsByTagName("input")[0].value;
		var photoId = div.getElementsByTagName("input")[1].value;
		
		if (confirm(confirmPhotoDelete)) {
			var ajax = new Request({url: "galleries/handlers/multimedia_edit.php?id=" + photoId + "&action=delete", method: "get"});
			ajax.send();
			removePhotoFromList(photo);
		}
	}
	
	function removePhotoFromList(photo) {
		var div = $(photo);
		var fx = new Fx.Morph(div, {duration: 1200});
		
		fx.start({
			'opacity': 0
		}).chain(function() {
			$("collectioneditphotos").removeChild(div);
			if ($("collectioneditphotos").getElementsByTagName("div").length == 0) {
				$("collectioneditphotos").innerHTML += '<p>' + collectionEmpty + '</p>';
			}
		});
	}
