/****************************/
/*Clase AuctionManager*/
/****************************/
function AuctionManager(iswatching){
	this.auctions={};
	this.ids=[];
	//timestamp
	this.waitingRequest = false;
	this.localRefreshing=false;
	this.REMOTE_CYCLE=3100;
	this.LOCAL_CYCLE=1000;
	this.curLocalCycle=this.LOCAL_CYCLE;
	this.remoteTimeout=null;
	this.localTimeout=null;
	this.time =0;
	this.iswatching=iswatching;
}

//constructor reference
AuctionManager.prototype.constructor = AuctionManager;

//methods
AuctionManager.prototype = {
	setTimeStamp:function(ut){
		this.ut=ut;
	},
	setTimeRemote : function(time){
	      var D = new Date();
		  var nt = time*1000;
	      D.setTime(nt);
	      this.time = D;
	      var C = (nt) % this.LOCAL_CYCLE;
	      this.curLocalCycle = this.LOCAL_CYCLE-C;
	      this.updateAuctionTime();
	      this.resetLocalCycle(this.curLocalCycle);
	},
	updateAuctionTime : function(){
	      var B = this.time.getTime();
	      for (var A in this.auctions)
	      {
	         this.auctions[A].setTime(B);
	      }
	},
	resetLocalCycle :function(cycle){
		  clearTimeout(this.localTimeout);
		  this.localTimeout = setTimeout(this.localCycle.bind(this),cycle); 
	},
	updateData:function(data){
	},
	localCycle:function(){
		clearTimeout(this.localTimeout);
		this.time.setTime(this.time.getTime()+this.LOCAL_CYCLE);
		this.updateAuctionTime();
		this.localTimeout = setTimeout(this.localCycle.bind(this),this.LOCAL_CYCLE);
	},
	updateAuctionTime : function(){
		var time = this.time.getTime();
		for (var A in this.auctions){
	         this.auctions[A].setTime(time);
	    }
	},
	remoteCycle:function(){
		clearTimeout(this.remotetimeout);
		this.remotetimeout = setTimeout(this.remoteCycle.bind(this),this.REMOTE_CYCLE);
		if(this.waitingRequest==false && this.ids.length>0){
			this.waitingRequest = true;
			var w = this.iswatching?"1":"0";
			reqJSON = {"ut":this.ut,"w":w,"ids[]":[this.ids]};
			$.ajax({
		        type: "POST",
		        url: "events_manager.php",
		        data: reqJSON,
		        dataType: "json",
		        success:this.remoteCycleSucess.bind(this),
		        error:this.remoteCycleError.bind(this)
		    });
		}
	},
	remoteCycleError:function(xhr, status, ex){
		this.waitingRequest = false;
		clearTimeout(this.remotetimeout);
    	this.remotetimeout = setTimeout(this.remoteCycle.bind(this),this.REMOTE_CYCLE);
	},
	remoteCycleSucess:function(msg){
		clearTimeout(this.remotetimeout);
		this.waitingRequest = false;
		this.setTimeRemote(msg.t);
		if(msg.data.length>0)
			this.ut = msg.let;
		this.updateAuctions(msg.data);
		this.remotetimeout = setTimeout(this.remoteCycle.bind(this),this.REMOTE_CYCLE);
	},
	updateAuctions:function (data){
		var reload=false;
		for (var A = 0; A < data.length; A ++ ){
			 if(parseInt(data[A].status)==4){
				 //reload page
				 //window.location.reload();
				 reload = true;
			 }	
			 else if (this.auctions[data[A].prodid]){
	            this.auctions[data[A].prodid].setData(data[A]);
	         }
	    }
		if(reload==true)
			window.location.reload();
	},
	initialize: function(ut){
		var $this = this;
		this.setTimeStamp(ut);
		$(document).unbind('click').bind('click', function(e) {
	        var result = true;
	        if ($(e.target).hasClass(".auction_bid")) {
	        	if(LoginManager.isLogged()){
	        	var current_auction = $this.auctions[$(e.target).attr('a_id')];
	        	if(current_auction!=null && current_auction.canBid())
	        		if(current_auction.offerPrice <=LoginManager.balance){
	        			 current_auction.bid();
		        		 this.ut = new Date().getTime();
	        		}
	        		else{
	        			window.location='yourbalance.php';
	        		}
	        	}
	        	else{
	        		LoginManager.doLogin();
	        	}
	            result = false
	        }
	        return result;
	    });
		this.setTimeRemote(this.ut);
		this.remoteTimeout = setTimeout(this.remoteCycle.bind(this),this.REMOTE_CYCLE);
		this.localCycle();
	},
	addAuction:function(auction){
		if(auction.getId() != ""){
			auction.manager = this;
			this.auctions[auction.id]=auction;
			this.ids.push(auction.id);
		}
		return auction;
	}
};





