// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var Counter = Class.create();
Counter.prototype = {
  initialize: function(value) {
    this.counter = value ? value : 0;
  },
  increment: function() {
    this.counter += 1;
    return this.counter;
  }
}

var _item_counters = {};
function next_item_index(collection_name) {
  if (!_item_counters[collection_name]) {
    // initialize the counter
    _item_counters[collection_name] = new Counter((document.getElementsByClassName(collection_name+'_item') || []).length-1);
  }
  return _item_counters[collection_name].increment();
}

function next_item_position(collection_name) {
  var ordered_items_inputs = document.getElementsByClassName(collection_name+'_position') || [];
  if (ordered_items_inputs.length>0)
    return ordered_items_inputs.max(function(input) { return parseInt($F(input))+1; });
  return 1;
}

function radio_selection_mediascopy(radio_value) {
  $('submit_miles').show();
  highlight_options = {startcolor:'#ffcc99', duration: 3};
  
  switch(radio_value) {
    case "mediascopy":
      var mediascopy_select = $('selection[mediascopy_id_for_miles]');
      $('selection_mediascopy').show();
      new Effect.Highlight('choose_mediascopy_prompt', highlight_options);
      $('selection_reason').hide();
      $('selection_miles_operation_title').value = mediascopy_select.options[mediascopy_select.selectedIndex].innerHTML;
      break;
    case "reason":
      $('selection_mediascopy').hide();
      $('selection_reason').show();
      new Effect.Highlight('reason_why_prompt', highlight_options);
    default:
      break;
  }
}