//<![CDATA[
/*
Původní verze:
	Multiple Counter 1.1
	Copyright 2006-2007 popKorn
	popkorn@post.cz
Úpravy: Maruxs
*/

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 = "countdown-name-" + (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] = "Epizoda";

	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 = "countdown-name-" + (id + 1);
		var obj = getRefTo(o);
		obj.innerHTML = episodeIDs[id];
	} else {
		if (tmpEpisodesDatesTimes.length != 0) {
			episodeIDs[id] = tmpEpisodesDatesTimes[tmpEpisodesDatesTimes.length - 1][0];
			var o = "countdown-name-" + (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;
	var textsecLeft = "sekund ";
	if(secLeft<5) textsecLeft = "sekundy ";
	if(secLeft==1)	textsecLeft = "sekunda ";
	if(secLeft==0)	textsecLeft = "sekund ";
	timeLeft = Math.floor(timeLeft / secPerMin);

	var minsLeft = timeLeft % minPerHr;
	var textminsLeft = "minut a ";
	if(minsLeft<5) textminsLeft = "minuty a ";
	if(minsLeft==1)	textminsLeft = "minuta a ";
 
	timeLeft = Math.floor(timeLeft / minPerHr);
	
	var hrsLeft = timeLeft % hrsPerDay;
	var texthrsLeft = "hodin ";
	if(hrsLeft<5) texthrsLeft = "hodiny ";
	if(hrsLeft==1)	texthrsLeft = "hodina ";
	if(hrsLeft==0)	{texthrsLeft = ""; hrsLeft = "";}
	
	var daysLeft = Math.floor(timeLeft / hrsPerDay);
	var textdaysLeft = "dní ";
	if(daysLeft<5) textdaysLeft = "dny ";
	if(daysLeft==1)	textdaysLeft = "den ";
	if(daysLeft==0)	{textdaysLeft = ""; daysLeft = "";}
	
	if((minsLeft==0)&&(hrsLeft==0)&&(daysLeft==0))  {
      textminsLeft = ""; minsLeft = "";
      } else if (minsLeft==0) {
      textminsLeft = " a "; minsLeft = "";
      }
  if((secLeft==0)&&(minsLeft==0)&&(hrsLeft==0)&&(daysLeft==0)) {
      textsecLeft = "Epizoda se právě vysílá"; secLeft = "";
      }
	
	var showText = daysLeft + textdaysLeft + hrsLeft + texthrsLeft
				+ minsLeft + textminsLeft + secLeft + textsecLeft;
	
	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);
}

//]]>