/**
 *	@copyright	Portland Webworks 2007
 */
 
/* class */ function LiteboxProgressBar( ) /* extends LiteBox */ {
	bind( this, Litebox )( );
	
	this.setIgnoreCanvasClick( true );
	this.setDismissOnClick( false );
	
	this.build( );
	
	return this;
	}

extend( LiteboxProgressBar.prototype, Litebox.prototype );
extend( LiteboxProgressBar.prototype, {

 	_visibleProgress				:	null
,	getVisibleProgress				:	function( ) {
		return this._visibleProgress;
		}
,	setVisibleProgress				:	function( v ) {
		this._visibleProgress = v;
		return this;
		}

,	_realProgress				:	null
,	getRealProgress				:	function( ) {
		return this._realProgress;
		}
,	setRealProgress				:	function( v ) {
		this._realProgress = v;
		return this;
		}

,	_message				:	null
,	getMessage				:	function( ) {
		return this._message;
		}
,	setMessage				:	function( v ) {
		this._message = v;
		return this;
		}

,	_pulseInterval					:	1000
,	getPulseInterval				:	function( ) {
		return this._pulseInterval;
		}
,	setPulseInterval				:	function( v ) {
		this._pulseInterval = v;
		return this;
		}
		
,	_updateURI						:	null
,	getUpdateURI					:	function( ) {
		return this._updateURI;
		}
,	setUpdateURI					:	function( v ) {
		this._updateURI = v;
		return this;
		}
		
,	_barNode				:	null
,	getBarNode				:	function( ) {
		return this._barNode;
		}
,	setBarNode				:	function( v ) {
		this._barNode = v;
		return this;
		}
		
,	_messageNode				:	null
,	getMessageNode				:	function( ) {
		return this._messageNode;
		}
,	setMessageNode				:	function( v ) {
		this._messageNode = v;
		return this;
		}
		
,	_completionCallback				:	null
,	getCompletionCallback				:	function( ) {
		return this._completionCallback;
		}
,	setCompletionCallback				:	function( v ) {
		this._completionCallback = v;
		return this;
		}

,   _autoDispose                    : true
,   getAutoDispose                  : function( ) {
        return this._autoDispose;
    }
,   setAutoDispose                  : function( b ) {
        this._autoDispose = b;
        return this;
    }
		
,	_done				:	null
,	getDone				:	function( ) {
		return this._done;
		}
,	setDone				:	function( v ) {
		this._done = v;
		if( v ) {
			this.setVisibleProgress( 1 );
			this.setRealProgress( 1 );
			}
		return this;
		}


,	update						: function( ) {

		this.getBarNode( ).style.width
			= (this.getVisibleProgress( ) * 100) + '%';
		
		setHTML( this.getMessageNode( ), this.getMessage( ) );
		
		}

,	build						: function( ) {

		var canvas = document.createElement( 'div' );
            canvas.id  = 'progressMeter';
            
			canvas.appendChild(
				job = document.createElement( 'div' )
			).id = 'pbJob';
			
			
			this.setMessageNode( job );
			
            canvas.appendChild(
                percentage = document.createElement( 'div' )
            ).id = 'pbPercentage';
			
			canvas.appendChild(
				outerBar = document.createElement( 'div' )
			).id = 'pbOuterBar';
			
			bar = document.createElement( 'div' );
			bar.id = 'pbBar';
			outerBar.appendChild( bar );
			this.setBarNode( bar );
			
			canvas.appendChild(
				metric = document.createElement( 'div' )
			).id = 'pbMetric';
			
		this.setRealProgress( 0 );
		this.setVisibleProgress( 0 );
		this.setMessage( 'Checking progress...' );
		this.update( );
		
		this.setCanvas( canvas );
		
		return this;
		}
		
,	show						: function( ) {
		bind( this, Litebox.prototype.show )( );
		this.pulse( );
		}

,	pulse						: function( ) {
		if( this.getUpdateURI( ) ) {
			LiteBridge.span(
				 this.getUpdateURI( )
				,bind( this, 'handlePulse' )
				);
			}
		}

,	handlePulse					: function( response ) {
		
		this.setVisibleProgress( response[ 'progress' ] );
		this.setMessage( response[ 'message' ] );
		this.setDone( parseInt( response[ 'done' ] ) );		
		this.update( );
	
		if( this.getDone( ) ) {
			callback = this.getCompletionCallback( );
			if( callback ) {
				if( callback( ) === false ) {
					return;
					}
				}
		    if( this.getAutoDispose( ) ) {
    			setTimeout( bind( this, 'dispose' ), 1200 );
    			}
			}
		else {
			setTimeout( bind( this, 'pulse' ), this.getPulseInterval( ) );
			}
		}


	} );

