(function( $ ) {

$.widget( "nmk.progressbar", {
    
	// set default options
    options: {
        value: 0
    },
    
	// initialize the plugin
	_create: function() {
		
		this.element.addClass( "progressbar" );
		this._update();
	},
	
	// clean up on destroy/removal
	destroy: function() {
		
		this.element
			.removeClass( "progressbar" )
			.text( "" );
		
		// call the base destroy function
		$.Widget.prototype.destroy.call( this );
	},
	
	// create a public method
	value: function( value ) {
		
		// no value passed, act as a getter
		if ( value === undefined ) {
			return this.options.value;
			
		// value passed, act as a setter
		} else {
			this.options.value = this._constrain( value );
			this._update();
		}
	},
	
	// react to option changes after initialization
	_setOption: function( key, value ) {
		
		// using a switch statement is an easy way to orgnaize option setting
		// as you add more options to your plugins
		switch ( key ) {
			
			case "value":
			
				this.options.value = this._constrain( value );
				break;
				
			default:
			
				this.options[ key ] = value;
				break;
		}
		
		this._update();
	},
	
	// create a private method
	_constrain: function( value ) {
		
		if ( value > 100 ) {
			value = 100;
		}
		
		if ( value < 0 ) {
			value = 0;
		}
		
		return value;
	},
	
	// create a private method
	_update: function() {
		
		var progress = this.options.value + "%";
		this.element.text( progress );
		
		// trigger a callback to allow users to react to state changes
		if ( this.options.value == 100 ) {
			this._trigger( "complete", null, { value: 100 } );
		}
	}
});

// modify the prototype
// this would normally be done in a separate file as an extension to an existing plugin
$.nmk.progressbar.prototype.reset = function() {
	
	this._setOption( "value", 0 );
};

})( jQuery );

