/*globals ClippingsManager, DWRHelper, MarkedListConnector, Paginator, ResultListConnector, document, window
*
*@author james@deepwebtech.com
*/


/**
 * Constructor for the MarkList
 * @param ssid
 */
function MarkList(ssid)
{
    // call the parent constructor to inherit methods and datamembers.
    Paginator.call(this);

    // the ssid associated with this clipping page.
    this.ssid = ssid;

    this.totalResultCount = 0;

    MarkList.instance = this;
}

/**
 * inherit methods and data members
 */
MarkList.prototype = new Paginator();

/**
 * The single instance of the MarkList object
 */
MarkList.instance = null;

/**
 * callback from the call to get results
 * @param data the result container object
 */
MarkList.updateCallback = function(data)
{

    if(document.getElementById("ImportData") != null)
    {
        document.getElementById("ImportData").value = data["markedResultsRIS"];
    }

    if (data["resultContainer"].results.length > 0)
    {
        MarkList.instance.update(data["resultContainer"]);
        MarkList.instance.setCount(data["resultContainer"].totalResults);
    }
};

/**
 * Error handler from the call to getClusters and getCustomClusters
 * @param message the error message
 * @param info a stack trace, and further info
 */
MarkList.update_errorHandler = function(message, info)
{
    DWRHelper.displayDWRError("MarkedListConnector.getResults", message, info);
};

/**
 * The object that will be used when updating the list of marked results
 */
MarkList.updateDWRObject = {
    callback:     MarkList.updateCallback,
    timeout:      DWRHelper.ajaxTimeout,
    errorHandler: MarkList.update_errorHandler
};

/**
 * Called when new results need to be displayed on the page
 * @param resultContainer element containing the new results.
 */
MarkList.prototype.update = function(resultContainer)
{
      // Update the displayed options for the new results
    this.resultCount = resultContainer.resultCount;
    this.totalResultCount = resultContainer.totalResults;
    this.startPosition = resultContainer.startPosition;
    
    var paneContainer = $('#clippings-list-div');
    
    paneContainer.empty();
    var orderedList = paneContainer.append('<div id="resultList"></div>');
    $(resultContainer.results).each(function(i, result){
    	orderedList.append(result.html); 
        $('#result_'+result.resultId).find('.clipping').append(ClippingsManager.createClippingIcon(i, result.resultId, result.marked));
    });

    this.paginate();
};

/**
 * Handles the callback from calls to remove an item from the marked item list
 * @param newSize the new size of the marked list
 */
MarkList.updateAfterRemove_callback = function (newSize)
{
    document.getElementById("clippings-count").innerHTML = newSize;

    if (newSize === 0)
    {
        document.getElementById("clippings-list").style.display = "none";
        document.getElementById("clippings-empty").style.display = "";
    }
    else
    {
        MarkList.instance.refreshResults(newSize);
    }
};

/**
 * function called following the update of the session preferences
 * @param prefs the new preferences object
 */
MarkList.onUpdatePreferences = function(prefs)
{
    if (prefs.resultsPerPage != Paginator.resultsPerPage)
    {
        Paginator.resultsPerPage = prefs.resultsPerPage;
        MarkList.instance.getPageOfResults(0, null, this.ssid, null);
    }
};

/**
 * Refreshes the mark list after an item has been deleted.
 * @param newSize the new size of the list
 */
MarkList.prototype.refreshResults = function(newSize)
{
    //this handles the case when you are on 11-11 of 11 and you hit remove.
    //Now, you will be brought back to the previous page.
    if (this.startPosition >= newSize)
    {
        this.showPrevious(this.ssid);
    }
    else
    {
        this.getPageOfResults(this.startPosition, null, this.ssid, null);
    }

    this.setCount(newSize);
};

/**
 * Sets the clippings count
 * @param newSize the new size of the clippings list
 */
MarkList.prototype.setCount = function(newSize)
{
    document.getElementById("clippings-count").innerHTML = newSize;
};

/**
 * Calls into the MarkedListConnector 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
 */
MarkList.prototype.getPageOfResults = function(startPosition, refresh, ssid, filters)
{
    MarkedListConnector.getResults(startPosition, Paginator.resultsPerPage, MarkList.updateDWRObject);
};

/**
 * Error handler for errors coming from calls to set the current clippings count
 * @param message the error message
 * @param info the detailed information, including the stack trace
 */
MarkList.setClippingsCount_errorHandler = function(message, info)
{
    DWRHelper.displayDWRError("MarkList.setClippingsCount", message, info);
};

/**
 * The object used when making calls to update the clippings
 */
MarkList.clippingsCallbackDWRObject = {
    callback:     MarkList.updateAfterRemove_callback,
    timeout:      DWRHelper.ajaxTimeout,
    errorHandler: MarkList.setClippingsCount_errorHandler
};

