var han = (function() {
	// Add-Event function -- the brute-force approach to unobtrusive JavaScript
	function add_event(elem, evt, func) {
		if (elem.addEventListener) {
			// If we can, use the W3C standard
			elem.addEventListener(evt, func, false);
		} else if (elem.attachEvent) {
			// If necessary, use the Microsoft JScript function
			elem.attachEvent('on' + evt, func);
		} else {// If all else fails, simply assign the function to the window.onload handler
			//-- based on the Simon Willison closure http://www.sitepoint.com/blogs/2004/05/26/closures-and-executing-javascript-on-page-load/
			var current_handler = elem['on' + evt];
			if (typeof(current_handler) === 'function') {
				elem['on' + evt] = function() {
					current_handler();
					func();
				};
			} else {
				elem['on' + evt] = func;
			}
		}
	}

	function ticking_clock() {
		setInterval(function() {

			// Set up the date parts
			var now = new Date(),
				secs = (now.getSeconds() <= 9) ? "0" + now.getSeconds() : now.getSeconds(),
				mins = (now.getMinutes() <= 9) ? "0" + now.getMinutes() : now.getMinutes(),
				hours = (now.getHours() <= 9) ? "0" + now.getHours() : now.getHours(),
				date = now.getDate(),
				month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'][now.getMonth()],
				year = now.getFullYear(),
				number = (date <= 9) ? date : date.toString().substring(1, 2),
				suffix = (date > 9 && date < 20) ? 'th' : ['st', 'nd', 'rd'][number - 1] || 'th';
			
			// If the full_date paragraph has been created, update specific parts of it
			if (document.getElementById('han_full_date')) {
				document.getElementById('han_time').innerHTML = hours + ":" + mins;
				document.getElementById('han_seconds').innerHTML = ":" + secs;
				document.getElementById('han_date').innerHTML = "&nbsp;" + month + " " + date + suffix + " " + year;
			
			// If the full_date paragraph has not been created, create it
			} else {
				
				// A span tag to hold the hours and minutes
				var time_span = document.createElement('span');
				time_span.setAttribute('id', 'han_time');
				time_span.appendChild(document.createTextNode(hours + ":" + mins)); //13:02
				
				// A span tag to hold the seconds
				var sec_span = document.createElement('span');
				sec_span.setAttribute('id', 'han_seconds');
				sec_span.appendChild(document.createTextNode(":" + secs)); //:23
				
				// A span tag to hold the date
				var date_span = document.createElement('span');
				date_span.setAttribute('id', 'han_date');
				date_span.appendChild(document.createTextNode(" " + month + " " + date + suffix + " " + year)); // Oct 17th 2006
				
				// A paragraph to hold all the spans
				var p = document.createElement('p');
				p.setAttribute('id', 'han_full_date');
				p.appendChild(time_span);
				p.appendChild(sec_span);
				p.appendChild(date_span);
				
				// Add the paragraph to the banner
				document.getElementById('han_banner').appendChild(p); 
				
				// Add a class to the heading to make space for the clock
				document.getElementById('han_banner').getElementsByTagName('h2')[0].className = "han_js_h1";
			}
		}, 1000);
	}

	// Add the clock as soon as possible
	add_event(window, 'load', function() {
		ticking_clock();
	});

	return {
		load: function(func) {
			return add_event(window, 'load', func);
		}
	}
}());
