#= require dependent-fields-toggle

google.load('visualization', '1', {packages:['table']})

$(document).ready ->

  $(".scrollable input").on "click", ->
    checkbox_container = $(this).closest(".scrollable")
    if checkbox_container.find("input").length == checkbox_container.find("input:checked").length
      checkbox_container.prev("label").find(".mark-all").prop("checked", true)
    else
      checkbox_container.prev("label").find(".mark-all").prop("checked", false)

  $(".mark-all").on "click", ->
    checkbox_container = $(this).closest("label").next(".scrollable")
    if checkbox_container.find("input").length == checkbox_container.find("input:checked").length
      checkbox_container.find("input").prop("checked", false)
    else
      checkbox_container.find("input").prop("checked", true)


  # ajax seding info information ranking calculation
  #
  $("#information-ranking-form").bind "ajax:beforeSend", (xhr) ->

    if $("#institution-checkboxes input:checked").length == 0
      $("#error-form").html("<div class='error'>Seleccione por lo menos una institución.</div>")
      return false

    if $("#information-standard-checkboxes input:checked").length == 0
      $("#error-form").html("<div class='error'>Seleccione por lo menos un estándar de información.</div>")
      return false

    if(($("#institution-checkboxes input:checked").length != 0) && ($("#information-standard-checkboxes input:checked").length != 0))
      $("#error-form").hide()

  # Information ranking table show
  #
  $("#information-ranking-form").bind "ajax:complete", (evt, xhr, status) ->

    response = $.parseJSON(xhr.responseText)
    data = new google.visualization.DataTable()

    data.addColumn('string', 'Institución')
    $.each response.headers, (index, header) ->
      data.addColumn('number', "<div>#{header}</div>")
    data.addColumn('number', '<div>Nota final</div>')

    data.addRows(response.results.length)
    for i in [0...response.results.length]
      row = response.results[i]
      data.setCell(i, 0, row[0])
      for j in [1...row.length]
        data.setCell(i, j, row[j].score, row[j].score, { className: "information #{row[j].info}" })
      data.setCell(i, row.length - 1, row[row.length - 1], row[row.length - 1], { className: null })

    # data.addRows(response.results)

    table = new google.visualization.Table(document.getElementById('information-ranking-result'))
    formatter = new google.visualization.ColorFormat()

    formatter.addRange(0, 2.4, '#b94a48', '#f2dede')
    formatter.addRange(2.5, 4.9, '#df521b', '#ffaf8f')
    formatter.addRange(5, 7.4, '#c09853', '#fcf8e3')
    formatter.addRange(7.5, null, '#468847', '#dff0d8')

    for i in [1..response.headers.length]
      formatter.format(data, i)

    table.draw(data, { sort: 'disable', allowHtml: true, width: "100%" })

    $("td.information").each (i) ->
      div = $("<div />")
      div.attr("data-content", $(this).attr("class").replace("information ", ""))
      div.attr("data-container", "body")
      div.attr("class", null)
      div.html($(this).html())
      div.popover(html: true, placement: "top", trigger: "hover")
      $(this).html("")
      $(this).append(div)
    ###
    selectHandler = ->
      selection = table.getSelection()
      message = ""
      for i in [0...selection.length]
        item = selection[i]
        if item.row != null && item.column != null
          str = data.getFormattedValue(item.row, item.column)
          message += '{row:' + item.row + ',column:' + item.column + '} = ' + str + '\n'
        else if item.row != null
          str = data.getFormattedValue(item.row, 0)
          message += '{row:' + item.row + ', column:none}; value (col 0) = ' + str + '\n'
        else if (item.column != null)
          str = data.getFormattedValue(0, item.column)
          message += '{row:none, column:' + item.column + '}; value (row 0) = ' + str + '\n'

      alert message

    google.visualization.events.addListener(table, 'select', selectHandler)
    ###

    $(".top-level").css("overflow", "visible")
    window.location.hash = '#information-ranking-result'
