﻿/*----------------------------------------------------------------------------\
|                                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._value=0;this._minimum=0;this._maximum=100;this._extent=0;this._isChanging=false}Range.prototype.setValue=function(A){A=Math.round(parseFloat(A));if(isNaN(A)){return }if(this._value!=A){if(A+this._extent>this._maximum){this._value=this._maximum-this._extent}else{if(A<this._minimum){this._value=this._minimum}else{this._value=A}}if(!this._isChanging&&typeof this.onchange=="function"){this.onchange()}}};Range.prototype.getValue=function(){return this._value};Range.prototype.setExtent=function(A){if(this._extent!=A){if(A<0){this._extent=0}else{if(this._value+A>this._maximum){this._extent=this._maximum-this._value}else{this._extent=A}}if(!this._isChanging&&typeof this.onchange=="function"){this.onchange()}}};Range.prototype.getExtent=function(){return this._extent};Range.prototype.setMinimum=function(A){if(this._minimum!=A){var B=this._isChanging;this._isChanging=true;this._minimum=A;if(A>this._value){this.setValue(A)}if(A>this._maximum){this._extent=0;this.setMaximum(A);this.setValue(A)}if(A+this._extent>this._maximum){this._extent=this._maximum-this._minimum}this._isChanging=B;if(!this._isChanging&&typeof this.onchange=="function"){this.onchange()}}};Range.prototype.getMinimum=function(){return this._minimum};Range.prototype.setMaximum=function(B){if(this._maximum!=B){var A=this._isChanging;this._isChanging=true;this._maximum=B;if(B<this._value){this.setValue(B-this._extent)}if(B<this._minimum){this._extent=0;this.setMinimum(B);this.setValue(this._maximum)}if(B<this._minimum+this._extent){this._extent=this._maximum-this._minimum}if(B<this._value+this._extent){this._extent=this._maximum-this._value}this._isChanging=A;if(!this._isChanging&&typeof this.onchange=="function"){this.onchange()}}};Range.prototype.getMaximum=function(){return this._maximum};
function Timer(A){this._pauseTime=typeof A=="undefined"?1000:A;this._timer=null;this._isStarted=false}Timer.prototype.start=function(){if(this.isStarted()){this.stop()}var A=this;this._timer=window.setTimeout(function(){if(typeof A.ontimer=="function"){A.ontimer()}},this._pauseTime);this._isStarted=false};Timer.prototype.stop=function(){if(this._timer!=null){window.clearTimeout(this._timer)}this._isStarted=false};Timer.prototype.isStarted=function(){return this._isStarted};Timer.prototype.getPauseTime=function(){return this._pauseTime};Timer.prototype.setPauseTime=function(A){this._pauseTime=A};
Slider.isSupported=typeof document.createElement!="undefined"&&typeof document.documentElement!="undefined"&&typeof document.documentElement.offsetWidth=="number";function Slider(B,D,C){if(!B){return }this._orientation=C||"horizontal";this._range=new Range();this._range.setExtent(0);this._blockIncrement=10;this._unitIncrement=1;this._timer=new Timer(100);if(Slider.isSupported&&B){this.document=B.ownerDocument||B.document;this.element=B;this.element.slider=this;this.element.unselectable="on";this.element.className=this._orientation+" "+this.classNameTag+" "+this.element.className;this.line=this.document.createElement("DIV");this.line.className="line";this.line.unselectable="on";this.line.appendChild(this.document.createElement("DIV"));this.element.appendChild(this.line);this.handle=this.document.createElement("DIV");this.handle.className="handle";this.handle.unselectable="on";this.handle.appendChild(this.document.createElement("DIV"));this.handle.firstChild.appendChild(this.document.createTextNode(String.fromCharCode(160)));this.element.appendChild(this.handle)}this.input=D;var A=this;this._range.onchange=function(){A.recalculate();if(typeof A.onchange=="function"){A.onchange()}};if(Slider.isSupported&&B){this.element.onfocus=Slider.eventHandlers.onfocus;this.element.onblur=Slider.eventHandlers.onblur;this.element.onmousedown=Slider.eventHandlers.onmousedown;this.element.onmouseover=Slider.eventHandlers.onmouseover;this.element.onmouseout=Slider.eventHandlers.onmouseout;this.element.onkeydown=Slider.eventHandlers.onkeydown;this.element.onkeypress=Slider.eventHandlers.onkeypress;this.element.onmousewheel=Slider.eventHandlers.onmousewheel;this.handle.onselectstart=this.element.onselectstart=function(){return false};this._timer.ontimer=function(){A.ontimer()};window.setTimeout(function(){A.recalculate()},1)}else{this.input.onchange=function(E){A.setValue(A.input.value)}}}Slider.eventHandlers={getEvent:function(B,A){if(!B){if(A){B=A.document.parentWindow.event}else{B=window.event}}if(!B.srcElement){var A=B.target;while(A!=null&&A.nodeType!=1){A=A.parentNode}B.srcElement=A}if(typeof B.offsetX=="undefined"){B.offsetX=B.layerX;B.offsetY=B.layerY}return B},getDocument:function(A){if(A.target){return A.target.ownerDocument}return A.srcElement.document},getSlider:function(B){var A=B.target||B.srcElement;while(A!=null&&A.slider==null){A=A.parentNode}if(A){return A.slider}return null},getLine:function(B){var A=B.target||B.srcElement;while(A!=null&&A.className!="line"){A=A.parentNode}return A},getHandle:function(C){var B=C.target||C.srcElement;var A=/handle/;while(B!=null&&!A.test(B.className)){B=B.parentNode}return B},onfocus:function(B){var A=this.slider;A._focused=true;A.handle.className="handle hover"},onblur:function(B){var A=this.slider;A._focused=false;A.handle.className="handle"},onmouseover:function(B){B=Slider.eventHandlers.getEvent(B,this);var A=this.slider;if(B.srcElement==A.handle){A.handle.className="handle hover"}},onmouseout:function(B){B=Slider.eventHandlers.getEvent(B,this);var A=this.slider;if(B.srcElement==A.handle&&!A._focused){A.handle.className="handle"}},onmousedown:function(D){D=Slider.eventHandlers.getEvent(D,this);var A=this.slider;if(A.element.focus){A.element.focus()}Slider._currentInstance=A;var C=A.document;if(C.addEventListener){C.addEventListener("mousemove",Slider.eventHandlers.onmousemove,true);C.addEventListener("mouseup",Slider.eventHandlers.onmouseup,true)}else{if(C.attachEvent){C.attachEvent("onmousemove",Slider.eventHandlers.onmousemove);C.attachEvent("onmouseup",Slider.eventHandlers.onmouseup);C.attachEvent("onlosecapture",Slider.eventHandlers.onmouseup);A.element.setCapture()}}if(Slider.eventHandlers.getHandle(D)){Slider._sliderDragData={screenX:D.screenX,screenY:D.screenY,dx:D.screenX-A.handle.offsetLeft,dy:D.screenY-A.handle.offsetTop,startValue:A.getValue(),slider:A}}else{var B=Slider.eventHandlers.getLine(D);A._mouseX=D.offsetX+(B?A.line.offsetLeft:0);A._mouseY=D.offsetY+(B?A.line.offsetTop:0);A._increasing=null;A.ontimer()}},onmousemove:function(F){F=Slider.eventHandlers.getEvent(F,this);if(Slider._sliderDragData){var C=Slider._sliderDragData.slider;var A=C.getMaximum()-C.getMinimum();var B,G,E;if(C._orientation=="horizontal"){B=C.element.offsetWidth-C.handle.offsetWidth;G=F.screenX-Slider._sliderDragData.dx;E=Math.abs(F.screenY-Slider._sliderDragData.screenY)>100}else{B=C.element.offsetHeight-C.handle.offsetHeight;G=C.element.offsetHeight-C.handle.offsetHeight-(F.screenY-Slider._sliderDragData.dy);E=Math.abs(F.screenX-Slider._sliderDragData.screenX)>100}C.setValue(E?Slider._sliderDragData.startValue:C.getMinimum()+A*G/B);return false}else{var C=Slider._currentInstance;if(C!=null){var D=Slider.eventHandlers.getLine(F);C._mouseX=F.offsetX+(D?C.line.offsetLeft:0);C._mouseY=F.offsetY+(D?C.line.offsetTop:0)}}},onmouseup:function(C){C=Slider.eventHandlers.getEvent(C,this);var A=Slider._currentInstance;var B=A.document;if(B.removeEventListener){B.removeEventListener("mousemove",Slider.eventHandlers.onmousemove,true);B.removeEventListener("mouseup",Slider.eventHandlers.onmouseup,true)}else{if(B.detachEvent){B.detachEvent("onmousemove",Slider.eventHandlers.onmousemove);B.detachEvent("onmouseup",Slider.eventHandlers.onmouseup);B.detachEvent("onlosecapture",Slider.eventHandlers.onmouseup);A.element.releaseCapture()}}if(Slider._sliderDragData){Slider._sliderDragData=null}else{A._timer.stop();A._increasing=null}Slider._currentInstance=null},onkeydown:function(C){C=Slider.eventHandlers.getEvent(C,this);var B=this.slider;var A=C.keyCode;switch(A){case 33:B.setValue(B.getValue()+B.getBlockIncrement());break;case 34:B.setValue(B.getValue()-B.getBlockIncrement());break;case 35:B.setValue(B.getOrientation()=="horizontal"?B.getMaximum():B.getMinimum());break;case 36:B.setValue(B.getOrientation()=="horizontal"?B.getMinimum():B.getMaximum());break;case 38:case 39:B.setValue(B.getValue()+B.getUnitIncrement());break;case 37:case 40:B.setValue(B.getValue()-B.getUnitIncrement());break}if(A>=33&&A<=40){return false}},onkeypress:function(B){B=Slider.eventHandlers.getEvent(B,this);var A=B.keyCode;if(A>=33&&A<=40){return false}},onmousewheel:function(B){B=Slider.eventHandlers.getEvent(B,this);var A=this.slider;if(A._focused){A.setValue(A.getValue()+B.wheelDelta/120*A.getUnitIncrement());return false}}};Slider.prototype.classNameTag="dynamic-slider-control",Slider.prototype.setValue=function(A){this._range.setValue(A);this.input.value=this.getValue()};Slider.prototype.getValue=function(){return this._range.getValue()};Slider.prototype.setMinimum=function(A){this._range.setMinimum(A);this.input.value=this.getValue()};Slider.prototype.getMinimum=function(){return this._range.getMinimum()};Slider.prototype.setMaximum=function(A){this._range.setMaximum(A);this.input.value=this.getValue()};Slider.prototype.getMaximum=function(){return this._range.getMaximum()};Slider.prototype.setUnitIncrement=function(A){this._unitIncrement=A};Slider.prototype.getUnitIncrement=function(){return this._unitIncrement};Slider.prototype.setBlockIncrement=function(A){this._blockIncrement=A};Slider.prototype.getBlockIncrement=function(){return this._blockIncrement};Slider.prototype.getOrientation=function(){return this._orientation};Slider.prototype.setOrientation=function(A){if(A!=this._orientation){if(Slider.isSupported&&this.element){this.element.className=this.element.className.replace(this._orientation,A)}this._orientation=A;this.recalculate()}};Slider.prototype.recalculate=function(){if(!Slider.isSupported||!this.element){return }var C=this.element.offsetWidth;var E=this.element.offsetHeight;var A=this.handle.offsetWidth;var D=this.handle.offsetHeight;var F=this.line.offsetWidth;var B=this.line.offsetHeight;if(this._orientation=="horizontal"){this.handle.style.left=(C-A)*(this.getValue()-this.getMinimum())/(this.getMaximum()-this.getMinimum())+"px";this.handle.style.top=(E-D)/2+"px";this.line.style.top=(E-B)/2+"px";this.line.style.left=A/2+"px";this.line.style.width=Math.max(0,C-A-2)+"px";this.line.firstChild.style.width=Math.max(0,C-A-4)+"px"}else{this.handle.style.left=(C-A)/2+"px";this.handle.style.top=E-D-(E-D)*(this.getValue()-this.getMinimum())/(this.getMaximum()-this.getMinimum())+"px";this.line.style.left=(C-F)/2+"px";this.line.style.top=D/2+"px";this.line.style.height=Math.max(0,E-D-2)+"px";this.line.firstChild.style.height=Math.max(0,E-D-4)+"px"}};Slider.prototype.ontimer=function(){var B=this.handle.offsetWidth;var D=this.handle.offsetHeight;var A=this.handle.offsetLeft;var C=this.handle.offsetTop;if(this._orientation=="horizontal"){if(this._mouseX>A+B&&(this._increasing==null||this._increasing)){this.setValue(this.getValue()+this.getBlockIncrement());this._increasing=true}else{if(this._mouseX<A&&(this._increasing==null||!this._increasing)){this.setValue(this.getValue()-this.getBlockIncrement());this._increasing=false}}}else{if(this._mouseY>C+D&&(this._increasing==null||!this._increasing)){this.setValue(this.getValue()-this.getBlockIncrement());this._increasing=false}else{if(this._mouseY<C&&(this._increasing==null||this._increasing)){this.setValue(this.getValue()+this.getBlockIncrement());this._increasing=true}}}this._timer.start()};

