/* 
* Javascript cron script
* developed by The-Di-Lab
* contact me at thedilab@gmail.com
*/

/*
 * Cron class
 * params:
 * formClass-class name of the element
 * speed: interval
 * url:   url to get json data
 */
function Cron(formClass,speed,url){
		this.auctionObjs = $('.'+formClass);
		this.cronUrl = url;
		this.interval=null;
		
		var thisObj=this;
		this.start= function(){
			this.interval = window.setInterval ( thisObj.getData, speed );
		}
		
		this.stop = function(){
			clearInterval(this.interval);
		}
	
		this.getData = function(){
			 $.ajax({
				  async: true,
				  type: 'POST',
				  url: thisObj.cronUrl,
				  dataType:'json',
				  data: $(this).serialize(),
				  error: function(e,text,xhr){
				    alert(text+", error while updating auction,please refresh page.");
				  },
				  ajaxError:function(){
				  	alert('error loading data, refresh your page.');
				  },
				  success:function(returnDt) { 
						
							$.each(returnDt,function(index,value){
								value.Auction.left_time = value[0].left_time;
																								
								var auction = new Auction(value.Auction);	
								
								auction.update();
							});
				  }
			});
		}
}

/*
 * Auction class
 * Cron Class uses this class to update content
 */
function Auction(jsonAuction){
		this.id = jsonAuction.id;
		this.form = $('#auction_'+this.id );
		
		
		this.leftTime = jsonAuction.left_time;
		this.endingTime = jsonAuction.ending_time;
		this.status = jsonAuction.status;
		this.currentPrice = jsonAuction.current_price;
		this.startPrice = jsonAuction.start_price;
		
		
		this.formLeftTime=$(this.form).children('#leftTime').eq(0);
		this.formEndingTime=$(this.form).children('#endingTime').eq(0);
		this.formStatus=$(this.form).children('#status').eq(0);
		this.formCurrentPrice=$(this.form).children('#current_price').eq(0);
		this.formStartPrice=$(this.form).children('#start_price').eq(0);
		
		
		this.isEndingTimeDiff = function(){ 
			
			return ( $(this.formEndingTime).val() != this.endingTime);
			
		}
		
		this.isStatusDiff = function(){
			return ($(this.formStatus).val()!= this.status);
		}
		
		this.isPriceDiff = function(){
			
			return ($(this.formCurrentPrice).val() != this.currentPrice || 
					$(this.formStartPrice).val() != this.startPrice
			);
		}
		
		this.updateEndTime = function(){
			
			var timerObj = $(this.form).children('.countdown').eq(0);
			$(this.formLeftTime).val(this.leftTime);
			$(this.formEndingTime).val(this.endingTime);
			
			$(timerObj).countdown('destroy');
		
			showTimeLeft(timerObj,$(this.formLeftTime).val());
		}
		
		this.updateStatus = function(){
			$(this.formStatus).val(this.status);
			
			$(this.form).children('.status').text(this.status);
			
		}
		
		this.updatePrice = function(){ 
			$(this.formCurrentPrice).val(this.currentPrice);
			
			
			var updatePrice  = parseFloat(this.currentPrice)>=parseFloat(this.startPrice)?this.currentPrice:this.startPrice;
			
			$(this.form).children('.price').text(updatePrice);
		}
		
		this.update = function(){
			if(this.isEndingTimeDiff()){
				this.updateEndTime();
			}
			
			if(this.isStatusDiff()){
				this.updateStatus();
			}
			
			if(this.isPriceDiff()){
				this.updatePrice();
			}
		}

}

