$(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);
		
		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);
			
			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 ( !buyNowAvailable ) {
			   details.find('.buynow').hide();
			}
			
			details.find('.bidding-ends').text(lot.endDate);
		}
		else {
			var originalURL = details.find('.biddingForm form').attr('action');
			if ( originalURL ) {
				var javascriptURL = originalURL.replace(/#.*$/, '') + "/jsbid";
				details.find('.biddingForm').load(javascriptURL);
			}
		}
		
	}
	
	setTimeout(function() { updateLots(processLotJSON); }, 500);
	
	attachAllJavascriptBidding($('#lots .lot .biddingForm'));
} );