t3a_addEvent(window, "load", exponentJSinitialize, false);
t3a_addEvent(window, "load", t3a_applyMicroformats, false);
t3a_addEvent(window, "load", t3a_overlabelInit, false);

//********************************************//
//** Cross-Browser function to add an event **//
//** handler to an event-generating element **//
//********************************************//
function t3a_addEvent(element, type, fn, useCapture) {
	if (element.addEventListener) {
		// DOM Level 2 Event Model implementation...
		element.addEventListener(type, fn, useCapture);
		return true;
	} else if (element.attachEvent) {
		// Non-standard IE Hack...
		return element.attachEvent('on' + type, fn);
	} else {
		// Catch-all last resort...
		var fnOnload = element['on' + type];
		if (typeof(fnOnload) == "function") {
			element['on' + type] = function() {
				fnOnload();
				fn();
			}
		} else element['on' + type] = fn;
	}
}

//********************************************//
//** Filter out specific elements and       **//
//** apply the MicroFormats where necessary **//
//********************************************//
function t3a_applyMicroformats() {
	t3a_mfImage();
}

function t3a_mfImage() {
	var colImages = document.getElementsByTagName("img");
	var regex = /^[Cc]aption:\s*([\w\W]*)$/;
	for (var i = 0; i < colImages.length; i++) {
		var img = colImages[i];
		// Reset the regular expression...
		regex.lastIndex = 0;
		var arrRes = regex.exec(img.getAttribute("alt"));
		if (arrRes && arrRes.length > 1) {
			img.alt = img.title = arrRes[1];
			var div = document.createElement("div");
			div.className = "t3a_mfImage";
			div.appendChild(img.parentNode.replaceChild(div, img));
			div.appendChild(document.createElement("h1")).appendChild(document.createTextNode(arrRes[1]));
		}
	}
}

//*************************************************//
//** Initialise the Overlabels on the Login form **//
//*************************************************//
function t3a_overlabelInit() {
	if (!document.getElementById) return;
	
	var div, labels, id, field;
	
	// Get the labels on the page...
	if (div = document.getElementById("t3a_header")) {
		labels = div.getElementsByTagName("label");
		for (var i = 0; i < labels.length; i++) {
			if (labels[i].className == "overlabel") {
				// Skip the labels that don't have a field association...
				id = labels[i].htmlFor || labels[i].getAttribute("for");
				if (!id || !(field = document.getElementById(id))) continue;
				
				// Update the label's class attribute...
				labels[i].className += " apply";
				
				// Hide the label for fields having an initial value...
				if (field.value !== "") t3a_overlabelHide(field.getAttribute("id"), true);
				
				// Set the field's event handlers to show/hide the label...
				field.onfocus = function() {
					t3a_overlabelHide(this.getAttribute("id"), true);
				};
				field.onblur = function() {
					if (this.value === "") t3a_overlabelHide(this.getAttribute("id"), false);
				};
				
				// Handle clicks to label elements (for Safari)
				labels[i].onlick = function() {
					var id, field;
					id = this.getAttribute("for");
					if (id && (field = document.getElementById(id))) field.focus();
				};
			}
		}
	}
}

//** Show\Hide the Overlabels as appropriate **//
function t3a_overlabelHide(field_id, hide) {
	var field_for;
	// Read through each of the labels, looking for the one associated with the passed field_id...
	var labels = document.getElementsByTagName("label");
	for (var i = 0; i < labels.length; i++) {
		field_for = labels[i].htmlFor || labels[i].getAttribute("for");
		if (field_for == field_id) {
			labels[i].style.textIndent = (hide) ? "-1000px" : "0px";
			return true;
		}
	}
}
