var refreshPaused = 0;
makeOfferBarState = 'bnb-closed-makeOfferInactive';


function openMakeOfferDialog(lotId)
{
	$('#makeOfferDialog').show();
	var currencyOfLot = $('#currencyOfLot' + lotId).html();
	$('#makeOfferDialogLotTitle').html($('#lotTitle'+lotId).html());
	$('#makeOfferDialogLotId').val(lotId);

	var usersHighestOfferAmount = $('#makeOfferUsersHighestOfferAmount'+lotId).html();
	if (usersHighestOfferAmount !== null && usersHighestOfferAmount > 0)
	{
		$('#makeOfferDialogUsersHighestOfferAmount').html( currencyOfLot + usersHighestOfferAmount );
		$('#makeOfferDialogUsersHighestOfferStatus').html( '[' + $('#makeOfferUsersHighestOfferStatus'+lotId).html()  + ']');
	}
	else
	{
		$('#makeOfferDialogUsersHighestOfferAmount').html( 'No offers made' );
		$('#makeOfferDialogUsersHighestOfferStatus').html();
	}

	var highestBid = $('#highestBid' + lotId).html().replace(/,/g,"");
	if (highestBid !== null && highestBid > 0)
	{
	$('#makeOfferDialogHighestBidAmount').html(currencyOfLot + highestBid );
	$('#makeOfferDialogHighestBid').show();
	}
	
	$('#makeOfferDialog').dialog('open');
	
	var itemType = $('#lotsTable' + lotId).attr('data-itemType');
	if (itemType === "Listing" )
	{
		$('#currentBidLabel').hide();
		$('#makeOfferDialogHighestBid').hide();
	}

	makeOfferBarState = 'bnb-open-makeOfferButtons';
	showMakeOfferBarState(lotId);
	
	return false;  
}

function initMakeOfferDialog()
{
			$("#makeOfferDialog").dialog({
			modal: true,
			bgiframe: false,
			draggable: true,
			resizable: false,
			autoOpen: false,
			width: 500,
			buttons: {
				'Submit offer': function() {
					var lotId = $('#makeOfferDialogLotId').val();
					$('#makeOfferDialogError').html('');
					$('#makeOfferDialogResult').html('');
					var minimumAcceptableOffer = $('#makeOfferDialogMinimumAcceptableOffer').attr('checked');
					var makeOfferAmount = $('#makeOfferDialogBuyNowAmount').val().replace(/,/g,"");
					var highestBid = $('#highestBid' + lotId).html().replace(/,/g,"");

					if (makeOfferAmount === '' || makeOfferAmount === null) 
					{
						$('#makeOfferDialogError').html('Please enter an amount!');
					}
					else
					{
						var registrantId = $('#lotRegistrantId' + lotId).val();
						makeOfferBarState = 'bnb-open-makeOfferCancelled';
						$.ajax({
						    url: '/auction/makeBuyNowOffer.htm',
						    type: 'POST',
						    data: ({lotId:lotId, amount:makeOfferAmount, registrantId:registrantId}),
						    dataType: "json",	
						    success: function(lotState){
									makeOfferBarState = 'bnb-open-makeOfferOfferJustMade';
									updateMakeOfferBarInformation(lotState, true);
									if (lotState.errored === true)
									{
										$('#makeOfferDialogError').html(lotState.messagesDto.placeBuyNowOfferResultMessage);
									}
									else 
									{
							    		$('#makeOfferMessage' + lotState.id ).html(lotState.messagesDto.placeBuyNowOfferResultMessage);
							    		$('#makeOfferMessage' + lotState.id ).show();
							    		$('#makeOfferDialogResult').html(lotState.messagesDto.placeBuyNowOfferResultMessage);
									}
									
									
									if (lotState.sessionUserDto.highestOfferAmount !== null && lotState.sessionUserDto.highestOfferAmount > 0)
									{
										$('#makeOfferDialogUsersHighestOfferAmount').html(lotState.currencySymbol + lotState.sessionUserDto.highestOfferAmount);
										$('#makeOfferDialogUsersHighestOfferStatus').html( '[' + lotState.sessionUserDto.highestOfferStatus + ']');
									}
									else
									{
										$('#makeOfferDialogUsersHighestOfferAmount').html( 'No offers made' );
										$('#makeOfferDialogUsersHighestOfferStatus').html();
									}

									if (lotState.type === "Lot")
									{
										$('#makeOfferDialogHighestBidAmount').html(lotState.currencySymbol + lotState.highBidAmount );
									}
									else
									{
										$('#currentBidLabel').hide();
										$('#makeOfferDialogHighestBid').hide();
									}
						    },
						    error: function(){
						    	$('#makeOfferDialogError').html('Apologies but we are unable to accept your offer at the moment.');
						    	$('#makeOfferDialogResult').html('Apologies but we are unable to accept your offer at the moment.');
						    }
						});
					}
				},
				'Return to bidding': function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				clearMakeOfferDialog();
			}
		});		
}

