var counterObjects = [];

function countdown(obj)
{
	this.obj		= obj;
	this.Div		= "clock";
	this.BackColor		= "white";
	this.ForeColor		= "black";
	this.TargetDate		= "12/31/2020 0:00 AM";
	this.DisplayFormat	= "%%D%% Days";
	this.CountActive	= false;
	
	this.DisplayStr		= "";

	this.timeDiff		= timeDiff;
	this.futureEvent	= 0;

}

function timeDiff()
{
	var nowDate = new Date();
	var TargetDate = new Date(this.TargetDate);
	var targetDate = this.TargetDate;
	
	var nowYear = nowDate.getFullYear();
	var nowMonth = nowDate.getMonth() + 1;
	var nowDay = nowDate.getDate();
	
	var targetYear = TargetDate.getFullYear();
	var targetMonth = TargetDate.getMonth() + 1;
	var targetDay = TargetDate.getDate();
	if (TargetDate < nowDate)
		this.futureEvent = 0;
	else
		this.futureEvent = 1;

	if (this.futureEvent)
	{
		var resultantYear = targetYear - nowYear;
		var resultantMonth = targetMonth - nowMonth;
		var resultantDay = targetDay - nowDay;
	}
	else
	{
		var resultantYear = nowYear - targetYear;
		var resultantMonth = nowMonth - targetMonth;
		var resultantDay = nowDay - targetDay;
	}

	if(resultantDay < 0){
		resultantMonth--;
		resultantDay = resultantDay + 32 - new Date(nowYear, nowMonth-1, 32).getDate();
	}
		
	if(resultantMonth < 0){
		resultantYear--;
		resultantMonth = resultantMonth + 12;
	}

	var resultantWeek = Math.round((resultantDay) / 7);
	resultantDay = resultantDay % 7;

	if (resultantYear > 0)
		if (resultantYear > 1)
			this.DisplayStr = this.DisplayStr+resultantYear+" years ";
		else
			this.DisplayStr = this.DisplayStr+resultantYear+" year ";
	if (resultantMonth > 0)
		if (resultantMonth > 1)
			this.DisplayStr = this.DisplayStr + resultantMonth + " months ";
		else
			this.DisplayStr = this.DisplayStr+resultantMonth+" month ";
	if (resultantWeek > 0)
		if (resultantWeek > 1)
			this.DisplayStr = this.DisplayStr + resultantWeek + " weeks ";
		else
			this.DisplayStr = this.DisplayStr+resultantWeek+" week ";
	if (resultantDay > 0)
		if (resultantDay > 1)
			this.DisplayStr = this.DisplayStr + resultantDay + " days ";
		else
			this.DisplayStr = this.DisplayStr+resultantDay+" day ";

	if (this.birthday==0)
	{
		if (this.futureEvent)
			this.DisplayStr=this.displayBeforeText+" " + this.DisplayStr+" til "+this.displayAfterText+" ";
		else
			this.DisplayStr=this.displayBeforeText+" "+this.DisplayStr+" since "+this.displayAfterText+" ";
		if ((resultantDay+resultantWeek+resultantMonth+resultantYear)==0)
		this.DisplayStr = this.displayBeforeText +" Today is "+this.displayAfterText;
	}

	if (this.birthday==1)
	{
		if ((this.futureEvent)||((resultantDay+resultantWeek+resultantMonth+resultantYear)==0))
			this.DisplayStr=this.displayAfterText+" hasn't been born yet! ";
		else
			this.DisplayStr=this.displayAfterText+" is "+this.DisplayStr+" old ";
	}

	if (this.birthday==2)
	{
		if (this.futureEvent)
			this.DisplayStr=this.displayAfterText+" is due in "+this.DisplayStr;
		else
			this.DisplayStr=this.displayAfterText+" was due "+this.DisplayStr+" ago ";
		if((resultantDay+resultantWeek+resultantMonth+resultantYear)==0)
			this.DisplayStr=this.displayAfterText+" is due today ";
	}

//	document.getElementById(this.Div).innerHTML = this.DisplayStr;
}

function createCounter(targetDate,beforeText,afterText,birthday)
{
	var counterIndex=counterObjects.length;
	counterObjects[counterIndex] = new countdown('counterObjects['+counterIndex+']');
	counterObjects[counterIndex].Div = "counterObjects" + counterIndex;
	counterObjects[counterIndex].TargetDate = targetDate;
	counterObjects[counterIndex].displayAfterText = afterText;
	counterObjects[counterIndex].displayBeforeText = beforeText;
	counterObjects[counterIndex].CountActive = 0;
	counterObjects[counterIndex].birthday = birthday;
	counterObjects[counterIndex].timeDiff();
	document.write('<span id="clockwrapper"><span class=counterdiv id="'+counterObjects[counterIndex].Div+'">'+counterObjects[counterIndex].DisplayStr+'</span></span>');
}
