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;*/
		this.days = ":";
		this.hours = ":";
		this.minutes = ":";
		this.seconds = "";
		this.divname1=divname;

		// 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();
                //alert(currentdate);
		this.timediff = (currentdate - this.startdate) / 1000;
		//$('testid').innerHTML=this.timediff;
		this.countdown = this.maxtime - this.timediff;
        //$('testid').innerHTML=this.divname1;
		//my logic
		if(parseInt(this.countdown)<=86400 && $(this.divname1).className!="TIME1" )
		{
		  $(this.divname1).className="TIME1";
		}
		
		// 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   = String(Math.floor((this.countdown - dayfield * oneDay) / oneHour))
		var minutefield = String(Math.floor((this.countdown - dayfield * oneDay - hourfield * oneHour) / oneMinute))
		var secondfield = String(Math.floor((this.countdown - dayfield * oneDay - hourfield * oneHour - minutefield * oneMinute)))
		if(minutefield.length <2)minutefield="0"+minutefield
		if(secondfield.length <2)secondfield="0"+secondfield

		// 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>";
			if ((arguments[0] == 0) && (arguments[1] == 0)) {
				displaystring += "<font class='lub_timer_red'>"
			}
			/*if (arguments[0] > 0 || this.mode != "hideempty")*/
			if (arguments[0] > 0){
				displaystring += arguments[0] + "D " + this.days
			}
			/*if (((arguments[0] > 0) || (arguments[1] > 0)) || this.mode != "hideempty")*/
			if (((arguments[0] > 0) || (arguments[1] > 0))){
				displaystring += arguments[1] + "h " + this.hours
			}
			displaystring += arguments[2] + "m " + this.minutes + arguments[3] + "s"+ this.seconds
			if ((arguments[0] == 0) && (arguments[1] == 0)) {
				displaystring += "</font>"
			}
			displaystring += "</span>"
		}
		else { //else if target date/time met
			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);
});

