
var ThomasAuctionObj = {};
var d = document;
var wrapper;
var debug=false;

var xmlWebConsumer;

//========================================

var activeLotContainer;
var lotContainerWidth = 200;
var lotContainerHeight = 120;
var lotContainerLine = 0;
var lotLinePosition = 0;
var lotCount = 1;
var lotIndex = 1;
var activeLot;
var Lot_Rows = multiDimensionalArray(20, 20);
var totalLotRows = 1;

//========================================

function setWrapper(el) {
    wrapper = $(el);
}

//========================================

function setActiveLotContainer(id) {
	if(LotContainer_Instances[id]) activeLotContainer=id;
}

function activateLotContainer(id) {
	if(!id && !activeLotContainer) return false;
	if(!id) id=activeLotContainer;
	showLotContainer(id);
	hideLogContainer();
	var hc = (FolderContainer_Instances[Folder_Instances[activeFolder].getParent()].getHeaderContainer());
	hc.setMenu("<br><a class='headerMenu' id='addNewLot' href='#'>Add New Lot</a> | <a class='headerMenu' id='viewLog' onclick='activateLogContainer()' href='#'>View Logs</a>");
	attachFolderMenuEvents();
}

function addLot(lotID, title, bidPrice, numberOfBids, icon, parentContainerID) {
	if(!parentContainerID && !activeLotContainer) return false;
	if(!parentContainerID) parentContainerID = activeLotContainer;
	var tmpD = new ThomasAuctionObj.Lot(lotID, parentContainerID);
	if (icon != undefined) tmpD.setIcon(icon);
	$(LotContainer_Instances[parentContainerID].getEl()).appendChild(tmpD.render());
	if (title != undefined) tmpD.setTitle(title);
	//if (description) tmpD.setDescription(description);
	if (bidPrice != undefined) tmpD.setCurrentBid(bidPrice);
	if (numberOfBids != undefined) tmpD.setNumberOfBids(numberOfBids);
	//if(dateCreated) tmpD.setDateCreated(dateCreated);
	Lot_Instances[tmpD.getID()] = tmpD;
	Lot_Instances_Index[tmpD.getLotDBID()] = tmpD.getID();
	return tmpD;
	tmpD = null;
}

function createLotContainer(id, w, h, l, t, wr) {
	if(!w) w=lotContainerWidth;
	if(!h) h=lotContainerHeight;
	if(!wr) wr=wrapper;
	if (wr && id) {
	    var tmpDC = new ThomasAuctionObj.LotContainer(id, w, h, l, t);
		wr.appendChild(tmpDC.render());
		LotContainer_Instances[tmpDC.id]=tmpDC;
		setActiveLotContainer(id);
		return tmpDC;
		tmpDC = null;
	}
}

function showLotContainer(id) {
	if(!id && !activeLotContainer) return false;
	if(!id) id=activeLotContainer;
	LotContainer_Instances[id].show();
}

function hideLotContainer(id) {
	if(!id && !activeLotContainer) return false;
	if(!id) id=activeLotContainer;
	LotContainer_Instances[id].hide();
}

function removeLot(id, dbID) {
	if(!id) return false;
	if (!Lot_Instances[id]) return false;
	Lot_Rows[Lot_Instances[id].getCurrentLine()][Lot_Instances[id].getCurrentLinePosition()] = -1;
	$(LotContainer_Instances[Lot_Instances[id].getParent()].getEl()).removeChild(Lot_Instances[id].getEl());
	Lot_Instances[id]=null;
	Lot_Instances_Index[dbID] = null;
	lotCount--;
}

function removeAllLots(parentContainerID) {
	if(!parentContainerID && !activeLotContainer) return false;
	if(!parentContainerID) parentContainerID = activeLotContainer;
	
	var tmpD;
	
	for(var i=0; i<Lot_Instances.length; i++ ) {
		tmpD = Lot_Instances[i+1];
		if(tmpD) {
			if(tmpD.getParent()==parentContainerID) removeLot(i+1);		
		}
	}

	lotContainerLine = 0;
	lotLinePosition = 0;
	lotCount = 1;
	lotIndex = 1;
}

function repositionLots(id) {

    //Debug
    writeDebug("repositionLots: Total lot rows: " + totalLotRows);
    
    var deletedRows = [];

    //Iterate among rows and find blank rows
    for (var i = 0; i < totalLotRows; i++) {

        if (Lot_Rows[i][0] == -1 && Lot_Rows[i][1] == -1) {
            deletedRows[deletedRows.length] = i;
            
        }
        if (Lot_Rows[i][0] == -1 || Lot_Rows[i][1] == -1) {
            //if(Lot_Rows[i, 0] == null) alert("Left slot blank");
            //if(Lot_Rows[i, 1] == null) alert("Right slot blank");
        }
    }

    //Debug
    writeDebug("repositionLots: Total deleted rows: " + deletedRows.length);
       
    for (var i = 0; i < deletedRows.length; i++) {
        
        var currentRow = deletedRows[i];
    
        //if (currentRow > 0) {
        
            for (var x = currentRow + 1; x <= totalLotRows; x++) {

                Lot_Rows[x - 1][0] = Lot_Rows[x][0];
                Lot_Rows[x - 1][1] = Lot_Rows[x][1];

                //Adjust lot positions
                var currentLot;

                //Left
                currentLot = Lot_Instances[Lot_Rows[x][0]];

                if (currentLot != null) {
                    currentLot.getEl().style.top = (x - 1) * 110 + 2 + "px";
                    currentLot.setCurrentLine(x - 1);
                }
                
                //Right
                currentLot = Lot_Instances[Lot_Rows[x][1]];

                if (currentLot != null) {
                    currentLot.getEl().style.top = (x -1)* 110 + 2 + "px";
                    currentLot.setCurrentLine(x - 1);
                }

            }

            Lot_Rows[x][0] = null;
            Lot_Rows[x][1] = null;
            
        //}

        totalLotRows--;
        lotContainerLine--;

    }

    //Debug
    writeDebug("repositionLots: Total lot rows (after reposition): " + totalLotRows);
    writeDebug("repositionLots: Lot Container Line (after reposition): " + lotContainerLine);

    deletedRows.length = 0;
     
    //Set height of lot container
    //LotContainer_Instances[activeLotContainer].getEl().style.height = (lotContainerLine + 1) * 170 + 2 + "px";

}

/*

function repositionLots(id) {

    var position1 = (LotContainer_Instances[id].width - (450 * 2) - 30) / 2;
    var position2 = position1 + 450 + 30; ;

    lotContainerLine = 0;
    lotLinePosition = 0;

    for (var i = 0; i < lotIndex; i++) {
        var x = i + 1;

        if (Lot_Instances[x] != null) {

            if (lotLinePosition == 2) {
                lotLinePosition = 0;
                lotContainerLine++;
            }

            if (lotLinePosition == 0) Lot_Instances[x].getEl().style.left = position1 + "px";
            if (lotLinePosition == 1) Lot_Instances[x].getEl().style.left = position2 + "px";

            Lot_Instances[x].getEl().style.top = lotContainerLine * 90 + 2 + "px";

            lotLinePosition++;

        }

    }

    //Set height of lot container
    LotContainer_Instances[activeLotContainer].getEl().style.height = (lotContainerLine + 1) * 90 + 2 + "px";

}

*/

function multiDimensionalArray(iRows, iCols) {
    var i;
    var j;
    var a = new Array(iRows);
    for (i = 0; i < iRows; i++) {
        a[i] = new Array(iCols);
        for (j = 0; j < iCols; j++) {
            a[i][j] = "";
        }
    }
    return (a);
} 
