/****************************/
/*Clase Auction 			*/
/****************************/
function Auction(id,visualRootId,remainedSeconds,offerPrice,duration,lastBidTime,currentBid,winner,checkHistory,initialStatus){
	this.id=id;
	this.visualRootId=visualRootId;
	this.remainedSeconds=Number(remainedSeconds);
	//interval
	this.duration=Number(duration); 
	this.offerPrice=new Number(offerPrice);
	this.currentBid = currentBid;
	this.LeadingZero = true;
	this.DisplayFormat = "%%D%%d %%H%%h %%M%%m %%S%%s.";
	this.ACTIVE = 0;
	this.SUSPENDED = 1;
	this.CLOSED = 2;
	this.CHECKING = 3;
	//events
	this.EVT_BIDED=0;
	this.EVT_SUSPENDED=1;
	this.EVT_CLOSED=2;
	this.EVT_RESTORED=3;
	this.status=this.ACTIVE;
	this.lastBidTime=new Number(lastBidTime);
	this.manager = null;
	this.winner=winner;
	this.bidded = false;
	this.checkHistory = checkHistory;
	this.checkHistoryTimeout = null;
	this.status = initialStatus;
}

//constructor reference
Auction.prototype.constructor = Auction;

//methods
Auction.prototype = {
	calcage:function (secs, num1, num2) {
		s = ((Math.floor(secs/num1))%num2).toString();
		if (this.LeadingZero && s.length < 2)
		s = "0" + s;
		return "<b>" + s + "</b>";
	},	
	canBid:function(){
		return (this.status== this.ACTIVE);
	},
	setData:function(data){
		this.status = this.CHECKING
		switch(parseInt(data.status)){
			case this.EVT_BIDED:{
				var nlbt=new Number(data.lbt);
				if(nlbt>this.lastBidTime){
					this.remainedSeconds = this.duration;
					this.currentBid      = data.np;
					this.winner          = data.w;
					this.lastBidTime     = nlbt;
					this.status          = this.ACTIVE;
					this.bidded          = true;
					if(this.checkHistory==true)
						this.checkBidHistory();
				}
			}
			break;
			case this.EVT_SUSPENDED:
			{
				this.status = this.SUSPENDED;
			}
			break;
			case this.EVT_CLOSED:{
				this.status = this.CLOSED;
			}
			break;
			case this.EVT_RESTORED:{
				this.status = this.ACTIVE;
			}
			break;
		}
		this.setTime(this.manager.time);
	},
	setTime:function(time){
		if(this.status == this.ACTIVE){
			if (this.lastBidTime > 0){
		       var B = this.lastBidTime + this.duration;
		       B *= 1000;
		       B -= (time);
		       B /= 1000;
		       this.remainedSeconds = Math.floor(B)+1;
		       this.remainedSeconds = (this.remainedSeconds>this.duration)?this.duration:this.remainedSeconds;
		    	   
			}
		    if (this.remainedSeconds < 0){
		        this.status = this.CHECKING
		    }
		}
		if (this.status == this.CHECKING){
	         if (this.lastBidTime > 0){
	            var B = this.lastBidTime + this.duration;
	            B *= 1000;
	            B -= time;
	            B /= 1000;
	            this.remainedSeconds = Math.floor(B)+1;
	            this.remainedSeconds = (this.remainedSeconds>this.duration)?this.duration:this.remainedSeconds;
	         }
	         if (this.remainedSeconds > 0)
	         {
	            this.status = this.ACTIVE
	         }
	   }
	   this.render();
	},
	render:function(){
		var $container= $('#'+this.visualRootId);
		switch(this.status){
			case this.CHECKING:{
				var btnActive = $('.active',$container);
				btnActive.css('display','none');
				var btnPending = $('.pending',$container);
				btnPending.css('display','');
				var btnChecking = $('.checking',$container);
				btnChecking.css('display','none');
				var btnHalted = $('.halted',$container);
				btnHalted.css('display','none');
				var btnStoped = $('.stopped',$container);
				btnStoped.css('display','none');
				var btnLeading = $('.leading',$container);
				btnLeading.css('display','none');
				var $visual_clock = $('#clock_'+this.id);
			    $visual_clock.css('display','none');
			}
			break;
			case this.ACTIVE:{
				var btnActive = $('.active',$container);
				btnActive.css('display','');
				var btnPending = $('.pending',$container);
				btnPending.css('display','none');
				var btnChecking = $('.checking',$container);
				btnChecking.css('display','none');
				var btnHalted = $('.halted',$container);
				btnHalted.css('display','none');
				var btnStoped = $('.stopped',$container);
				btnStoped.css('display','none');
				var btnLeading = $('.leading',$container);
				btnLeading.css('display','none');
				var txtCurBid = $('.current_price',$container);
				var iconstart = $('#iconstar_'+this.id,$container);
				var txtWinner = $('.lastbidder',$container);
				txtCurBid[0].innerHTML = this.currentBid;
				txtWinner[0].innerHTML = this.winner;
				if(this.winner!='')
					iconstart.removeClass('hidden');
				var secs = this.remainedSeconds;
				var clock = this.DisplayFormat.replace(/%%D%%/g, this.calcage(secs,86400,1000000));
		        clock = clock.replace(/%%H%%/g, this.calcage(secs,3600,24));
		        clock = clock.replace(/%%M%%/g, this.calcage(secs,60,60));
		        clock = clock.replace(/%%S%%/g, this.calcage(secs,1,60));
		        var $visual_clock = $('#clock_'+this.id);
		        $visual_clock.css('display','');
		        if(secs<=0){
		        	$visual_clock[0].innerHTML = FinishMessage1;
		        	this.status=this.CLOSED;
		        }
		        else{
		        	if(secs>20){
		        		$visual_clock.addClass('secactive');
		        		$visual_clock.removeClass('tminus10');
		        		$visual_clock.removeClass('tminus20');
		        	}
		        	else if(secs<=20 && secs>10){
		        		if(!$visual_clock.hasClass('tminus20')){
			        		$visual_clock.removeClass('secactive');	
			        		$visual_clock.removeClass('tminus10');
			        		$visual_clock.addClass('tminus20');	
		        		}
		        	}
		        	else if(secs<=10){
		        		if(!$visual_clock.hasClass('tminus10')){
		        			$visual_clock.removeClass('secactive');
		        			$visual_clock.addClass('tminus10');	
		        			$visual_clock.removeClass('tminus20');
		        		}
		        	}
		        	$visual_clock[0].innerHTML = clock;
		        }
		        if(this.bidded == true){
		        	this.bidded = false;
		        	var options = {};
					//run the effect
		        	txtWinner.effect('highlight',options,1000);
		        	txtCurBid.effect('highlight',options,1000);
		        	$visual_clock.effect('highlight',options,1000);
		        }
			}
			break;
			case this.SUSPENDED:{
				var $visual_clock = $('#el_'+this.id);
				$visual_clock.css('display','none');
				var btnActive = $('.active',$container);
				btnActive.css('display','none');
				var btnPending = $('.pending',$container);
				btnPending.css('display','none');
				var btnChecking = $('.checking',$container);
				btnChecking.css('display','none');
				var btnHalted = $('.halted',$container);
				btnHalted.css('display','');
				var btnStoped = $('.stopped',$container);
				btnStoped.css('display','none');
				var btnLeading = $('.leading',$container);
				btnLeading.css('display','none');
			}
			break;
			case this.CLOSED:{
				var $visual_clock = $('#el_'+this.id);
				$visual_clock.css('display','none');
				var btnActive = $('.active',$container);
				btnActive.css('display','none');
				var btnChecking = $('.checking',$container);
				btnChecking.css('display','none');
				var btnPending = $('.pending',$container);
				btnPending.css('display','none');
				var btnHalted = $('.halted',$container);
				btnHalted.css('display','none');
				var btnStoped = $('.stopped',$container);
				btnStoped.css('display','');
				var btnLeading = $('.leading',$container);
				btnLeading.css('display','none');
			}
			break;
		}
	},
	checkBidHistory:function(){
		clearTimeout(this.checkHistoryTimeout);
		var req = new Object(); 
		req.auctionId = this.id;	//this.id;
		$.ajax({
	        type: "POST",
	        url: 'ajax/bidhistory.php',
	        data: req,
	        dataType: "json",
	        success:this.successBidHistory.bind(this),
	        error:this.errorBidHistory.bind(this)
	    });
	},
	successBidHistory:function(msg){
		$('#bid_top_ten_panel')[0].innerHTML=msg.data;
	},
	errorBidHistory:function(xhr, status, ex){
		clearTimeout(this.checkHistoryTimeout);
		this.checkHistoryTimeout = setTimeout(this.checkBidHistory.bind(this),1000);
	},
	bid: function(){
		var req = new Object(); 
		req.auctionId = this.id;	//this.id;
		var $this = this;
		$.ajax({
	        type: "POST",
	        url: 'bid_classic_async.php',
	        data: req,
	        dataType: "json",
	        success: function(msg) {
				if(msg.result==true){
					$this.currentBid      = msg.np;
					$this.offerPrice      = msg.nbv;
					$this.winner 	      = msg.w;
					$this.lastBidTime     = Number(msg.lbt);
					$this.remainedSeconds = $this.duration;
					LoginManager.updateBalance(msg.nb,msg.nbp);
					$this.bidded = true;
					$this.setTime($this.manager.time);
					if($this.checkHistory){
						$this.checkBidHistory();
					}
				}
				else{
					alert(msg.bid_result);
				}
	        },
	        error:function(xhr, status, ex){
	        	alert('Try later.')
	        }
	    });
	},
	getRemainedSeconds:function(){
		return this.remainedSeconds; 
	},
	setRemainedSeconds:function(remainedSeconds){
		this.remainedSeconds=remainedSeconds; 
	},
	getId:function(){
		return this.id; 
	}
};


