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('h1')[0].className = "han_js_h1";
			}
		}, 1000);
	}

	// Add the clock as soon as possible
	add_event(window, 'load', function() {
		ticking_clock();
	});
	
	// Add the amount of times pushed (Action 1)
	add_event(window, 'load', function() {
		var p = document.createElement('p');
		p.setAttribute('id', 'han_pushed');
		p.appendChild(document.createTextNode(han.readActionXML('action1')));
		document.getElementById('han_popup_1').appendChild(p);
	});

	// Add the amount of money donated (Action 2)
	add_event(window, 'load', function() {
		var p = document.createElement('p');
		p.setAttribute('id', 'han_donated');
		p.appendChild(document.createTextNode(han.readActionXML('action2')));
		document.getElementById('han_popup_2').appendChild(p);
	});

	// Add the amount of money raised (Action 3)
	add_event(window, 'load', function() {
		var p = document.createElement('p');
		p.setAttribute('id', 'han_somethingmoney');
		p.appendChild(document.createTextNode(han.readActionXML('action3')));
		document.getElementById('han_popup_3').appendChild(p);
	});

	// Add the amount of Eco Warriors bought (Action 4)
	add_event(window, 'load', function() {
		var p = document.createElement('p');
		p.setAttribute('id', 'han_eco_bought');
		p.appendChild(document.createTextNode(han.readActionXML('action4')));
		document.getElementById('han_popup_4').appendChild(p);
	});

	// Add the amount of Wave participants  (Action 5)
	add_event(window, 'load', function() {
		var p = document.createElement('p');
		p.setAttribute('id', 'han_participants');
		p.appendChild(document.createTextNode(han.readActionXML('action5')));
		document.getElementById('han_popup_5').appendChild(p);
	});

	// Add the amount of people signed (Action 6)
	add_event(window, 'load', function() {
		var p = document.createElement('p');
		p.setAttribute('id', 'han_signed');
		p.appendChild(document.createTextNode(han.readActionXML('action6')));
		document.getElementById('han_popup_6').appendChild(p);
	});

	return {
		load: function(func) {
			return add_event(window, 'load', func);
		}
		,box: function(id) {
			if (typeof(id) === "undefined") {
				document.getElementById(han.box.modal).style.display = "none";
				//document.getElementById('han_popup_backdrop').style.display = "none";
			} else {
				han.box.modal = "han_popup_" + id.split('_')[2];
				document.getElementById(han.box.modal).style.display = "block";
				//document.getElementById('han_popup_backdrop').style.display = "block";
			}
		}
		,text: function() {
			var full_name = '';
			if (document.getElementById('firstname1').value !== document.getElementById('firstname1').defaultValue) {
				full_name += document.getElementById('firstname1').value;
			}
//			var full_name = (document.getElementById('firstname1').value === document.getElementById('firstname1').defaultValue) ? '' : document.getElementById('firstname1').value;
			if (document.getElementById('lastname1').value !== document.getElementById('lastname1').defaultValue) {
				full_name += ' ' + document.getElementById('lastname1').value;
			}
			full_name = full_name || '[Your name]';
			document.getElementById('han_action_name').innerHTML = full_name;
		}
		,readActionXML: function(action){
			var xmlDoc;
			// code for IE
			if (window.ActiveXObject)
			{
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			}
			// code for Mozilla, Firefox, Opera, etc.
			else if (document.implementation.createDocument)
			{
			xmlDoc=document.implementation.createDocument("","",null);
			}
			else
			{
			//alert('Your browser cannot handle this script');
			return "If you enable ActiveXObject, or upgrade your browser, you will see the latest figures here.";
			}
			xmlDoc.async=false;
			xmlDoc.load("images/han_actions.xml");
			
			var x=xmlDoc.documentElement.childNodes;
			
			for (var i=0;i<x.length;i++)
			{ 
			if (x[i].nodeType==1)
			  { 
				//Check to see that the action node exists
				if(x[i].nodeName == action){
					//Check to make sure that the action has a value
					if(x[i].childNodes[0].nodeValue != undefined){
						//Let's print out this action :-)
						//document.write(x[i].childNodes[0].nodeValue);	
						return x[i].childNodes[0].nodeValue;
					}
					
				}
			  } 
			}
			// If we've got this far, just leave it blank
			return null;
		}
	}
}());