/*
enhance.js
Generic enhancement functions (DOM)
*/

var W3CDOM = (document.createElement && document.getElementsByTagName);

// Dependant on elements in HTML
//addLoadEvent(replaceTextLoader);

// Moved to end of unit, no longer called onload
//addLoadEvent(replaceEmailAddr);
//addLoadEvent(showDecor);

// Attach functions to window.onload event
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

// Searches doc for anchors with class="email", replaces content and sets href attribute
// To prevent SPAM
function replaceEmailAddr() {
	if (!W3CDOM) return;
	var email, enode, n;
	var links = document.getElementsByTagName('a');
	for (var i=0; i<links.length; i++) {
		// Using getAttribute('class') does not work on IE! 
		if (links[i].className == 'email') {
			// Reconstruct email address
			email = links[i].firstChild.nodeValue;
			email = email.replace(/ -at- /g,'@');
			email = email.replace(/ -dot- /g,'.');			
			//alert(email);
			enode = document.createTextNode(email);
			//links[i].setAttribute('href','mailto:'+email);
			links[i].href = 'mailto:' + email;
			links[i].replaceChild(enode,links[i].firstChild);
		}
	}
}

// Initiates function to replace text headings with images
function replaceTextLoader() {
	if (!W3CDOM) return;
	var test = new Image();
	var tmp = new Date();
	var suffix = tmp.getTime();
	test.src = '/img/test.gif?'+suffix;
	test.onload = replaceTextInit;
}

// Which groups of elements to replace...
function replaceTextInit() {
	replaceText(document.getElementsByTagName('h1'));
	replaceText(document.getElementsByTagName('h2'));
}

// Look for elements with id attribute within group
function replaceText(x) {
  var txtnode;
	var replace = document.createElement('img');
	for (var i=0; i<x.length; i++) {
		if (x[i].id) {
			// Quicker than creating element from scratch each time
			var y = replace.cloneNode(true);
			y.src = '/img/' + x[i].id + '.png';
			y.setAttribute('border','0');
			// Find first textNode, as we might have an anchor within this one!
			txtnode = findTextNode(x[i]);
		  y.alt = txtnode.nodeValue;
			txtnode.parentNode.replaceChild(y,txtnode);
		}
	}
}

// Finds and returns first TEXT_NODE, even if within another ELEMENT_NODE (recursive)
function findTextNode(elem) {
	//alert(elem.nodeType);
	// Works, but is it quite correct in [ALL] situations?
	if (elem.nodeType != 3) {
	  if (elem.hasChildNodes) { 
			return findTextNode(elem.firstChild);
		}
	} else {
		// TEXT_NODE
		return elem;
	}
}


/************************************************************************************
Make decor image fade/move onto screen
NB: hide.css will have been included (with JS) prior to this function being called
************************************************************************************/

// Pass.. Op (opacity) 0..10
// NB: opacity:0..1, alpha(opacity=0..100)
function setDecor(yPos,Op) {
	if (!W3CDOM) {return;}
	obj = document.getElementById('decor');
	if (obj) {
		obj.style.top = Math.floor(yPos) + 'px';
		obj.style.opacity = Op/10;
		obj.style.filter = 'alpha(opacity=' + Op*10 + ')';
		obj.style.visibility = 'visible';
	}	
}

// Start timed anim
function showDecor() {
	if (!W3CDOM) {return;}
	
	var y_pos = -120;	// Needs to match value in stylesheet
	var op = 0;
	var max_steps = 80;	// Whatever steps necessary
	var interval = 10;	// msec between *visual* frames
	
	// Decelerate to 0 (in whatever time)
	// Calc a (px/msec/msec)
	// v^2 = u^2 + 2as;
	//var t = interval * max_steps;	// msec
	var u = 4;	// Initial speed px/msec
	var a = -(u * u) / (2 * Math.abs(y_pos));
	//var a = (-u / t);
	//a *= interval;		
	
	// Linear speed
	//var y_inc = Math.abs(y_pos) / max_steps;
	var op_inc = 10 / max_steps;
	
	obj = document.getElementById('decor');
	if (obj) {
		/*
		for (var i=1; i <= max_steps; i++) {
			//y_pos += y_inc;
			y_pos += u;
			u += a;
			op += op_inc;
			setTimeout('setDecor('+y_pos+','+op+')',i*interval)
		}
		*/
		var i = 1;
		while ((u > 0) || (op < 10)) {
			i++;
			setTimeout('setDecor('+y_pos+','+op+')',i*interval);
			y_pos += u;
			if (y_pos > 0) {y_pos = 0; a =0; u = 0;}
			u += a;
			op += op_inc;
			if (op > 10) {op = 10;}
		}
	}
}

/**
 * Popup window
 * Should be called from a.onclick
 * eg. onclick="return popupWin(this.href);"
 * @see popupWinInit()
 */
function popupWin(url) {
	popupHandle = window.open(url,'popup','height=400,width=700,scrollbars=yes');
	if (window.focus) {popupHandle.focus()}
	return false;
}

/**
 * Assign onlick event to all anchors that have the 'popup' class
 */
function popupWinInit() {
	var anchors = document.getElementsByTagName('a');
	if (anchors) {
		for (var i=0; i<anchors.length; i++) {
			if (anchors[i].className != 'popup') {
				continue;
			}
			// Append comment to TITLE attrib
			var title = anchors[i].getAttribute('title');
			if (title) {
				title += ' (Popup)';
			} else {
				title = '(Opens in a popup window)';
			}
			anchors[i].setAttribute('title',title);
			anchors[i].onclick = function() {return popupWin(this.getAttribute('href'));}
		}
	}
}

/**
 * External links - just adds an additional comment to the TITLE attribute
 */
function extLinksInit() {
	var anchors = document.getElementsByTagName('a');
	if (anchors) {
		for (var i=0; i<anchors.length; i++) {
			if (anchors[i].className != 'external') {
				continue;
			}
			
			// Append comment to TITLE attrib
			var title = anchors[i].getAttribute('title');
			if (title) {
				title += ' (External Website)';
			} else {
				title = '(External Website)';
			}
			anchors[i].setAttribute('title',title);
			
			// Make all external links open in popup!? BAD
			//anchors[i].onclick = function() {return popupWin(this.getAttribute('href'));}
		}
	}
}


/**
 * Preload Images
 */
function preloadImages() {

}

/**
 * Units called immediately
 */
replaceEmailAddr();
showDecor();
popupWinInit();
extLinksInit();
//preloadImages();

