function intToIP(int) {
  var part1 = int & 255;
  var part2 = ((int >> 8) & 255);
  var part3 = ((int >> 16) & 255);
  var part4 = ((int >> 24) & 255);

  return part4 + "." + part3 + "." + part2 + "." + part1;
}

function handleJamTrackRightsForm() {
  var $jamTrackRights = $('form#new_jam_ruby_jam_track_right');
  var $jamTrack = $jamTrackRights.find('#jam_ruby_jam_track_right_jam_track_id');
  var $jamTrackVal = $jamTrackRights.find('#jam_ruby_jam_track_right_jam_track_id_val')

  var $user = $jamTrackRights.find('#jam_ruby_jam_track_right_user_id');
  var $userVal = $jamTrackRights.find('#jam_ruby_jam_track_right_user_id_val');
  $jamTrack.on('change', function(){
      console.log("change jam track");
  });

  /**
  $user.on('change', function(){
    console.log("change user");
  });

  $jamTrack.on('focus', function(){
    $userVal.val('')
  });*/

  $jamTrack.bind('railsAutocomplete.select', function(event, data){
    $jamTrackVal.val('');
    $jamTrackVal.val(data.item.id);
    console.log("jam track selected with id " + data.item.id);
  });

  $user.bind('railsAutocomplete.select', function(event, data){
    $userVal.val('');
    $userVal.val(data.item.id);
    console.log("user selected with id " + data.item.id);
  });
}
function handleUserLatencyForm(){
  var $userLatenciesForm   = $('form#user_latencies_form');
  var $latenciesMyUser     = $userLatenciesForm.find('#latencies_my_user');
  var $latenciesMyUserId   = $userLatenciesForm.find('#latencies_my_user_id');
  var $latenciesMyPublicIp = $userLatenciesForm.find('#latencies_my_public_ip');

  $latenciesMyUser.on('focus', function(){
    $latenciesMyUserId.val('')
  });

  $latenciesMyUser.bind('railsAutocomplete.select', function(event, data){
    $latenciesMyUserId.val('');
    $latenciesMyPublicIp.val('');
    if(data.item.last_jam_addr){
      var ipAddr = intToIP(data.item.last_jam_addr);
      $latenciesMyPublicIp.val(ipAddr);
    }
    $latenciesMyUserId.val(data.item.id);
  });

  $userLatenciesForm.find('#latencies_user_1').bind('railsAutocomplete.select', function(event, data){
    $userLatenciesForm.find('#latencies_user_1_id').val(data.item.id);
  });

  $userLatenciesForm.find('#latencies_user_2').bind('railsAutocomplete.select', function(event, data){
    $userLatenciesForm.find('#latencies_user_2_id').val(data.item.id);
  });

  $userLatenciesForm.find('#latencies_user_3').bind('railsAutocomplete.select', function(event, data){
    $userLatenciesForm.find('#latencies_user_3_id').val(data.item.id);
  });

  $userLatenciesForm.find('#latencies_user_4').bind('railsAutocomplete.select', function(event, data){
    $userLatenciesForm.find('#latencies_user_4_id').val(data.item.id);
  });

  $userLatenciesForm.find('#latencies_user_5').bind('railsAutocomplete.select', function(event, data){
    $userLatenciesForm.find('#latencies_user_5_id').val(data.item.id);
  });
}

function handleLatencyRecommendationForm(){
  var $latencyRecommendationForm = $('form#latency_recommendation_form');
  var $latenciesMyUser     = $latencyRecommendationForm.find('#latency_recommendation_my_user');
  var $latenciesMyUserId   = $latencyRecommendationForm.find('#latency_recommendation_my_user_id');
  var $latenciesMyPublicIp = $latencyRecommendationForm.find('#latency_recommendation_my_public_ip');

  $latenciesMyUser.on('focus', function(){
    $latenciesMyUserId.val('')
  });

  $latenciesMyUser.bind('railsAutocomplete.select', function(event, data){
    $latenciesMyUserId.val('');
    $latenciesMyPublicIp.val('');
    if(data.item.last_jam_addr){
      var ipAddr = intToIP(data.item.last_jam_addr);
      $latenciesMyPublicIp.val(ipAddr);
    }
    $latenciesMyUserId.val(data.item.id);
  });

  
}

$(document).ready(function() {
  handleUserLatencyForm();
  handleLatencyRecommendationForm();
  handleJamTrackRightsForm();
});