window.undefined = window.undefined;
pO = this;
if(pO.Counter == undefined)
	pO.Counter = {};
pO.Counter = function(elem_id,options) {
	this.option = options;
	this.elem_id = elem_id;
	this.format = $(elem_id).text();
	$(elem_id).html("");
	var date = new Date();
	date.setDate(this.option.d);
	date.setFullYear(this.option.y);
	date.setMonth(this.option.m - 1);
	date.setHours(this.option.h);
	date.setMinutes(this.option.i);
	date.setSeconds(this.option.s);
	this.countbackTo = date;
	this.startCounter();
	return;
};
pO.Counter.prototype.startCounter = function() {
	CounterPointer=this;
	this.cntId = setTimeout("CounterPointer.startCounter()",1000);
	this.refreshOutput();
	return;
};
pO.Counter.prototype.endCounter = function() {
	this.cntId = setTimeout("CounterPointer.startCounter()",1000);
	if (this.cntId) clearTimeout(this.cntId);
	$(this.elem_id).html(this.option.endCounter ? this.option.endCounter : "");
	return;
};
pO.Counter.prototype.refreshOutput = function() {
	var currentTime = new Date();
	var dif = Math.round((this.countbackTo.getTime() - currentTime.getTime()) / 1000);
	if (dif<1) return this.endCounter();
	var s = this.lz(dif%60);
	var i = this.lz(Math.floor(dif/60)%60);
	var h = this.lz(Math.floor(dif/3600)%24);
	var d = Math.floor(dif/(3600*24));
	var str = this.format;
	str = str.replace("ss",s).replace("mm",i).replace("hh",h).replace("dd",d);
	$(this.elem_id).html(str);
	return;
};
pO.Counter.prototype.lz = function(i) {
	if (i.toString().length<2) i = "0"+i;
	return i;
};
