var Radar = Class.create  (
{
    range: 10,
    maxRadius: 20000,
    slider: null,
    initialize:function(km)
	{
		this.slider = new Control.Slider('radarRadiusSliderHandle', 'radarRadiusSlider',
        {
            range: $R(0, this.range),
            sliderValue: this.convertKmToValue(km)
        });
        this.slider.options.onSlide = function(value)
        {
            var radius = this.convertValueToMeters(value);
            this.update(radius);
        }.bind(this);

        this.update(km * 1000);
	},

    update: function(meters)
    {
        var radius = this.roundRadius(meters);

        $('gmapRadarRadius').value = radius / 1000; //prevod do km
        $('radarRadiusSliderValue').update(this.getRadiusText(radius));

        this.showGmapRadarCircle();
    },

    showGmapRadarCircle: function()
    {
        if(!gmap || !gmap.marker)
        {
            setTimeout(this.showGmapRadarCircle.bind(this), 100);
            return;
        }
        gmap.setRadarCircle();
    },

    convertValueToMeters: function(value)
    {
        var a = (this.maxRadius - 1) / (Math.exp(this.range) - 1);
        var b = 1 - a;
        return a * Math.exp(value) + b;
    },

    convertKmToValue: function(km)
    {
        var a = (this.maxRadius - 1) / (Math.exp(this.range) - 1);
        var b = 1 - a;
        return Math.log((km * 1000 - b) / a);
    },

    roundRadius: function(radius)
    {
        if(radius >= 1000)
        {
            return Math.round(radius / 1000) * 1000;
        }
        else if(radius >= 100)
        {
            return Math.round(radius / 10) * 10;
        }
        return Math.round(radius);
    },

    getRadiusText: function(radius)
    {
        return radius >= 1000 ? (radius / 1000) + "km" : radius + "m";
    }
});


