/*global DWRHelper, CollectionPanelConnector, document, window */

/**
 * This class defines the interface for retrieving the results from a specific collection
 * and inserting those results into an html element for display on the results page.
 * @author Peter R. G. Small
 */

/**
 * Creates a new instance of the collection panel and immediately starts polling the
 * server to retrieve the results that are associated with the collection
 * @param ssid the ssid of the search being queried for
 * @param panelId the id the html element that will receive the results
 * @param innerPanelId the id of the inner panel that will display error messages
 * @param collectionCode the collection code to get the results for
 * @param stylesheet the stylesheet used to transform the results into html
 * @param maxResults the maximum number of results to return
 * @param resultOrder the order that the resutl should appear in.
 */
function CollectionPanel(ssid, panelId, innerPanelId, collectionCode, stylesheet, maxResults, resultOrder)
{
    var inst = this;

    // Define the function to retrieve the data for the collection.
    // This uses TWO closures!
    this.retrieveFunction = function(currentSsid, outputPanelId, cc, xsl, max, ro, inst)
    {
        var callbackFunction = function(oppid, instance)
        {
            return function(serverData)
            {
                CollectionPanel.getData_callback(serverData, oppid, instance);
            };
        }(outputPanelId, inst);

        var errorHandler = function (errorPanelId)
        {
            return function(message, info)
            {
                CollectionPanel.getData_errorHandler(message, info, errorPanelId);
            };
        }(innerPanelId);

        var retObj = {
            callback:     callbackFunction,
            timeout:      DWRHelper.ajaxTimeout,
            errorHandler: errorHandler
        };

        return function()
        {
            CollectionPanelConnector.retrieveEntry(currentSsid, cc, xsl, max, ro, retObj);
        };

    }(ssid, panelId, collectionCode, stylesheet, maxResults, resultOrder, inst);

    window.setTimeout(this.retrieveFunction, 4000);
}

/**
 * This is the call back method from CollectionPanelConnector.retrieveEntry
 * @param html the data returned from the server
 * @param panelId the id of the HTML element that will contain the HTML from the server
 * @param instance The instance of CollectionPanel used to reschedule the function call,
 * if there was no data returned.
 */
CollectionPanel.getData_callback = function(html, panelId, instance)
{
    if (html === "")
    {
        window.setTimeout(instance.retrieveFunction, 2000);
    }
    else
    {
        document.getElementById(panelId).innerHTML = html;
    }
};

/**
 * The error handler from the call to CollectionPanelConnector.retrieveEntry
 * @param message the error message
 * @param info the stack trace and other info that provides greater detail into the error
 * @parma errorPanelId the id of the panel that will contain any error messages.
 */
CollectionPanel.getData_errorHandler = function(message, info, errorPanelId)
{
    var pos;

    if (message == "Timeout")
    {
        message = CollectionPanel.noSourceMessage;
    }
    else
    {
        // Attempt to extract the message by removing the java class name
        pos = message.indexOf(info.javaClassName);
        if (pos != -1)
        {
            message = message.substr(pos + info.javaClassName.length + 1);
        }
    }

    document.getElementById(errorPanelId).innerHTML = '<div class="collectionPanelError">' + message + '</div>';
};

/**
 * The message to display when there is no source
 */
CollectionPanel.noSourceMessage = "undefined";
