
$(document).ready(
	function() {
		// add edited class so we can tell
		// whether we should auto-update input
		// box or not
		function setEdited() {
			$(':text[name=incrAmount]').focus(
				function() {
					$(this).addClass("edited");
				}
			);
		}
		setEdited();
		function updateBiddingStatus(biddingStatus, lot) {
			var statusClasses = [ 'winning', 'losing', 'won', 'lost', 'belowReserve', 'noBid' ];
			var status = '';
			if ( lot.belowReserve && lot.winning ) {
				status = 'belowReserve';
			}
			else if ( lot.winning && lot.biddingOpen ) {
				status = 'winning';
			}
			else if ( lot.losing && lot.biddingOpen ) {
				status = 'losing';
			}
			else if ( lot.winning && !lot.biddingOpen ) {
				status = 'won';
			}
			else if ( lot.losing && !lot.biddingOpen ) {
				status = 'lost';
			}
			for ( var i = 0; i < statusClasses.length; i++ ) {
				var statusi = statusClasses[i];
				if ( statusi != status ) {
					biddingStatus.removeClass(statusi);
				}
			}
			if ( status ) {
				biddingStatus.addClass(status);
			}
		}
		function calcTimeRemaining(lot) {
			if (lot.biddingOpen) {
				var timeRemainingMillis = lot.timeRemainingMillis;
				if ( timeRemainingMillis > 0 ) {
					var days    = Math.floor(timeRemainingMillis/(1000*24*60*60));
					var hours   = Math.floor((timeRemainingMillis/(1000*60*60)) - days*24);
					var minutes = Math.floor((timeRemainingMillis/(1000*60)) - 60*(days*24 + hours));
					var seconds = Math.floor((timeRemainingMillis/(1000)) - 60*(60*(days*24 + hours) + minutes));

					days    = days    > 9 ? days    :
							  days    < 1 ? "00"    : "0" + days;
					hours   = hours   > 9 ? hours   :
							  hours   < 1 ? "00"    : "0" + hours;
					minutes = minutes > 9 ? minutes :
							  minutes < 1 ? "00"    : "0" + minutes;
					seconds = seconds > 9 ? seconds :
							  seconds < 1 ? "00"    : "0" + seconds;

					return days + ":" + hours + ":" + minutes + ":" + seconds;
				}
				else {
					return "bidding ended";
				}
			}
			else {
				if(lot.boughtNow) {
					return "item sold";
				}
				else {
					return 'bidding ended';
				}
			}
		}
		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;
			//buy now
			var boughtNow   = lot.boughtNow;
			var buyNowPrice = lot.buyNowPrice
			var details = $('#lot_'+id);
			var currentBidLabel = 'Current bid:';
			if ( bidCount == 0 ) {
				currentBidLabel = 'Opening bid:';
			}
			else if ( !biddingOpen ) {
				currentBidLabel = 'Final bid:';
			}
			//buy now
			if (boughtNow) {
				currentBidLabel = 'Bought for:';
				currentBid = ""+buyNowPrice;
			}
			details.find('.currentBid .label').text(currentBidLabel);
			details.find('.currentBid .value').text(currentBid);
			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
					);
				}
			}
			var biddingStatus = details.find('.biddingStatus');
			updateBiddingStatus(biddingStatus, lot)
			if ( lot.timeRemainingMillis <= 0 ) {
				details.find('div.itemwrap div.itemBidding').html("<p><strong>Bidding Has Ended</strong></p>");
				details.find('form.bidForm tr.endDate span.remaining').html(" bidding ended ");
				details.find('form.bidForm tr.endDate span.remaining').addClass("ended");
			}
			else {
				details.find('.bidCount .value').text(bidCount);
				details.find('.endDate .value').text(lot.endDate);
				details.find('.endDate .remaining').text(calcTimeRemaining(lot));
				details.find('tr.overtime .value').text(lot.overtimeActive? "Overtime bidding" : "");
			}
			if ( !biddingOpen ) {
				details.find('tr.overtime').remove();
				details.find('tr.errors').remove();
				details.find('tr.incrementalBid').remove();
				details.find('tr.autoBid').remove();
				details.find('tr.buyNow').remove();
			}
			else {
				if ( userAutobidAmount > currentBid ) {
					details.find('.currentAutobid').html("(Current autobid is &pound;"+userAutobidAmount+")");
				}
				else {
					details.find('.currentAutobid').html("");
				}
			}
		}
		attachAllJavascriptBidding($('div.itemBidding'), setEdited);
		setTimeout(function() { updateLots(processLotJSON); }, 500);
	}
);