$("body").on("contextmenu", function(e) {
  return false;
});

$(document).ready(function() {
  $("#listSongs li:first-child").addClass("selected");
});

function searchSong() {
    var input, filter, ul, li, a, i, txtValue;
    input = document.getElementById("rechercher");
    filter = input.value.toUpperCase();
    ul = document.getElementById("listSongs");
    li = ul.getElementsByTagName("li");
    for (i = 0; i < li.length; i++) {
        a = li[i].getElementsByTagName("span")[0];
        txtValue = a.textContent || a.innerText;
        if (txtValue.toUpperCase().indexOf(filter) > -1) {
            li[i].style.display = "";
        } else {
            li[i].style.display = "none";
        }
    }
}

var ul = document.getElementById('listSongs');
var liSelected;
var index = 0;

document.addEventListener('keydown', function(event) {
  var len = ul.getElementsByTagName('li').length - 1;
  if (event.which === 40) {
    index++;
    //down
    if (liSelected) {
      removeClass(liSelected, 'selected');
      next = ul.getElementsByTagName('li')[index];
      if (typeof next !== undefined && index <= len) {

        liSelected = next;
      } else {
        index = 0;
        liSelected = ul.getElementsByTagName('li')[0];
      }
      addClass(liSelected, 'selected');
      console.log(index);
    } else {
      index = 0;

      liSelected = ul.getElementsByTagName('li')[0];
      addClass(liSelected, 'selected');
    }
  } else if (event.which === 38) {

    //up
    if (liSelected) {
      removeClass(liSelected, 'selected');
      index--;
      console.log(index);
      next = ul.getElementsByTagName('li')[index];
      if (typeof next !== undefined && index >= 0) {
        liSelected = next;
      } else {
        index = len;
        liSelected = ul.getElementsByTagName('li')[len];
      }
      addClass(liSelected, 'selected');
    } else {
      index = 0;
      liSelected = ul.getElementsByTagName('li')[len];
      addClass(liSelected, 'selected');
    }
  }
}, false);

function removeClass(el, className) {
  if (el.classList) {
    el.classList.remove(className);
  } else {
    el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
  }
};

function addClass(el, className) {
  if (el.classList) {
    el.classList.add(className);
  } else {
    el.className += ' ' + className;
  }
};

function setSongid(t) {
  var e=t.getAttribute("data-song-id");
  document.getElementById("cdwall1").setAttribute("data-songid-cdwall",e);
  document.getElementById("cdwall2").setAttribute("data-songid-cdwall",e);
  document.getElementById("cdwall3").setAttribute("data-songid-cdwall",e);
  document.getElementById("cdwall4").setAttribute("data-songid-cdwall",e);
  document.getElementById("cart1").setAttribute("data-songid-cart",e);
  console.log('Set SongID OK '+e);
}

function updateSongCD1(b) {
  var d=b.getAttribute("data-songid-cdwall");
  $('#updateSongid').attr('src', "./update.php?songID="+d+"&slot=cdwall1")
  console.log("Update DB SongID OK");
}

function updateSongCD2(b) {
  var d=b.getAttribute("data-songid-cdwall");
  $('#updateSongid').attr('src', "./update.php?songID="+d+"&slot=cdwall2")
  console.log("Update DB SongID OK");
}

function updateSongCD3(b) {
  var d=b.getAttribute("data-songid-cdwall");
  $('#updateSongid').attr('src', "./update.php?songID="+d+"&slot=cdwall3")
  console.log("Update DB SongID OK");
}

function updateSongCD4(b) {
  var d=b.getAttribute("data-songid-cdwall");
  $('#updateSongid').attr('src', "./update.php?songID="+d+"&slot=cdwall4")
  console.log("Update DB SongID OK");
}

function updateSongCART1(b) {
  var d=b.getAttribute("data-songid-cart");
  $('#updateSongid').attr('src', "./update.php?songID="+d+"&slot=cart1")
  console.log("Update DB SongID OK");
}
