/*
	Multiple Counter 1.1
	Copyright 2006-2007 popKorn
	popkorn@post.cz
	----------------------------
	modified to Birthday Counter
	modified by dejv, david@czpilar.net
*/

var timerIDsBc = new Array(),
	timerRunningsBc = new Array(),
	episodeIDsBc = new Array(),
	bigDaysBc = new Array(),
	foundCountersBc = new Array(),
	foundCountersTimerIDBc = null,
	foundCountersRunningBc = false,
	pageLoadedBc = false;

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

function runMCBc() {
	for(var i = 0; i < episodesDatesTimesBc.length; i++) {
		foundCountersBc[i] = false;
	}
	foundCountersBc[episodesDatesTimesBc.length] = 0;
		
	stopFindingCountersBc();
	findingCountersBc();

}

function stopFindingCountersBc() {
	if (foundCountersRunningBc) {
		clearTimeout(foundCountersTimerIDBc);
	}
	foundCountersRunningBc = false;
}

function findingCountersBc() {

	checkAndRunBc();

	if ((pageLoadedBc)||(foundCountersBc[episodesDatesTimesBc.length] == episodesDatesTimesBc.length)) {
		stopFindingCountersBc();
		checkAndRunBc();
	} else {
		foundCountersTimerIDBc = setTimeout("findingCountersBc()", 10);
	}
}

function checkAndRunBc() {
	for(var i = 0; i < episodesDatesTimesBc.length; i++) {
		if (foundCountersBc[i]) {
			continue;
		}
		
		var o1 = "showtextBc" + (i + 1);
		var o2 = "showbirthdateBc" + (i + 1);
		var o3 = "showdateBc" + (i + 1);
		var o4 = "countdownBc" + (i + 1);
		var obj1 = getRefToBc(o1);
		var obj2 = getRefToBc(o2);
		var obj3 = getRefToBc(o3);
		var obj4 = getRefToBc(o4);
		
		if ((obj1 != null)&&(obj2 != null)&&(obj3 != null)&&(obj4 != null)) {
			foundCountersBc[i] = true;
			foundCountersBc[episodesDatesTimesBc.length]++;
			startCountdownBc(i);
		}
	}
}

function getRefToBc(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 prepareCountdownBc(id) {
	var thisIsTheEpisode = null;
	var thisIsTheEpisodeTime = null;
	var tmpToday = new Date();
	var tmpbigDay = new Date();
	var tmpBirthYear = null;

	tmpToday = new Date(tmpToday.getFullYear(), tmpToday.getMonth(), tmpToday.getDate(), 0, 0, 0);
	
	timerIDsBc[id] = null;
	timerRunningsBc[id] = false;
	bigDaysBc[id] = null;
	episodeIDsBc[id] = "";
	
	var tmpEpisodesDatesTimes = episodesDatesTimesBc[id];
	for(var i = 0; i < tmpEpisodesDatesTimes.length; i++) {
		var ep = tmpEpisodesDatesTimes[i];
		
		tmpbigDay = new Date(tmpToday.getFullYear(), ep[2] - 1, ep[3], 0, 0, 0);
		
		if (tmpbigDay.getTime() < tmpToday.getTime()) {
			tmpbigDay = new Date(tmpToday.getFullYear() + 1, ep[2] - 1, ep[3], 0, 0, 0);
		}
				
		if ((thisIsTheEpisodeTime != null && tmpbigDay.getTime() < thisIsTheEpisodeTime.getTime())
				|| (thisIsTheEpisodeTime == null)) {
			thisIsTheEpisode = tmpEpisodesDatesTimes[i];
			thisIsTheEpisodeTime = tmpbigDay;
			tmpBirthYear = ep[1];
		}
	}
	if (thisIsTheEpisodeTime != null) {
		bigDaysBc[id] = thisIsTheEpisodeTime;
		episodeIDsBc[id] = thisIsTheEpisode[0];
		
		var o = "showtextBc" + (id + 1);
		var obj = getRefToBc(o);
		obj.innerHTML = episodeIDsBc[id];
		
		o = "showbirthdateBc" + (id + 1);
		obj = getRefToBc(o);
		obj.innerHTML = bigDaysBc[id].getDate() + "." + (bigDaysBc[id].getMonth() + 1) + "." + tmpBirthYear;
		
		o = "showdateBc" + (id + 1);
		obj = getRefToBc(o);
		obj.innerHTML = bigDaysBc[id].getDate() + "." + (bigDaysBc[id].getMonth() + 1)
				+ "." + bigDaysBc[id].getFullYear() + "&nbsp;&raquo;&raquo;&nbsp;"
				+ (bigDaysBc[id].getFullYear() - tmpBirthYear) + " let";
		
	} else {
		bigDaysBc[id] = null;
	}
}

function startCountdownBc(id) {
	prepareCountdownBc(id);
	
	if (bigDaysBc[id] != null) {
		startclockBc(id);
	} else {
		stopclockBc(id);
	}
}

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

	var msPerSec = 1000,
		secPerMin = 60,
		minPerHr = 60,
		hrsPerDay = 24;
	
	var tmpbigDay = bigDaysBc[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 " + checkZeroBc(hrsLeft) + ":"
				+ checkZeroBc(minsLeft) + ":" + checkZeroBc(secLeft);
	
	var o = "countdownBc" + (id + 1);
	var obj = getRefToBc(o);
	obj.innerHTML = showText;
	
	if ((daysLeft <= 0)&&(hrsLeft <= 0)&&(minsLeft <= 0)&&(secLeft <= 0)) {
		stopclockBc(id);
		timerIDsBc[id] = setTimeout("startCountdownBc(" + id + ")", secPerMin * msPerSec);
		
		obj.innerHTML = happyBirthdayText;
		
	} else {
		timerIDsBc[id] = setTimeout("showtimeBc(" + id + ")", msPerSec);
	}
	timerRunningsBc[id] = true;
}

function stopclockBc(id) {
	if (timerRunningsBc[id]) {
		clearTimeout(timerIDsBc[id]);
	}
	timerRunningsBc[id] = false;
}

function startclockBc(id) {
	stopclockBc(id);
	showtimeBc(id);
}

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