
ThomasAuctionObj.Lot = function(id, parent) {
    var _id;
    var _lotDBID = id;
    var _el;
    var _imageEl;
    var _titleEl;
    var _optionsEl;
    var _timeRemainingEl;
    var _extendedEl;
    var _highBidderSpanEl;
    var _bidButtonEl;
    var _bidButtonTextEl;
    var _numberOfBidsEl;
    var _currentBidEl;
    var _currentBidLabelSpanEl;
    var _bidCellEl;
    var _reservePriceEl;
    var _bidButtonDivEl;
    var _maxBidEl;

    var _parent = parent;

    var _auctionID;
    var _numberOfBids;
    var _lastUpdateTime;
    var _lotStartDate;
    var _lotEndDate;
    var _currentBid;
    var _timeRemaining;
    var _isExtended;
    var _title;
    var _description;
    var _lotNumber;
    var _nextBidPrice;
    var _originalNextBidPrice;
    var _maxBidPrice;
    var _bidPrice;
    var _reservePrice
    var _displayNextBidPrice;
    var _dateCreated;
    var _icon;
    var _currentLinePosition;
    var _currentLine;
    var _bidUserID;
    var _highBidderMessage;
    var _currencyType;

    this.setEl = function(el) { _el = el; };
    this.getEl = function() { return _el; };

    this.setImageEl = function(el) { _imageEl = el; };
    this.getImageEl = function() { return _imageEl; };

    this.setTitleEl = function(el) { _titleEl = el; };
    this.getTitleEl = function() { return _titleEl; };

    this.setBidCellEl = function(el) { _bidCellEl = el; };
    this.getBidCellEl = function() { return _bidCellEl; };

    this.setID = function(id) { _id = id; };
    this.getID = function() { return _id; };

    this.setLotDBID = function(id) { _lotDBID = id; };
    this.getLotDBID = function() { return _lotDBID; };

    this.setParent = function(parent) { _parent = parent; };
    this.getParent = function() { return _parent; };

    this.setTimeRemainingEl = function(el) { _timeRemainingEl = el; };
    this.getTimeRemainingEl = function() { return _timeRemainingEl; };

    this.setExtendedEl = function(el) { _extendedEl = el; };
    this.getExtendedEl = function() { return _extendedEl; };

    this.setMaxBidEl = function(el) { _maxBidEl = el; };
    this.getMaxBidEl = function() { return _maxBidEl; };

    this.setReservePriceEl = function(el) { _reservePriceEl = el; };
    this.getReservePriceEl = function() { return _reservePriceEl; };

    this.setCurrentBidEl = function(el) { _currentBidEl = el; };
    this.getCurrentBidEl = function() { return _currentBidEl; };

    this.setCurrentBidLabelSpanEl = function(el) { _currentBidLabelSpanEl = el; };
    this.getCurrentBidLabelSpanEl = function() { return _currentBidLabelSpanEl; };

    this.setNumberOfBidsEl = function(el) { _numberOfBidsEl = el; };
    this.getNumberOfBidsEl = function() { return _numberOfBidsEl; };

    this.setBidButtonTextEl = function(el) { _bidButtonTextEl = el; };
    this.getBidButtonTextEl = function() { return _bidButtonTextEl; };

    this.setBidButtonEl = function(el) { _bidButtonEl = el; };
    this.getBidButtonEl = function() { return _bidButtonEl; };

    this.setBidButtonDivEl = function(el) { _bidButtonDivEl = el; };
    this.getBidButtonDivEl = function() { return _bidButtonDivEl; };

    this.setHighBidderSpanEl = function(el) { _highBidderSpanEl = el; };
    this.getHighBidderSpanEl = function() { return _highBidderSpanEl; };

}

