var sliderValueElem;
var ratingResultElem;
var doodadId;

var RatingSliderManager = Class.create();

RatingSliderManager.prototype = {

    initialize: function(doodadId, actionName, currentRating) {
    
        this.actionName = actionName;
        this.doodadId = doodadId;
        this.currentRating = currentRating;
        this.sliderValueElem = $('sliderValue');
        this.ratingResultElem = $('ratingResult');
        this.computedRatingElem = $('computedRating');
        this.handle = $('handle');
        this.handleDisabled = new Image();
        this.handleDisabled.src = '/images/rating_handle_disabled.gif';
        this.handleEnabled = new Image();
        this.handleEnabled.src = '/images/rating_handle.gif';
        this.submitButton = $('submitButton');
        this.value = 50;
        this.slider;
        this.submitButtonClickHandler;
        this.sliderMoved = false;
    },
    
    start: function() {
    
        this.submitButton.manager = this;
        this.submitButtonClickHandler = this.doRate.bindAsEventListener(this);
        Event.observe(this.submitButton, 'click', this.submitButtonClickHandler);
        this.handle.src = this.handleEnabled.src;
        $("sliderCo").style.backgroundImage = "url(/images/rating_track.gif)";
        
        var manager = this;
        this.slider = new Control.Slider('handle', 'track', {
            range: $R(0, 100),
            sliderValue: 50,
            handleImage: 'handle',
            onSlide: function(value) {
                manager.setValue(value);
                if (!manager.sliderMoved) {
                    //            manager.submitButton.setStyle({color : "#000000", border : "1px solid #000000"});
                    
                    manager.sliderMoved = true;
                }
            }
        })
    },
    
    setValue: function(value) {
        this.value = value;
        this.submitButton.innerHTML = "Заценить";
        //this.sliderValueElem.innerHTML = value;
        this.ratingResultElem.innerHTML = this.computeValue(value);
    },
    
    computeValue: function(value) {
        return value;
        //return this.currentRating.rate(value);
    },
    
    doRate: function(event) {
    
        if (!this.sliderMoved) {
            this.submitButton.innerHTML = "Сперва поставьте движком оценку!";
            return;
        }
        
        var url = '/4duk/' + this.actionName + '.action?stuffId=' + this.doodadId + '&rating=' + Math.round(this.value);
        
        new Ajax.Request(url, {
            method: 'get',
            onComplete: function(transport) {
                //    new
                ((navigator.appName.indexOf("Microsoft") != -1) ? window["flashPlayer"] : document["flashPlayer"]).updateRating(transport.responseText);
                //    old
                //          this.computedRatingElem.innerHTML = transport.responseText;
            }
.bind(this)
        });
        
        Event.stopObserving(this.submitButton, 'click', this.submitButtonClickHandler);
        this.slider.disabled = true;
        this.handle.src = this.handleDisabled.src;
        
        this.submitButton.setStyle({
            color: "979797",
            border: "1px solid #979797"
        });
        
        this.submitButton.innerHTML = 'Спасиба';
        
        $("sliderCo").style.backgroundImage = "url(/images/rating_track_disabled.gif)";
        
    }
}

var RangeRating = Class.create();

RangeRating.prototype = {

    initialize: function(count, cumulatedRating, weighted) {
    
        this.count = count;
        this.cumulatedRating = cumulatedRating;
        this.weighted = weighted;
        
    },
    
    rate: function(rating) {
    
        this.cumulatedRating += rating;
        this.count++;
        this.weighted = this.cumulatedRating / this.count;
        return this.weighted;
    },
    
    getWeighted: function() {
        return this.weighted;
    }
}

function initRatingSlider(doodadId, actionName, currentRating) {
    new RatingSliderManager(doodadId, actionName, currentRating).start();
}

