function Timer(obj)
{
	this.seconds	=	obj.seconds;
	this.painter	=	obj.elements;
	this.custom     =   null;
	this.positions	=	[0, 29, 29*2, 29*3, 29*4, 29*5, 29*6, 29*7, 29*8, 29*9];
	this.interval	=	null;
	this.setTime();
}


Timer.prototype.setTime		=	function()
{
	var d = Math.ceil(this.seconds / 86400) - 1;//(this.seconds - (this.seconds % 86400)) / 86400;
	var h = Math.ceil(this.seconds / 3600) - d*24 - 1;// - ;// - (d*24);//(this.seconds - (this.seconds % 3600)) / 3600;
	var m = Math.ceil((this.seconds - d*86400 - h*3600) / 60 );// - ((this.seconds - h * 3600)%60)) / 60;
	var s = Math.ceil((this.seconds - d*86400 - h*3600 - (m-1)*60));
	//alert(this.seconds+'-'+d*86400+'-'+(h*3600));
	//alert(s+'-'+m+'-'+h+'-'+d);
	this.days		=	d;
	this.hours		=	h;
	this.mins		=	m;
	this.secs		=	s;
}


// Тук не пипаш

Timer.prototype.start		=	function()
{
	var self = this;
	this.interval = setInterval(function(){
		self.countdown();
	},1000);
}


// Тук изчисляваш новите [hours , mins , secs]

Timer.prototype.calculate	=	function()
{
	
	var secs	=	this.secs == 0 ? 59 : this.secs - 1;
	var mins	=	this.secs == 0 && this.mins > 0 ? this.mins - 1 : (this.mins == 0 && this.secs == 0 ) ? 59 : this.mins;
	var hours	=	this.secs == 59 && this.mins == 59 && this.hours > 0 ? this.hours - 1 : (this.hours);
	var days	=	this.secs == 59 && this.mins == 59 && this.hours > 0 && this.days > 0 ? this.days - 1 : (this.days);
	
	this.days  =   days;
	this.hours  =   hours;
	this.mins   =   mins;
	this.secs	=	secs;
}

// Тук ъпдейтваш елементите в this.painter спрямо this.hours, this.mins, this.secs

Timer.prototype.paint		=	function(format)
{
	for(var i = 0; i < format.length; i++)
	{
		this.animate(this.painter[i], format[i]);
	}
}

Timer.prototype.countdown	=	function()
{
	this.calculate();
	
	if(this.hours == 0 && this.mins == 0 && this.secs == 0)
	{
		this.finish();
	}

	var format = this.format();
	this.paint(format);
}

Timer.prototype.finish		=	function()
{
	clearInterval(this.interval);
	this.animate(this.painter[5],0);
	$('#buyNow').css({visibility:'hidden'});
	$('#recommend').css({visibility:'hidden'});
}


// Тук форматираш this.hours, this.mins, this.seconds.
// Да речем минутите са 8, трябва да е 08, а не само 8.
// Не променяш this.hours, this.mins, this.secs тук, а само връщаш форматирани.

Timer.prototype.format		=	function()
{

	var d1 = (this.days < 10) ? '0' : this.days.toString().charAt(0);
	var d2 = (this.days < 10) ? this.days.toString().charAt(0) : this.days.toString().charAt(1);

	var h1 = (this.hours < 10) ? '0' : this.hours.toString().charAt(0);
	var h2 = (this.hours < 10) ? this.hours.toString().charAt(0) : this.hours.toString().charAt(1);
	
	var m1 = (this.mins < 10) ? '0' : this.mins.toString().charAt(0);
	var m2 = (this.mins < 10) ? this.mins.toString().charAt(0) : this.mins.toString().charAt(1);
	
	var s1 = (this.secs < 10) ? '0' : this.secs.toString().charAt(0);
	var s2 = (this.secs < 10) ? this.secs.toString().charAt(0) : this.secs.toString().charAt(1);
	
	return [d1,d2,h1,h2,m1,m2,s1,s2];
}

Timer.prototype.animate		= function(e,pos)
{
	var bpos = "-"+this.positions[pos]+"px";
	e.animate({top:bpos},{duration:600,easing:'easeOutElastic'});
}

