/********************************************************************************
  faq.js
   Copyright (c) 2006, 
     Lee Feigenbaum ( lee AT thefigtrees DOT net )
	 
   Slightly adapted by Ivan Herman, ivan AT w3.org

  All rights reserved.

    Permission is hereby granted, free of charge, to any person obtaining a copy of
    this software and associated documentation files (the "Software"), to deal in
    the Software without restriction, including without limitation the rights to
    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
    of the Software, and to permit persons to whom the Software is furnished to do
    so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.

*******************************************************************************/
/*
  To use, an XHTML file should include this scrpit file and should structure its
  content as follows. It also needs to invoke the function Faq.prepareFaq(); when the page
  has loaded ( <body onload="Faq.prepareFaq()"> )

<div class="category">
<h3 class="category-title">...</h3>

<div class="faq" id="@@" updated="YYYY-MM-DDTHH:MM:SSZ">
<div class="question">@@</div>
<div class="answer">
<p>@@</p>
</div>
</div>

*/
var Faq = {
    options : { number : true, categories : true }
};

Faq.questionClicked = function(event, answer) {
    // toggle status of the answer (change class info for faq/question?)
    // note that the default status needs to be all visible, but it's ok to do
    // other changes via CSS class changes which hide/reveal elements.
    Element.toggle(answer);
}

Faq._prepareFaqs = function(parent, preface) {
    var i = 1;
    document.getElementsByClassName("faq", parent).each(
      function(faq) {
        // @@ find internal links w/in the page and add a click event handler
        //    which expands the target question (need to strip frag ID and 
        //    then match strings? (#href shows up as the full URL in a.href)
        var q = document.getElementsByClassName("question", faq)[0];
        q.innerHTML = '<a href="#' + faq.id + '" name="#' + faq.id + '" class="question-text">' + q.innerHTML + '</a>';
        var qtext = document.getElementsByClassName("question-text", q)[0];
        var a = document.getElementsByClassName("answer", faq)[0];
        if (Faq.options.number) {
            new Insertion.Top(q, preface + i++ + ". ");
        }
        // add a permalink
        //new Insertion.Bottom(q, '<a class="permalink" href="#' + faq.id + '">permalink</a>');
        Element.hide(a);
        Event.observe(qtext, "click", function (e) { Faq.questionClicked(e, a); return false; }, false);
        Event.observe(qtext, "mouseover", function(e) { Element.addClassName(qtext, "as-link"); });
        Event.observe(qtext, "mouseout", function(e) { Element.removeClassName(qtext, "as-link"); });
      }
    );
}

Faq._toggleAll = function(expand) {
    document.getElementsByClassName("answer").each(
      function (answer) {
        if (expand) Element.show(answer); else Element.hide(answer);
      }
    );
}

Faq.expandAll = function() {
    Faq._toggleAll(true);
}
Faq.collapseAll = function() {
    Faq._toggleAll(false);
}

Faq.gotoFaq = function(part) {
    var faq = $(part);
    Element.scrollTo(faq);
    Element.show(document.getElementsByClassName("answer", faq)[0]);
}

Faq.prepareFaq = function() {
    // collapse and (if desired) number all the categories
    var i = 1;
    if (Faq.options.categories) {
        document.getElementsByClassName("category").each(
          function(category) {
            var preface = i++ + ".";
            if (Faq.options.number) {
                new Insertion.Top(document.getElementsByClassName("category-title", category)[0], preface + " ");
            }
            Faq._prepareFaqs(category, preface);
          }
        );
    } else {
        Faq._prepareFaqs(document, "");
    }
    var part = document.location.hash;
    if (part && part.length > 1) {
        Faq.gotoFaq(part.substring(1));
    }
        // add expand/collapse all link
    var d = document.createElement("div");
    Element.addClassName(d, "top-left");
    d.innerHTML = '<a href="#" onclick="Faq.expandAll();">expand all</a> | <a href="#" onclick="Faq.collapseAll();">collapse all</a>';
    document.body.insertBefore(d, document.body.firstChild);
}
