function initOverLabels () {
	if (!document.getElementById) return false;      
	var labels, id, field, i;
	labels = document.getElementsByTagName('label');
	for (i=0; i < labels.length; i++) {
		if (labels[i].className == 'overlabel') {
			id = labels[i].htmlFor || labels[i].getAttribute('for');
			if (!id || !(field = document.getElementById(id))) { continue; } 
			labels[i].className = 'overlabel-apply';
			if (field.value !== '') { hide_label(field.getAttribute('id'), true); }
			field.onfocus = function () { hide_label(this.getAttribute('id'), true); };
			field.onblur = function () { if (this.value === '') { hide_label(this.getAttribute('id'), false); } };
			labels[i].onclick = function () {
				var id, field;
				id = this.getAttribute('for');
				if (id && (field = document.getElementById(id))) { field.focus(); }
			};
		}
	}
	field=labels=null;
}

function resetOverLabels (root) {
	if (!document.getElementById) return false;      
	var labels, id, field, i;
	root = root || document;
	labels = root.getElementsByTagName('label');
	for (i=0; i < labels.length; i++) {
		if (labels[i].className == 'overlabel-apply') {
			id = labels[i].htmlFor || labels[i].getAttribute('for');
			field = document.getElementById(id);
			if (field && field.value === '') {
				labels[i].style.textIndent = "0px";
			} else if (field && field.value !== '') {
				labels[i].style.textIndent = "-1000px";
			}
		}
	}
}

function hide_label (field_id, hide) {
	if (!document.getElementsByTagName) return false;
	var field_for, labels, i;
	labels = document.getElementsByTagName('label');
	for (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;
		}
	}
}


$(document).ready(function () {
	var search = $('#search-box');
	if (search) {
		search.addClass('pretty');
	}

	function externalizeLinks() {
		$('[rel=external]').each(function () {
			// do not apply the graphic to image links
			if (this.firstChild && (!this.firstChild.tagName || this.firstChild.tagName.toLowerCase() != 'img')) {
				$(this).addClass('external');
			}

			// open in new window, and kill default action
			$(this).click(function () {
				window.open(this.href);
				return false;
			});
		});
	}

	initOverLabels();
	externalizeLinks();
});
