if (!DGN) var DGN = { };

DGN.formatNumber = function(number,decimals,decimalpoint,thousendseparator) {
    var n = (number || 0)+'';
    if( n == '0' ) return n;
    var c = Math.abs( decimals ) + 1 ? decimals : 2;
    var d = decimalpoint || ",";
    var t = thousendseparator || ".";
    var m = /(\d+)(?:(\.\d+)|)/.exec(n);
    var x = m[1].length % 3;
   	return ( x ? m[1].substr( 0, x ) + t : "" ) +
            m[1].substr( x ).replace( /(\d{3})(?=\d)/g, "$1" + t ) +
            ( c ? d + ( +m[2] ).toFixed( c ).substr( 2 ) : "" );
}
DGN.stripNumber = function(number) {
    // strip alles wat geen cijfer is, 0 als lege string overblijft
    return ((number + '').replace(/[^0-9]/g, '')) || 0;
}
DGN.EuroSlider = Class.create({
    initialize: function(name, min, max, current, step) {
        this.name = name;
        this.step = step || 1;
        this.min = min || 0;
        this.max = max || 100;
        this.value = current || 0;

        this.input = $('slider_'+name+'_input');
        this.track = $('slider_'+name+'_track');
        this.handle = $('slider_'+name+'_handle');
        this.indicator = $('slider_'+name+'_indicator') || null;

        this.slider = new Control.Slider('slider_'+name+'_handle',
                                         'slider_'+name+'_track',
                                         {range: $R(this.min,this.max)} );

        this.slider.options.onSlide = this.onSliderChange.bindAsEventListener(this);
        this.slider.options.onChange = this.onSliderChange.bindAsEventListener(this);

        this.input.observe('focus',this.onInputFocus.bindAsEventListener(this));
        this.input.observe('blur',this.onInputBlur.bindAsEventListener(this));
        this.input.observe('change',this.onInputChange.bindAsEventListener(this));
        this.input.controller = this;

        if(DGN.stripNumber(this.input.value) != '') {
            this.value = DGN.stripNumber(this.input.value);
            this.updateSlider();
        }
        this.updateInput();
    },
    updateInput: function() {
        if(this.input.hasFocus) {
            this.input.value = this.value;
        } else {
            this.input.value = DGN.formatNumber(this.value,0);
        }
    },
    updateSlider: function() {
        this.slider.setValue(this.value || 0);
        if(this.indicator != null) {
            this.indicator.style.width = (this.handle.offsetLeft + Math.ceil(this.handle.getWidth()/2)) + 'px';
        }
    },
    onSliderChange: function(value) {
        if(Object.isNumber(value)) {
            this.setValue(value);
        } else {
            // for some reason, this event gets fired a lot...
            // posibly there is a problem with mouse down events on the slider
        }
    },
    onInputFocus: function(e) {
        e.target.value = DGN.stripNumber(e.target.value);
    },
    onInputBlur: function(e) {
        e.target.value = DGN.formatNumber(DGN.stripNumber(e.target.value),0);
    },
    onInputChange: function(e) {
        e.target.controller.setValue(DGN.stripNumber(e.target.value));
    },
    setValue: function(value) {
        if(this.value != value) {
            this.value = Math.round(value/this.step)*this.step;
            if(!this.value) this.value = 0;
            this.updateSlider();
            this.updateInput();
        }
        if(DGN.stripNumber(this.input.value) != this.value) {
            this.updateInput();
        }
    },
    destroy: function() {
        if(this.input != null) {
            this.input.stopObserving();
            this.input.controller = null;
            this.input = null;
        }
        if(this.slider != null) {
            this.slider.dispose();
            this.slider = null;
        }
    }

});