// JScript File

// StarRating wrapper
function StarRatingWidget()
{
    this.isReadonly = false;
}

function StarRatingWidget(isEnabled)
{
    this.isReadonly = !isEnabled;
}

StarRatingWidget.prototype.onUserVoted = function()
{
    var quote = new QuoteElement(this.parentNode.parentNode.parentNode.parentNode);
    var value = $(this).text();
    document.ActionsFacade.VoteForQuote(quote.GetId(), value);
}

StarRatingWidget.prototype.getLine = function(mark, cls, caption)
{
    var curClass = (this.isReadonly)? "disabled" : cls;
    var result=$('<li><a class="'+curClass+'" title="'+caption+'"('+mark+'/5)">'+mark+'</a></li>');
    result.get(0).widget = this;
    result.bind("click", this.onUserVoted);
    return result;
}

StarRatingWidget.prototype.DisableRating = function()
{
    this.isReadonly = true;
}

StarRatingWidget.prototype.GetElement = function()
{
    var elem = $('<ul class="star-rating small-star"></ul>');
    this.ratingElement = $('<li class="current-rating" style="width: 50%;">Currently 2.5/5 Stars.</li>')
    elem.append(this.ratingElement);
       
    elem.append(this.getLine("1","one-star","???? ?????? ? ?????????"));
    elem.append(this.getLine("2","two-stars","?????? ???????? ?????????"));
    elem.append(this.getLine("3","three-stars","???????, ?????? ??????"));
    elem.append(this.getLine("4","four-stars","????????? ? ??????!"));
    elem.append(this.getLine("5","five-stars","??? ??????-??, ???????!"));
    
    var result=$('<span class="inline-rating"></span>');
    result.append(elem);
       
    return result;
}

StarRatingWidget.prototype.SetValue = function(value)
{
    var valuePercent = (value/5);
    this.ratingElement.css("width",valuePercent+"%");
}

StarRatingWidget.prototype.Render = function(parent)
{
    var value = parent.GetRating();
    this.SetValue(value);
}

StarRatingWidget.prototype.RatingDataUpdated = function(data)
{
    var ratingValue = data.State;
    if(ratingValue<0)
    {
         alert('?? ??? ?????????? ?? ??? ??????!');
         ratingValue*=-1;
    }
    this.SetValue(ratingValue);
}

StarRatingWidget.prototype.GetWidgetName = function()
{
    return "star_rating";
}

// Binds widget to quote control. Widget reacts on 'vote' event.
StarRatingWidget.prototype.SetParentControl = function(parentControl)
{
    $(parentControl.domNode).bind("vote_completed",
        function(event, action, data)
        {
            var ratingWidget = this.Widgets["star_rating"];
            StarRatingWidget.prototype.RatingDataUpdated.call(ratingWidget,data);
        }
    );
}

// Rating text data wrapper

function TextRatingWidget()
{
}

TextRatingWidget.prototype.GetElement = function()
{
    var elem = $("<span></span>");
    this.rating = $("<span class='quoteStatic'>???????: [1/5]</span>");
    elem.append(this.rating);
    this.votes = $("<span class='quoteStatic'>???????: 0</span>");
    elem.append(this.votes);
    
    return elem;
}

TextRatingWidget.prototype.GetWidgetName = function()
{
    return "text_rating";
}

TextRatingWidget.prototype.SetValue = function(value)
{
    this.rating.text("???????: ["+value+"/5]");
}

TextRatingWidget.prototype.SetVotesValue = function(value)
{
    this.votes.text("???????: "+value);
}

TextRatingWidget.prototype.RatingDataUpdated = function(data)
{
    var ratingValue = data.State;
    if(ratingValue<0)
    {
       ratingValue*=-1;
    }
    this.SetValue(ratingValue/100); 
}

TextRatingWidget.prototype.Render = function(parent)
{
    this.SetValue(parent.GetRating()/100);
    this.SetVotesValue(parent.GetVotes());
    
}

TextRatingWidget.prototype.SetParentControl = function(parentControl)
{
 $(parentControl.domNode).bind("vote_completed",
        function(event, action, data)
        {
            var ratingWidget = this.Widgets["text_rating"];
            TextRatingWidget.prototype.RatingDataUpdated.call(ratingWidget,data);
        }
    );
}

// Favorites widget allows to add and remove quote from favorites.
function FavoritesWidget()
{
}

FavoritesWidget.prototype.onClick = function()
{
    // Refactor this!
    var quote = new QuoteElement(this.parentNode.parentNode.parentNode);
    document.ActionsFacade.SwitchFavorites(quote.GetId());
}


FavoritesWidget.prototype.GetWidgetName = function()
{
    return "favorites";
}

FavoritesWidget.prototype.SetValue = function(value)
{
    this.image.attr("src", ("1"==value)? "icons/heart_delete.png" : "icons/heart_add.png");
    this.image.attr("title",("1"==value)? "??????? ?? ??????????" : "???????? ?? ?????????");
    this.widget.get(0).textContent = "";//;
}

FavoritesWidget.prototype.GetElement = function()
{
    var elem = $("<span class='quoteButton' style='margin-left:40px;'></span>");
    this.widget = $("<span ></span>");
    this.image = $("<img src='icons/heart_add.png' title='???????? ?? ?????????'/>");
    elem.append(this.image);
    elem.append(this.widget);
    this.image.bind('click',this.onClick);
    this.widget.bind('click',this.onClick);
        
    return elem;
}

FavoritesWidget.prototype.FavDataUpdated = function(data)
{
   var curValue = data.State;
   this.SetValue(curValue);
}

FavoritesWidget.prototype.SetParentControl = function(parentControl)
{
 $(parentControl.domNode).bind("fav_completed",
        function(event, action, data)
        {
            var favWidget = this.Widgets["favorites"];
            FavoritesWidget.prototype.FavDataUpdated.call(favWidget,data);
        }
    );
}



// 08.09.2010 7:29:13