var Countdown = {
	
	interval : null,
	
	finalDate : null,
	
	name : null,
	
	msecs : [86400000, 3600000, 60000, 1000],
	
	digit : function (d,a) {var y=Math.floor(d/a); return (y>9)?y:"0"+y;},
	
	remainingTime : function (over) {
		var remain = over - new Date().getTime();
		var tmp = {};
		if 	(remain <= 0) {
			tmp = null;
			}
		else {
			tmp.day = Countdown.digit(remain,Countdown.msecs[0]);
			remain=remain%Countdown.msecs[0];
			tmp.hour = Countdown.digit(remain,Countdown.msecs[1]);
			remain=remain%Countdown.msecs[1];
			tmp.minute = Countdown.digit(remain,Countdown.msecs[2]);
			remain=remain%Countdown.msecs[2];
			tmp.second = Countdown.digit(remain,Countdown.msecs[3]);
			};
		return tmp;
		},
	
	count : function () {
		var date = Countdown.remainingTime(Countdown.finalDate), str;
		if (!date) {
			str = Countdown.name+" hats geschafft. Viel Spass beim feiern";
			window.clearInterval(Countdown.interval);
			}
		else str = Countdown.name+" hat noch "+date.day+" Tage "+date.hour+" Stunden "+date.minute+" Minuten "+date.second+" Sekunden bis zur Bewusstlosigkeit!";
		
		document.getElementById("cntdwn").firstChild.data = str;
		
	
		},
	
	init : function (date, name) {
		Countdown.finalDate = date;
		Countdown.name = (name) ?name :"Jemand";
		Countdown.interval = window.setInterval(Countdown.count, 1000);
		}
	};


createCountdown = {
	
	GET : (function () {
		if (location.search) {
			var search = location.search, obj={}, tmp;
			if (search.indexOf("?") == 0) search = search.slice(1,search.length);
			search = search.split("&");
			for (var i=0;i<search.length;i++) {
				tmp = decodeURI(search[i]).split("=");
				obj[tmp[0]] = tmp[1];
				};
			return obj;
			}
		else return {};
		})(),
	
	KEKS : (function () {
		if (document.cookie) {
			var keks = document.cookie.split(";"), obj={}, tmp;
			for (var i=0; i<keks.length; i++) {
				tmp = keks[i].split("=");
				obj[tmp[0].replace(/\s/, "")] = unescape(tmp[1]);
				};
			return obj;
			}
		else return {};
		})(),
	
	parseDate : function (elem) {
		var extract = function (obj) {
			for (var i=0; i<obj.length; i++) {
				if (obj[i].selected) return obj[i].value || null;
				};
			};
		var date = new Date();
		with (date) {
			setDate(extract(elem.day));
			setMonth(extract(elem.month));
			setYear(extract(elem.year));
			setHours(extract(elem.hour));
			setMinutes(extract(elem.minute));
			setSeconds(00);
			};
		return date.getTime();
		},
	
	startCountdown : function () {
		var date,name;
		
		if (date = createCountdown.GET["t"]) {
			name = createCountdown.GET["n"];
			document.getElementById("cntdwn_display").style.display = "inline";
			document.getElementById("distURL").value = "http://"+location.hostname+location.pathname+"?t="+encodeURI(date);
			document.getElementById("distURL").value += (name)?"&n="+encodeURI(name):"";
			document.title = (name) ?name+" der Countdown":"Examenscountdown";
			document.getElementById("distro").style.display = "inline";
			Countdown.init(date, createCountdown.GET["n"]);
			}		
		else if (date = createCountdown.KEKS["finish"]) {
			if (createCountdown.GET["new"] == "true") {
				document.cookie = "name=bla;expires="+new Date(new Date().getTime()-60000).toGMTString();
				document.cookie = "finish=123;expires="+new Date(new Date().getTime()-60000).toGMTString();
				location.search = "";
				};
			name = createCountdown.KEKS["name"]
			document.title = (name) ?name+" - der Countdown":"Examenscountdown";
			document.getElementById("cntdwn_display").style.display = "inline";
			document.getElementById("distURL").value = "http://"+location.hostname+location.pathname+"?t="+encodeURI(date);
			document.getElementById("distURL").value += (name)?"&n="+encodeURI(name):"";
			document.getElementById("distro").style.display = "inline";
			Countdown.init(date, name || null);
			}
		else {
			document.getElementById("cntdwn_input").style.display = "inline";
			$("#create").click(function () {
				var date = createCountdown.parseDate(document.datum);
				var name = document.datum.name.value;
				document.title = (name) ?name+" - der Countdown":"Examenscountdown";
				document.cookie = "finish="+escape(date)+";expires="+new Date(date).toGMTString();
				if (name) document.cookie = "name="+escape(name)+";expires="+new Date(date).toGMTString();
				document.getElementById("distURL").value = "http://"+location.hostname+location.pathname+"?t="+encodeURI(date);
				document.getElementById("distURL").value += (name)?"&n="+encodeURI(name):"";
				$("#cntdwn_input").slideUp(400, function () {
					$("#cntdwn_display").slideDown(400, function () {$("#distro").slideDown(400)});
					
					})
				Countdown.init(date, name);
				});
			};
		}
}

$(createCountdown.startCountdown);


