var XmasCountdown = {

	eParent: false,

	oDate: {
		days: -1, hours: -1, minutes: -1, seconds: -1
	},

	nDate: {
		days: 0, hours: 0, minutes: 0, seconds: 0
	},
	
	i: 0,
	q: 0,
	actualOpacity: 1,

	e: {
		p: 0, day: 0, hour: 0, minutes: 0, second: 0, sA: [], sB: []
	},

	timeRun: 0,
	timeHelper: 0,
	working: 1,
	isIE: false,
	isOk: false,
	toAnimate: false,
	jump: 0.07,

	greetings: ' ',

	start: function() {
		if (!this.isOk)
			return;

		if (this.i){
			clearTimeout(this.i);
			clearInterval(this.i);
		}
		if (this.timeRun > 0) {
			this.working = 1;
			XmasCountdown.work();
			this.i = setInterval('XmasCountdown.work()',1000);
		}
		else {
			if (this.e.p) {
				this.e.p.className = 'greet';
				while(this.e.p.firstChild){
					this.e.p.removeChild(this.e.p.firstChild);
				}
				this.e.p.appendChild(this.cE('span'));
				this.e.p.lastChild.innerHTML = this.greetings;
			}
		}
	},

	sklonuj: function(pocet,kus,kusy,kusu,over) {
		if (over && pocet > 9 && pocet % 10 > 1) {
			pocet = pocet % 10;
		}

		if (!pocet || pocet > 4)
			return kusu;
		else if(pocet == 1)
			return kus;
		else
			return kusy;
	},

	gId: function(t) {
		if (t)
			return document.getElementById(t);
		return false;
	},

	cE: function(typ) {
		return document.createElement(typ);
	},

	init: function(a) {
		if (!this.gId('xmascnt')) {
			return this;
		}
		this.isOk = true;
		this.isIE = (!window.ActiveXObject) ? false : ((window.XMLHttpRequest) ? ((document.querySelectorAll) ? 6 : 5) : 4); 

		if (a) {
			this.timeRun = parseInt(a,10);
		}
		this.eParent = this.gId('xmascnt');
		this.e.p = this.eParent.appendChild(this.cE('p'));
		this.e.p.appendChild(this.cE('span'));
		this.e.p.lastChild.innerHTML = '';
		this.e.p.lastChild.className = 'bliziSe';

		this.e.sA[0] = this.e.p.appendChild(this.cE('span'));
		this.e.sA[0].className = 'cnt_days';
		this.e.sB[0] = this.e.p.appendChild(this.cE('span'));
		this.e.sB[0].className = 'cnt_days';

		this.e.sA[1] = this.e.p.appendChild(this.cE('span'));
		this.e.sA[1].className = 'cnt_hours';
		this.e.sB[1] = this.e.p.appendChild(this.cE('span'));
		this.e.sB[1].className = 'cnt_hours';

		this.e.sA[2] = this.e.p.appendChild(this.cE('span'));
		this.e.sA[2].className = 'cnt_minutes';
		this.e.sB[2] = this.e.p.appendChild(this.cE('span'));
		this.e.sB[2].className = 'cnt_minutes';

		this.e.sA[3] = this.e.p.appendChild(this.cE('span'));
		this.e.sA[3].className = 'cnt_seconds';
		this.e.sB[3] = this.e.p.appendChild(this.cE('span'));
		this.e.sB[3].className = 'cnt_seconds';

		return this;
	},

	numFormat: function(t){
		if (t == 0) {
			return '00';
		}
		else if (t<10) {
			return '0'+t+'';
		}
		else {
			return t;
		}
	},

	work: function() {
		if (this.timeRun > 1 && this.working) {
			this.timeRun--;

			this.timeHelper = this.timeRun;

			this.toAnimate = [];

			this.nDate.days = this.oDate.days;
			this.nDate.hours = this.oDate.hours;
			this.nDate.minutes = this.oDate.minutes;
			this.nDate.seconds = this.oDate.seconds;

			this.oDate.seconds = this.timeHelper % 60;
			this.timeHelper -= this.oDate.seconds;

			this.timeHelper = this.timeHelper / 60;
			this.oDate.minutes = this.timeHelper % 60;
			this.timeHelper -= this.oDate.minutes;

			this.timeHelper = this.timeHelper / 60;
			this.oDate.hours = this.timeHelper % 24;
			this.timeHelper -= this.oDate.hours;

			this.timeHelper = this.timeHelper / 24;
			this.oDate.days = this.timeHelper;

			if (this.nDate.days != this.oDate.days) {
				this.toggle(0);
				this.e.sB[0].innerHTML = this.e.sA[0].innerHTML;
				this.e.sA[0].innerHTML = '<strong>'+this.numFormat(this.oDate.days)+'</strong>'+this.sklonuj(this.oDate.days,'den','dny','dnù');
				this.toAnimate.push('0');
			}
			if (this.nDate.hours != this.oDate.hours) {
				this.toggle(1);
				this.e.sB[1].innerHTML = this.e.sA[1].innerHTML;
				this.e.sA[1].innerHTML = '<strong>'+this.numFormat(this.oDate.hours)+'</strong>'+this.sklonuj(this.oDate.hours,'hodina','hodiny','hodin');
				this.toAnimate.push('1');
			}
			if (this.nDate.minutes != this.oDate.minutes) {
				this.toggle(2);
				this.e.sA[2].innerHTML = '<strong>'+this.numFormat(this.oDate.minutes)+'</strong>'+this.sklonuj(this.oDate.minutes,'minuta','minuty','minut');
				this.toAnimate.push('2');
			}
			if (this.nDate.seconds != this.oDate.seconds) {
				this.toggle(3);
				this.e.sA[3].innerHTML = '<strong>'+this.numFormat(this.oDate.seconds)+'</strong>'+this.sklonuj(this.oDate.seconds,'sekunda','sekundy','sekund',1);
				this.toAnimate.push('3');
			}
			if (this.toAnimate.length > 0) {
				this.animate();
			}
		}
		else {
			this.timeRun = 0;
			this.start();
		}
	},

	toggle: function(a) {
		this.e.sB[a].innerHTML = this.e.sA[a].innerHTML;
	},

	animate: function() {
		if (this.actualOpacity <= 1.05){
			if (this.isIE) {
				for(this.q=0;this.q<this.toAnimate.length;this.q++){
					this.animStepIE(this.toAnimate[this.q]);
				}
			}
			else {
				for(this.q=0;this.q<this.toAnimate.length;this.q++){
					this.animStepOther(this.toAnimate[this.q]);
				}
			}
			this.actualOpacity += this.jump;
			setTimeout('XmasCountdown.animate()',20);
		}
		else {
			this.actualOpacity = 0;
		}
	},

	animStepIE: function(el){
		if (this.actualOpacity <= 1) {
			this.e.sA[el].style.filter = (this.actualOpacity >= 1) ? '' : 'alpha(opacity=' + this.actualOpacity * 100 + ')';
			this.e.sB[el].style.filter = (this.actualOpacity >= 1) ? '' : 'alpha(opacity=' + (100 - this.actualOpacity * 100) + ')';
		}
		else {
			this.e.sA[el].style.filter = '';
			this.e.sB[el].style.filter = 'alpha(opacity=0)';
		}
	},
	
	animStepOther: function(el){
		if (this.actualOpacity <= 1) {
			this.e.sA[el].style.opacity = this.actualOpacity;
			this.e.sB[el].style.opacity = (1-this.actualOpacity);
		}
		else {
			this.e.sA[el].style.opacity = 1;
			this.e.sB[el].style.opacity = 0;
		}
	}

}
