// DDC mods to remove extent and use low and high values - Jason

/*----------------------------------------------------------------------------\
|                                Range Class                                  |
|-----------------------------------------------------------------------------|
|                         Created by Erik Arvidsson                           |
|                  (http://webfx.eae.net/contact.html#erik)                   |
|                      For WebFX (http://webfx.eae.net/)                      |
|-----------------------------------------------------------------------------|
| Used to  model the data  used  when working  with  sliders,  scrollbars and |
| progress bars.  Based  on  the  ideas of  the javax.swing.BoundedRangeModel |
| interface  defined  by  Sun  for  Java;   http://java.sun.com/products/jfc/ |
| swingdoc-api-1.0.3/com/sun/java/swing/BoundedRangeModel.html                |
|-----------------------------------------------------------------------------|
|                Copyright (c) 2002, 2005, 2006 Erik Arvidsson                |
|-----------------------------------------------------------------------------|
| Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| use this file except in compliance with the License.  You may obtain a copy |
| of the License at http://www.apache.org/licenses/LICENSE-2.0                |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| Unless  required  by  applicable law or  agreed  to  in  writing,  software |
| distributed under the License is distributed on an  "AS IS" BASIS,  WITHOUT |
| WARRANTIES OR  CONDITIONS OF ANY KIND,  either express or implied.  See the |
| License  for the  specific language  governing permissions  and limitations |
| under the License.                                                          |
|-----------------------------------------------------------------------------|
| 2002-10-14 | Original version released                                      |
| 2005-10-27 | Use Math.round instead of Math.floor                           |
| 2006-05-28 | Changed license to Apache Software License 2.0.                |
|-----------------------------------------------------------------------------|
| Created 2002-10-14 | All changes are in the log above. | Updated 2006-05-28 |
\----------------------------------------------------------------------------*/


function Range() {
	this._lowValue = 0;
	this._highValue = 0;
	this._minimum = 0;
	this._maximum = 100;
	this._minRange = 1;

	this._isChanging = false;
}

Range.prototype.setLowValue = function (value) {
	value = Math.round(parseFloat(value));
	if (isNaN(value)) return;
	if (this._lowValue != value) {
		if (value < this._minimum)
			this._lowValue = this._minimum;
		else if (value > this._maximum)
			this._lowValue = this._maximum;
		else
			this._lowValue = value;
		if (this._lowValue + this._minRange >= this._highValue){
			if( this._lowValue + this._minRange >= this._maximum){
				this._highValue = this._maximum;
				this._lowValue = this._maximum - this._minRange;
			}
			else{
				this._highValue = this._lowValue + this._minRange;
			}
		}
		if (!this._isChanging && typeof this.onchange == "function")
			 this.onchange();
	}
};

Range.prototype.getLowValue = function () {
	return this._lowValue;
};

Range.prototype.setHighValue = function (value) {
	value = Math.round(parseFloat(value));
	if (isNaN(value)) return;
	if (this._highValue != value) {
		if (value < this._minimum)
			this._highValue = this._minimum;
		else if (value > this._maximum)
			this._highValue = this._maximum;
		else
			this._highValue = value;
		if (this._highValue - this._minRange <= this._lowValue){
			if( this._highValue - this._minRange <= this._minimum){
				this._lowValue = this._minimum;
				this._highValue = this._minimum + this._minRange;
			}
			else{
				this._lowValue = this._highValue - this._minRange;
			}
		}
		if (!this._isChanging && typeof this.onchange == "function")
			 this.onchange();
	}
};

Range.prototype.getHighValue = function () {
	return this._highValue;
};

Range.prototype.setMinimum = function (minimum) {
	if (this._minimum != minimum) {
		var oldIsChanging = this._isChanging;
		this._isChanging = true;

		this._minimum = minimum;

		if (minimum > this._lowValue)
			this.setLowValue(minimum);
		if (minimum + this._minRange > this._maximum) {
			this.setMaximum(minimum + this._minRange);
			this.setLowValue(minimum)
		}

		this._isChanging = oldIsChanging;
		if (!this._isChanging && typeof this.onchange == "function")
			this.onchange();
	}
};

Range.prototype.getMinimum = function () {
	return this._minimum;
};

Range.prototype.setMaximum = function (maximum) {
	if (this._maximum != maximum) {
		var oldIsChanging = this._isChanging;
		this._isChanging = true;

		this._maximum = maximum;

		if (maximum < this._highValue)
			this.setHighValue(maximum);
		if (maximum < this._minimum + this._minRange) {
			this.setMinimum(maximum - this._minRange);
			this.setHighValue(this._maximum);
		}

		this._isChanging = oldIsChanging;
		if (!this._isChanging && typeof this.onchange == "function")
			this.onchange();
	}
};

Range.prototype.getMaximum = function () {
	return this._maximum;
};

Range.prototype.setMinRange = function(minRange){
	if(this._minRange != minRange){
		var oldIsChanging = this._isChanging;
		this._isChanging = true;
		
		if(minRange > this._maximum - this._minimum) this._maximum = this._minimum + minRange;			this._minRange = minRange;
		if(this._highValue - this._lowValue < this._minRange){
			this.setHighValue(this._lowValue + this._minRange);
		}
		
		this._isChanging = oldIsChanging;
		if(!this._isChanging && typeof this.onchange == "function")
			this.onchange();
	}
};

Range.prototype.getMinRange = function () {
	return this._minRange;
};
