<% unless goal.nil? %>
  <% experiment_class = "experiment experiment_with_goal" %>
<% else %>
  <% experiment_class = "experiment" %>
<% end %>

<% experiment.calc_winning_alternatives %>

<div class="<%= experiment_class %>">
  <div class="experiment-header">
    <h2>
      Experiment: <%= experiment.name %>
      <% if experiment.version > 1 %><span class='version'>v<%= experiment.version %></span><% end %>
      <% unless goal.nil? %><span class='goal'>Goal:<%= goal %></span><% end %>
      <% metrics = @metrics.select {|metric| metric.experiments.include? experiment} %>
      <% unless metrics.empty? %>
        <span class='goal'>Metrics:<%= metrics.map(&:name).join(', ') %></span>
      <% end %>
    </h2>

    <% if goal.nil? %>
      <div class='inline-controls'>
        <small><%= experiment.start_time ? experiment.start_time.strftime('%Y-%m-%d') : 'Unknown' %></small>
        <%= erb :_controls, :locals => {:experiment => experiment} %>
      </div>
    <% end %>
  </div>
  <table>
    <tr>
      <th>Alternative Name</th>
      <th>Participants</th>
      <th>Non-finished</th>
      <th>Completed</th>
      <th>Conversion Rate</th>
      <th>
        <form>
          <select id="dropdown-<%=experiment.jstring(goal)%>" name="dropdown-<%=experiment.jstring(goal)%>">
            <option value="confidence-<%=experiment.jstring(goal)%>">Confidence</option>
            <option value="probability-<%=experiment.jstring(goal)%>">Probability of being Winner</option>
          </select>
        </form>
      </th>
      <th>Finish</th>
    </tr>

    <% total_participants = total_completed = total_unfinished = 0 %>
    <% experiment.alternatives.each do |alternative| %>
      <tr>
        <td>
          <%= alternative.name %>
          <% if alternative.control? %>
            <em>control</em>
          <% end %>
        </td>
        <td><%= alternative.participant_count %></td>
        <td><%= alternative.unfinished_count %></td>
        <td><%= alternative.completed_count(goal) %></td>
        <td>
          <%= number_to_percentage(alternative.conversion_rate(goal)) %>%
          <% if experiment.control.conversion_rate(goal) > 0 && !alternative.control? %>
            <% if alternative.conversion_rate(goal) > experiment.control.conversion_rate(goal) %>
              <span class='better'>
                +<%= number_to_percentage((alternative.conversion_rate(goal)/experiment.control.conversion_rate(goal))-1) %>%
              </span>
            <% elsif alternative.conversion_rate(goal) < experiment.control.conversion_rate(goal) %>
              <span class='worse'>
                <%= number_to_percentage((alternative.conversion_rate(goal)/experiment.control.conversion_rate(goal))-1) %>%
              </span>
            <% end  %>
          <% end %>
        </td>
        <script type="text/javascript" id="sourcecode">
          $(document).ready(function(){
            $('.probability-<%=experiment.jstring(goal)%>').hide();
            $('#dropdown-<%=experiment.jstring(goal)%>').change(function() {
              $('.box-<%=experiment.jstring(goal)%>').hide();
              $('.' + $(this).val()).show();
            });
          });
        </script>
        <td>
          <div class="box-<%=experiment.jstring(goal)%> confidence-<%=experiment.jstring(goal)%>">
            <span title='z-score: <%= round(alternative.z_score(goal), 3) %>'><%= confidence_level(alternative.z_score(goal)) %></span>
            <br>
          </div>
          <div class="box-<%=experiment.jstring(goal)%> probability-<%=experiment.jstring(goal)%>">
            <span title="p_winner: <%= round(alternative.p_winner(goal), 3) %>"><%= number_to_percentage(round(alternative.p_winner(goal), 3)) %>%</span>
          </div>
        </td>        
        <td>
          <% if experiment.has_winner? %>
            <% if experiment.winner.name == alternative.name %>
              Winner
            <% else %>
              Loser
            <% end %>
          <% else %>
            <form action="<%= url('experiment') + '?experiment=' + experiment.name %>" method='post' onclick="return confirmWinner()">
              <input type='hidden' name='alternative' value='<%= h alternative.name %>'>
              <input type="submit" value="Use this" class="green">
            </form>
          <% end %>
        </td>
      </tr>

      <% total_participants += alternative.participant_count %>
      <% total_unfinished += alternative.unfinished_count %>
      <% total_completed += alternative.completed_count(goal) %>
    <% end %>

    <tr class="totals">
      <td>Totals</td>
      <td><%= total_participants %></td>
      <td><%= total_unfinished %></td>
      <td><%= total_completed %></td>
      <td>N/A</td>
      <td>N/A</td>
      <td>N/A</td>
    </tr>
  </table>
</div>