/**
 * @author fred
 */
// TODO: add options to animate
var DynamicSlider = Class.create( MatchedSlider,
{
	lastValue:	0,
	
	startDrag: function( $super, event )
	{
		this.lastValue = this.values[0];
		$super(event);
	},
	
	update: function( $super, event )
	{
		this.lastValue = this.values[0];
		$super(event);
	},
	
	setRange: function( range )
	{
		this.range = range;
		this.maximum = range.end;
    	this.minimum = range.start;
	}
});

var DynamicSliders = Class.create( MatchedSliders,
{
	animTime:		0.5,
	minAnimDist:	20,
	
	createSlider: function( handle, track, options )
	{
		return new DynamicSlider( handle, track, options );
	},
	
	setRange: function( range, updateStyle )
	{
		var oldRange		= this.sliders[0].range;
		var currentValue	= this.sliders[0].values[0];
		
		for( var i=0; i<this.sliders.length; i++ )
			this.sliders[i].setRange( range );
		
		switch( updateStyle || 'smooth' )
		{
			case 'jump':
				this.setValue( currentValue );
					
				break;
				
			case 'smooth':
				// TODO: correct this calculation - doesn't work correctly on some range changes
				this.animate(
					( (currentValue - oldRange.start) / oldRange.end ) * range.end + range.start,
					currentValue,
					true
				);
				break;
				
			case 'wait':
		}
	},
	
	getRange: function()
	{
		return this.sliders[0].range;
	},
	
	recieveChange: function( $super, i, v )
	{
		if( Math.abs(this.sliders[i].lastValue - v) > this.minAnimDist )
		{
			this.animate(this.sliders[i].lastValue, v, true);
			
			if( this.onChange )
				this.onChange(this.sliders[i].values[0]);
		}
		else 
			$super(i, v);
	},
	
	animate: function( start, end, noEvents )
	{
		this.setDisabled();
		
		new Effect.Tween( null, start, end, {
			transition:		Effect.Transitions.easeOutQuad,
			duration:		this.animTime,
			afterFinish:	this.setEnabled.bind(this)
		}, noEvents? this.forceValueNoEvent.bind(this) : this.forceValue.bind(this));
	},
	
	animateTo: function(end, noEvents)
	{
		this.animate( this.sliders[0].values[0], end, noEvents );
	},
	
	forceValue: function( v, noEvent )
	{
		for (var i = 0; i < this.sliders.length; i++) 
			this.sliders[i].safeSetValue(v);
		
		if (!noEvent && this.onChange) 
			this.onChange(v);
	},
	
	forceValueNoEvent: function(v)
	{
		this.forceValue( v, true );
	}
});
