     $.each $('.competitive_list li'), (index, element) ->
       positions[currentPosition] = $(element).data('id')
       $(element).data('position', currentPosition)  
-      $(element).find('.competitorPosition').html(currentPosition)
+      $(element).find('.competitor_position').html(currentPosition)
       currentPosition += 1  
       
     data = { _method: 'put', positions: positions, matches: JSON.stringify(window.matches) }  
-    $.post $('.competitive_list').data('update-all-positions-path'), data    
+    $.post $('.competitive_list').data('update-all-positions-path'), data   
<form class="form-inline" style="margin:0px;">
  <div class="modal-header">
    <button type="button" id="close_bootstrap_modal_button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3>Appoint Winner (#{window.matchesLeft} matches left)</h3>
  </div>
  <div class="modal-body" style="overflow-y:none;">
    <div class="controls" style="margin-left:50px">

class @CompetitiveList
  competitiveListOptions: []
  competitors: []
  competitorsOfCompetitor: {}
  defeatedCompetitorsByCompetitor: {}
  outmatchedCompetitorsByCompetitor: {}
  matchesLeft: 0
  currentMatch: null
  currentMatchIndex: 0
  
  constructor: (options) ->
    @competitiveListOptions = options
    @competitiveListOptions ||= {}
    
    $(document.body).on "click", ".competitive_list_start_link", (event) =>
      event.preventDefault()
      @start()
      
    $(document.body).on "click", ".cancel_tournament_button", (event) =>
      event.preventDefault()
      @cancelTournament()   
      
    $(document.body).on "click", ".select_winner_button", (event) =>
      event.preventDefault()
      @appointWinnerOfMatchByInput()
    
  start: () ->     
    matchesAlreadyExist = false
    window.matches ||= []
    
    if window.matches.length > 0
      matchesAlreadyExist = true
      
    @competitors = jQuery.map($('.competitive_list li'), (c) ->
      return $(c).data('id')
    )
    @competitorsOfCompetitor = {}
    @defeatedCompetitorsByCompetitor = {}
    @outmatchedCompetitorsByCompetitor = {}
    
    if matchesAlreadyExist
      if confirm('Remove previous results and start over?')
        window.matches = []
      else
        @removeMatchesOfNonExistingCompetitors() 
    
    @generateMatches()
    
    matchesWithoutWinner = jQuery.map(window.matches, (m) ->
      if m['winner_competitor'] == undefined
        return m
    )
    
    @matchesLeft = matchesWithoutWinner.length
    
    if @nextMatch()
      $('#bootstrap_modal').modal('show')
      
  removeMatchesOfNonExistingCompetitors: () ->
    $.each window.matches, (index, match) =>
      notExistingCompetitors = jQuery.map(match['competitors'], (c) =>
        if $.inArray(c, @competitors) == -1
          return c
      )
      
      if notExistingCompetitors.length > 0
        window.matches = @removeItemFromArray(window.matches, match)
      else
        @competitorsOfCompetitor[match['competitors'][0]] ||= []
        @competitorsOfCompetitor[match['competitors'][0]].push match['competitors'][1]
        @competitorsOfCompetitor[match['competitors'][1]] ||= []
        @competitorsOfCompetitor[match['competitors'][1]].push match['competitors'][0]
        
        unless match['winner_competitor'] == undefined
          loserId = @otherCompetitorOfMatch(match, match['winner_competitor'])
          @updateDefeatedAndOutmatchedCompetitorsByCompetitor(match['winner_competitor'], loserId)
  
  removeItemFromArray: (array, item) ->
    list = []
    
    $.each array, (index, workingItem) =>
      unless JSON.stringify(workingItem) == JSON.stringify(item)
        list.push workingItem
        
    return list
          
  generateMatches: () ->
    $.each @competitors, (index, competitor_id) =>
      @competitorsOfCompetitor[competitor_id] ||= []
        
      $.each @competitors, (index, otherCompetitorId) =>
        @competitorsOfCompetitor[otherCompetitorId] ||= []
        
        if competitor_id != otherCompetitorId && $.inArray(otherCompetitorId, @competitorsOfCompetitor[competitor_id]) == -1
          window.matches.push { competitors: [competitor_id, otherCompetitorId] }
          @competitorsOfCompetitor[competitor_id].push otherCompetitorId
          @competitorsOfCompetitor[otherCompetitorId].push competitor_id
          
  nextMatch: () ->
    @currentMatch = null
    
    $.each window.matches, (index, match) =>
      if match['winner_competitor'] == undefined
        @currentMatch = match
        @currentMatchIndex = index
        
        return
    
    if @currentMatch == null
      alert 'No matches to rate left.'
      
      return false
