// used to disable href and action
var jsVoidUri = "javascript:void(0)";

// returns an element or null matching the given id
function getElById (id) {
	var el;
	
	if (document.getElementById) {
		el = document.getElementById (id);
	} 
	else if (document.all) {
		el = eval ("document.all." + id);
	}
	 
	return el;
}


// returns an element or null matching the given element name
function getElsByName (name) {
	var el;
	
	if (document.getElementsByTagName) {
		el = document.getElementsByTagName (name);
	}

	return el;
}


// returns the text value of an element
function getTextOfEl (el) {
	if (! el || ! el.firstChild) {
		return null;
	}
	
	return el.firstChild.nodeValue;
}


// returns the value of a checked radio button matching the given name
function getRadioButtonValue (name) {
	var els = getElsByName (name);
	
	if (els) {
		for (el in els.length) {
			if (el.checked) {
				return el.value;
			}
		}
	}
	
	return null;
}


// returns an arrayof values for the checked checkbox button matching the given name
function getCheckButtonValues (name) {
	var els = getElsByName (name);
		
	if (els) {
		var vals = new Array ();

		for (el in els) {
			if (el.checked) {
				vals[vals.length] = el.value;
			}
		}
		
		return vals;
	}
	
	return null;
}


// attaches a function to a event handler to an element identified by an Id
function wireById (elId, onEvent, functionHandler) {
	var el = getElById (elId);
	
	return wireByEl (el, onEvent, functionHandler);
}


// attaches a function to a event handler to an element
function wireByEl (el, onEvent, functionHandler) {
	if (! el) {
		return;
	}
	
	if (document.addEventListener) {
		el.addEventListener(onEvent, functionHandler, true);
	}
	else if (document.attachEvent) {
		el.attachEvent("on" + onEvent, functionHandler);
	}
	else {
		var sigHandler = eval(elId + ".on" + onEvent);
		sigHandler = functionHandler;
	}
}


// returns a function that binds two elements that toggle between 
// two states (css classes) in unison
function createToggleClosure (togger, toggerInactClass, toggerActClass, toggee, toggeeInactClass, toggeeActClass) {
	return (
		function (e) {
			if (! e) {
				e = window.event;
			}
			
			var toggerEl = getElById (togger);
			var toggeeEl = getElById (toggee);
			
			if (! toggerEl || ! toggeeEl) {
				return;
			} 
			
			if (toggeeActClass == toggeeEl.className) {
				toggeeEl.className = toggeeInactClass;
				toggerEl.className = toggerInactClass;
			}
			else {
				toggeeEl.className = toggeeActClass;
				toggerEl.className = toggerActClass;
			}
		}
	);
}


// select and set the caption information from a list of images.
function editPictureCaptions (e) {
	if (typeof activePic != "undefined") {
		activePic.style.border = "3px solid #ffffff";
		activePic.style.opacity= ".66";
		activePic.style.filter = "alpha(opacity=50)";
	}
	
	activePic = this;
	this.style.border = "3px solid #003366";
	this.style.opacity= "1";
	this.style.filter = "alpha(opacity=100)";
	
	var capP = getElById ("caption")
	var capText = getElById ("captionText");
	var galleryLoc = getElById ("galleryLoc");
	var capPic = getElById ("captionPic");

	if (! capP || ! capText || ! galleryLoc || ! capPic) {
		return;
	}
	
	capP.innerHTML = this.alt;
	capText.value = this.alt;
	galleryLoc.value = this.title;
	capPic.innerHTML = this.title
}


// submits a new caption for an image and updates the caption in the page
function submitCaption (e) {
	if (! e) {
		e = window.event;
	}
  	
	var capForm = getElById ("captionForm");
	var capP = getElById ("caption")
	var galleryLoc = getElById ("galleryLoc")
	
	if (! capForm || ! capP || ! galleryLoc) {
		// let the form process via standard html+http
		return false;
	}
	
	var gallery = galleryLoc.value;
	var operation = capForm.operation.value.toLowerCase() + "_caption";
	var caption = encodeURIComponent (capForm.caption.value);
	
	if (! gallery || 0 == gallery.length) {
		alert ("Select a picture first, then edit it's caption.");
	}
	
	var params = galleryLoc.name + "=" + gallery + "&operation=" + operation + "&caption=" + caption;
	
	var uri = capForm.originalAction;
	
	if (! uri) {
		capForm.originalAction =  capForm.action;
		capForm.action = jsVoidUri;
		uri = capForm.originalAction
	}
	
	request = new sack (uri);
	
	if (request.failed) {
		alert ("Could not create the request.");
	}	
	
	var orginalCap = capP.innerHTML;
	
	request.method = 'POST';
	request.onLoading = function () { 
		capP.innerHTML = "Sending Data..."; 
	};
	request.onLoaded = function () { 
		capP.innerHTML = "Data Sent..."; 
	};
	request.onInteractive = function () { 
		capP.innerHTML = "Processing Data..."; 
	};
	request.onCompletion = function () { 
		if (! request.responseXML) {
			capP.innerHTML = orginalCap;
			alert ("server did not respond.")
			return;
		}
				
		var resultEl = request.responseXML.getElementsByTagName ("result")[0];
		var capEl = request.responseXML.getElementsByTagName ("caption")[0];
		
		if (! resultEl || ! capEl) {
			capP.innerHTML = orginalCap;
			alert ("Server response was not understood.\n" + request.response);
		}

		if ("fail" == getTextOfEl(resultEl)) {
			capP.innerHTML = orginalCap;
			alert ("Server cold not update the image caption.");
			return;
		}
		
		if (capEl) {
			capP.innerHTML = getTextOfEl (capEl);
		}
	};
	
	request.runAJAX (params);

	return false;
}


// initialize the page
function main () {
	var toggleImageData = createToggleClosure ("exifDisclosure", "action-pre", "action-post", "imageData", "display-none", "display-table")
	wireById ("exifDisclosure", "click", toggleImageData);

	var toggleCommentForm = createToggleClosure ("commentDisclosure", "action-pre", "action-post", "commentform", "display-none", "display-block")
	wireById ("commentDisclosure", "click", toggleCommentForm);	
	
	var toggleImageCaption = createToggleClosure ("captionDisclosure", "action-pre", "action-post", "captionForm", "display-none", "display-block")
	wireById ("captionDisclosure", "click", toggleImageCaption);
	
	wireById ("captionForm", "submit", submitCaption);
}


// start
main ();