// if(Type != null)
Type.registerNamespace("Demo");

Demo.ProgressBar = function(associatedElement)
{
    // alert('coucou');
    
    Demo.ProgressBar.initializeBase(this, [associatedElement]);
    
    var _timer;
    var _progress = 0;
    var _serviceURL;
    var _serviceMethod;
    var _responsePending;
    var _tickHandler;
    var _obj = this;
    var _timerinterval = 0;
    
    this.get_progress = function()
    {
        return _progress;
    }
    
    this.set_progress = function(value)
    {
        if(value == undefined)
            return;
            
        if (value >= 100)
        {
            // _timer.set_enabled(false);
            clearTimeout(_timer);
        }
        _progress = value;
        associatedElement.style.width = value + "%";
        associatedElement.innerHTML = value == 100 ? "Done!" : value + "%";
    }
    
    this.get_interval = function()
    {
        return _timerinterval;
    }
    
    this.set_interval = function(value)
    {
        _timerinterval = 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.getDescriptor = function()
    {
        var td = Sys.UI.ProgressBar.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 des;
    }
    
    this.initialize = function()
    {
        Demo.ProgressBar.callBaseMethod(this, 'initialize');
                
        this.set_progress(0);
    }
    
    this.dispose = function()
    {
        if (_timer)
        {
            clearTimeout(_timer);
        }
        _timer = null;
        
        associatedElement = null;
        _obj = null;
    
        Demo.ProgressBar.callBaseMethod(this, 'dispose');
    }
    
    this.start = function()
    {
        if (_timer)
        {
            clearTimeout(_timer);
        }
        _timer = setTimeout("MyUpdateTimer()", _timerinterval);
    }
    
    this.stop = function()
    {
        if (_timer)
            clearTimeout(_timer);
        _timer = null;
    }
        
    this.UpdateTimer = function()
    {        
        if(_timer)
        {
            clearTimeout(_timer);
        }

        if (!_responsePending)
        {
            _responsePending = true;
            
            // Asynchronously call the service method.
            Sys.Net.WebServiceProxy.invoke(_serviceURL, _serviceMethod, false, null, _onMethodComplete);
        }
        
        _timer = setTimeout("MyUpdateTimer()", _timerinterval);
    }
    
    function _onMethodComplete(result)
    {   
        // alert(result);
        // alert(result.innerHTML);
        // Update the progress bar.
        _obj.set_progress(result.text);
        _obj.set_progress(result);
        _responsePending = false;
    }
}
Demo.ProgressBar.registerClass('Demo.ProgressBar', Sys.UI.Control);
// Type.registerSealedClass('Sys.UI.ProgressBar', Sys.UI.Control);
// Sys.TypeDescriptor.addType('script','progressBar', Sys.UI.ProgressBar);