function clearMakeOfferDialog()
{
	var lotId = $('#makeOfferDialogLotId').val();
	$('#makeOfferDialogError').html('');
	$('#makeOfferDialogResult').html('');
	$('#makeOfferDialogBuyNowAmount').val('');
	$('#makeOfferDialogLotId').val('');
	
	var usersHighestOfferAmount = $('#makeOfferUsersHighestOfferAmount'+lotId).html();
	makeOfferBarState = 'bnb-open-makeOfferOffersMade';
	if (usersHighestOfferAmount === null || usersHighestOfferAmount <= 0)
	{
		makeOfferBarState = 'bnb-closed-makeOfferActive';
	}
	showMakeOfferBarState(lotId);
	restartRefresh('makeOfferBarControlGroup',lotId);
}

function dialogFormSubmit()
{
	$('#makeOfferDialogError').html('Please press \'Return to bidding\' or press \'Submit offer\'');
	return false;
}

function pauseRefresh(bar, lotId)
{	
	refreshPaused = 1;
	$('#' + bar + lotId).css('display','none');
	$('#' + bar + 'Spinner' + lotId).css('display','inline');
}

function restartRefresh(bar, lotId)
{	
	$('#' + bar + lotId).css('display','block');
	$('#' + bar + 'Spinner' + lotId).css('display','none');
	refreshPaused = 0;
}

function getCurrentBidding(lotIds, registrantIds)
{	
	if (refreshPaused == 1)
	{
		return;
	}
	refreshPaused = 1;
	
	$.ajax({
	    url: '/auction/currentBidding.htm',
	    type: 'POST',
	    data: ({lotIds:lotIds, registrantIds:registrantIds}),
	    dataType: "json",	
	    success: function(lotStateData){
			for (lotStateIndex in lotStateData)
			{ 
				try 
				{
					var lotState = lotStateData[lotStateIndex];
					updateLotTableInformation(lotState);
					updateMakeOfferBarInformation(lotState, false);
				}
				catch (e) 
				{ 
					$('#globalBiddingError').html('Unable to get current bidding status.');
					//$('#globalBiddingError').css('display','inline');
				}
			}
			refreshPaused = 0;
	    },
	    error: function(){
	    	$('#globalBiddingError').html('Unable to get current bidding status.');
			//$('#globalBiddingError').css('display','inline');
			refreshPaused = 0;
	    }
	});
}

function watchLot(event) 
{	
	var lotId = event.data.lotId;
	var lotIsWatched = event.currentTarget.checked;
	$.ajax({
	    url: '/auction/watchLot.htm',
	    type: 'POST',
	    data: ({lotId:lotId, watchLot:lotIsWatched}),
	    dataType: "json",	
	    success: function(message){
			if (lotIsWatched){
		    	$('#watchLot' + lotId).html(' <b>watching this item</b>');
			}
			else{
		    	$('#watchLot' + lotId).html(' watch this item');
			}
	    },
	    error: function(){
	    	$('#globalBiddingError').html('Unable to watch the lot.');
			$('#globalBiddingError').css('display','inline');
	    }
	});
}

