var Livematch = function()
{
    this.animateComment = function(rallyId)
    {
        if (rallyId > $('#rallyId').val()) {
            $('#rallyId').val(rallyId);
            var commentDuration = 1000;
            $('.commentLine').hide();
            $('.commentLine').each(function(index) {
                $(this).delay(index * commentDuration).fadeIn('slow');
            });
            var timeOfLastComment = ($('.commentLine').length - 1) * commentDuration;
            _updateScore(timeOfLastComment);
            _updateStatistics(timeOfLastComment);
        }
    };

    var _updateScore = function(timeOfLastComment)
    {
        _updateScoreElement('service1', timeOfLastComment);
        _updateScoreElement('service2', timeOfLastComment);
        _updateScoreElement('sets1', timeOfLastComment);
        _updateScoreElement('sets2', timeOfLastComment);
        _updateScoreElement('games1', timeOfLastComment);
        _updateScoreElement('games2', timeOfLastComment);
        _updateScoreElement('points1', timeOfLastComment);
        _updateScoreElement('points2', timeOfLastComment);
        _updateScoreElement('sets11', timeOfLastComment);
        _updateScoreElement('sets21', timeOfLastComment);
        _updateScoreElement('sets31', timeOfLastComment);
        _updateScoreElement('sets41', timeOfLastComment);
        _updateScoreElement('sets12', timeOfLastComment);
        _updateScoreElement('sets22', timeOfLastComment);
        _updateScoreElement('sets32', timeOfLastComment);
        _updateScoreElement('sets42', timeOfLastComment);
    };

    var _updateScoreElement = function(scoreElement, timeOfLastComment)
    {
        setTimeout(function() {
            var originalColor = $('#' + scoreElement).css('color');
            if ($('#' + scoreElement).html() != $('#' + scoreElement + '_update').val()) {
                $('#' + scoreElement).html($('#' + scoreElement + '_update').val());
                $('#' + scoreElement).animate({
                    color: '#ffffff'
                }, 500, function() {
                    $('#' + scoreElement).animate({
                        color: originalColor
                    }, 500);
                });
            }
        }, timeOfLastComment);
    };

    var _updateStatistics = function(timeOfLastComment)
    {
        _updateStatisticsElement('firstServiceQuote', 'One', timeOfLastComment);
        _updateStatisticsElement('firstServiceQuote', 'Two', timeOfLastComment);
        _updateStatisticsElement('winningOnFirstServiceQuote', 'One', timeOfLastComment);
        _updateStatisticsElement('winningOnFirstServiceQuote', 'Two', timeOfLastComment);
        _updateStatisticsElement('winningOnSecondServiceQuote', 'One', timeOfLastComment);
        _updateStatisticsElement('winningOnSecondServiceQuote', 'Two', timeOfLastComment);
        _updateStatisticsElement('netPointsWonQuote', 'One', timeOfLastComment);
        _updateStatisticsElement('netPointsWonQuote', 'Two', timeOfLastComment);
        _updateStatisticsElement('breakPointsWonQuote', 'One', timeOfLastComment);
        _updateStatisticsElement('breakPointsWonQuote', 'Two', timeOfLastComment);
        _updateStatisticsElement('aces', 'One', timeOfLastComment);
        _updateStatisticsElement('aces', 'Two', timeOfLastComment);
        _updateStatisticsElement('doubleFaults', 'One', timeOfLastComment);
        _updateStatisticsElement('doubleFaults', 'Two', timeOfLastComment);
        _updateStatisticsElement('unforcedErrors', 'One', timeOfLastComment);
        _updateStatisticsElement('unforcedErrors', 'Two', timeOfLastComment);
        _updateStatisticsElement('wonPoints', 'One', timeOfLastComment);
        _updateStatisticsElement('wonPoints', 'Two', timeOfLastComment);
    };

    var _updateStatisticsElement = function(statisticsElement, player, timeOfLastComment)
    {
        setTimeout(function() {
            if ($('#' + statisticsElement + player).html() != $('#' + statisticsElement + player + '_update').val()) {
                $('#' + statisticsElement + player).html($('#' + statisticsElement + player + '_update').val());
                $('#' + statisticsElement + player).animate({
                    color: '#ffffff'
                }, 500, function() {
                    $('#' + statisticsElement + 'One').animate({
                        color: $('#' + statisticsElement + 'OneColor_update').val()
                    }, 500);
                    $('#' + statisticsElement + 'Two').animate({
                        color: $('#' + statisticsElement + 'TwoColor_update').val()
                    }, 500);
                });
            }
        }, timeOfLastComment);
    };
};

