Ext.namespace("Ext.ux");
 
/**
  * Ext.ux.Countdown Class
  *
  * @author  Rhys Jones
  * @version 1.0 beta
  *
  * @class Ext.ux.Countdown
  * @constructor
  * Creates new Ext.ux.Countdown
  *
  * @cfg {object} config Configuration options
  * @return {object} Ext.ux.Countdown Object
  */

Ext.ux.Countdown = function(config) {
	this.config = config;
	this.init();
	return this;
};

Ext.ux.Countdown.prototype = {
	
	target: null,
	format: '',
	period: 0,
	
	init: function() {
		this.target = Ext.get(this.config.target);
		this.format = this.config.format;
		this.period = this.config.period;
	},
		
	show: function() {
		if(this.target) {
			dString = this.format.replace('%d%',this.calcdiff(this.period,86400000,100000));
			dString = dString.replace('%h%',this.calcdiff(this.period,3600000,24));
			dString = dString.replace('%m%',this.calcdiff(this.period,60000,60));
			dString = dString.replace('%s%',this.calcdiff(this.period,1000,60));									
			this.target.update(dString);
		}
	},
	
	start: function() {
		this.show();
		thisObj = this;
		this.period = this.period - 1000;
		if(this.period > -1) {
			setTimeout(function() { thisObj.start(); }, 1000);
		} else {
			var callBack = this.config.callback;
			callBack();
		}
	},
	
	calcdiff: function(rem,mseqv,tcount) {
	  s = ((Math.floor(rem/mseqv))%tcount).toString();
	  if (s.length < 2) s = "0" + s;
	  return s;	
	}
	
};