/*******************************************************/
function AuctionWatchList(id,visualRootId,remainedSeconds,offerPrice,duration,lastBidTime,currentBid,winner,checkHistory,initialStatus){
	Auction.call(this,id,visualRootId,remainedSeconds,offerPrice,duration,lastBidTime,currentBid,winner,checkHistory,initialStatus);
}

//inherit from the Auction prototype
AuctionWatchList.prototype = new Auction();
//put the correct constructor reference back (not essential)
AuctionWatchList.prototype.constructor = AuctionWatchList;


//override
AuctionWatchList.prototype.render = function() {
	var $container= $('#'+this.visualRootId);
	switch(this.status){
		case this.CHECKING:{
			var $visual_clock = $('#clock_'+this.id);
		    $visual_clock.css('display','none');
		}
		break;
		case this.ACTIVE:{
			var iconstart = $('#iconstar_'+this.id,$container);
			var txtWinner = $('.lastbidder',$container);
			var txtCurBid = $('.current_price',$container);
			txtCurBid[0].innerHTML = this.currentBid;
			txtWinner[0].innerHTML = this.winner;
			if(this.winner!='')
				iconstart.removeClass('hidden');
			var secs = this.remainedSeconds;
			var clock = this.DisplayFormat.replace(/%%D%%/g, this.calcage(secs,86400,1000000));
	        clock = clock.replace(/%%H%%/g, this.calcage(secs,3600,24));
	        clock = clock.replace(/%%M%%/g, this.calcage(secs,60,60));
	        clock = clock.replace(/%%S%%/g, this.calcage(secs,1,60));
	        var $visual_clock = $('#clock_'+this.id);
	        $visual_clock.css('display','');
	        if(secs<=0){
	        	$visual_clock[0].innerHTML = FinishMessage1;
	        	this.status=this.CLOSED;
	        }
	        else{
	        	if(secs>20){
	        		$visual_clock.addClass('secactive');
	        		$visual_clock.removeClass('tminus10');
	        		$visual_clock.removeClass('tminus20');
	        	}
	        	else if(secs<=20 && secs>10){
	        		if(!$visual_clock.hasClass('tminus20')){
		        		$visual_clock.removeClass('secactive');	
		        		$visual_clock.removeClass('tminus10');
		        		$visual_clock.addClass('tminus20');	
	        		}
	        	}
	        	else if(secs<=10){
	        		if(!$visual_clock.hasClass('tminus10')){
	        			$visual_clock.removeClass('secactive');
	        			$visual_clock.addClass('tminus10');	
	        			$visual_clock.removeClass('tminus20');
	        		}
	        	}	
	        	$visual_clock[0].innerHTML = clock;
	        }
	        if(this.bidded == true){
	        	this.bidded = false;
	        	var options = {};
				//run the effect
	        	txtWinner.effect('highlight',options,1000);
	        	$visual_clock.effect('highlight',options,1000);
	        }
		}
		break;
		case this.SUSPENDED:{
			var $visual_clock = $('#el_'+this.id);
			$visual_clock.css('display','none');
		}
		break;
		case this.CLOSED:{
			var $visual_clock = $('#el_'+this.id);
			$visual_clock.css('display','none');
		}
		break;
	}
}




