/**
 * This class handles paging from within the alerts pages.
 * @author peter@deepwebtech.com
 */
function AlertsPaginator(searchId, alertId, totalResultCount)
{
    this.searchId = searchId;
    this.alertId = alertId;

    // Base class variables
    this.totalResultCount = totalResultCount;
    this.suffix = [searchId];
}

/**
 * Base the AlertsPaginator off of the Paginator object.
 */
AlertsPaginator.prototype = new Paginator();

/**
 * Calls into the AlertsConnector 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
 */
AlertsPaginator.prototype.getPageOfResults = function(startPosition, refresh, ssid)
{
    AlertsConnector.getAlertResults(this.searchId, this.alertId, startPosition, Paginator.resultsPerPage,
        this.getResultsDWRObject());
};

/**
 * Callback from AlertsConnector.getAlertResults
 * @param alertResults the list of alert results
 */
AlertsPaginator.getResults_callback = function(alertResultContainer, searchId)
{
    // Create an ordered list
    var orderedList = document.createElement("ol");

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

        var listItem = document.createElement("li");
        orderedList.appendChild(listItem);

        var alertItemDiv = document.createElement("div");
        alertItemDiv.id = "resultItem" + i;
        alertItemDiv.className = "alertResultItem";
        listItem.appendChild(alertItemDiv);

        alertItemDiv.innerHTML = result.html;

        var clippingIcon = ClippingsManager.createClippingIcon(i, result.resultId, result.marked, true, searchId);
        alertItemDiv.insertBefore(clippingIcon, alertItemDiv.firstChild);
    }

    // Remove all nodes from the pane container
    var innerAlertResultsDiv = document.getElementById("innerAlertResultsDiv" + alertResultContainer.searchId);
    while (innerAlertResultsDiv.hasChildNodes())
    {
        innerAlertResultsDiv.removeChild(innerAlertResultsDiv.firstChild);
    }

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

    var activePaginator = AlertList.instance.getActivePaginator();

    activePaginator.startPosition = alertResultContainer.startPos;
    activePaginator.resultCount   = alertResultContainer.count;
    activePaginator.paginate();
};

/**
 * The error handler method for calls to AlertsPaginator.getResults
 * @param message the error message to be displayed
 */
AlertsPaginator.getResults_errorHandler = function(message, info)
{
    AlertList.pageErrorHandler("AlertsConnector.getAlertResults", message, info);
};

/**
 * The object used when making calls to getResults
 */
AlertsPaginator.prototype.getResultsDWRObject = function()
{
    var returnFunction = function(searchId)
                         {
                             return function(serverData){AlertsPaginator.getResults_callback(serverData, searchId);};
                         }(this.searchId);

    var retObj =    {
      callback:     returnFunction,
      timeout:      DWRHelper.ajaxTimeout,
      errorHandler: AlertsPaginator.getResults_errorHandler
    };

    return retObj;
};
