﻿Sys.UI.Progressor = function(associatedElement) {
    Sys.UI.Progressor.initializeBase(this, [associatedElement]);
    
    this.element = associatedElement;
    
    var _timer = new Sys.Timer();
    var _progress = 0;
    var _showPercentage = true;
    var _serviceURL;
    var _serviceMethod;
    var _responsePending;
    var _tickHandler;
    var _type = "Indicator";
    
    this.get_progress = function() {
        return _progress;
    }
    
    this.set_progress = function(value) { 
        if (value >= 100) {
            _timer.set_enabled(false);
        }
        _progress = value;
        if (_type == "ProgressBar") {
            this.element.style.width = value.toString() + "%";
            if (_showPercentage && value > 5)
                this.element.innerHTML = value + "% &nbsp;";
            else
                this.element.innerHTML = "  ";
        }
    }
    
    this.set_showPercentage = function(value) {
        _showPercentage = value;
    }
    this.get_showPercentage = function() {
        return _showPercentage; 
    }    
    
    this.get_interval = function() {
        return _timer.get_interval();
    }
    
    this.set_interval = function(value) {
        _timer.set_interval(value);
    }
    
    this.get_serviceURL = function() {
        return _serviceURL;
    }
    
    this.set_serviceURL = function(value) {
        _serviceURL = value;
    }
    
    this.get_serviceMethod = function() {
        return _serviceMethod;
    }
    
    this.set_serviceMethod = function(value) {
        _serviceMethod = value;
    }
    
    this.get_type = function() {
        return _type;
    }
    
    this.set_type = function(value) {
        _type = value;
    }
    
    this.getDescriptor = function() {
        var td = Sys.UI.Progressor.callBaseMethod(this, 'getDescriptor');
        td.addProperty('interval', Number);
        td.addProperty('progress', Number);
        td.addProperty('serviceURL', String);
        td.addProperty('serviceMethod', String);
        td.addMethod('start');
        td.addMethod('stop');
        return td;
    }
    
    this.initialize = function() {
        Sys.UI.Progressor.callBaseMethod(this, 'initialize');
        _tickHandler = Function.createDelegate(this, this._onTimerTick);
        _timer.Tick.add(_tickHandler);
        this.set_progress(0);
    }
    
    this.dispose = function() {
        if (_timer) {
            _timer.Tick.remove(_tickHandler);
            _tickHandler = null;
            _timer.dispose();
        }
        _timer = null;
    
        Sys.UI.Progressor.callBaseMethod(this, 'dispose');
    }
    
    this.start = function() {
        _timer.set_enabled(true);
    }
    
    this.stop = function() {
        _timer.set_enabled(false);
    }
    
    this._onTimerTick = function(sender, eventArgs) {
        if (!_responsePending) {
            _responsePending = true;
            var num = new Object();
            num.ProgressValue = -1;
            
            var serviceMethod = new Sys.Net.ServiceMethod(_serviceURL, _serviceMethod, null /*? _appUrl */);
            var _request = serviceMethod.invoke({},_onMethodComplete, _onError, 
                                                _onError,_onError, this,
                                                2000, Sys.Net.WebRequestPriority.Normal);
        }
    }
    
    function _onMethodComplete(result, response, context) {
        debug.dump("Success: ");
        debug.dump(result.get_message());
        var behavior = context[0];
        behavior.set_progress(result);
        _responsePending = false;
    }
    function _onError(result,response,context) {
        debug.dump("Error");
        debug.dump(result);
        debug.dump(context);
    }
    
}
Type.registerClass('Sys.UI.Progressor', Sys.UI.Control);
