// fActionPosition is a float that indicates how many screens away from the bottom before sAction is executed
var ScrollDetectorInterval = 0;
function ScrollDetector(fActionPosition, sAction)
{
    ScrollDetectorInterval = setInterval("ScrollDetectorPolling(" 
                           + fActionPosition + ", \"" + sAction + "\")", 20);
}

function ScrollDetectorPolling(fActionPosition, sAction)
{
    var nPos = GetScrollTop();
    var nRemaining = GetDocumentHeight() - nPos;
    var fRemainingScreens = nRemaining / screen.height;
    
    if (fRemainingScreens < fActionPosition)
        eval(sAction);
}

function GetScrollTop()
{
    if (navigator.appName == "Microsoft Internet Explorer")
    {
        if (document.documentElement)   // IE6 + DTD 4.01 but no scrolling going on
            return document.documentElement.scrollTop;
        else if (document.body)         // IE5 or DTD 3.2
            return document.body.scrollTop;
    }
    else    // Firefox
        return window.pageYOffset;
}

function GetDocumentHeight()
{
    return document.body.scrollHeight;
}

var nLoadImagePage  = 1;
var bLoading        = false;
var bDone           = false;
function LoadImages(sBaseURL, sTableID, sPagingID, nPPP, bEventTracking)
{
    if (bDone)
        return;
    if (bLoading)
        return;

    if (sPagingID != "" && nLoadImagePage == 1)
    {
        var oPaging = document.getElementById(sPagingID);
        if (oPaging != null)
            oPaging.style.display = "none";        
    }
        
    bLoading = true;    // Race condition!
    nLoadImagePage++;

    // Compute URL    
    var sURL = sBaseURL;
    if (sURL.indexOf('?') == -1)
        sURL = sURL + '?';
    else
        sURL = sURL + '&';
    sURL = sURL + 'curpage=' + nLoadImagePage;
    
    // First create placeholder elements
    // Create table elements, insert them at end of table
    var oTable  = document.getElementById(sTableID);
    var nRows   = oTable.rows.length;
    var nCols   = oTable.rows[nRows - 1].cells.length;
    var sWidth  = '25%';
    var nSize   = 0;
    if (nCols == 3)
    {
        sWidth  = '248';
        nSize   = 1;
    }
    
    var nNewRows = Math.ceil(nPPP / nCols);
    var aCells   = [];
    
    for (var nRow = 0; nRow < nNewRows; nRow++)
    {
        var oRow    = oTable.insertRow(nRows + nRow);
        for (var nCol = 0; nCol < nCols; nCol++) 
        {
            var oCell                   = oRow.insertCell(nCol);
            oCell.width                 = sWidth;
            oCell.vAlign                = 'top';
            oCell.align                 = 'center';
            aCells[nRow * nCols + nCol] = oCell;
            oCell.innerHTML             = '<div>&nbsp</div>';
        }
    }

    // Load next page of product details
    var IVGDH               = new ivgDataHandler();
    var ProductSnippetArray = IVGDH.FetchProductListSized(sURL, nSize);
    
    var nProds              = ProductSnippetArray != null ? ProductSnippetArray.length : 0;
    
    // Populate new cells
    for (var nProd = 0; nProd < nPPP; nProd++)
    {
        var oCell = aCells[nProd];
        if (nProd < nProds)
            oCell.innerHTML = ProductSnippetArray[nProd] ;
        else
            oCell.innerHTML = ''
    }
    
    if (nProds < nPPP)
    {
        bDone = true;
        clearInterval(ScrollDetectorInterval);
    }

    bLoading = false;

    if (bEventTracking && window.urchinTracker)
        urchinTracker(sURL);
}

