
$(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 = '';
            var statusText = '';
            if ( lot.belowReserve && lot.winning ) {
                status = 'belowReserve';
                statusText = 'Below Reserve';
            }
            else if ( lot.winning && lot.biddingOpen ) {
                status = 'winning';
                statusText = 'You are winning';
            }
            else if ( lot.losing && lot.biddingOpen ) {
                status = 'losing';
                statusText = 'You are losing';
            }
            else if ( lot.winning && !lot.biddingOpen ) {
                status = 'won';
                statusText = 'You won';
            }
            else if ( lot.losing && !lot.biddingOpen ) {
                status = 'lost';
                statusText = 'You lost';
            }
            for ( var i = 0; i < statusClasses.length; i++ ) {
                var statusi = statusClasses[i];
                if ( statusi != status ) {
                    biddingStatus.removeClass(statusi);
                }
            }
            if ( status ) {
                biddingStatus.addClass(status);
            }
            biddingStatus.text(statusText);
        }
        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));
                    if ( days > 0 ) {
                        return days+"d "+hours+"h";
                    }
                    else if ( hours > 0 ) {
                        return hours+"h "+minutes+"m";
                    }
                    else if ( minutes > 0 ) {
                        return minutes+"m "+seconds+"s";
                    }
                    else {
                        return seconds+"s";
                    }
                }
                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('div.itemBidding form.bidForm tr.endDate span.remaining').html(" bidding 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);
    }
);