function placeBid(lotId)
{	
	pauseRefresh('biddingBarControlGroup',lotId);
	
	var amountIn = $('#placeBidAmount' + lotId).html().replace(/,/g,"");
	var registrantId = $('#lotRegistrantId' + lotId).val();
	$.ajax({
	    url: '/auction/placeBid.htm',
	    type: 'POST',
	    data: ({lotId:lotId, amount:amountIn, registrantId:registrantId}),
	    dataType: "json",	
	    success: function(lotState){
			try {
				updateLotTableInformation(lotState);
				$('#bidAcceptedMessage' + lotState.lotId).html(lotState.messagesDto.placeBidResultMessage);
				$('#bidAcceptedMessage' + lotState.lotId).css('display','inline');
				resetBidding(lotId);
				$('#bidButtons' + lotState.lotId).css('display','none');
			}
			catch (e) {
			}
			restartRefresh('biddingBarControlGroup',lotId);
	    },
	    error: function(){
	    	$('#globalBiddingError').html('Apologies but we are unable to take your bid at the moment.');
	    	$('#globalBiddingError').show();
			restartRefresh('biddingBarControlGroup',lotId);
	    }
	});
	return false;
}

function placeAutoBid(lotId) 
{	
	pauseRefresh('biddingBarControlGroup',lotId);

	var amountIn = $('#autoBidAmount' + lotId).val().replace(/,/g,"");
	if (amountIn === '' || amountIn === null)
	{
		$('#bidAcceptedMessage' + lotId).html('Please enter a value (higher then the current bid).');
		$('#bidAcceptedMessage' + lotId).css('display','inline');
		restartRefresh('biddingBarControlGroup',lotId);
		return;
	}
	
	var highestBid = $('#highestBid' + lotId).html().replace(/,/g,"");
	if (amountIn > 10*highestBid)
	{
		if ( confirm("You have placed an autobid of 10 times the highest bid. Please confirm your autobid of "+amountIn+" or press cancel.") !== true ) 
		{
			restartRefresh('biddingBarControlGroup',lotId);
			return;
		}
	}
	var registrantId = $('#lotRegistrantId' + lotId).val();


	$.ajax({
	    url: '/auction/placeAutoBid.htm',
	    type: 'POST',
	    data: ({lotId:lotId, amount:amountIn, registrantId:registrantId}),
	    dataType: "json",	
	    success: function(lotState){
				updateLotTableInformation(lotState);
				if (lotState.errored === true)
				{
					$('#bidAcceptedMessage' + lotState.lotId).html(lotState.messagesDto.placeAutoBidResultMessage);
					$('#bidAcceptedMessage' + lotState.lotId).css('display','inline');
					$('#bidAcceptedMessage' + lotState.lotId).addClass('biddingBarFieldError');
					$('#bidAcceptedMessage' + lotState.lotId).removeClass('biddingBarFieldResult');
				}
				else 
				{
					$('#bidAcceptedMessage' + lotState.lotId).html(lotState.messagesDto.placeAutoBidResultMessage);
					$('#bidAcceptedMessage' + lotState.lotId).css('display','inline');
					// remove any earlier error
					$('#bidAcceptedMessage' + lotState.lotId).removeClass('biddingBarFieldError');
					$('#bidAcceptedMessage' + lotState.lotId).addClass('biddingBarFieldResult');
					$('#biddingControlButtons' + lotId).show();
					$('#autoBidButtons' + lotState.lotId).css('display','none');
					$('#autoBidAmount' + lotState.lotId).val('');
				}
				restartRefresh('biddingBarControlGroup',lotId);
	    },
	    error: function(){
	    	$('#globalBiddingError').html('Apologies but we are unable to take your bid at the moment.');
	    	$('#globalBiddingError').show();
			restartRefresh('biddingBarControlGroup',lotId);
	    }
	});
	return false;
}

/**
 * Helper method for updating current bidding information.
 * @param {Object} lotState - a LotStateDTO object in JSON format.
 */
