﻿/// <reference name="MicrosoftAjax.js"/>



Type.registerNamespace("TUI.Web.UI.WebControls");



TUI.Web.UI.WebControls.RatingStarImageSize = function() {}
TUI.Web.UI.WebControls.RatingStarImageSize.prototype =
{
    small: 0,
    medium: 1,
    large: 2
}
TUI.Web.UI.WebControls.RatingStarImageSize.registerEnum('TUI.Web.UI.WebControls.RatingStarImageSize');



TUI.Web.UI.WebControls.Rating = function(element) {
    TUI.Web.UI.WebControls.Rating.initializeBase(this, [element]);
    this._ratedValue = null;
    this._hiddenField = null;
    this._$element = $(element);
    this._imageSize = TUI.Web.UI.WebControls.RatingStarImageSize.medium;

    if (this._clientSideOnly = this._$element.children().length == 0)
    {
        // Clientside creation
        if (element.tagName.toLowerCase() != 'span')
        {
            throw Error.argument('element', 'An error occured during clientside creation of a Rating control. The top element must be of type SPAN.');
        }

        this._$element.addClass('rating rating-medium');
        this._$element.append("<span class='rating-heading'></span>");
        this._$element.append("<img src='/util/images/clear.gif' /><img src='/util/images/clear.gif' /><img src='/util/images/clear.gif' /><img src='/util/images/clear.gif' /><img src='/util/images/clear.gif' />");
        this._$element.append("<span class='number-of-raters'></span>");
        
        if ($.browser.msie && $.browser.version <= 6)
        {
            this._$element.addClass('rating-ie6');
        }
    }

    this._$ratingStarImages = this._$element.children("img");
}

TUI.Web.UI.WebControls.Rating.prototype = {
    initialize: function()
    {
        TUI.Web.UI.WebControls.Rating.callBaseMethod(this, 'initialize');

        if (this.get_allowRating())
        {
            for (var i = 0; i < this._$ratingStarImages.length; i++)
            {
                $addHandlers
                (
                    this._$ratingStarImages.get(i),
                    {
                        "click" : this.ratingStarImageOnClick,
                        "mouseover" : this.ratingStarImageOnMouseOver,
                        "mouseout" : this.ratingStarImageOnMouseOut
                    },
                    this
                );
            }
        }
        
        if (this._clientSideOnly)
        {
            // Rating displays needs to be initialized when created on the client
            this.resetRatingDisplay();
        }
    },
    dispose: function()
    {
        for (var i = 0; i < this._$ratingStarImages.length; i++)
        {
            $clearHandlers(this._$ratingStarImages.get(i));
        }
        TUI.Web.UI.WebControls.Rating.callBaseMethod(this, 'dispose');
    },
    ratingStarImageOnClick: function(e)
    {
        this.get_hiddenField().value = this.get_element().value = this._$ratingStarImages.index(e.target) + 1;
        if (this.get_autoPostBackExpression())
        {
            eval(this.get_autoPostBackExpression());
        }
    },
    ratingStarImageOnMouseOver: function(e)
    {
        this.resetRatingDisplay(this._$ratingStarImages.index(e.target) + 1);
    },
    ratingStarImageOnMouseOut: function(e)
    {
        this.resetRatingDisplay();        
    },
    resetRatingDisplay: function(ratedValue) 
    {
        if (!ratedValue)
        {
            ratedValue = this.get_ratedValue();
        }
        
        this._$ratingStarImages.removeClass('rated unrated');
        
        for (var i = 0; i < ratedValue; i++)
        {
            if (i < ratedValue)
            {
                this._$ratingStarImages.eq(i).addClass('rated');
            }
            else
            {
                this._$ratingStarImages.eq(i).addClass('unrated');    
            }   
        }
    },
    get_allowRating: function() { return this._allowRating; },
    set_allowRating: function(value)
    {
        this._allowRating = value;
        
        if (value)
        {
            this.removeCssClass('rating-disabled');
        }
        else
        {
            this.addCssClass('rating-disabled');
        }
    },
    get_numberOfRaters: function() { return this._numberOfRaters; },
    set_numberOfRaters: function(value) { this._numberOfRaters = value; },
    get_imageSize: function() { return this._imageSize; },
    set_imageSize: function(value)
    {
        this._imageSize = value;
        this._$element.removeClass('rating-small rating-medium rating-large');
        
        switch (value)
        {
            case TUI.Web.UI.WebControls.RatingStarImageSize.small:
                this._$element.addClass('rating-small');
                break;
            case TUI.Web.UI.WebControls.RatingStarImageSize.medium:
                this._$element.addClass('rating-medium');
                break;
            case TUI.Web.UI.WebControls.RatingStarImageSize.large:
                this._$element.addClass('rating-large');
                break;
        }
    },
    get_hiddenField: function() { return this._hiddenField; },
    set_hiddenField: function(value) { this._hiddenField = value; },
    get_autoPostBackExpression: function() { return this._autoPostBackExpression; },
    set_autoPostBackExpression: function(value) { this._autoPostBackExpression = value; },
    get_ratedValue: function()
    {
        if (this._clientSideOnly)
        {
            return this._ratedValue;
        }
        else
        {
            return this.get_hiddenField().value;
        }
    },
    set_ratedValue: function(value) { this._ratedValue = value; }
}
TUI.Web.UI.WebControls.Rating.registerClass('TUI.Web.UI.WebControls.Rating', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();