//--------------------------------------------------------------------------------
// 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.
//--------------------------------------------------------------------------------

/**
 * 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;

/**
 * The list of listeners that will recieve status complete messages
 */
CollectionStatusPanel.listeners = [];

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

/**
 * 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="images/' + status + '.gif" width="16" alt="' + altTitle + '" title="' + altTitle + '">';
}

CollectionStatusPanel.collectionNameFormatter = function(elCell, oRecord, oColumn, oData)
{
    YAHOO.util.Dom.addClass(elCell, "name");
    
    var collectionName = oRecord.getData("name");
    if (collectionName.indexOf("zz") == 0)
    {
        collectionName = collectionName.substr(2);
    }
    elCell.innerHTML = collectionName;
}

CollectionStatusPanel.totalsFormatter = function(elCell, oRecord, oColumn, oData)
{
    YAHOO.util.Dom.addClass(elCell, "status");

    var totalCollectionCounts = oRecord.getData("totalCollectionCounts");
    if (totalCollectionCounts == null || totalCollectionCounts == 0)
    {
        totalCollectionCounts = "N/A";
    }

    elCell.innerHTML = 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:0, x:leftPos, zIndex:1000, width:"320px", draggable:true, visible:false, constraintoviewport:true } );
    CollectionStatusPanel.panel.hideEvent.subscribe(function(){CollectionStatusPanel.isVisible = false;}, null, false);
    CollectionStatusPanel.panel.showEvent.subscribe(function(){CollectionStatusPanel.isVisible = true;}, null, false);

    // Define the columns for the table
    var columnDefs = [
        {key:"name", label: "Collection Name", formatter:CollectionStatusPanel.collectionNameFormatter, sortable:true, resizeable:true},
        {key:"status", className: "statusColumn", label: "", width: "30px", formatter:CollectionStatusPanel.collectionStatusFormatter, sortable:false, resizeable:false},
        {key:"numResults", className: "numResultsColumn", label: "Results", formatter:YAHOO.widget.DataTable.formatNumber, sortable:true, resizeable:true},
        {key:"totalCollectionCounts", className: "totalCollectionCountsColumn", label: "Totals", formatter:CollectionStatusPanel.totalsFormatter, sortable:true, 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()
{
    document.getElementById("collectionStatusPanel").style.display = "";
    
    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();
}

/**
 * Makes an AJAX call to get the collection status
 * @param ssid the id of the current search
 * @param filters the list of filters to be applied to the data
 */
CollectionStatusPanel.getCollectionStatus = function(ssid, filters)
{
    ResultListConnector.getCollectionSearchInfo(ssid, filters, CollectionStatusPanel.getCollectionStatusDWRObject);
}

/**
 * Callback function to the method ResultListConnector.getCollectionSearchInfo
 * @param data the data received from the AJAX call.
 */
CollectionStatusPanel.getCollectionStatus_callback = function(data)
{
    if (CollectionStatusPanel.collectionStatusTable == null)
    {
        CollectionStatusPanel.createCollectionStatusTable(data);
    }
    else
    {
        CollectionStatusPanel.updateCollectionStatusTable(data);
    }

    // Notify the listeners that status has been gotten
    for (i in CollectionStatusPanel.listeners)
    {
        CollectionStatusPanel.listeners[i].collectionStatusComplete();
    }
}

CollectionStatusPanel.addListener = function(listener)
{
    CollectionStatusPanel.listeners[CollectionStatusPanel.listeners.length] = listener;
}


/**
 * Callback function to the method ResultListConnector.getCollectionSearchInfo
 * @param data the data received from the AJAX call.
 */
CollectionStatusPanel.getCollectionStatus_errorHandler = function(message, info)
{
    DWRHelper.displayDWRError("CollectionStatusPanel.getCollectionStatus_errorHandler", message, info);
}

/**
 * The object used when making calls to ResultListConnector.getCollectionSearchInfo
 */
CollectionStatusPanel.getCollectionStatusDWRObject = {
  callback:     CollectionStatusPanel.getCollectionStatus_callback,
  timeout:      20000,
  errorHandler: CollectionStatusPanel.getCollectionStatus_errorHandler
};

