/*
     $Revision$
     $Id$

     File:        faq.js
*/
function showFaq(faq_key)
{
  if (faq_key > 0)
    FcltMgr.openDetail("appl/fac/fac_faq.asp?urole=fe&faq_key=" + faq_key, L("lcl_faq_item"), {reuse: true});
}

function show_fixed_faq(blk, changed, fixed_faq, max_faqs)
{
  var $tbl = $("#"+blk).find("table");

  var alg = [null, 'X', 'L', 'G', 'V', 'R'];

  //var plaatsChanged = changed >= 2 && changed <= 5;

  for (var a = 1; a <= 5; a++)
  { // Zowel de plaatsgebonden kennisbankitems als de kennisbankitems die over vakgroepen/standaardmeldingen worden nu in de popup getoond (indien aangegeven volgens setting).
    // Alle kennisbank items zijn opgehaald. Nu kan alles verwijderd worden en de juiste kennisbankitems worden getoond.
    $tbl.find("tr.faq_X" + alg[a]).remove();
    $tbl.find("tr.faq_I" + alg[a]).remove();
    $tbl.find("tr.faq_S" + alg[a]).remove();
  }

  if (fixed_faq && fixed_faq.length)
  {
    if (max_faqs > 0 && !$("#" + blk + " div.show-more-container").length)
    {
      $("#" + blk + " div.inside").prepend("<div class='show-more-container'></div>");
      $("#" + blk + " div.show-more-container").append("<div class='show-more'><a href='#'>"+L('lcl_more')+"</a></div>");
      $("#" + blk + " div.show-more-container").append("<div class='show-less hidden'><a href='#'>"+L('lcl_less')+"</a></div>");
    }
    $("#" + blk + " div.show-more-container").toggle(fixed_faq.length > max_faqs);
    for (var i = 0; i < fixed_faq.length; i++)
    {
      var faq_key = fixed_faq[i].faq_key;
      var $newrow = $("<tr class='faqcollapsable faqcollapsed "
                        + (i >= max_faqs && max_faqs > 0 ? "hide-more hidden " : "")
                        +fixed_faq[i].faq_type+(fixed_faq[i].extraClass?fixed_faq[i].extraClass:"")
                        +"'><td class='fixedfaqquestion lhint'>" + safe.html(fixed_faq[i].question) + "</td></tr>")
                        .click(function () {
                                            $(this).next().toggle();
                                            $(this).toggleClass('faqcollapsed');
                                            FcltMgr.resized(window);
                                           });
      $tbl.append($newrow)
      var safe_answer = fixed_faq[i].safe_answer;
      if (fixed_faq[i].safe_answer2)
      {
        safe_answer += "<div class='faqanswer2' id='"+faq_key+"'>"
                    + "  <a name='"+faq_key+"' href='javascript:showAnswer2("+faq_key+")'>"+L("lcl_faq_answer2")+"</a>"
                    + "  <div style='display:none;'>"
                    + fixed_faq[i].safe_answer2
                    + "  </div>"
                    + "</div>";
      }
      if (fixed_faq[i].hurl)
        safe_answer += "<br><a href='{0}' target='_new'>{1}</a>".format(safe.htmlattr(fixed_faq[i].hurl), safe.html(fixed_faq[i].hlnk));
      else if (fixed_faq[i].has_kenm)
        safe_answer += "<div class='faqlink'><a href='javascript:showFaq("+faq_key+")'>"+L('lcl_more')+"</a></div><br>";
      $tbl.append("<tr style='display:none' class='"+fixed_faq[i].faq_type+"'><td class='fixedfaqanswer'><span class='fclthtml'>" + safe_answer + "</span></td></tr>");
    }
    $("#"+blk).show();
    $("div.show-more, div.show-less", $("#"+blk)).off().on("click", function() { $("div.show-more, div.show-less, tr.hide-more", $("#"+blk)).toggleClass("hidden"); });
  }
  if (!$tbl.find("tr").length)
  {
    $tbl.empty();
    $("#"+blk).hide();
  }
  FcltMgr.resized(window); // initial resize in case there are a lot of faq's
}

