﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

/// <reference path="jquery-1.2.6-vsdoc.js"/>

SCOTT.CSC.TipRating = function(clientId, behaviorId)
{
    this.ClientID = clientId;
    this.BehaviorID = behaviorId;
    this.Selector = '#' + clientId;
    this.Initialize();
};

SCOTT.CSC.TipRating.prototype =
{
    Name: 'SCOTT.CSC.TipRating',
    __typeName: 'SCOTT.CSC.TipRating',
    __class: true,
    AttachEvents: function() {
        var __app = this;

        var context = $(this.Selector);

        // fix the tip rating href which has an empty hash value by default
        $('div.tipRating a', context).attr('href', 'javascript:void(0);');

        this.AdjustRatingControl();
    },
    ChangeLoginStatus: function(isAuthenticated) {
        if (isAuthenticated) {
            var rater = $find(this.BehaviorID);
            rater.set_ReadOnly(false);
            var tipNum = rater.get_Tag();

            // get rating value
            var updateRating = function(value) {
                if (value > 0) {
                    rater.set_Rating(value, true);
                }
            };

            if (tipNum) {
                SCOTT.CSC.WebServices.Tips.GetRatingValue(tipNum, updateRating);
            }
        }
    },
    AdjustRatingControl: function() {
        var __app = this;
        var context = __app.Selector;

        var rater = $find(this.BehaviorID);
        rater.set_Rating = function(value, clientOnly) {
            var isAuthenticated = SCOTT.CSC.GetApplication().IsUserAuthenticated();
            if (isAuthenticated) {
                this._ratingValue = value;
                this._currentRating = value;

                if (this.get_isInitialized()) {
                    if ((value < 0) || (value > this._maxRatingValue)) {
                        return;
                    }

                    this._update();

                    AjaxControlToolkit.RatingBehavior.callBaseMethod(this, 'set_ClientState', [this._ratingValue]);
                    this.raisePropertyChanged('Rating');
                    this.raiseRated(this._currentRating);
                    this._waitingMode(true);

                    var tipNum = this.get_Tag();
                    var newRating = this.get_Rating();

                    if (!clientOnly) {
                        SCOTT.CSC.WebServices.Tips.RateTip(tipNum, newRating, function(ratingSummary) {
                            rater._receiveServerData.apply(rater, [ratingSummary, rater]);
                            //rater.set_ReadOnly(true);
                            $('div.tipSummary', context).text(ratingSummary);
                            $('div.tipRating', context).removeClass('Unrated');
                        });
                    }

                }
            }
            else {
                SCOTT.CSC.GetApplication().ShowAutoLogin();
            }
        };
    }
};

SCOTT.CSC.Extend(SCOTT.CSC.TipRating, SCOTT.CSC.UserControl);
