﻿// Based on http://msdn.microsoft.com/msdnmag/issues/07/07/cuttingedge/default.aspx 

// Define the namespace of the class
Type.registerNamespace('GoRedSea');

// Constructor
GoRedSea.Progress = function GoRedSea$Progress() 
{
    GoRedSea.Progress.initializeBase(this);
    _timerID = null;
    _taskID = null;
    _progressCallback = null;
    _msInterval = null;
    _callback = null;
    _clearProgressCallback = null;
    _isStoppingMonitor = false;
}

// Start the timer to periodically check the status of the ongoing task
function GoRedSea$Progress$startMonitor(taskID, msInterval, progressCallback, progressCompletedCallback) { 
    if (arguments.length !== 4) throw Error.parameterCount();
    
    // Update internal members
    if (taskID != null)
        _taskID = taskID;
    _msInterval = msInterval;
    _progressCallback = progressCallback;
    _progressCompletedCallback = progressCompletedCallback;
    
    this._startTimer();
}

// Crear progress
function GoRedSea$Progress$clearProgressStatus(taskID, progressCallback, progressCompletedCallback) { 
    if (arguments.length !== 3) throw Error.parameterCount();
    
    // Update internal members
    if (taskID != null)
        _taskID = taskID;
    _clearProgressCallback = progressCallback;
    _clearProgressCompletedCallback = progressCompletedCallback;
    
    this._clearProgress();
}

// Stop the timer  
function GoRedSea$Progress$stopMonitor() { 
    window.clearTimeout(_timerID);
    if (!_isStoppingMonitor && _progressCompletedCallback !== null)
    {
        _isStoppingMonitor = true;
        _progressCompletedCallback();    
        _isStoppingMonitor = false;
    }
}

// Get task ID
function GoRedSea$Progress$getTaskID() { 
    return _taskID;
}

// Start the timer to control progress
function GoRedSea$Progress$_startTimer() {
    _callback = Function.createDelegate(this, this._checkProgress);
    _timerID = window.setTimeout(_callback, _msInterval);
}

// Modify the request to add the task ID to a hidden field (for UpdatePanel pages)
function GoRedSea$Progress$modifyRequestForTaskId(request, taskID, hiddenField) {
    var body = request.get_body();
    var token = "&" + hiddenField + "=";
    body = body.replace(token, token + taskID);
    request.set_body(body);
    
    return request;
}

// Timer function(s)
function GoRedSea$Progress$_checkProgress() {
    GRS.Distribution.GoRedSea.Client.WebApp.WebServices.SearchProgress.GetProgressStatus(_taskID, this._onFeedbackReceived, this._onFeedbackFailed, this);
}

function GoRedSea$Progress$_onFeedbackReceived(results, context) {
    context._startTimer();
    
    if (_progressCallback !== null)
        _progressCallback(results);
}

function GoRedSea$Progress$_onFeedbackFailed(results) {
}

// Clear progress
function GoRedSea$Progress$_clearProgress() {
    GRS.Distribution.GoRedSea.Client.WebApp.WebServices.SearchProgress.ClearProgressStatus(_taskID, this._onClearFeedbackReceived, this._onClearFeedbackFailed, this);
}

function GoRedSea$Progress$_onClearFeedbackReceived(results, context) {
    if (_clearProgressCallback !== null)
        _clearProgressCallback(results);
}

function GoRedSea$Progress$_onClearFeedbackFailed(results) {
}


// Class prototype
GoRedSea.Progress.prototype = 
{
    getTaskID:                  GoRedSea$Progress$getTaskID,
    startMonitor:               GoRedSea$Progress$startMonitor,
    stopMonitor:                GoRedSea$Progress$stopMonitor,
    modifyRequestForTaskId:     GoRedSea$Progress$modifyRequestForTaskId,
    clearProgressStatus:        GoRedSea$Progress$clearProgressStatus,
    _startTimer:                GoRedSea$Progress$_startTimer,
    _checkProgress:             GoRedSea$Progress$_checkProgress,
    _onFeedbackReceived:        GoRedSea$Progress$_onFeedbackReceived,
    _onFeedbackFailed:          GoRedSea$Progress$_onFeedbackFailed,
    _clearProgress:             GoRedSea$Progress$_clearProgress,
    _onClearFeedbackReceived:   GoRedSea$Progress$_onClearFeedbackReceived,
    _onClearFeedbackFailed:     GoRedSea$Progress$_onClearFeedbackFailed
}

// Register the new class
GoRedSea.Progress.registerClass('GoRedSea.Progress');

// Required to load the script through script manager
Sys.Application.notifyScriptLoaded();




