$(document).ready( function() {
	
	/**
	 * process the json received with info about the 
	 * changes in the lot
	 **/
	function processLotJSON(id, lot) {
		var currentBid			= lot.currentBid;
		var nextBid				= lot.nextBid;
		var belowReserve		= lot.belowReserve;
		var userAutobidAmount	= lot.userAutobidAmount;
		var winning				= lot.winning;
		var losing				= lot.losing;
		var biddingOpen			= lot.biddingOpen;
		var bidCount			= lot.bidCount;
		var buyNowAvailable		= lot.buyNowAvailable;
		
		var details = $('#lot_'+id);
		
		var currentBidLabel = 'Current Bid:';
		if ( bidCount == 0 ) {
			currentBidLabel = 'Opening Bid:';
		}
		else if ( !biddingOpen ) {
			currentBidLabel = 'Final Bid:';
		}
		
		details.find('.currentBidLabel').text(currentBidLabel);
		details.find('.current-bid-amount .value').text(currentBid);
		details.find('.auctionEndsLabel').text(biddingOpen? 'Auction ends:' : 'Auction ended:');
		details.find('.bidding-ends-date').text(lot.endDate);
		details.find('.overtime').remove();
		if ( lot.overtimeActive && biddingOpen ) {
			details.find('.bidding-ends-date').after(
				'<p class="detailed overtime">' +
				'Extended Bidding' +
				'</p>'
			);
		}
		
		var biddingStatusHTML = [];
		
		if ( biddingOpen ) {
			if ( winning ) {
				biddingStatusHTML.push(
					'<p>You are currently the highest bidder on this lot.</p>'
				);
			}
			if ( belowReserve ) {
				biddingStatusHTML.push(
					'<p>The current bid is below reserve.</p>'
				);
			}
		}
		else {
			if ( winning ) {
				biddingStatusHTML.push(
					'<p>You were the highest bidder on this lot.</p>'
				);
			}
			if ( belowReserve ) {
				biddingStatusHTML.push(
					'<p>Bidding did not meet the reserve.</p>'
				);
			}
		}
		
		details.find('.lotBiddingStatus').html(
			biddingStatusHTML.join("")
		);
	}
	
	setTimeout(function() { updateLots(processLotJSON, 5000); }, 500);
} );