//CountDown object - calculates and formats time string based on end date and current server time, and updates the display of the given element

function CountDown(dateEndOfAuct, currentDate, displayElement, strDay, strOver, auctId)
{
	this.endDate = new Date(dateEndOfAuct*1000);
	this.currentDate = new Date(currentDate*1000);
	this.diff = new Date(this.endDate - this.currentDate);
	this.Secs = Math.floor(this.diff.valueOf()/1000);
	this.dayLabel = strDay;
	this.overLabel = strOver;
	this.format = "%%D%% "+this.dayLabel+", %%H%%:%%M%%:%%S%%";
	this.LeadingZero = true;
	this.display = document.getElementById(displayElement);
	this.selfReferenceId = auctId;
	arrGlobalTimerContainer[this.selfReferenceId] = this;
	
	if (this.Secs > 0)
		this.countActive = true;
	else
		this.countActive = false;
	
}

CountDown.prototype = {
		
	calcTime: function(secs, num1, num2, day)
	{
		var retval = ((Math.floor(secs/num1)) % num2).toString();
		if (this.LeadingZero && retval.length < 2 && !day)
			retval = "0" + retval;
		
		return "<b>" + retval + "</b>";
	},
	
	removeButtons: function()
	{
		var bidButton = document.getElementById('bidButton'+this.selfReferenceId);
		var buyoutButton = document.getElementById('buyoutButton'+this.selfReferenceId);
		
		if (bidButton !== null)
			jQuery(bidButton).css('display', 'none');
		
		if (buyoutButton !== null)
			jQuery(buyoutButton).css('display', 'none');
	},
	
	countBack: function()
	{
		var oThis = this;
		
		if (this.Secs <= 0)
		{
			this.countActive = false;
			this.removeButtons();
			var DisplayStr = this.overLabel;
		}
		else
		{
			var DisplayStr = this.format.replace(/%%D%%/g, this.calcTime(this.Secs,86400,100000,true));
			DisplayStr = DisplayStr.replace(/%%H%%/g, this.calcTime(this.Secs,3600,24,false));
			DisplayStr = DisplayStr.replace(/%%M%%/g, this.calcTime(this.Secs,60,60,false));
			DisplayStr = DisplayStr.replace(/%%S%%/g, this.calcTime(this.Secs,1,60,false));
			this.Secs--;
		}

		if (this.display !== null)
			this.display.innerHTML = DisplayStr;
		
		if (this.countActive)
			setTimeout("arrGlobalTimerContainer["+this.selfReferenceId+"].countBack()", 1000);
			
	}
	
}

function timerObjectManager(strAuctIds, strEncaps, currentDate, strDay, strOver)
{
	var arrAuctIds = strAuctIds.split(strEncaps);
	
	for (i = 0; i < arrAuctIds.length; i++)
	{
		arrTimerObjects.push(new CountDown(document.getElementById("timeleft"+arrAuctIds[i]).value, currentDate, "timedisplay"+arrAuctIds[i], strDay, strOver, arrAuctIds[i]));
		arrTimerObjects[i].countBack();
	}
}

//END