function updateLotTableInformation(lotState)
{ 
	var lotId = lotState.lotId;
	var sessionUser = lotState.sessionUserDto;

	$('#lotRegistrantId' + lotId).val(lotState.sessionUserDto.registrantId);
	$('#biddingStatusMessage'+lotId).html(lotState.messagesDto.biddingStatusMessage);
	$('#biddingStatusMessage' + lotId).css("color", "#000");
	
	if (lotState.biddingProhibited)
	{
		$('#biddingBar'+lotId).css('display','none');
		$('#biddingBarOpener'+lotId).css('display','none');
		$('#timeLeftMessage'+lotId).css('display','none');
		$('#bidIncrementMessage'+lotId).css('display','none');
		$('#sessionUserStatusContainer'+lotId).css('display','none');
		$('#highestBidMessage'+lotId).css('display','none');
		if (lotState.biddingClosed)
		{
			updateBiddingBarInformation(lotState);
			if (sessionUser.winning && !lotState.acceptedOfferOnLot)
			{
				$('#highestBid' + lotId).html(currencyFormatted(lotState.highBidAmount));
				$('#highestBidMessage'+lotId).css('display','inline');
			}
			else
			{
				$('#highestBidMessage'+lotId).css('display','none');
			}
		}
	}
	else
	{
		$('#sessionUserStatusContainer'+lotId).css('display','inline-block');
		if (lotState.overtimeBidding)
		{
			$('#biddingStatusMessage' + lotId).css("color", "red");
			$('#overtimeBiddingHelp' + lotId).css('display','inline');
		}
		else
		{
			$('#biddingStatusMessage' + lotId).css("color", "#000");
			$('#overtimeBiddingHelp' + lotId).css('display','none');
		}
			
		$('#highestBid' + lotId).html(currencyFormatted(lotState.highBidAmount));
		$('#highestBidMessage'+lotId).css('display','inline');
		$('#timeLeftMessage' + lotId).html(lotState.messagesDto.timeLeftMessage);
		$('#timeLeftMessage'+lotId).css('display','inline');
		$('#bidIncrementMessage' + lotId).css('display','inline');
		$('#increment' + lotId).html(currencyFormatted(lotState.increment));
		updateBiddingBarInformation(lotState);
	}

	// Now show bidding information after everything else has completed.
	$('.initialHide').show();
}

function updateBiddingBarInformation(lotState)
{
	var sessionUser = lotState.sessionUserDto;
	
	if (lotState.biddingProhibited)
	{
		if (lotState.biddingClosed)
		{
			if (sessionUser.highestBid > 0 || sessionUser.offererOnLot)
			{
				$('#biddingBar' + lotState.lotId).css('display', 'block');
				$('#biddingBarControlGroup' + lotState.lotId).css('display', 'none');
				updateBiddingBarWinningStatusInformation(lotState);
			}
		}
		return;
	}
	
	
	if (!sessionUser.loggedIn)
	{
		$('#notLoggedIn'+lotState.lotId).css('display','inline');
		$('#loggedIn'+lotState.lotId).css('display','none');
	}
	else
	{
		$('#notLoggedIn'+lotState.lotId).css('display','none');
		$('#loggedIn'+lotState.lotId).css('display','inline-block');
		if (sessionUser.registrationStatus == 'APPROVED')
		{
			$('#registerForAuction'+lotState.lotId).css('display','none');
			$('#registrantApprovedGroup'+lotState.lotId).css('display','inline');
			$('#registrantNotApprovedGroup'+lotState.lotId).css('display','none');
			$('#auctionRegistrantStatus').html('Approved to bid');
			$('#nextBidAmount' + lotState.lotId).html(currencyFormatted(lotState.highBidAmount + lotState.increment));
			//if (sessionUser.highestBid > 0 || sessionUser.highestAutoBid > 0)
			if (sessionUser.status > 0)
			{
				$('#biddingBar'+lotState.lotId).css('display','block');
				$('#biddingBarControlGroup' + lotState.lotId).css('display', 'block');
				$('#sessionUsersHighestBid' + lotState.lotId).html('Your highest bid: ' + lotState.currencySymbol + currencyFormatted(sessionUser.highestBid));
				$('#biddingBarOpener'+lotState.lotId).css('display','none');
				if (sessionUser.highestAutoBid > 0)
				{
					$('#sessionUsersHighestBid' + lotState.lotId).html('Your highest bid/autobid: ' + lotState.currencySymbol + (currencyFormatted(sessionUser.highestBid))  +  '/' + lotState.currencySymbol + (currencyFormatted(sessionUser.highestAutoBid)));
				}
			}
			else
			{
				if ($('#biddingBar'+lotState.lotId).css('display') == 'none')
				{
					$('#biddingBarOpener'+lotState.lotId).css('display','inline-block');
				}
			}
			if (!sessionUser.winning)
			{
				$('#bidAcceptedMessage' + lotState.lotId).html('');
			}
		}
		else
		{
			$('#biddingBar'+lotState.lotId).css('display','none');
			$('#registerForAuction'+lotState.lotId).css('display','none');
			$('#registrantApprovedGroup'+lotState.lotId).css('display','none');
			$('#registrantNotApprovedGroup'+lotState.lotId).css('display','inline-block');
			if (sessionUser.registrationStatus == 'PENDING')
			{
				$('#registrantNotApprovedGroup'+lotState.lotId).html('Approval pending');
				$('#auctionRegistrantStatus').html('Approval pending');
			}
			else if (sessionUser.registrationStatus == 'SUSPENDED')
			{
				$('#registrantNotApprovedGroup'+lotState.lotId).html('Approval suspended');
				$('#auctionRegistrantStatus').html('Approval suspended');
			}
			else if (sessionUser.registrationStatus == 'AWAITING_DEPOSIT')
			{
				$('#registrantNotApprovedGroup'+lotState.lotId).html('Awaiting Deposit');
				$('#auctionRegistrantStatus').html('Awaiting Deposit');
			}
			else
			{
				$('#registrantNotApprovedGroup'+lotState.lotId).css('display','none');
				$('#registerForAuction'+lotState.lotId).css('display','inline-block');
			}
		}
	}
	updateBiddingBarWinningStatusInformation(lotState);
}

