var StaticSpotlightIndexes = new Array();
var RandomSpotlightIndexes = new Array();
var ShownSpotlight = new Array(); //for all iterations over spotlights

function AddPotentialListing(ListingID)
{
    if (!ContainElement(StaticSpotlightIndexes,ListingID))
        RandomSpotlightIndexes.push(ListingID);
}
function ContainElement(array, ListingID)
{
    var i = array.length;
    while (i > 0 )
    {
        i--;
        if(array[i] == ListingID)
            return true;
    }
    return false;
}
function showSpotlights(prefix, div, spots)
{    
    var cDiv = document.getElementById(div);
    var shownCount = 0; //for the current iteration
    if (cDiv) 
    {  
        while (shownCount < spots && StaticSpotlightIndexes.length > 0)
        {
            showSingleSpotlight(prefix+'/Listing/GetSingleSpotlight.aspx', StaticSpotlightIndexes[StaticSpotlightIndexes.length-1], div);
            shownCount++;
            ShownSpotlight.push(StaticSpotlightIndexes.pop());    
        }     
        while (shownCount < spots && RandomSpotlightIndexes.length > 0)
        {   
            var ix = Math.floor(RandomSpotlightIndexes.length*Math.random());
            var potentialIndex = RandomSpotlightIndexes[ix];
           
            if (!ContainElement(ShownSpotlight, potentialIndex))
            {   
                ShownSpotlight.push(potentialIndex);
                showSingleSpotlight(prefix+'/Listing/GetSingleSpotlight.aspx', potentialIndex, div);
                shownCount++;
                RandomSpotlightIndexes.splice(ix,1);
            }
        }
    }
    
}
function showSingleSpotlight(prefix,ListingID,locationDiv) 
{
    urchinTracker('/FeaturedListingImpression.pstat?ListingID='+ ListingID);
    GetInitScript(prefix+"?Spotlight=S&ListingID=" + ListingID, function(data) { setContents(data, locationDiv); });
}

function setContents(data, dest)
{  
    if(data.indexOf('error') == -1)
    {
        var dataDest = document.getElementById(dest);
        if (dataDest)
        {
            dataDest.innerHTML += data;   
        }
    }
}