/*
	Multiple Counter 1.1
	Copyright 2006-2007 popKorn
	popkorn@post.cz
*/

var timerIDs = new Array(),
	timerRunnings = new Array(),
	episodeIDs = new Array(),
	bigDays = new Array(),
	foundCounters = new Array(),
	foundCountersTimerID = null,
	foundCountersRunning = false,
	pageLoaded = false;

window.onload = function() { pageLoaded = true; }

function runMC() {
	for(var i = 0; i < episodesDatesTimes.length; i++) {
		foundCounters[i] = false;
	}
	foundCounters[episodesDatesTimes.length] = 0;
		
	stopFindingCounters();
	findingCounters();

}

function stopFindingCounters() {
	if (foundCountersRunning) {
		clearTimeout(foundCountersTimerID);
	}
	foundCountersRunning = false;
}

function findingCounters() {

	checkAndRun();

	if ((pageLoaded)||(foundCounters[episodesDatesTimes.length] == episodesDatesTimes.length)) {
		stopFindingCounters();
		checkAndRun();
	} else {
		foundCountersTimerID = setTimeout("findingCounters()", 10);
	}
}

function checkAndRun() {
	for(var i = 0; i < episodesDatesTimes.length; i++) {
		if (foundCounters[i]) {
			continue;
		}
		
		var o1 = "countdownepisode" + (i + 1);
		var o2 = "countdown" + (i + 1);
		var obj1 = getRefTo(o1);
		var obj2 = getRefTo(o2);
		
		if ((obj1 != null)&&(obj2 != null)) {
			foundCounters[i] = true;
			foundCounters[episodesDatesTimes.length]++;
			startCountdown(i);
		}
	}
}

function getRefTo(objID) {
	var objRef = null;
	var bbdc = document;

	if ((bbdc.getElementById)&&(bbdc.getElementById(objID) != null)) {
		objRef = bbdc.getElementById(objID);
	} else {
		if ((bbdc.layers)&&(bbdc.layers[objID] != null)) {
			objRef	= bbdc.layers[objID];
		} else if (bbdc.all) {
			objRef = bbdc.all[objID];
		}
	}

	return objRef;
}

function prepareCountdown(id) {
	var thisIsTheEpisode = null;
	var tmpToday = new Date();
	var tmpbigDay = new Date();
	
	timerIDs[id] = null;
	timerRunnings[id] = false;
	bigDays[id] = null;
	episodeIDs[id] = "0x00";

	var tmpEpisodesDatesTimes = episodesDatesTimes[id];
	for(var i = 0; i < tmpEpisodesDatesTimes.length; i++) {
		var ep = tmpEpisodesDatesTimes[i];
		
		tmpbigDay = new Date(Date.UTC(ep[1], ep[2] - 1, ep[3], ep[4], ep[5], ep[6]));
				
		if (tmpToday.getTime() < tmpbigDay.getTime()) {
			thisIsTheEpisode = tmpEpisodesDatesTimes[i];
			break;
		}
	}
	if (thisIsTheEpisode != null) {
		bigDays[id] = tmpbigDay;
		episodeIDs[id] = thisIsTheEpisode[0];
		var o = "countdownepisode" + (id + 1);
		var obj = getRefTo(o);
		obj.innerHTML = episodeIDs[id];
	} else {
		if (tmpEpisodesDatesTimes.length != 0) {
			episodeIDs[id] = tmpEpisodesDatesTimes[tmpEpisodesDatesTimes.length - 1][0];
			var o = "countdownepisode" + (id + 1);
			var obj = getRefTo(o);
			obj.innerHTML = episodeIDs[id];
		}
		bigDays[id] = null;
	}
}

function startCountdown(id) {
	prepareCountdown(id);
	
	if (bigDays[id] != null) {
		startclock(id);
	} else {
		stopclock(id);
	}
}

function showtime(id) {
	var today = new Date();

	var msPerSec = 1000,
		secPerMin = 60,
		minPerHr = 60,
		hrsPerDay = 24;
	
	var tmpbigDay = bigDays[id];
	
	var timeLeft = (tmpbigDay.getTime() - today.getTime());
	timeLeft = (timeLeft < 0) ? 0 : timeLeft;
	timeLeft = Math.floor(timeLeft / msPerSec);

	var secLeft = timeLeft % secPerMin;
	timeLeft = Math.floor(timeLeft / secPerMin);

	var minsLeft = timeLeft % minPerHr;
	timeLeft = Math.floor(timeLeft / minPerHr);
	
	var hrsLeft = timeLeft % hrsPerDay;
	var daysLeft = Math.floor(timeLeft / hrsPerDay);
	
	var showText = daysLeft + "d " + checkZero(hrsLeft) + ":"
				+ checkZero(minsLeft) + ":" + checkZero(secLeft);
	
	var o = "countdown" + (id + 1);
	var obj = getRefTo(o);
	obj.innerHTML = showText;
	
	if ((daysLeft <= 0)&&(hrsLeft <= 0)&&(minsLeft <= 0)&&(secLeft <= 0)) {
		stopclock(id);
		timerIDs[id] = setTimeout("startCountdown(" + id + ")", secPerMin * msPerSec);
	} else {
		timerIDs[id] = setTimeout("showtime(" + id + ")", msPerSec);
	}
	timerRunnings[id] = true;
}

function stopclock(id) {
	if (timerRunnings[id]) {
		clearTimeout(timerIDs[id]);
	}
	timerRunnings[id] = false;
}

function startclock(id) {
	stopclock(id);
	showtime(id);
}

function checkZero(number) {
	return ((number < 10) ? ("0" + number) : number);
}