function updateBiddingBarWinningStatusInformation(lotState)
{
	var lotId = lotState.lotId;
	var sessionUser = lotState.sessionUserDto;

	for (i=0; i<=13; i++)
	{
		if (i == sessionUser.status)
		{
			$('#BID_STATUS_' + i + '_'+lotId).css('display','inline');
			if (sessionUser.loggedIn)
			{
				if (lotState.biddingClosed) 
				{
					if (sessionUser.winning) 
					{
						// green: they are winning - either through bidding or they have an accepted offer.
						$('#bidStatusContainer' + lotId).css('background', '#bcda9d');
						$('#bidStatusContainer' + lotId).css('color', '#000000');
					}
					else if (sessionUser.highestBid > 0 || sessionUser.offererOnLot) 
					{
						// red: they are not winning but have placed (they may or may not have placed an offer)
						$('#bidStatusContainer' + lotId).css('background', '#bb3c2a');
						$('#bidStatusContainer' + lotId).css('color', 'white');
					}
					else {
						// white: they are not winning and haven't placed a bid....
					}
				}
				else
				{
					if (sessionUser.winning) 
					{
						// green: they are winning - either through bidding or they have an accepted offer.
						$('#bidStatusContainer' + lotId).css('background', '#bcda9d');
						$('#bidStatusContainer' + lotId).css('color', '#000000');
					}
					else if (sessionUser.highestBid > 0) 
					{
						// red: they are not winning but have placed (they may or may not have placed an offer)
						$('#bidStatusContainer' + lotId).css('background', '#bb3c2a');
						$('#bidStatusContainer' + lotId).css('color', 'white');
					}
					else {
						// white: they are not winning and haven't placed a bid....
					}
				}
			}
		}
		else
		{
			$('#BID_STATUS_' + i + '_'+lotId).css('display','none');
		}
	}
}

