﻿var ThumbRatingJs = function(contentId, userName, ip, containerId) {
    this.contentId = contentId;
    this.userName = userName;
    this.ip = ip;
    this.init(containerId);
    this.getRating();
}
ThumbRatingJs.prototype.init = function(containerId){
    this.containerId = containerId;
    this.container = document.getElementById(containerId);

    this.voteContainer = this.container.children[0];
    this.btnRatingUp = this.voteContainer.children[0];
    this.btnRatinDown = this.voteContainer.children[1];
    this.userVoteResult = this.container.children[1];
    this.ratingNum = this.container.children[2];

}

ThumbRatingJs.prototype.getRatingReturnFunc = function(result, eventArgs, elem) {
eventArgs.SetRatingImp(eventArgs.ratingNum, eventArgs.userVoteResult, eventArgs.voteContainer, result);
}

ThumbRatingJs.prototype.setRatingReturnFunc = function(result, eventArgs) {
}

ThumbRatingJs.prototype.getRating = function() {
    Milanor.Wmj.WMJWebSite.WebServices.ThumbRating.GetRating(this.contentId, this.ip, this.userName, this.getRatingReturnFunc, null, this);
    return false;
}

ThumbRatingJs.prototype.setRating = function(vote) {
    Milanor.Wmj.WMJWebSite.WebServices.ThumbRating.SetRating(this.contentId, this.userName, this.ip, vote, this.getRatingReturnFunc, null, this);
    return false;
}

ThumbRatingJs.prototype.SetRatingImp = function(ratingNum, userVoteResult, voteContainer, result) {
      if (result.Rating == 0)
            ratingNum.className = "zero";
      else if (result.Rating > 0)
            ratingNum.className = "positive";
      else if (result.Rating < 0)
            ratingNum.className = "negative";

      var userVote =
    ratingNum.innerHTML = result.Rating > 0 ? '+' + result.Rating : result.Rating;

      if (result.HasRightToVote) {
            voteContainer.style.display = '';
            userVoteResult.style.display = 'none';
      }
      else {
            voteContainer.style.display = 'none';
            userVoteResult.style.display = '';
            userVoteResult.className = result.UserVote > 0 ? 'thumbUp' : 'thumbDown';
      }
}


function initThumbRatingOnCallback(thumbRatingJsId, containerClass) {
    thumbRatingJsId.init(containerClass);
    thumbRatingJsId.getRating();
}
$(document).ready(function () {
    var thumb = $('#btnRatinDown'), needReg = $('.need-reg'), usrIn = $('.usr-in');
    if (thumb) {
        $('#btnRatingUp')[0].title = '';
        $('#voteContainer').append('<span id="thumb-title"></span>');
        var thumbTitle = $('#thumb-title');
        thumbTitle.css({
            'background' : 'url("/storage/calendar/thumb-title.png") top left no-repeat transparent',
            'display' : 'none',
            'height' : '30px',
            'left' : '102px',
            'line-height' : '25px',
            'margin' : '0',
            'opacity' : '0',
            'padding' : '0',
            'position' : 'absolute',
            'text-align' : 'center',
            'top' : '-30px',
            'width' : '118px',
            'z-index' : '100'
        });
	    thumb.mouseover(function () {
            this.title = '';
	        thumbTitle.html('Не понравилось').css('display', 'block').animate({opacity : '1'}, 500);
	    });
        thumb.mouseout(function () {
            thumbTitle.html('').css({'display' : 'none', 'opacity' : '0'});
        });
    }
    needReg.bind('click', function (event) {
        event.preventDefault();
        usrIn.trigger('click');
    });
});
