﻿var arr = [1256284800, 1261645200, 1269082800, 1275721200];
var arText = ["Sügise vaheajani: ", "Talve vaheajani: ", "Kevade vaheajani: ", "Suve vaheajani: "];
var message = ["Ilusat sügisvaheaega!", "Ilusaid jõule!", "Ilusat kevadvaheaega!", "Ilusat suve!"];
var ar = new Array();
var tick = 0;
var id = 0;
var messageTime = 828000; 

function ct(countDown, suffix) {
	var x = ar[id] / countDown;
	x = Math.floor(x);
	ar[id] = ar[id] - (x * countDown);
	if(x < 10 && x >= 0) {
		return "0" + x + suffix;
	}
	return x + suffix;
}
function incId() {
	if( id < arr.length) {
		id++;
	} else {
		id = 0;
	}
}

function countdown() {
	if(arr.length > 0 && arText.length > 0 && arr.length == arText.length) {
		if( tick == 4 ) {
			tick = 0;
			incId();
		} else {
			tick++;
		}
		var time = new Date();
		ar[id] = arr[id] - ((time.getTime() - time.getMilliseconds()) / 1000);
		if(ar[id] >= 0) {
			document.getElementById("countDown").innerHTML = arText[id] + ct(604800, "w ") + ct(86400, "d ") + ct(3600, "h ") + ct(60, "m ") + ct(1, "s");
			setTimeout(countdown, 1000);
		} else {
			var x = arr[id] - ((time.getTime() - time.getMilliseconds()) / 1000) + messageTime; 
			if(x >= 0) {
				document.getElementById("countDown").innerHTML = message[id];
				setTimeout(countdown, 1500);
			} else {
				if((arr[arr.length-1] - ((time.getTime() - time.getMilliseconds()) / 1000)) >= 0 ) {
					tick = 4;
					countdown();
				}
			}
			
		}
	}		
}

