// create div shells
var obamaDiv = document.createElement("div");
var headlinediv = document.createElement("div");
var subheadlinediv = document.createElement("div");
var daydiv = document.createElement("div")
var countdowndiv = document.createElement("div")
var linknode;

// place the divs
obamaDiv.appendChild(headlinediv);
obamaDiv.appendChild(subheadlinediv);
obamaDiv.appendChild(document.createElement("br"));
obamaDiv.appendChild(daydiv);
obamaDiv.appendChild(document.createElement("br"));
obamaDiv.appendChild(countdowndiv);
obamaDiv.appendChild(document.createElement("br"));

headlinediv.innerHTML = "Time Remaining";
subheadlinediv.innerHTML = "until Election Day";

// create links
if (omc_registerEventHandler(obamaDiv, "click", omc_linkToSite)) {
	obamaDiv.style.cursor = "hand";
	linknode = document.createElement("div");
	linknode.innerHTML = "http://www.anobamaminute.com/";
	obamaDiv.appendChild(linknode);
} else {
	// create links in text
	linknode = document.createElement("a");
	linknode.href="http://www.anobamaminute.com/";
	linknode.target="_blank";
	linknode.innerHTML="http://www.anobamaminute.com/";
	obamaDiv.appendChild(linknode);
}

// style the nested divs
headlinediv.style.color = "#626060";
headlinediv.style.fontSize = "20px";

subheadlinediv.style.color = "#626060";
subheadlinediv.style.fontSize = "12px";

daydiv.style.color = "#02a2f7";
daydiv.style.fontWeight = "bold";
daydiv.style.fontSize = "22px";

countdowndiv.style.color = "#626060";
countdowndiv.style.fontWeight = "bold";
countdowndiv.style.fontSize = "14px";

linknode.style.color = "#626060";
linknode.style.fontSize = "12px";

obamaDiv.style.textAlign = "center";
obamaDiv.style.fontFamily = "Arial";
obamaDiv.style.fontSize = "10px";
obamaDiv.style.backgroundColor = "#FFFFFF";
obamaDiv.style.backgroundImage = "";
obamaDiv.style.backgroundRepeat = "no-repeat";
obamaDiv.style.backgroundPosition = "center center";


// place the div in page
document.getElementById("div_obama_minute").appendChild(obamaDiv);

// start the timer
omc_countdown();
setInterval(omc_countdown,1000);

// utility functions: omc_linkToSite opens a window to 
// http://www.anobamaminute.com/
function omc_linkToSite() {
	window.open("http://www.anobamaminute.com/");
}

// utility functions: omc_registerEventHandler registers actionType as 
// an eventHandler to obj's eventType event
function omc_registerEventHandler(obj, eventType, actionType) {
	if (obj.addEventListener) {
		obj.addEventListener(eventType, actionType, false);
		return true;
	} else if (obj.attachEvent) {
		objReport = obj.attachEvent("on" + eventType, actionType);
		return objReport;
	} else {
		return false;
	}

}

// utility functions: omc_countdown runs the countdown timer
function omc_countdown() {

	var time = new Date();
	var gmtMS = time.getTime() + (time.getTimezoneOffset() * 60000);
	var now =  new Date(gmtMS);

	var event = new Date("Nov 4 2008 04:00:00");
	// set to 5pm EST (12pm GMT) because client clock is not 
	// reporting daylight savings time adjustment

	time = (event - now);

	days = (time - (time % 86400000)) / 86400000;
	time = time - (days * 86400000);
	hours = (time - (time % 3600000)) / 3600000;
	time = time - (hours * 3600000);
	minutes = (time - (time % 60000)) / 60000;
	time = time - (minutes * 60000);
	seconds = (time - (time % 1000)) / 1000;		

	if((seconds <= 0) && (minutes <= 0) && (hours <= 0) && (days <= 0))
	{
		daydiv.innerHTML = "Vote Today!";
		countdowndiv.innerHTML = "";
		return true;
	}


	if ((seconds >= 0) && (days >= 0)) {

		if((hours <= 9) && (minutes <= 9) && (seconds <= 9)) {
			daydiv.innerHTML = days + " days";
			countdowndiv.innerHTML = hours + " hours : " + minutes + " min : " + seconds + " sec";
		} else if((hours <= 9) && (minutes <= 9) && (seconds >= 9)) {
			daydiv.innerHTML = days + " days";
			countdowndiv.innerHTML = hours + " hours : " + minutes + " min : " + seconds + " sec";
		} else if((hours <= 9) && (minutes >= 9) && (seconds <= 9)) {
			daydiv.innerHTML = days + " days";
			countdowndiv.innerHTML = hours + " hours : " + minutes + " min : " + seconds + " sec";
		} else if((hours <= 9) && (minutes >= 9) && (seconds >= 9)) {
			daydiv.innerHTML = days + " days";
			countdowndiv.innerHTML = hours + " hours : " + minutes + " min : " + seconds + " sec";
		} else if((hours >= 9) && (minutes <= 9) && (seconds <= 9)) {
			daydiv.innerHTML = days + " days";
			countdowndiv.innerHTML = hours + " hours : " + minutes + " min : " + seconds + " sec";
		} else if((hours >= 9) && (minutes <= 9) && (seconds >= 9)) {
			daydiv.innerHTML = days + " days";
			countdowndiv.innerHTML = hours + " hours : " + minutes + " min : " + seconds + " sec";
		} else if((hours >= 9) && (minutes >= 9) && (seconds <= 9)) {
			daydiv.innerHTML = days + " days";
			countdowndiv.innerHTML = hours + " hours : " + minutes + " min : " + seconds + " sec";
		} else if((hours >= 9) && (minutes >= 9) && (seconds >= 9)) {
			daydiv.innerHTML = days + " days";
			countdowndiv.innerHTML = hours + " hours : " + minutes + " min : " + seconds + " sec";
		} else {
			daydiv.innerHTML = days + " days";
			countdowndiv.innerHTML = hours + " hours : " + minutes + " min : " + seconds + " sec";
		}
	}
}