﻿// Register the namespace for client control.
Type.registerNamespace('Telerik.Samples.WebControls');

// ------------------------------------------------------------------------
// Definition of SitefinityRating class and properties
// ------------------------------------------------------------------------
Telerik.Samples.WebControls.SitefinityRating = function(element) {

    Telerik.Samples.WebControls.SitefinityRating.initializeBase(this, [element]);
    // ------------------------------------------------------------------------
    // SitefinityRating class properties
    // ------------------------------------------------------------------------
    this._contentId = null;
    this._providerName = null;
    this._radRatingId = null;
    this._ratingId = null;
    this._ratingCountId = null;
    this._ratingSum = null;
}

Telerik.Samples.WebControls.SitefinityRating.prototype = {
    // ------------------------------------------------------------------------
    // initialization and clean-up
    // ------------------------------------------------------------------------
    initialize: function() {
        Telerik.Samples.WebControls.SitefinityRating.callBaseMethod(this, 'initialize');
        Sys.Application.add_load(Function.createDelegate(this, this._onLoad));

    },
    dispose: function() {
        Telerik.Samples.WebControls.SitefinityRating.callBaseMethod(this, 'dispose');
    },
    // ------------------------------------------------------------------------
    // public functions
    // ------------------------------------------------------------------------
    rate: function(rating) {
        this.get_radRating().set_readOnly(true);
        Telerik.Samples.WebServices.RatingService.RateContent(this.get_contentId(), this.get_providerName(), rating, Function.createDelegate(this, this._onRateSuccess), this._onRateFailure);
    },
    // ------------------------------------------------------------------------
    // private functions
    // ------------------------------------------------------------------------
    _onLoad: function(sender, args) {
        var radRating = this.get_radRating();
        if (radRating) {
            radRating.add_rating(Function.createDelegate(this, this._onRatingPreview));
        }
    },
    _onRatingPreview: function(sender, args) {
        var rating = args.get_newValue();
        if(!this.get_radRating().get_readOnly())
            this.rate(rating);
    },
//    _onRateSuccess: function(caller, successData) {
//        var rating = this.get_rating();
//        var ratingCount = this.get_ratingCount();
//        var radRating = this.get_radRating();
//        var ratingSum = this.get_ratingSum();
//        var newRatingSum = ratingSum + radRating.get_value();
//        var newRatingCount = parseInt(ratingCount.textContent) + 1;
//        var newRating = newRatingSum / newRatingCount;
//        radRating.set_value(newRating);
//        rating.textContent = newRating;
//        ratingCount.textContent = newRatingCount;
    //    },

_onRateSuccess: function(caller, successData) {
    var rating = this.get_rating();
    var ratingCount = this.get_ratingCount();
    var radRating = this.get_radRating();
    var ratingSum = this.get_ratingSum();
    var newRatingSum = ratingSum + radRating.get_value();
    var newRatingCount = parseInt(ratingCount.textContent) + 1;
    var newRating = newRatingSum / newRatingCount;
    radRating.set_value(newRating);
    //first fromat newRating and then set it to the rating cotnrol
    rating.textContent = newRating;
    ratingCount.textContent = newRatingCount;
},


    _onRateFailure: function(errorData) {
    },
    // ------------------------------------------------------------------------
    // property accessors
    // ------------------------------------------------------------------------
    get_contentId: function() {
        return this._contentId;
    },
    set_contentId: function(value) {
        if (this._contentId !== value) {
            this._contentId = value;
            this.raisePropertyChanged('contentId');
        }
    },
    get_providerName: function() {
        return this._providerName;
    },
    set_providerName: function(value) {
        if (this._providerName !== value) {
            this._providerName = value;
            this.raisePropertyChanged('providerName');
        }
    },
    get_radRatingId: function() {
        return this._radRatingId;
    },
    set_radRatingId: function(value) {
        if (this._radRatingId !== value) {
            this._radRatingId = value;
            this.raisePropertyChanged('radRatingId');
        }
    },
    get_radRating: function() {
        var rating = $find(this.get_radRatingId());
        return rating;
    },
    get_ratingId: function() {
        return this._ratingId;
    },
    set_ratingId: function(value) {
        if (this._ratingId !== value) {
            this._ratingId = value;
            this.raisePropertyChanged('ratingId');
        }
    },
    get_rating: function() {
        var rating = $get(this._ratingId);
        return rating;
    },
    get_ratingCountId: function() {
        return this._ratingCountId;
    },
    set_ratingCountId: function(value) {
        if (this._ratingCountId !== value) {
            this._ratingCountId = value;
            this.raisePropertyChanged('ratingCountId');
        }
    },
    get_ratingCount: function() {
        var rating = $get(this.get_ratingCountId());
        return rating;
    },
    get_ratingSum: function() {
        return this._ratingSum;
    },
    set_ratingSum: function(value) {
        if (this._ratingSum !== value) {
            this._ratingSum = value;
            this.raisePropertyChanged('ratingSum');
        }
    }
}
Telerik.Samples.WebControls.SitefinityRating.registerClass('Telerik.Samples.WebControls.SitefinityRating', Sys.UI.Control);