function updateMakeOfferBarInformation(lotState, calledFromMakeOffer) 
{
	var lotId = lotState.lotId;
	var sessionUser = lotState.sessionUserDto;
	
	if (lotState.biddingProhibited)
	{
		makeOfferBarState = 'bnb-closed-makeOfferProhibited';
	}
	else
	{
		if (sessionUser.loggedIn && sessionUser.registrationStatus == 'APPROVED')
		{
			$('#makeOfferMessage'+lotId).html(lotState.messagesDto.placeBuyNowOfferResultMessage);
			$('#makeOfferMessage'+lotId).css('display','inline');
	
			if (sessionUser.offererOnLot)
			{
				$('#makeOfferUsersHighestOfferAmount'+lotId).html(lotState.sessionUserDto.highestOfferAmount);
				$('#makeOfferUsersHighestOfferStatus'+lotId).html(lotState.sessionUserDto.highestOfferStatus);
				if (calledFromMakeOffer) 
				{
					makeOfferBarState = 'bnb-open-makeOfferOfferJustMade';
				}
				else
				{
					makeOfferBarState = 'bnb-open-makeOfferOffersMade';
				}
				if (sessionUser.offerLimitReached || sessionUser.offerAccepted || !lotState.makeOfferActive)
				{
					makeOfferBarState = 'bnb-open-makeOfferOffersExceeded';
				}
			}
			else
			{
				if (lotState.makeOfferActive)
				{
					makeOfferBarState = 'bnb-either-makeOfferActiveUnchanged';
				}
				else
				{
					makeOfferBarState = 'bnb-closed-makeOfferInactive';
				}
			}
		}
		else
		{
			makeOfferBarState = 'bnb-closed-makeOfferProhibited';
		}
	}
	showMakeOfferBarState(lotId);
	
	if (sessionUser.offererOnLot)
	{
		if (sessionUser.status == 12)
		{
			if (sessionUser.offerAccepted)
			{
				$('#acceptedOfferAmount' + lotState.lotId).html(currencyFormatted(sessionUser.acceptedOfferAmount));
				$('#acceptedOfferAmount'+lotState.lotId).css('display','inline');
			}
		}
	}
}

function openBiddingBar(lotId) 
{ 
	$('#biddingBar' + lotId).slideDown('slow');
	$('#biddingBarOpener' + lotId).fadeOut('slow');
	return false;
}

function openBidButtons(lotId)
{
	hideBidding(lotId);
	$('#bidButtons' + lotId).css('display','inline');
	$('#bidAcceptedMessage' + lotId).css('display','none');
	$('#placeBidAmount' + lotId).html($('#nextBidAmount' + lotId).html());
	return false;
}

function openAutoBidButtons(lotId)
{
	hideBidding(lotId);
	$('#autoBidButtons' + lotId).css('display','inline');
	$('#autoBidAmount' + lotId).val('');
	$('#autoBidAmount' + lotId).focus();
	$('#bidAcceptedMessage' + lotId).css('display','none');
	return false;
}

function cancelBid(lotId){
	$("#biddingControlButtons"+lotId).show();
	$('#bidButtons' + lotId).css('display','none');
	return false;
}

function cancelAutoBid(lotId)
{
	$("#biddingControlButtons"+lotId).show();
	$('#autoBidButtons' + lotId).css('display','none');
	return false;
}

   
function showMakeOfferBarState(lotId)
{
	if (this.makeOfferBarState == 'bnb-closed-makeOfferInactive')
	{
		$('#makeOfferBar' + lotId).hide();
		$('#makeOfferBarOpener' + lotId).hide();
	}
	else if (makeOfferBarState == 'bnb-closed-makeOfferActive')
	{
		$('#makeOfferBar' + lotId).hide();
		$('#makeOfferBarOpener' + lotId).show();
	}
	else if (makeOfferBarState == 'bnb-open-makeOfferButtons')
	{
		$('#makeOfferBar' + lotId).show();
		$('#makeOfferBarOpener' + lotId).hide();
		$('#makeOfferButtonsOpener' + lotId).hide();
	}
	else if (makeOfferBarState == 'bnb-open-makeOfferCancelled')
	{
		$('#makeOfferBar' + lotId).show();
		$('#makeOfferBarOpener' + lotId).hide();
		//$('#makeOfferButtonsOpener' + lotId).show();
		$('#makeOfferButtonsOpener'+lotId).css('display','inline-block');
	}
	else if (makeOfferBarState == 'bnb-open-makeOfferOffersMade')
	{
		$('#makeOfferBar'+lotId).show();
		$('#makeOfferBarOpener'+lotId).hide();
		if ($('#makeOfferButtonsOpener'+lotId).css('display') == 'none')
		{
			//$('#makeOfferButtonsOpener'+lotId).show();
			$('#makeOfferButtonsOpener'+lotId).css('display','inline-block');
		}
	}
	else if (makeOfferBarState == 'bnb-open-makeOfferOfferJustMade')
	{
		$('#makeOfferBar'+lotId).show();
		$('#makeOfferBarOpener'+lotId).hide();
		//$('#makeOfferButtonsOpener'+lotId).show();
		$('#makeOfferButtonsOpener'+lotId).css('display','inline-block');
	}
	else if (makeOfferBarState == 'bnb-open-makeOfferOffersExceeded')
	{
		$('#makeOfferBar'+lotId).show();
		$('#makeOfferBarOpener'+lotId).hide();
		$('#makeOfferButtonsOpener'+lotId).hide();
	}
	else if (makeOfferBarState == 'bnb-closed-makeOfferProhibited')
	{
		$('#makeOfferBar'+lotId).hide();
		$('#makeOfferBarOpener'+lotId).hide();
	}
	else if (makeOfferBarState == 'bnb-either-makeOfferActiveUnchanged')
	{
		if ($('#makeOfferBar'+lotId).css('display') == 'none')
		{
			$('#makeOfferBarOpener'+lotId).show();
		}
	}
}

