/*

Common behaviour

*/

// detect IE5/Mac
var isMacIE = (navigator.userAgent.indexOf('MSIE 5') != -1
	&& navigator.userAgent.indexOf('Mac') != -1);

// checks support for W3C DOM
/* - Example -
// do not continue if not supported
if (!supportsW3CDOM()) return;
*/
function supportsW3CDOM() {
	return !isMacIE
		&& document.getElementById
		&& document.getElementsByTagName
		&& (document.createElement || document.createElementNS);
}

// useful load event function written by Simon Willison
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') window.onload = func;
	else {
		window.onload = function() {
			if (oldonload) oldonload();
			func();
		}
	}
}

// function to create elements circumventing XHTML/DOM compliance
function createElement(element) {
	element = element.toLowerCase();
	if (typeof document.createElementNS != 'undefined')
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	if (typeof document.createElement != 'undefined')
		return document.createElement(element);
	return false;
}

// function that handles opening external links in new windows
function doExternalLinks() {
	//if (!supportsW3CDOM()) return;
	var anchors = document.getElementsByTagName("a");
	var numAnchors = anchors.length;
	for (var i=0;i<numAnchors;i++) {
		var anchor = anchors[i];

		if (anchor.getAttribute("href")) {
			// external link?
			if (anchor.getAttribute("rel") && anchor.getAttribute("rel").match('external')) {
				// open in a new window?
				// would ideally use:
				// if (anchor.getAttribute("class") && anchor.getAttribute("class").match('popup')) {
				if (anchor.className.match('popup')) {
					anchor.target = "_blank";
					anchor.title = (anchor.title != "") ? anchor.title+" (opens in a new window)" : "External link opens in a new window";
					// don't add a class name, it's already there
				}
				else anchor.title = (anchor.title != "") ? anchor.title+" (external website)" : "External website";

				// add class name (IE6 on down lacking CSS selector support)
				// not required at the moment
				//anchor.className = (anchor.className != '') ? anchor.className+' external' : 'external';
			}

			// preview link?
			else if (anchor.getAttribute("rel") && anchor.getAttribute("rel").match("preview")) {
				anchor.target = "_blank";
				anchor.title = (anchor.title != "") ? anchor.title+" (opens in a new window)" : "Link opens in a new window";
				// add class name for IE6 on down
				anchor.className = (anchor.className != '') ? anchor.className+' preview' : 'preview';
			}
			// internal popup (e.g. for images on the site)
			// would ideally use:
			// if (anchor.getAttribute("class") && anchor.getAttribute("class").match('popup')) {
			if (anchor.className.match('popup')) {
				anchor.target = "_blank";
				anchor.title = (anchor.title != "") ? anchor.title+" (opens in a new window)" : "Link opens in a new window";
				// don't add a class name, it's already there
			}
		}
	}
}

function doFormPlaceholders() {

	if (!document.getElementsByTagName) return true;

	ourForms = document.getElementsByTagName('form');

	// go through each form
	var numForms = ourForms.length;
	for (var i=0;i<numForms;i++) {

		// go through each form element
		var numFormElements = ourForms[i].elements.length;
		for (var j=0;j<numFormElements;j++) {
		
			// if we got a text type input
			if (ourForms[i].elements[j].type == "text") {
				// only populate if we want it to
				// note: might want title attribute but no pre-population of inputs
				var ourClassName = ourForms[i].elements[j].className;
				if (ourClassName.match('auto-select')) {
					// only populate if empty
					if (ourForms[i].elements[j].value == '') ourForms[i].elements[j].value = ourForms[i].elements[j].title;
				}

				// add auto select if class contains auto-select
				// note: else if below so auto-select takes precedence (assuming select is better than clear)
				if (ourForms[i].elements[j].className.match('auto-select')) {
					ourForms[i].elements[j].onfocus = function () {
						if (this.value == this.title) this.select();
					}
					if (ourForms[i].elements[j].captureEvents) ourForms[i].elements[j].captureEvents(Event.FOCUS);
				}
			}

			// if we got a textarea
			if (ourForms[i].elements[j].type == "textarea") {
				// only populate if we want it to
				// note: might want title attribute but no pre-population of inputs
				var ourClassName = ourForms[i].elements[j].className;
				if (ourClassName.match('auto-select')) {
					// only populate if empty
					if (ourForms[i].elements[j].value == '') ourForms[i].elements[j].value = ourForms[i].elements[j].title;
				}

				// add auto select if class contains auto-select
				if (ourForms[i].elements[j].className.match('auto-select')) {
					ourForms[i].elements[j].onfocus = function () {
						if (this.value == this.title) this.select();
					}
					if (ourForms[i].elements[j].captureEvents) ourForms[i].elements[j].captureEvents(Event.FOCUS);
				}
			}

		}

	}

}

addLoadEvent(doExternalLinks);
addLoadEvent(doFormPlaceholders);
