var cbpe = '';
// Prototype class to keep track of counters and update them.
var CountBidster = Class.create({
	initialize: function() {
		this.counters = new Array();
	},
	registerCounter: function(name) {
		this.counters.push(name);
	},
	unregisterCounter: function(name) {
		var newarr = this.counters.without(name);
		this.counters = newarr.clone();
	},
	updateCounters: function() {
		if (!this.counters) {
			cbpe.stop();
			return;
		}
		this.counters.each(function(item) {
			eval(item + '.updateTime()');
		});
	},
	updateServerTime: function(time) {
		if (!this.counters) {
			cbpe.stop();
			return;
		}
		this.counters.each(function(item) {
			eval(item + '.setServerTime("' + time + '")');
		});
	}
});

// Prototype class to create a single counter.
var cbCounter = Class.create({
	initialize: function(divname, objname, serverdate, targetdate, mode, auctionended, days, hours, minutes, seconds) {
		if (!document.getElementById || !document.getElementById(divname)) return;
		if (serverdate == "" || targetdate == "") return;

		if (!document.getElementById(divname)) return;
		this.container  = document.getElementById(divname);

		this.serverdate = new Date(serverdate);
		this.targetdate = new Date(targetdate);
		this.startdate  = new Date();
		this.timesup = false;
		this.objname = objname;
		this.mode = mode;
		this.auctionended = auctionended;
		this.days = days;
		this.hours = hours;
		this.minutes = minutes;
		this.seconds = seconds;

		// Maximum seconds this counter will run before it is turned off.
		this.maxtime = (this.targetdate - this.serverdate) / 1000;

		// Register this counter to be updated.
		cbobj.registerCounter(objname);
	},
	setServerTime: function(time) {
		this.serverdate = new Date(time);
	},
	updateTime: function() {
		if (!this.startdate || this.timesup == true) {
			return;
		}

		// How much time (in seconds) has elapsed since the start?
		var currentdate = new Date();
		this.timediff = (currentdate - this.startdate) / 1000;
		this.countdown = this.maxtime - this.timediff;

		// Check if time is up.
		if (this.timediff > this.maxtime) {
			this.timesup = true;
			this.container.innerHTML = this.formatresults();

			// Unregister this counter so it won't be updated anymore.
			var obj = this.objname;
			cbobj.unregisterCounter(obj);
		} else {
			this.displayCountdown();
		}
	},
	displayCountdown: function() {

		// Divide seconds into days, hours, minutes and seconds.
		var oneMinute   = 60 //minute unit in seconds
		var oneHour     = oneMinute * 60 //hour unit in seconds
		var oneDay      = oneHour * 24 //day unit in seconds
		var dayfield    = Math.floor(this.countdown / oneDay)
		var hourfield   = Math.floor((this.countdown - dayfield * oneDay) / oneHour)
		var minutefield = Math.floor((this.countdown - dayfield * oneDay - hourfield * oneHour) / oneMinute)
		var secondfield = Math.floor((this.countdown - dayfield * oneDay - hourfield * oneHour - minutefield * oneMinute))

		// Display the counter.
		this.container.innerHTML = this.formatresults(dayfield, hourfield, minutefield, secondfield);
	},
	formatresults: function() {
		if (this.timesup == false) { //if target date/time not yet met
                    //			var displaystring = "<span>";
			var displaystring = "";
			if ((arguments[0] == 0) && (arguments[1] == 0)) {
				displaystring += ""
			}
			if (arguments[0] > 0 || this.mode != "hideempty") {
				displaystring += arguments[0] + this.days + " "
			}
			if (((arguments[0] > 0) || (arguments[1] > 0)) || this.mode != "hideempty") {
				displaystring += arguments[1] + this.hours + " "
			}
			displaystring += arguments[2] + this.minutes + " " + arguments[3] + this.seconds
			if ((arguments[0] == 0) && (arguments[1] == 0)) {
				displaystring += ""
			}
                        //			displaystring += "</span>"
			displaystring += ""
		}
		else { //else if target date/time met
                    //			var displaystring = "<font class='red_text'>" + this.auctionended + "</font>"
			var displaystring = " " + this.auctionended + " "
		}
		return displaystring
	}
});

// Runs the updateCounters on the BidsterCount object.
function startCounters()
{
	cbobj.updateCounters();
}

// Create a counter object.
var cbobj = new CountBidster();

// When DOM is fully loaded, start the counters.
document.observe("dom:loaded", function() {
	new Ajax.Request("servertime.php", {
		method: 'get',
		onSuccess: function(transport) {
			ajaj = eval('(' + transport.responseText + ')');
			cbobj.updateServerTime(ajaj.time);
		}
	});
	cbpe = new PeriodicalExecuter(startCounters, 0.2);
});
