c24_slider = {

    draft : 0.02,

    slider : [],

    add : function(s) {
        this.slider.push(s);
    },

    register : function() {

        $.each(this.slider, function(key, s) {

            var s_default = {
                animate: false,
                delay: 0,
                distance: 0,
                max: 100,
                min: 0,
                orientation: 'horizontal',
                range: false,
                step: 1,
                value: 0,
                values: null,
                type: 'linear',
                max_value_callback: ''
            };

            var s_settings = $.extend(s_default, s);

            $('#'+ s.id).slider(s_settings);

        });

    },

    max_value_default_callback : function() {
        return 1000;
    },

    startup_input_to_slider : function(value, max_value) {
        var real_percent = (100 * value) / max_value / 100;
        return Math.log(real_percent * Math.pow(Math.E, (100 * this.draft)) - real_percent + 1) / this.draft;
    },

    calculate_slider_to_input : function(slider, value) {

        var slider_max_value_callback = slider.slider('option', 'max_value_callback');

        var real_percent = ((Math.pow(Math.E, (value * this.draft)) -1) / (Math.pow(Math.E, (100 * this.draft)) -1)) * 100;
        return Math.round(slider_max_value_callback.call(this) / 100 * real_percent);

    },

    calculate_input_to_slider : function(slider, value) {

        var slider_max_value_callback = slider.slider('option', 'max_value_callback');

        var real_percent = (100 * value) / slider_max_value_callback.call(this) / 100;
        return Math.log(real_percent * Math.pow(Math.E, (100 * this.draft)) - real_percent + 1) / this.draft;

    },

    slider_button_handle : {

        sliding : false,
        sliding_timeout : 100,
        sliding_step : 1,
        sliding_source : null,
        sliding_slider : null,
        sliding_type : '',
        sliding_runs : 0,

        start : function(source, type) {

            this.sliding = true;
            this.sliding_type = type;

            this.sliding_slider = $('#slider_' + source);
            this.sliding_source = $('#resultform input[name=' + source + ']');

            this.slide();

        },

        stop : function() {

            this.sliding = false;
            this.sliding_timeout = 100;
            this.sliding_step = 1;
            this.sliding_source = null;
            this.sliding_slider = null;
            this.sliding_type = '';
            this.sliding_runs = 0;

        },

        slide : function() {

            // Sliding logic

            if (this.sliding != true) {
                return;
            }

            this.sliding_runs++;

            if (this.sliding_runs % 5 == 0) {
                this.sliding_step = this.sliding_step * 2;
            }

            // Slide

            var type = this.sliding_slider.slider('option', 'type');

            var min = this.sliding_slider.slider('option', 'min');
            var max = this.sliding_slider.slider('option', 'max');

            if (type == 'interval') {
                min = c24_slider.calculate_slider_to_input(this.sliding_slider, min);
                max = c24_slider.calculate_slider_to_input(this.sliding_slider, max);
            }

            var val = 0;

            if (this.sliding_type == 'plus') {

                val = parseInt(this.sliding_source.val());

                if (val + this.sliding_step <= max) {
                    this.sliding_source.val(val + this.sliding_step);
                    this.sliding_source.trigger('change');
                } else {

                    this.sliding_source.val(max);
                    this.sliding_source.trigger('change');

                    this.stop();
                    return;

                }

            } else {

                val = parseInt(this.sliding_source.val());

                if (val - this.sliding_step >= min) {
                    this.sliding_source.val(val - this.sliding_step);
                    this.sliding_source.trigger('change');
                } else {

                    this.sliding_source.val(min);
                    this.sliding_source.trigger('change');

                    this.stop();
                    return;

                }

            }

            setTimeout('c24_slider.slider_button_handle.slide()', this.sliding_timeout);

        }

    }

};