/*
     $Revision$
     $Id$
*/

var rowIndex = -1;

function FavItem(fav_name, bes_item_info, recent_date, fav_key)
{
  this.fav_name = fav_name;
  this.bes_item_info = bes_item_info;
  this.recent_date = recent_date;
  this.fav_key = fav_key;

  this.row = null;
}

function onClickItem(tr)
{
  // Wordt aangeroepen na één klik op een item
  $(tr).toggleClass("selected");
}

function onDblClickItem(tr) {
    $(tr).closest("table").find("tr").removeClass("selected"); // Alle andere regels deselecteren
    $(tr).addClass("selected");
    window.parent.doSubmit();
}

function AddItem(fav_name, bes_item_info, recent_date, recent, fav_key)
{
  CreateRow(fav_name, bes_item_info, recent_date, recent, fav_key);
}

function CreateRow(fav_name, bes_item_info, recent_date, recent, fav_key)
{
  rowIndex++;
  var table = $("#besfavtable>tbody")[0];

  // Insert data rows
  tr = table.insertRow(-1);
  tr.myData = { fav_name: fav_name, bes_item_info: bes_item_info, recent_date: recent_date, fav_key: fav_key};
  tr.value = rowIndex;
  var classname = "disabled";
  if (!bes_item_info.vervallen)
  {
    tr.onclick = function(){onClickItem(this)};
    tr.ondblclick = function(){onDblClickItem(this)};
    classname = "result";
  }

  if (!recent) {
    // Create drop button
    cell = tr.insertCell(-1);
    cell.className = "result";
    cell.align = "center";
    cell.innerHTML = "<i class='fa fa-fw fa-trash' OnClick=delFavourites(this) style=cursor:pointer></i>";
  }

  // Create srtdeel nr field
  cell = tr.insertCell(-1);
  cell.className = classname;
  cell.innerHTML = bes_item_info.srtdeel_nr;
  cell.align = 'left';

  // Create srtdeel description field
  cell = tr.insertCell(-1);
  cell.className = classname;
  cell.innerHTML = bes_item_info.srtdeel_omschrijving;
  cell.align = 'left';

  // Create recently date field
  cell = tr.insertCell(-1);
  cell.className = classname;
  cell.innerHTML = recent_date;
  cell.align = 'left';
}

function delFavourites(img)
{
  // Verwijder het aangeklikte item uit de lijst (de regel) en de favorieten lijst (bes_favoriet tabel)
  FcltMgr.confirm(L("lcl_bes_cancelfav"), function() {
      var tr = $(img).closest("tr")[0];
      if (tr.myData.fav_key != null) {
          fav_key = tr.myData.fav_key;

          document.getElementById("besfavtable").deleteRow(tr.rowIndex);

          document.getElementById("itemsInList").value = document.getElementById("itemsInList").value - 1;
          document.getElementById("item_in_list").innerHTML = document.getElementById("itemsInList").value + " " + L("lcl_bes_fac_items_inlist")

          var data = {};
          protectRequest.dataToken(data);
          $.post("delFavourites.asp?fav_key=" + fav_key, data);
      }
  });
}
