/**********************************************************************
* cheslers.faq.js - FAQ page utilities
*
* Copyright (c) 2009, White Horse Technology Consulting Inc.
* Copyright (c) 2009, Cheslers Shoes Inc.
***********************************************************************/ 

/**********************************************************************
 initFaqTabs - What it says

 Initialises our tabs by loading our list of FAQ questions into each
 pane from their individual pages, and then initialises our 
 tab/accordion control.
**********************************************************************/
function initFaqTabs () {
  // Iterate through our FAQ panes and load them from the feed pages
  $jq("div.faqPane").each(
    function (index) {
      try {
        $jq("#" + this.id).load(this.attributes["name"].value);
        }
      catch (err) {
        }
      });
  
  // Now, initialise our tabs
  $jq("div#faqTabs").tabs("#faqTabs div.faqPane", 
                        {tabs: 'h1', effect: 'slide', initialIndex: null, onClick: faqSectionChange});
  } //END: initFaqTabs

  
/**********************************************************************
 selectFaq - Marks an FAQ as selected and loads it.
**********************************************************************/
function selectFaq (faqId, faqUrl) {
  try {
    clearSelectedFaqs();
    document.getElementById(faqId).className = "faqSectionQuestionSelected";
  
    // Finally, load the answer - Because we can't set the frame for a 
    // database feed entry, we need to target exactly the section we want to load.
    $jq("#faqAnswerPane").load(faqUrl + " #faqAnswer", "", selectFaqDone);
    }
  catch (err) {
    if (debugMessages == true) {
      alert("selectFaq: " + err.message);
      }
    }
  } //END: selectFaq

/**********************************************************************
 selectFaqDone - Called when FAQ has been loaded
**********************************************************************/
function selectFaqDone () {
  try {
    $jq("#faqAnswer .emailAddress").defuscate();
    
    }
  catch (err) {
    if (debugMessages == true) {
      alert("selectFaqDone: " + err.message);
      }
    }
  } //END: selectFaqDone

  
/**********************************************************************
 clearSelectedFaqs - Changes all FAQ questions to non-selected
**********************************************************************/
function clearSelectedFaqs() {
  var selectedQuestions = $jq("h2.faqSectionQuestionSelected");
  selectedQuestions.addClass("faqSectionQuestion");
  selectedQuestions.removeClass("faqSectionQuestionSelected");
  } //END: clearSelectedFaqs
  
/**********************************************************************
 faqSectionChange - Clear FAQ selections and answer pane when 
                    section changed.
**********************************************************************/
function faqSectionChange() {
  try {
    clearSelectedFaqs();
    document.getElementById("faqAnswerPane").innerHTML = "";
    }
  catch (err) {
    if (debugMessages == true) {
      alert("faqSectionChange: " + err.message);
      }
    }
  } //END: faqSectionChange
  
  
/**********************************************************************
 loadFaqList - Load list of questions for each FAQ section
 
 Meant to be called from jQuery's .each function. As documented in
 that function, it is called in the context of the specific element.
 
 We take the elements href attribute, which points to the 3D Cart 
 feed page for this section, and load its contents into the div. A
 minimal custom frame is used for the FAQ feed pages, so the only 
 content we should have is the list of questions. 
**********************************************************************/
function loadFaqList(index) { 
  $jq("#"+this.id).load(this.href);
  } //END: loadFaqList
  
  
function initFaqPage() {

  initFaqTabs();
  } //END: initFaqPage
