/*
 * jQuery UI Progressbar 1.8.1
 *
 * Copyright (c) 2010 AUTHORS.txt (http://jqueryui.com/about)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://docs.jquery.com/UI/Progressbar
 *
 * Rewritten by DKW to animate the progress bar
 *
 * Depends:
 *   jquery.ui.core.js
 *   jquery.ui.widget.js
 */
(function( $ ) {

$.widget( "ui.animatedprogressbar", {
	options: {
		value: 0,
		step: 3
	},
	_create: function() {
		this.element
			.addClass( "ui-progressbar ui-widget ui-widget-content ui-corner-all" )
			.attr({
				role: "progressbar",
				"aria-valuemin": this._valueMin(),
				"aria-valuemax": this._valueMax(),
				"aria-valuenow": this._value()
			});

		// DKW - Clear contents of the div
		this.element.html('');
		
		this.valueDiv = $( "<div class='ui-progressbar-value ui-widget-header ui-corner-left'></div>" )
			.appendTo( this.element );

		this._refreshValue();
	},

	destroy: function() {
		this.element
			.removeClass( "ui-progressbar ui-widget ui-widget-content ui-corner-all" )
			.removeAttr( "role" )
			.removeAttr( "aria-valuemin" )
			.removeAttr( "aria-valuemax" )
			.removeAttr( "aria-valuenow" );

		this.valueDiv.remove();

		$.Widget.prototype.destroy.apply( this, arguments );
	},

	value: function( newValue ) {
		if ( newValue === undefined ) {
			return this._value();
		}

		this._setOption( "value", newValue );
		return this;
	},

	_setOption: function( key, value ) {
		switch ( key ) {
			case "value":
				this.options.value = value;
				this._refreshValue();
				this._trigger( "change" );
				break;
		}

		$.Widget.prototype._setOption.apply( this, arguments );
	},

	_value: function() {
		var val = this.options.value;
		// normalize invalid value
		if ( typeof val !== "number" ) {
			val = 0;
		}
		if ( val < this._valueMin() ) {
			val = this._valueMin();
		}
		if ( val > this._valueMax() ) {
			val = this._valueMax();
		}

		return val;
	},

	_valueMin: function() {
		return 0;
	},

	_valueMax: function() {
		return 100;
	},

	_refreshValue: function() {
		var value = this.value();
		this.valueDiv
			[ value === this._valueMax() ? "addClass" : "removeClass"]( "ui-corner-right" )
			.width( value + "%" );
		this.element.attr( "aria-valuenow", value );
		
		// DKW: Added a callback function that uses
		// setTimeout to animate the bar. Change ms to change the speed and smoothness
		
			this.element.children('.progress-value').remove();
			
		// Only animate if we are visible
		if (this.element.css("display") != 'none'){
			var _self = this;
			var ms = 25;
			setTimeout(function(ms){
				_self.alertBar();
				}, ms);
		}
	},
	
	// Adde by DKW
	alertBar: function () {
		var value = this.value();
		
		// Modified by DKW
		
		if (value == null){
			value = 0;
		}
		var step = this.options.step;
		if (step == 0){
			step = 1;
		}
		// checks it's own attribute value
		//console.log(value + ':' + step + ':' + this.options.value + ':' + this.element.attr("value") + ':' + Math.floor(this.element.attr("value")));
		if (this.element.attr("value") > (value + step) ){
			// Set timeout
			this._setOption("value", value + step);
		} else if (value != Math.floor(this.element.attr("value"))) {
			// Jump up to the set value
			this._setOption("value", Math.floor(this.element.attr("value")));
		}  else {			
			// Set the value span
			this.element.children('.progress-value').remove();
			this.element.parent().append('<span class="progress-value">' + this.element.attr("value") + '<span class="progress-percent">%</span></span>');
			
		}
	}
});

$.extend( $.ui.progressbar, {
	version: "1.8.1"
});

})( jQuery );

