﻿var BerichtenbalkTimer;
var BerichtenbalkNeedsAttention = false;

function AddQueueToBerichtenBalk(aQueueItem)
{
  //TODO: Iets met client-side templates!!??    
  var tabel = $("#MessageBarQueueItems");
  var row = $("#MessageBarQueueItems_" + aQueueItem.ID);
  var cell;
  if (row.length == 0)
  {
    //Create
    var row = document.createElement("tr");
    row.id = "MessageBarQueueItems_" + aQueueItem.ID;
    tabel.find("tbody")[0].appendChild(row);
    cell = document.createElement("td");
    row.appendChild(cell);
    cell = document.createElement("td");
    row.appendChild(cell);
    cell = document.createElement("td");
    row.appendChild(cell);
    cell = document.createElement("td");
    row.appendChild(cell);
    cell = document.createElement("td");
    row.appendChild(cell);
  }
  else
  {
    row = row[0];
  }

  if (aQueueItem.Artikel)
  {
    row.cells[0].innerHTML = "<b>" + lArtikel + ":</b> [" + aQueueItem.Artikel.Code + "] " + aQueueItem.Artikel.Omschrijving;
    //row.cells[1].innerHTML = "---"; //Prijs???
    row.cells[2].innerHTML = "<b>" + lAantal + ":</b> " + aQueueItem.AantalEenhedenBesteld;
  }
  else
  {
    row.cells[0].innerHTML = "<b>" + lActie + ":</b> " + aQueueItem.Omschrijving;
  }

  row.cells[3].innerHTML = "<b>" + lDatum + ":</b> " + aQueueItem.RequestDateTime;
  row.cells[4].innerHTML = "<b>" + lStatus + ":</b> " + aQueueItem.StatusOmschrijving;
  row.className = "MessageBarQueueItem_" + aQueueItem.Status;

  if (aQueueItem.Status == "Exception")
    BerichtenbalkNeedsAttention = true;

  if (BerichtenBalkMutatienummer < aQueueItem.Mutatienummer)
    BerichtenBalkMutatienummer = aQueueItem.Mutatienummer;
}

function FinalizeQueueToBerichtenBalk()
{
  var tabel = $("#MessageBarQueueItems");
  var rows = tabel.find("tbody tr");

  var i = 0;
  while (rows.length > 10 && i < rows.length)
  {

    if (rows[i].className == "MessageBarQueueItem_Done")
    {
      $(rows[i]).remove();
      rows = tabel.find("tbody tr");
    }
    else
      i++;
  }

  // Als de balk al open staat, dan altijd de grote aanpassen
  if (msgbar.is(":visible"))
    clearTimeout(BerichtenbalkTimer);
  else
  //Alleen openen als het echt nodig is.
    if (BerichtenbalkNeedsAttention)
      ShowBerichtenBalk();

  BerichtenbalkTimer = setTimeout("HideBerichtenBalk()", 5000);
  BerichtenbalkNeedsAttention = false;
}

function ToggleBerichtenBalk()
{
  if (!msgbar.is(":visible"))
    ShowBerichtenBalk();
  else
    HideBerichtenBalk();
}

function ShowBerichtenBalk()
{
  msgbar.slideDown("slow");
  $("#imgToggleMessageBar").attr("class", "dArrowDown");
}

function HideBerichtenBalk()
{
  msgbar.slideUp("slow");
  $("#imgToggleMessageBar").attr("class", "dArrowUp");
}

function DisplayMessage(aMessage)
{
  noticebar.find(":first-child").html(aMessage);
  if (aMessage !== "")
  {
    if (!noticebar.is(":visible"))
      noticebar.slideDown("slow");
  }
  else
  {
    if (noticebar.is(":visible"))
      noticebar.slideUp("slow");
  }
}
