var mEndYear = 2012;
var mEndMonth = 6;
var mEndDay = 17;
var mEndHour = 8;
var mEndMinute = 0;
var mEndSecond = 0;


function _calcCountdown()
{
	var Dates = _getDates();
	var tmpValue;
	
	// Year
	tmpValue = _calcValue(Dates.SecToEnd, 31536000);
	this.Years = tmpValue.value;
	// Days
	tmpValue = _calcValue(tmpValue.seconds, 86400);
	this.Days = tmpValue.value;

	// Hours
	tmpValue = _calcValue(tmpValue.seconds, 3600);
	if (tmpValue.value < 10)
	{
		this.Hours = "0" + tmpValue.value;
	}
	else
	{
		this.Hours = tmpValue.value;
	}

	// Minutes
	tmpValue = _calcValue(tmpValue.seconds, 60);
	if (tmpValue.value < 10)
	{
		this.Minutes = "0" + tmpValue.value;
	}
	else
	{
		this.Minutes = tmpValue.value;
	}

	// Seconds
	if (tmpValue.seconds < 0) tmpValue.seconds = 0;
	if (tmpValue.seconds < 10)
	{
		this.Seconds = "0" + tmpValue.seconds;
	}
	else
	{
		this.Seconds = tmpValue.seconds;
	}

	return this;
}
function _getDates()
{
	this.Now = new Date();
	this.EndDate = new Date(mEndYear, mEndMonth -1, mEndDay, mEndHour, mEndMinute, mEndSecond);
	
	var MilSecToEnd = EndDate.getTime() - Now.getTime();
	this.SecToEnd = Math.floor(MilSecToEnd/1000);

	return this;
}
function _calcValue(aSeconds, aFactor)
{
	if (aSeconds > aFactor)
	{
		this.value = Math.floor(aSeconds/aFactor);
	}
	else
	{
		this.value = 0;
	}
	this.seconds = aSeconds - (this.value * aFactor);

	return this; 
}
function _buildValueSpan(aValue)
{
	return '<span class="CountdownValue">' + aValue + '</span>';
}
function buildCountdown()
{
	var lCountdown = _calcCountdown();
	var lFlag = false;
	var lRet = "";
	
	if (lCountdown.Years > 0)
	{
		lFlag = true;
		lRet = _buildValueSpan(lCountdown.Years) + " Jahr";
		if (lCountdown.Years != 1) lRet += "e";
	}
	if((lCountdown.Days > 0) || (lFlag))
	{
		lFlag = true;
		lRet += " " + _buildValueSpan(lCountdown.Days) + " Tag";
		if (lCountdown.Days != 1) lRet += "e";
	}
	if((lCountdown.Hours > -1) || (lFlag))
	{
		lFlag = true;
//		lRet += " " + _buildValueSpan(lCountdown.Hours) + " Stunde";
		lRet += " " + lCountdown.Hours;
//		if (lCountdown.Hours != 1) lRet += "n";
	}
	if((lCountdown.Minutes > -1) || (lFlag))
	{
		lFlag = true;
//		lRet += " " + _buildValueSpan(lCountdown.Minutes) + " Minute";
		lRet += ":" + lCountdown.Minutes;
//		if (lCountdown.Minutes != 1) lRet += "n";
	}
	if((lCountdown.Seconds > -1) || (lFlag))
	{
		lFlag = true;
//		lRet += " " + _buildValueSpan(lCountdown.Seconds) + " Sekunde";
		lRet += ":" + lCountdown.Seconds;
//		if (lCountdown.Seconds != 1) lRet += "n";
	}
	if (lFlag) lRet = "noch " + lRet;
	
	document.getElementById('Countdown').innerHTML = lRet;
	window.setTimeout('buildCountdown()', 1000);
}