function removeCurrencyErrMsg(lotId)
{
	$('#bidAcceptedMessage' + lotId).html('');
	$('#bidAcceptedMessage' + lotId).css('display','none');
	return false;
}

function resetBidding(lotId) 
{
	$("#biddingControlButtons"+lotId).show();
}

function hideBidding(lotId) 
{
	$("#biddingControlButtons"+lotId).hide();
}

$(document).ready(function ()
{
	setupLots();
});

function setupLots()
{
	// note that (typeof lotIdsOnPage === 'undefined') is the only thing that appears to stop IE complaining that lotIdsOnPage is undefined!
	if (typeof lotIdsOnPage === 'undefined'){return;}
	// null is not the same as undefined
	if (lotIdsOnPage === null){return;}
	if (lotIdsOnPage.length <= 0){return;}
	
	var lotIds = lotIdsOnPage.split(" ");
	for (lotIdIndex in lotIds)
	{
		var lotId = lotIds[lotIdIndex];
		
		$('#makeOfferBarOpener' + lotId).show();
		$('#biddingBarOpener'+lotId).css('display','inline-block');
	
		$('#biddingBarOpener'+lotId).click(function(){
			openBiddingBar($(this).attr('data-id'));
		});
	    $('#bidButtonsOpener'+lotId).click(function(){
	    	openBidButtons($(this).attr('data-id'));
		});
	    $('#cancelBid'+lotId).click(function(){
	    	cancelBid($(this).attr('data-id'));
		});
	    $('#confirmBid'+lotId).click(function(){
	    	placeBid($(this).attr('data-id'));
		});
	    $('#autoBidButtonsOpener'+lotId).click(function(){
	    	openAutoBidButtons($(this).attr('data-id'));
		});
	    $('#autoBidAmount'+lotId).click(function(){
	    	removeCurrencyErrMsg($(this).attr('data-id'));
		});
	    $('#confirmAutoBid'+lotId).click(function(){
	    	placeAutoBid($(this).attr('data-id'));
		});
	    $('#cancelAutoBid'+lotId).click(function(){
	    	cancelAutoBid($(this).attr('data-id'));
		});
	    $('#makeOfferBarOpener'+lotId).click(function(){
	    	openMakeOfferDialog($(this).attr('data-id'));
		});
	    $('#makeOfferButtonsOpener'+lotId).click(function(){
	    	openMakeOfferDialog($(this).attr('data-id'));
		});
	    
	    // TODO DRW this is not so simple...
//			    $('#watchingLot'+lotId).click(function(){
//			    	watchLot($(this).attr('data-id'));
//				});

	    $('#watchingLot'+lotId).bind("click", {lotId: lotId}, watchLot);
	    
	}
	
	getCurrentBidding(lotIdsOnPage,registrantIdsOnPage);
	setInterval ("getCurrentBidding(lotIdsOnPage,registrantIdsOnPage)",  timeInterval);
	
	// can't make an offer without lots so including this in setupLots()
	$('#makeOfferDialogAbout').click(function() {
    	$('#makeOfferDialogAboutText').slideToggle();
    });
	initMakeOfferDialog();
}
