$(document).ready(function() {
  // Show first item in FAQ
  xeko.widgets.faq.selectTopic($(".folder:first").attr("id").substring(6));

  $(".folder").click(function(e) {
    e.preventDefault();
    var id = $(this).attr("id").substring(6);
    xeko.widgets.faq.selectTopic(id);
  });

  // Search box
  $("#faqInputSearch").bind("keyup click change", function(e) {
    xeko.widgets.faq.getFilteredRules();
  });

  // Clear button
  $("#faqButtonClear").click(function(e) {
    e.preventDefault();
    $("#faqInputSearch").val("");
    $("#faqInputSearch").trigger("blur");
  });

  // add inline search instructions prompt
  $("#faqInputSearch").val($("#faqLabelSearch").text());
  $("#faqInputSearch").addClass("instructional");

  // remove prompt text on field focus
  $("#faqInputSearch").focus(function(e) {
    if (this.value == $("#faqLabelSearch").text()) this.value = "";
    $(this).removeClass("instructional");
  });

  // re-add prompt text as necessary
  $("#faqInputSearch").blur(function(e) {
    if (this.value == "") {
      this.value = $("#faqLabelSearch").text();
      $(this).addClass("instructional");
    }
  })
  ;
});

xeko.widgets.faq =
{
  "getFilteredFaqs": function() {
    // TODO
  },
  "selectTopic": function(id) {
    // Reset folders
    $(".faq").hide();
    $(".subFolder").hide();
    //$(".folder img").attr("src", "/images/g/g_folder_closed.gif");
    $(".folder").removeClass("selected");

    // Highlight selected folder 
    $("#folder" + id).addClass("selected");
    //$("#folder" + id + " img").attr("src", "/images/g/g_folder_open.gif");
    $("#" + id).show();
    $("#subfolder" + id).show();
  }
};