var TimerLeft = Class.create();

TimerLeft.prototype = {
	initialize : function(DivId,PriceDivId,QuizId) {
		this.div_id=DivId;
		this.price_div_id=PriceDivId;
		this.quiz_id=QuizId;
		all_seconds=$(this.div_id).innerHTML;
		this.EvaluateTime(all_seconds);
	},

	EvaluateTime: function (all_seconds){
		if(all_seconds>0)
		{
			all_minutes=Math.floor(all_seconds/60);
			all_hours=Math.floor(all_minutes/60);
			all_days=Math.floor(all_hours/24);
			this.seconds=all_seconds-all_minutes*60;
			this.minutes=all_minutes-all_hours*60;
			this.hours=all_hours-all_days*24;
			this.days=all_days;
			this.can_dec=true;
			this.can_refresh=true;
		}
		else
		{
			this.can_dec=false;
			this.can_refresh=false;
		}
	},

	DecrementSecond: function (){
		if(this.seconds>0)
		{
			this.seconds--;
		}
		else if(this.minutes>0)
		{
			this.seconds=59;
			this.minutes--;
		}
		else if(this.hours>0)
		{
			this.seconds=59;
			this.minutes=59;
			this.hours--;
		}
		else if(this.days>0)
		{
			this.seconds=59;
			this.minutes=59;
			this.hours=23;
			this.days--;
		}
		else
		{
			this.can_dec=false;
			if(this.can_refresh)
			{
				//window.location.reload();
			}
		}

		var str='';

		if(this.days>0)
		{
			str=this.days+'d ';
		}
		str+=((this.hours>9)?this.hours:("0"+this.hours))+':'+((this.minutes>9)?this.minutes:("0"+this.minutes))+':'+((this.seconds>9)?this.seconds:("0"+this.seconds));
		if(!this.can_dec)
		{
			str='00:00:00';
		}
		$(this.div_id).innerHTML=str;
		this.timer = setTimeout(this.DecrementSecond.bind(this), 1000);
		if(this.refresh_timer==null)
		{
			this.refresh_timer = setTimeout(this.ReloadAuctionData.bind(this), 10000);
		}
	},


	onTimerEvent: function() {
		this.DecrementSecond();
	},


	StartTimer: function (){
		this.timer = setInterval(this.onTimerEvent.bind(this), 1000);
	},

	StopTimer: function (){
		clearTimeout(this.timer);
	},

	ReloadAuctionData: function (){
		clearTimeout(this.refresh_timer);
		this.refresh_timer=null;
		GetTimerAndPrice(this);
	}
}

function GetTimerAndPrice(Obj)
{
	if(!Obj.is_reloading)
	{
		Obj.is_reloading=true;
		new Ajax.Request('/ajax_get_quiz_timer.php?quiz_id=' + encodeURIComponent(Obj.quiz_id), {
			method: 'get',
			onSuccess: function(transport) {
				if (transport.responseText!='|')
				{
					data=transport.responseText.split('|');
					now_ts=Number(new Date().getTime()).toString();
					now_ts=now_ts.substring(0,now_ts.length-3);
					Obj.StopTimer();
					Obj.EvaluateTime(Number(data[0]));
					Obj.DecrementSecond();
					old_price=$(Obj.price_div_id).innerHTML;
					$(Obj.price_div_id).innerHTML=data[1].toString();
					$('players_left_'+Obj.quiz_id).innerHTML=data[2].toString();
					$('user_name_'+Obj.quiz_id).innerHTML=data[3].toString();
					Obj.is_reloading=false;
					if(old_price!=data[1].toString())
					{
						$(Obj.div_id).hide();
						$(Obj.div_id).show();
						//new Effect.Highlight($(Obj.price_div_id), {startcolor: '#FF0000', endcolor: '#FFFFFF', duration: 2.5});
					}
				}
			}
		});
	}
}

TimerLeft.methods = {


}