// JavaScript Document
var auctions={
	itemidArray:[],
	productArray:[],
	bidArray:[],
	currentTime:0,
	flashTimeInterval:[],
	getProductsInterval:0,
	isItem:0,
	bidid:0,
	hasEnded:false,
	cancelPay:false,
	setItemidArray:function(itemStr){
		eval("this.itemidArray="+itemStr);
	},
	run:function(){
		//this.getProductsInterval=setInterval(function(){auctions.getProductArray();},1000);
		auctions.getProductArray();
	},
	getProductArray:function(){
		try{
			$.post(ROOT_PATH+"/auction-index-auctiondetail",{'itemid[]':this.itemidArray,isItem:this.isItem,bidid:this.bidid},function(data) {
				var dataArray=data.split('|');
				auctions.currentTime=dataArray[1];
				eval("auctions.productArray="+dataArray[0]+";");
				auctions.fillInItems();
				if(auctions.isItem==1){
					eval("auctions.bidArray="+dataArray[2]+";");
					auctions.fillInBids();
					auctions.checkWinner();
					if(!auctions.hasEnded){
						setTimeout(function(){auctions.getProductArray();},800);
					}
				}else{
					setTimeout(function(){auctions.getProductArray();},800);	
				}
			});
		}catch(e){
			alert(e);
		}
	},
	fillInItems:function(){
		var len=this.productArray.length;
		for(var i=0;i<len;i++){
			var itemidInput=$('.item_input[value="'+this.productArray[i].itemid+'"]');
			var li=itemidInput.parent().parent();
			var addTimeInput=itemidInput.next();
			var remainTime=parseInt(addTimeInput.val())+parseInt(this.productArray[i].valid_seconds)-parseInt(this.currentTime);
			if(remainTime<0){
				remainTime=0;
				li.find('.itemBid input').attr('disabled',true);
				li.find('.itemBid input').val("Traded");
				li.find('.itemTime').css('color','#fc0309');
				this.hasEnded=true;
			}
			li.find('.currentPrice').html('$'+this.productArray[i].current_price);
			li.find('.winUser').html(this.productArray[i].win_username||'----');
			li.find('.itemTime').html(countDown.setTimes(remainTime));
			if(remainTime<15&&remainTime!=0){
				if(!this.flashTimeInterval[this.productArray[i].itemid]){
					this.flashTime(li.find('.itemTime'),this.productArray[i].itemid);
				}
			}else{
				li.find('.itemTime').css('color','green');
				if(this.flashTimeInterval[this.productArray[i].itemid]){
					clearInterval(this.flashTimeInterval[this.productArray[i].itemid]);
					this.flashTimeInterval[this.productArray[i].itemid]=null;
				}
			}
			itemidInput.next().next().val(remainTime);
		}
	},
	fillInBids:function(){

		if(this.bidArray.length>0){
			var str='';
			for(var i=0;i<this.bidArray.length;i++){
				this.bidArray[i].username=subStrs(this.bidArray[i].username,20);
				if(this.bidArray[i].username==getCookie(COOKIE_PRE+'username')){
					this.bidArray[i].username='<font color="green">'+this.bidArray[i].username+'</font>';	
				}
				str+='<tr><td>'+this.bidArray[i].bid_price+'</td><td>'+this.bidArray[i].username+'</td><td>'+this.bidArray[i].add_time+'</td>';
			}
			$('.auctionRecordDiv table').append(str);
			this.bidid=this.bidArray[this.bidArray.length-1].bidid;
		}
		var size=$('.auctionRecordDiv tr').size();
		var shouldDelArray={};
		if(size>11){
			var shouldDelArray;
			for(var i=1;i<size-10;i++){
				shouldDelArray[i]=$('.auctionRecordDiv tr')[i];
			}
		}
		for(var j in shouldDelArray){
			$(shouldDelArray[j]).remove();
		}

	},
	flashTime:function(div,itemid){
		var color=1;
		this.flashTimeInterval[itemid]=setInterval(
											function(){
												if(color==1){
													$(div).css('color','#fc0309');
													color=0;	
												}else{
													$(div).css('color','green');
													color=1;	
												}
											},
											500
										);
	},
	bid:function(thisInput){
		if(getCookie(COOKIE_PRE+'uid')==''){
			if(confirm("You haven't loged in WhyMind.com,do you want to log in right now?")){
				window.location.href="buyer-auth-login?preurl="+encodeURIComponent(window.location.href);	
			}
			return;
		}
		showMsg("<img src='html/images/loading.gif' />");
		var itemid=$(thisInput).parent().next().children().get(0).value;
		$.post(ROOT_PATH+"/auction-cate-bid",{'itemid':itemid},function(data) {
			shutMsg();
			if(data=='notuser'){
				if(confirm("You haven't loged in WhyMind.com,do you want to log in right now?")){
					window.location.href="buyer-auth-login?preurl="+encodeURIComponent(window.location.href);	
				}
			}else if(data=='invalid'){
				alert("The auction has been shut down!");
			}else if(data=='virtual'){
				alert("You don't have enough virtual money!");	
			}
		});	
	},
	checkWinner:function(){
		if(this.hasEnded){
			if(this.productArray[0].has_paid=='0'){
				if(!this.cancelPay){
					if(this.productArray[0].win_username!=''&&this.productArray[0].win_username==getCookie(COOKIE_PRE+'username')){
						if(confirm("Your have win this product! Do you want to pay it right now?")){
							window.location.href="adminb-auction-winauction?type=notpaid";
						}else{
							this.cancelPay=true;	
						}
					}
				}
			}
		}
	},
	checkHasEnded:function(){
		if(this.hasEnded){
			clearInterval(this.getProductsInterval);
		}	
	}
}

