- index ||= ''
- if defined?(package)
  - controller = 'package'
- else
  - controller = 'project'
.box-header.header-tabs
  %ul{id: "result_select_#{index}"}
    %li.selected
      %a{ class: "result_select_link_#{index}", href: '#', id: "result_select_link_#{index}_0" } Build Results
      = sprite_tag('reload', title: 'Reload', onclick: 'update_build_result()', class: "result_reload_#{index}", id: "result_reload_#{index}_0")
      = image_tag('ajax-loader.gif', id: "result_spinner_#{index}_0")
    - if controller == 'package'
      %li
        %a{ class: "result_select_link_#{index}", href: '#', id: "result_select_link_#{index}_1" } Rpmlint Results
        = sprite_tag('reload', title: 'Reload', onclick: 'update_rpmlint_result()', class: "result_reload_#{index}", id: "result_reload_#{index}_1", style: 'display: none')
        = image_tag('ajax-loader.gif', id: "result_spinner_#{index}_1", class: 'hidden')
%div{ class: "result_display_#{index}", id: "result_display_#{index}_0" }
- if controller == 'package'
  %div{ class: "result_display_#{index}", id: "result_display_#{index}_1" }

- ajax_data = []
- ajax_data.push("'project': '#{h(project)}'") if defined?(project)
- ajax_data.push("'package': '#{h(package)}'") if defined?(package)
- ajax_data.push("'index': '#{index}'") if defined?(index)
- ajax_data = raw(ajax_data.join ', ')

= javascript_tag do
  - if controller == 'package'
    :plain
      $('.result_select_link_#{index}').click(function (event) {
        $('#result_select_#{index} li.selected').attr('class', '');
        $(event.target).parent().attr('class', 'selected')
        $('.result_display_#{index}').hide();
        $('.result_reload_#{index}').hide();
        var tab_index = event.target.id.split('result_select_link_#{index}_')[1];
        $('#result_reload_#{index}_' + tab_index).show();
        $('#result_display_#{index}_' + tab_index).show();
        return false;
      });

      function update_rpmlint_result() {
        $('#result_spinner_#{index}_1').show();
        $('#result_reload_#{index}_1').hide();
        $.ajax({
          url: '#{ url_for(controller: controller, action: 'rpmlint_result') }',
          data: {#{ajax_data}},
          success: function(data) {
            $('#result_display_#{index}_1').html(data);
          },
          error: function(data) {
            $('#result_display_#{index}_1').html('<p>No rpmlint results available</p>');
          },
          complete: function(data) {
            $('#result_spinner_#{index}_1').hide();
            if ($('#result_select_link_#{index}_1').parent().hasClass('selected')) {
              $('#result_reload_#{index}_1').show();
            }
          }
        });
      }

      $('#result_display_#{index}_1').hide();
      update_rpmlint_result();

  :plain
    function update_build_result() {
      $('#result_spinner_#{index}_0').show();
      $('#result_reload_#{index}_0').hide();
      $.ajax({
        url: '#{ url_for(controller: controller, action: 'buildresult') }',
        data: {#{ajax_data}},
        success: function(data) {
          $('#result_display_#{index}_0').html(data);
        },
        error: function(data) {
          $('#result_display_#{index}_0').html('<p>No build results available</p>');
        },
        complete: function(data) {
          $('#result_spinner_#{index}_0').hide();
          if ($('#result_select_link_#{index}_0').parent().hasClass('selected')) {
            $('#result_reload_#{index}_0').show();
          }
        }
      });
    }

    update_build_result();