ThomasAuctionObj.Lot.prototype = {
    Parent: function() { return this.getParent(); },
    render: function() {
        if (this.getParent()) var parentLotContainer = LotContainer_Instances[this.getParent()];

        this.setID(lotIndex);

        var ntable = d.createElement("table");
        ntable.setAttribute("id", "lotTable" + this.getID());
        ntable.setAttribute("lotID", this.getID());
        ntable.setAttribute("lotDBID", this.getLotDBID());
        ntable.setAttribute("auctionID", this.getAuctionID());
        ntable.className = "lotTable";

        var titleRow = ntable.insertRow(0);

        var titleCell = titleRow.insertCell(0);
        titleCell.className = "lotTitle";
        titleCell.setAttribute("id", "lotTitle" + this.getID());
        titleCell.setAttribute("lotDBID", this.getLotDBID());
        titleCell.setAttribute("auctionID", this.getAuctionID());
        titleCell.colSpan = "7";
        titleCell.innerHTML = "";

        var nr = ntable.insertRow(1);

        var imageCell = nr.insertCell(0);
        var timeRemainingCell = nr.insertCell(1);
        var currentBidCell = nr.insertCell(2);
        var numberOfBidsCell = nr.insertCell(3);
        var bidStatusCell = nr.insertCell(4);
        var maxBidCell = nr.insertCell(5);
        var bidCell = nr.insertCell(6);

        //Image Cell
        var ni = d.createElement("img");
        ni.setAttribute("id", "lotImage" + this.getID());
        ni.setAttribute("lotDBID", this.getLotDBID());
        ni.setAttribute("auctionID", this.getAuctionID());
        ni.className = "lotImage";
        ni.style.visibility = 'hidden';

        imageCell.className = "lotCell";
        imageCell.style.width = "90px";
        imageCell.appendChild(ni);

        //Time Remaining Cell
        timeRemainingCell.className = "lotCell borderRight";
        timeRemainingCell.style.width = "150px";
        timeRemainingCell.style.textAlign = "left";
        timeRemainingCell.innerHTML = "Time Remaining:";

        var timeRemainingSpan = d.createElement("span");
        timeRemainingSpan.setAttribute("id", "timeRemaining" + this.getID());
        timeRemainingSpan.innerHTML = "";

        var extendedSpan = d.createElement("span");
        extendedSpan.setAttribute("id", "extended" + this.getID());
        extendedSpan.innerHTML = ""; //(Extended)

        timeRemainingCell.appendChild(d.createElement("br"));
        timeRemainingCell.appendChild(timeRemainingSpan);
        //timeRemainingCell.appendChild(d.createElement("br"));
        timeRemainingCell.appendChild(extendedSpan);

        //Current Bid Cell
        currentBidCell.className = "lotCell borderRight";
        currentBidCell.style.width = "110px";

        var currentBidLabelSpan = d.createElement("span");
        currentBidLabelSpan.innerHTML = "Current Bid:";

        var currentBidSpan = d.createElement("span");
        currentBidSpan.setAttribute("id", "currentBid" + this.getID());
        currentBidSpan.innerHTML = "";



        currentBidCell.appendChild(currentBidLabelSpan);
        currentBidCell.appendChild(d.createElement("br"));
        currentBidCell.appendChild(currentBidSpan);
        
    

        // # of bids cell
        numberOfBidsCell.className = "lotCell borderRight";
        numberOfBidsCell.style.width = "110px";
        
        
        var numberOfBidsSpan = d.createElement("span");
        numberOfBidsSpan.setAttribute("id", "numberOfBids" + this.getID());
        numberOfBidsSpan.innerHTML = "";

        var numberOfBidsLabel = d.createElement("span");
        numberOfBidsLabel.innerHTML = "# bids: ";
        
        numberOfBidsCell.appendChild(numberOfBidsLabel);
        numberOfBidsCell.appendChild(d.createElement("br"));
        numberOfBidsCell.appendChild(numberOfBidsSpan);
        
        // bid status cell
        
        bidStatusCell.className = "lotCell borderRight";
        bidStatusCell.style.width = "150px";
        
        var bidStatusLabel = d.createElement("span");
        bidStatusLabel.innerHTML = "Bid Status: ";
        
        var highBidderSpan = d.createElement("span");
        highBidderSpan.className = "nothighBidder";
        highBidderSpan.innerHTML = "You're not the high bidder"; //You're the current high bidder
        
        bidStatusCell.appendChild(bidStatusLabel);
        bidStatusCell.appendChild(d.createElement("br"));
        bidStatusCell.appendChild(highBidderSpan);
        
        // max bid cell
        
        maxBidCell.className = "lotCell borderRight";
        maxBidCell.style.width = "150px";
        maxBidCell.innerHTML ="";
        
        var maxBidSpan = d.createElement("span");
        maxBidSpan.className = "highBidder";
        maxBidSpan.innerHTML = ""; //User's Max Bid
        
        //bidCell.appendChild(d.createElement("br"));
        maxBidCell.appendChild(maxBidSpan);
        
        //Bid Cell
        bidCell.className = "lotCell";
        //bidCell.style.width = "10px";
        bidCell.style.textAlign = "center";
        bidCell.innerHTML = "";

        var bidButtonDiv = d.createElement("div");
        bidButtonDiv.className = "buttonwrapper";
        bidButtonDiv.style.textAlign = "center";
        bidButtonDiv.setAttribute("style", "visiblity: visible;");

        var bidButton = d.createElement("a");
        bidButton.className = "ovalbutton";
        bidButton.href = "javascript:confirmBid('" + this.getLotDBID() + "','" + this.getNextBidPrice() + "');";
        var bidButtonText = d.createElement("span");

        bidButton.appendChild(bidButtonText)
        bidButtonDiv.appendChild(bidButton)



        var reserveText = d.createElement("span");
        reserveText.innerHTML = "";
        reserveText.className = "reserveText";



        bidCell.appendChild(bidButtonDiv);
        //bidCell.appendChild(d.createElement("br"));
        bidCell.appendChild(reserveText);




        //ntable.style.top = lotLinePosition * 40 + 2 + "px";

        if (this.getParent()) {

            var position1 = (parentLotContainer.width - (450 * 2) - 30) / 2;
            var position2 = position1 + 450 + 30; ;

            if (lotLinePosition == 2) {
                lotLinePosition = 0;
                lotContainerLine++;
                totalLotRows++;
            }

           // if (lotLinePosition == 0) ntable.style.left = "35px";
           // if (lotLinePosition == 1) ntable.style.left = "35px";

           // ntable.style.top = lotContainerLine * 110 + 2 + "px";

            this.setCurrentLinePosition(lotLinePosition);
            this.setCurrentLine(lotContainerLine);

            Lot_Rows[lotContainerLine][lotLinePosition] = this.getID();

            /*
            if ((450 * lotLinePosition) + 20 > parentLotContainer.width) {
            lotLinePosition = 0;
            lotContainerLine++;
            }

            if (lotLinePosition == 0) {
            ntable.style.left = 0 + "px";
            }
            else {
            ntable.style.left = (450 * lotLinePosition) + 30 + "px";
            }

            ntable.style.top = lotContainerLine * 90 + 2 + "px";
            */

        }

        this.setTitleEl(titleCell);
        this.setEl(ntable);
        this.setImageEl(ni);
        this.setTimeRemainingEl(timeRemainingSpan);
        this.setCurrentBidEl(currentBidSpan);
        this.setCurrentBidLabelSpanEl(currentBidLabelSpan);
        this.setNumberOfBidsEl(numberOfBidsSpan);
        this.setBidButtonTextEl(bidButtonText);
        this.setBidButtonEl(bidButton);
        this.setHighBidderSpanEl(highBidderSpan);
        this.setExtendedEl(extendedSpan);
        this.setMaxBidEl(maxBidSpan);
        this.setBidCellEl(bidCell);
        this.setReservePriceEl(reserveText);
        this.setLastUpdateTime(new Date());
        this.setBidButtonDivEl(bidButtonDiv);

        ntable.onmouseover = function() { highlightLot(this, 'on'); };
        ntable.onmouseout = function() { highlightLot(this, 'off'); };
        ni.onclick = function() { handleRedirect('AuctionLotDetails.aspx?AID=' + this.auctionID + '&LID=' + this.lotDBID); };
        titleCell.onclick = function() { handleRedirect('AuctionLotDetails.aspx?AID=' + this.auctionID + '&LID=' + this.lotDBID); };

        if (debug) d.getElementById('debug').innerHTML += this.getID() + "<br>";
        if (debug) d.getElementById('debug').innerHTML += fc.innerHTML;

        lotLinePosition++;
        lotCount++;
        lotIndex++;

        return ntable;
    },
    setTitle: function(text) {
        if (text.length > 52) text = text.substr(0, 52) + "...";
        this._title = text;
        $(this.getTitleEl()).innerHTML = this._title;
    },
    getTitle: function() { return this._title; },
    setImage: function(image) {
        if (image) {
            $(this.getImageEl()).setAttribute("src", image);
            $(this.getImageEl()).style.visibility = 'visible';
        }
    },
    getLotCount: function() { return this._lotCount; },
    setLotCount: function(count) { this._lotCount = count; },
    setAuctionID: function(text) {
        this._auctionID = text;
        $(this.getImageEl()).setAttribute("auctionID", text);
        $(this.getTitleEl()).setAttribute("auctionID", text);
    },
    getAuctionID: function() { return this._auctionID; },
    setDescription: function(text) { this._description = text; },
    getDescription: function() { return this._description; },
    setLotNumber: function(text) { this._lotNumber = text; },
    getLotNumber: function() { return this._lotNumber; },
    setReservePrice: function(text) {
        this._reservePrice = text;
        if (parseInt(this._reservePrice) > parseInt(this._bidPrice)) {
            $(this.getReservePriceEl()).innerHTML = "Reserve Not Met";
        }
        else {
            $(this.getReservePriceEl()).innerHTML = "";
        }
    },
    getReservePrice: function() { return this._reservePrice; },
    setLotStartDate: function(date) { this._lotStartDate = date; },
    getLotStartDate: function() { return this._lotStartDate; },
    setLotEndDate: function(date) { this._lotEndDate = date; },
    getLotEndDate: function() { return this._lotEndDate; },
    setBidPrice: function(text) {
        this._bidPrice = text;
        if (parseInt(text) == 0) {
            $(this.getCurrentBidLabelSpanEl()).innerHTML = 'Opening Bid:';
        }
        else {
            $(this.getCurrentBidLabelSpanEl()).innerHTML = 'Current Bid:';
        }
    },
    getBidPrice: function() { return this._bidPrice; },
    setNextBidPrice: function(text) {
        this._nextBidPrice = text;
        $(this.getBidButtonEl()).href = "javascript:confirmBid('" + this.getLotDBID() + "','" + text + "');";
    },
    getNextBidPrice: function() { return this._nextBidPrice; },
    setOriginalNextBidPrice: function(text) {
        this._originalNextBidPrice = text;
    },
    getOriginalNextBidPrice: function() { return this._originalNextBidPrice; },
    setLastUpdateTime: function(date) { this._lastUpdateTime = date; },
    getLastUpdateTime: function() { return this._lastUpdateTime; },
    
    getMaxBidPrice: function() { return this._maxBidPrice; },
    setMaxBidPrice: function(text, text2) {
      
        if (text == "" || text == undefined) {
            this._maxBidPrice = text;
           
            $(this.getMaxBidEl()).innerHTML = '';
            $(this.getMaxBidEl()).style.visibility = 'hidden';
         } else if (parseInt(text) < parseInt(text2)) {
            this._maxBidPrice = text;
     
            $(this.getMaxBidEl()).innerHTML = '';
            $(this.getMaxBidEl()).style.visibility = 'hidden';
        } else if (parseInt(this._maxBidPrice) < parseInt(this._bidPrice)) {
            this._maxBidPrice = text;
            $(this.getMaxBidEl()).innerHTML = '';
            $(this.getMaxBidEl()).style.visibility = 'hidden';
       
        } else {
            this._maxBidPrice = text;
            $(this.getMaxBidEl()).innerHTML = 'Your Max Bids Is $' + text;
            $(this.getMaxBidEl()).style.visibility = 'visible';
        }
    },
    
    
    setExtended: function(text) {
        if (text == "") {
            this._isExtended = false;
            $(this.getExtendedEl()).innerHTML = '';
            $(this.getExtendedEl()).style.visibility = 'hidden';
        }
        else {
            this._isExtended = true;
            $(this.getExtendedEl()).innerHTML = '&nbsp; Extended';
            $(this.getExtendedEl()).style.fontWeight = 'Bold';
            $(this.getExtendedEl()).style.visibility = 'visible';
        }
    },
    getExtended: function() { return this._isExtended; },
    setIcon: function(text) {
        if (text == "") {
            this._icon = text;
            $(this.getImageEl()).src = "";
            $(this.getImageEl()).style.visibility = 'hidden';
        }
        else {
            this._icon = text;
            $(this.getImageEl()).src = "/handlers/AuctionLotPhotoHandler.ashx?mw=60&mh=60&filename=" + this.getIcon() + "&auctionID=" + this.getAuctionID();
            $(this.getImageEl()).style.visibility = 'visible';
        }
    },
    getIcon: function() { return this._icon; },
    setTimeRemaining: function(text) {
        this._timeRemaining = text;
        $(this.getTimeRemainingEl()).innerHTML = this._timeRemaining;
    },
    getTimeRemaining: function() { return this._timeRemaining; },
    setNumberOfBids: function(text) {
        this._numberOfBids = text;
        $(this.getNumberOfBidsEl()).innerHTML = this._numberOfBids;
    },
    getNumberOfBids: function() { return this._numberOfBids; },
    setBidButton: function(text) {
        this._displayNextBidPrice = text;
        $(this.getBidButtonTextEl()).innerHTML = "Bid " + this._displayNextBidPrice + ' (' + this.getCurrencyType() + ')';
    },
    getBidButton: function() { return this._nextBidPrice; },
    setCurrentBid: function(text) {
        this._currentBid = text;
        if (parseInt(this._bidPrice) == 0) {
            $(this.getCurrentBidEl()).innerHTML = this._displayNextBidPrice + ' (' + this.getCurrencyType() + ')';
        }
        else {
            $(this.getCurrentBidEl()).innerHTML = this._currentBid + ' (' + this.getCurrencyType() + ')';
        }
        //this._currentBid = text;
        //$(this.getCurrentBidEl()).innerHTML = this._currentBid;
    },
    getCurrentBid: function() { return this._currentBid; },
    disableBidding: function() {
        $(this.getBidButtonDivEl()).style.visibility = 'hidden';
    },
    enableBidding: function() {
        $(this.getBidButtonDivEl()).style.visibility = 'visible';
    },
    disable: function() {
        $(this.getBidCellEl()).style.visibility = 'hidden';
    },
    enable: function() {
        $(this.getBidCellEl()).style.visibility = 'visible';
    },
    setCurrentLinePosition: function(text) {
        this._currentLinePosition = text;
    },
    getCurrentLinePosition: function() {
        return this._currentLinePosition;
    },
    setCurrentLine: function(text) {
        this._currentLine = text;
    },
    getCurrentLine: function() {
        return this._currentLine;
    },
    setBidUserID: function(text) {
        this._bidUserID = text;
    },
    getBidUserID: function() {
        return this._bidUserID;
    },
    setHighBidderMessage: function(text) {
        if (text == "HighBidder") {
            this._highBidderMessage = "You're the high bidder";
            $(this.getHighBidderSpanEl()).innerHTML = this._highBidderMessage;
            $(this.getHighBidderSpanEl()).className = "highBidder";
            //this.disableBidding();
            this.enableBidding();
        }
        if (text == "Outbid") {
            this._highBidderMessage = "You've been outbid";
            $(this.getHighBidderSpanEl()).innerHTML = this._highBidderMessage;
            $(this.getHighBidderSpanEl()).className = "outbid";
            this.enableBidding();
        }
        if (text == "") {
            this._highBidderMessage = "";
            this.enableBidding();
        }
    },
    getHighBidderMessage: function() {
        return this._highBidderMessage;
    },
    setCurrencyType: function(text) {
        this._currencyType = text;
    },
    getCurrencyType: function() {
        return this._currencyType;
    }
}

function highlightLot(el,state) {
	if(state=='on') {
		//Lot_Instances[el.getAttribute("lotID")].getImageEl().setAttribute("title",Lot_Instances[el.getAttribute("LotID")].getTitle() + " :: " + Lot_Instances[el.getAttribute("LotID")].getDescription());
	    //el.setAttribute("origbgcolor",el.style.backgroundColor);
	    //el.style.backgroundColor = '#f4f6d1';
	    el.setAttribute("origbordercolor",el.style.borderColor);
	    el.style.borderColor = '#aeda94';	
	}
	if(state=='off') {
		//el.style.backgroundColor=el.getAttribute("origbgcolor");
	    el.style.borderColor = el.getAttribute("origbordercolor");
    }
}

var Lot_Instances=[]; //Collection to store Lot Instances
var Lot_Instances_Index = []; //Handles reference between Lot Instance and database Lot ID

