//---------------------------------------------------------------------------------------
// This class contains the information that is associated with each result pane
// Dependencies:
//      ResultOrder.js
//      Paginator.js
//---------------------------------------------------------------------------------------

/**
 * Sets the Base class of the ResultPane
 */
ResultPane.prototype = new Paginator();

/**
 * Defines the text that will be displayed at the top of the list of sources
 */
ResultPane.allSources = "Not filled in yet...";

/**
 * Constructor for the result pane object
 * @param paneId the id of this result pane
 * @param resultOrder the initial sort order of this pane
 * @param collections the set of collections that belong with this pane.
 */
function ResultPane(paneId, resultOrder, description, collections)
{
    // The unique identifier for this pane, that will distinguish it from other panes
    this.paneId = paneId;

    // The currently selected collection id
    this.selectedCollection = "";

    // The initial sort order for result items in this pane
    this.resultOrder = resultOrder;

    // The desription associated with each pane
    this.description = description;

    // The currently viewed collection id/name pairs
    this.collections = collections;

    // The Map of result counts per collection for the container
    this.collectionCounts = null;

    // The number of displayable results
    this.displayCount = 0;
}

/**
 * Sets the collection counts for this pane
 * @param collectionCounts the new value of collectionCounts
 */
ResultPane.prototype.setCollectionCounts = function(collectionCounts)
{
    if (this.collectionCounts == null || collectionCounts == null ||
        this._mapLength(collectionCounts) > this._mapLength(this.collectionCounts))
    {
        this.collectionCounts = collectionCounts;
    }
}

/**
 * Returns the length of a map object
 * @param map
 */
ResultPane.prototype._mapLength = function(map)
{
    var ret = 0;
    for (var i in map)
    {
        ret++;
    }
    return ret;
}

/**
 * Sets the selected collection used to view the data
 * @param the id of the collection to set. 
 */
ResultPane.prototype.setSelectedCollection = function(collectionId)
{
    this.selectedCollection = collectionId;
}

/**
 * Resets the selected collection used to view the data
 */
ResultPane.prototype.resetSelectedCollection = function()
{
    this.selectedCollection = "";
}

/**
 * Calls into the ResultListConnector to get a page of results
 * @param startPosition the position where the list of results should start from
 * @param refresh true if a new snapshot should be generated
 * @param ssid the id of the search
 * @param filters the list of filters to apply to the results
 */
ResultPane.prototype.getPageOfResults = function(startPosition, refresh, ssid, filters)
{
    ResultListConnector.getResults(
            ssid, this.paneId, refresh, startPosition, Paginator.resultsPerPage,
            filters, this.resultOrder, this.getCollectionIdList(), ResultList.getResultsDWRObject);
}

/**
 * Gets an array with the pane's collection ids
 */
ResultPane.prototype.getCollectionIdList = function()
{
    var ret = [];

    if (this.selectedCollection == "")
    {
        for (var i = 0; i < this.collections.length; ++i)
        {
            ret[i] = this.collections[i].id;
        }
    }
    else
    {
        ret[0] = this.selectedCollection;
    }
    
    return ret;
}

/**
 * Sets the sort-by options from the current tab
 */
ResultPane.prototype.setSortByOptions = function()
{
    var limitToElements = [document.getElementById("sort_by"), document.getElementById("sort_by_bottom")];

    for (var ele_idx = 0; ele_idx < limitToElements.length; ++ele_idx)
    {
        // Update the sort by option to reflect current pane.
        var options = limitToElements[ele_idx].options;

        for (var i = 0; i < options.length; i++)
        {
            options[i].selected = (options[i].value == this.resultOrder.resultField);
        }
    }
}

/**
 * Sets the collections in the drop down
 */
ResultPane.prototype.setLimitToOptions = function()
{
    var limitToElements = [document.getElementById("limit-to"), document.getElementById("limit-to_bottom")];

    for (var ele_idx = 0; ele_idx < limitToElements.length; ++ele_idx)
    {
        // Update the limit-to by option to reflect current pane.
        var options = limitToElements[ele_idx].options;
        options.length = 0;
        options[0] = new Option(ResultPane.allSources,"");

        var option_idx = 0;

        for (var i = 0; i < this.collections.length; i++)
        {
            if (this.hasResults(this.collections[i].id))
            {
                option_idx++;
             if(this.collections[i].name == "zzContributed Collections") {
               var colname = this.collections[i].name;
               var newName = colname.substring(2, colname.length);
               options[option_idx] = new Option(newName, this.collections[i].id);
              } else {               
                options[option_idx] = new Option(this.collections[i].name, this.collections[i].id);
             }
                options[option_idx].selected = (this.collections[i].id == this.selectedCollection);
            }
        }
    }
}

/**
 * Checks to see if the collection with the given id actually has results
 * @param id the id of the collection to check
 * @return true if the collection has results, false otherwise
 */
ResultPane.prototype.hasResults = function(id)
{
    return (this.collectionCounts == null ||
        (this.collectionCounts[id] != null && this.collectionCounts[id] > 0));
}

/**
 * Updates the resultItems displayed in the resultPane
 * @param resultContainer the results to display
 * @param paneContainer the pane into which to set the results
 */
ResultPane.prototype.update = function(resultContainer, paneContainer)
{
    // Create an ordered list
    var orderedList = document.createElement("ol");

    for (var i = 0; i < resultContainer.results.length; ++i)
    {
        var result = resultContainer.results[i];

        var listItem = document.createElement("li");
        listItem.id = "resultItem" + i;
        orderedList.appendChild(listItem);

        listItem.innerHTML = result.html;

        var clippingIcon = ClippingsManager.createClippingIcon(i, result.resultId, result.marked);
        listItem.appendChild(clippingIcon);
    }

    // Remove all nodes from the pane container
    while (paneContainer.hasChildNodes())
    {
        paneContainer.removeChild(paneContainer.firstChild);
    }

    // Add the orderedList element to the paneConainer
    paneContainer.appendChild(orderedList);

    // Update the displayed options for the new results
    this.setSortByOptions();
    this.setLimitToOptions();
    this.paginate();
}
