//--------------------------------------------------------------------------------
// CollectionStatusPanel.js
// Handles the construction and display of the collectionStatusPanel that is used
// to display the status of collections as a search is going on.
//--------------------------------------------------------------------------------

/*global YAHOO, document */

/**
 * Default constructor... not used to create an instance
 */
function CollectionStatusPanel()
{
}

/**
 * The strings used for labeling the columns in this panel
 */
CollectionStatusPanel.nameColumnLabel    = "<<>>";
CollectionStatusPanel.resultsColumnLabel = "<<>>";
CollectionStatusPanel.totalsColumnLabel  = "<<>>";

/**
 * The data source for the collectionStatusTable
 */
CollectionStatusPanel.dataSource = null;

/**
 * The table that will display the collection status
 */
CollectionStatusPanel.collectionStatusTable = null;

/**
 * The panel that the table will be rendered in
 */
CollectionStatusPanel.panel = null;

/**
 * True if the panel is visible
 */
CollectionStatusPanel.isVisible = false;

/**
 * Formatter function for the status column in the datatable
 * @param elCell the cell element
 * @param oRecord the record being written to the table
 * @param oColumn the column being written
 * @param oData extra data for the cell.  Not used
 */
CollectionStatusPanel.collectionStatusFormatter = function(elCell, oRecord, oColumn, oData)
{
    YAHOO.util.Dom.addClass(elCell, "status");

    var altTitle = oRecord.getData("errorDetails");
    if (altTitle === null)
    {
        altTitle = "";
    }
    else
    {
        altTitle = altTitle.substring(altTitle.indexOf(":")+1, altTitle.length);
    }

    var status = oRecord.getData("status").toLowerCase();
    elCell.innerHTML = ' <img src="' + getImageDir() + '/' + status + '.gif" width="16" alt="' + altTitle + '" title="' + altTitle + '">';
};

/**
 * Formatter used to display the total column
 * @param elCell the cell element
 * @param oRecord the record being written to the table
 * @param oColumn the column being written
 * @param oData extra data for the cell.  Not used
 */
CollectionStatusPanel.totalsFormatter = function(elCell, oRecord, oColumn, oData)
{
    YAHOO.util.Dom.addClass(elCell, "status");

    var totalCollectionCounts = oRecord.getData("totalCollectionCounts");
    var numResults = oRecord.getData("numResults");
    if (totalCollectionCounts === null || parseInt(totalCollectionCounts, 10) < parseInt(numResults, 10))
    {
        totalCollectionCounts = numResults;
    }

    elCell.innerHTML = formatInteger(totalCollectionCounts);
};

/**
 * Creates an instance of the collection status panel
 * @param data the data to use to create the table
 */
CollectionStatusPanel.createCollectionStatusTable = function(data)
{
    // Determine the position on the screen at which to put the status panel
    var leftPos = YAHOO.util.Dom.getViewportWidth() - 340;

    // Create the panel that will contain the collection status
    CollectionStatusPanel.panel = new YAHOO.widget.Panel("collectionStatusPanel", { y:100,
                                                                                    x:leftPos,
                                                                                    zIndex:1000,
                                                                                    width:"320px",
                                                                                    draggable:true,
                                                                                    visible:false,
                                                                                    constraintoviewport:true
                                                                                   } );
    CollectionStatusPanel.panel.hideEvent.subscribe(function(){
                                                                document.getElementById("collectionStatusPanel").style.display = "none";
                                                                CollectionStatusPanel.isVisible = false;
                                                              },
                                                              null,
                                                              false);
    CollectionStatusPanel.panel.showEvent.subscribe(function(){
                                                                document.getElementById("collectionStatusPanel").style.display = "";
                                                                CollectionStatusPanel.isVisible = true;
                                                              },
                                                              null,
                                                              false);

    // Define the columns for the table
    var columnDefs = [
        {key:"name", label: CollectionStatusPanel.nameColumnLabel, sortable:false, resizeable:true},
        {key:"status", className: "statusColumn", label: "", width: "30px", formatter:CollectionStatusPanel.collectionStatusFormatter, sortable:false, resizeable:false},
        {key:"numResults", className: "numResultsColumn", label: CollectionStatusPanel.resultsColumnLabel, formatter:YAHOO.widget.DataTable.formatNumber, sortable:false, resizeable:true},
        {key:"totalCollectionCounts", className: "totalCollectionCountsColumn", label: CollectionStatusPanel.totalsColumnLabel, formatter:CollectionStatusPanel.totalsFormatter, sortable:false, resizeable:true}
    ];

    CollectionStatusPanel.dataSource = new YAHOO.util.DataSource(data);
    CollectionStatusPanel.dataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
    CollectionStatusPanel.dataSource.responseSchema = {
        fields: ["name","id","status","numResults", "totalCollectionCounts","errorDetails"]
    };

    CollectionStatusPanel.collectionStatusTable = new YAHOO.widget.DataTable("collectionStatusPanelBody",
            columnDefs, CollectionStatusPanel.dataSource,
            {
                caption:"",
                sortedBy:{key:"name", dir:"asc"}
            }
    );

    // Render the panel
    CollectionStatusPanel.panel.render();
};

/**
 * Toggles the visibility state of the panel
 * @return always return false to prevent the onclick event from the initiating button
 * from being propagted.
 */
CollectionStatusPanel.toggle = function()
{
    if (!CollectionStatusPanel.panel)
    {
        return;
    }
    


    if (CollectionStatusPanel.isVisible)
    {
        CollectionStatusPanel.panel.hide();
    }
    else
    {
        CollectionStatusPanel.panel.show();
    }
    return false;
};

/**
 * Refreshes the data in the table
 * @param data the new set of data
 */
CollectionStatusPanel.updateCollectionStatusTable = function(data)
{
    CollectionStatusPanel.collectionStatusTable.getRecordSet().replaceRecords(data);
    CollectionStatusPanel.collectionStatusTable.refreshView();
};

/**
 * Sets the collection status.  It will create the collectionStatusTable, if needed.
 * @param collectionStatus a list of collectionSearchInfo objects
 */
CollectionStatusPanel.setCollectionStatus = function(collectionStatus)
{
    if (CollectionStatusPanel.collectionStatusTable === null)
    {
        CollectionStatusPanel.createCollectionStatusTable(collectionStatus);
    }
    else
    {
        CollectionStatusPanel.updateCollectionStatusTable(collectionStatus);
    }
};