﻿/*

Author: Kai Husen
http://plainjoestudios.com
http://kaihusen.com

This was created with MediaSocial in mind.

*/

var iInitialServerTime;
var iShowtime;

function clock(currenttime, showtime) {

	iInitialServerTime = currenttime;
	iShowtime = 1287331920;

	//This has been modified to display every Sunday
	//DOES NOT GRAB SHOWTIME FROM MEDIA SOCIAL
	window.setInterval(timechange, 1);

	window.setInterval(write, 999);

}
//This has been modified to display every Sunday
//DOES NOT GRAB SHOWTIME FROM MEDIA SOCIAL
function timechange() {
	if (iInitialServerTime > iShowtime) {
		iShowtime = iShowtime + 604800;
	}
}

function write(launch) {

	var d = new Date();
	var seconds = d.getTime() / 1000;

	var sec = iShowtime - seconds;
	var min = sec / 60;
	var hours = sec / 3600;
	var days = sec / 86400;

	var subdays = Math.floor(days);
	var subhours = hours - (Math.floor(days) * 24);
	var submin = min - (Math.floor(hours) * 60);
	var subsec = sec % 60;


	//Add Zeros in front of single digit numbers
	subhours = Math.floor(subhours);
	submin = Math.floor(submin);
	subsec = Math.floor(subsec);

	(String(subhours).length < 2) ? subhours = String("0" + subhours) : subhours = String(subhours);
	(String(submin).length < 2) ? submin = String("0" + submin) : submin = String(submin);
	(String(subsec).length < 2) ? subsec = String("0" + subsec) : subsec = String(subsec);

	//alert(iShowtime);

	if (iShowtime == -1) {

		DisplayStr = "NO TIMES"

		document.getElementById("countdownText").innerHTML = DisplayStr;

	}

	else if (iShowtime < seconds) {

		DisplayStr = "LIVE NOW!"

		document.getElementById("countdownText").innerHTML = DisplayStr;

	}

	else {

		DisplayFormat = "<span class='cntdwnthick'>%%D%%</span>d <span class='cntdwnthick'>%%H%%</span>h <span class='cntdwnthick'>%%M%%</span>m";

		DisplayStr = DisplayFormat.replace(/%%D%%/g, (subdays));
		DisplayStr = DisplayStr.replace(/%%H%%/g, (subhours));
		DisplayStr = DisplayStr.replace(/%%M%%/g, (submin));
		DisplayStr = DisplayStr.replace(/%%S%%/g, (subsec));

		document.getElementById("countdownText").innerHTML = DisplayStr;
	}

	return;

}
