$(document).ready(
	function() {
		
		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_details_'+id);
			
			if ( biddingOpen ) {
				details.find('.current-bid-label').text(bidCount == 0? 'The opening bid is:' : 'The current bid is:');
				details.find('.current-bid .number').text(currentBid);
				details.find('.minimum-bid .number').text(nextBid);
				
				var bidBox = details.find(':text[name=incrAmount]');
				if ( bidBox.size() > 0 ) {
					if ( !(bidBox.hasClass("edited") || bidBox.hasClass("error")) && bidBox.val() != nextBid ) {
						bidBox
							.val(nextBid)
							.css('background', '#FFFF00');
						setTimeout(
							function() { bidBox.css('background', 'none') },
							500
						);
					}
				}
				
				details.find('.bid-status .highest-bidder').remove();
				if ( winning ) {
					if ( lot.incrementalBiddingAvailable || lot.autoBiddingAvailable ) {
						var highestBidder = $("<li id='highest-bidder' class='highest-bidder'> <span class='label'>You are currently the highest bidder.</span> </li>");
						if ( userAutobidAmount > currentBid ) {
							highestBidder.append(
								"<span id='my-max-bid-container' class='my-max-bid-container'> With a maximum bid of " +
								"<span id='my-max-bid' class='value redlight'>&pound;"+userAutobidAmount+"</span></span>"
							);
						}
						details.find('.bid-status').append(highestBidder);
					}
				}
				
				details.find('.bid-status .overtime').remove();
				if ( lot.overtimeActive ) {
					details.find('.bid-status').append("<li class='overtime'>Extended Bidding - bid now so you don't miss out</li>");
				}
				
				details.find('.bid-status .below-reserve').remove();
				if ( belowReserve ) {
					details.find('.bid-status')
						.append("<li class='below-reserve'>The current bid is below reserve.</li>");
						
					if ( lot.reserveTemperature != undefined ) {
						var temperature = lot.reserveTemperature;
						var temperatueSrc = TEMPERATURE_URL.replace(/\d+/,temperature);
						
						details.find('.bid-status')
							.append($("<li class='below-reserve'>Reserve-o-meter: </li>").append($("<img />").attr({src: temperatueSrc, alt: temperature})));
					}
				}
				
				if ( !buyNowAvailable ) {
					details.find('.buynow').hide();
				}
				
				details.find('.bidding-ends').text(lot.endDate);
			}
			else {
				// bidding closed so make sure it shows that
				if ( details.find('#bidding-ui form.bidForm').size() > 0 ) {
					window.location.reload();
				}
			}
		}
		
		attachAllJavascriptBidding($('#bidding-ui'));
		
		setTimeout(function() { updateLots(processLotJSON); }, 500);
	}
);