﻿//<![CDATA[
function KA_Slider(options) {
    this.sliderBID = options.sliderBID;
    this.tbSliderValueID = options.tbSliderValueID;
    this.callback = options.callback;

    this.tbSliderValue = $get(this.tbSliderValueID);
    if (this.callback.length > 0) {
        this.callback = decodeURI(this.callback);
        this.callback = unescape(this.callback);
        
        var index = this.callback.lastIndexOf(')');
        if (index > 0) {
            this.callback = this.callback.substring(0, index) + ',';
        } else {
            this.callback = this.callback + '(';
        }
    }
}
KA_Slider.prototype = {
    get_tbSliderValue: function() {
        if (this.tbSliderValue == null) {
            this.tbSliderValue = $get(this.tbSliderValueID);
        }
        return this.tbSliderValue;
    },
    onSliderChanged: function() {
        var val = parseInt($find(this.sliderBID).get_Value(), 10);
        var tbSliderValue = this.get_tbSliderValue();
        if (tbSliderValue != null) {
            var fireOnChange = tbSliderValue.getAttribute('fireOnChange');
            if (fireOnChange != 'true')
                tbSliderValue.value = val;
            if (this.callback.length > 0) {
                eval(this.callback + val + ');');
            }
            setTimeout(function() { tbSliderValue.removeAttribute('fireOnChange'); }, 200);
        }
    },
    appelerOnSliderChanged: function() {

        var _tbSliderValue = this.get_tbSliderValue();
        if (_tbSliderValue.disabled)
            this.disableSlider();

        var _this = this;
        return (function() {
            if (!_tbSliderValue.disabled)
                _this.onSliderChanged();
        });
    },
    onSliderValueChanged: function() {
        var tbSliderValue = this.get_tbSliderValue();
        var val = parseInt(tbSliderValue.value, 10);
        if (val > 0) {
            tbSliderValue.setAttribute('fireOnChange', true);
            $find(this.sliderBID).set_Value(val);
        }
    },
    onSliderValueKeyPress: function(e) {
        var charCode;
        if (e && e.which) {
            charCode = e.which;
        } else if (window.event) {
            e = window.event;
            charCode = e.keyCode;
        }
        if (charCode == 13) {
            var tbSliderValue = this.get_tbSliderValue();
            tbSliderValue.setAttribute('fireOnChange', true);
            this.onSliderValueChanged();
            e.cancelBubble = true;
            if (e.stopPropagation)
                e.stopPropagation();
            return false;
        }
        return true;
    },
    disableSlider: function() {
        var slider = $find(this.sliderBID);
        if (slider != null) {
            $clearHandlers(slider._handle);
            $clearHandlers(slider._railElement);
        }
    }
}
//]]

