var NXC = NXC || {};
NXC.BarLoader = new Class( {
	Implements: [Options, Chain],

	options: {		
		'transitionDuration': 500,
		'textPattern':        '%loadPercent% %'
	},
	
    initialize: function( block, bar, barImage, text, options ) {
		this.block    = $( block );
		this.bar      = $( bar );
		this.barImage = $( barImage );
		this.text     = $( text );

		this.barImage.setStyle( 'margin-left', -1 * this.barImage.getStyle( 'width' ).toInt() );
		
    	this.setOptions( options );
    },

    progress: function( loadPercent ) {
    	if( loadPercent <= 0 || loadPercent > 100 ) {
    		return false;
    	}

		var barWidth          = this.bar.getStyle( 'width' ).toInt();
		var newBarImageMargin = ( barWidth * ( loadPercent / 100 ) ).toInt() - barWidth;
		this.barImage.get( 'tween', { property: 'margin-left', duration: this.options.transitionDuration } ).start( newBarImageMargin );
		
		this.text.set( 'html', this.options.textPattern.replace( '%loadPercent%', loadPercent.toFixed( 0 ) ) );
    },

	remove: function() {
		this.block.get( 'tween', { property: 'opacity', duration: this.options.transitionDuration } ).start( 0 ).chain( function() {
			this.block.destroy();
		}.bind( this ) );
	}
} );