/**
 *  Pass in any element under a question's li, to show the associated answer blocks
 * @element
 */
function openParentAnswer(element) {
    //Find the parent li of the clicked node
    var question_li = findParentByTagName(element, 'li' );
    //Get the answer dl's under the li
    var answer = getElementsByClassName('answer', question_li );
    //Finally trigger the show/hide
    for (var j=0,jj=answer.length;j<jj;j++) {
        answer[j].style.display = answer[j].style.display == 'none' ? '' : 'none';
    }
}



addDomEvent(window, 'load', function(){
    //Hide the answers
    var answers = getElementsByClassName('answer', document.getElementById('esp_faqs') );
    for (var i=0,ii=answers.length;i<ii;i++) {
        answers[i].style.display = 'none';
    }
    //Attach onclicks to the questions
    var questions = getElementsByClassName('question', document.getElementById('esp_faqs') );
    for (var y=0,yy=questions.length;y<yy;y++) {
        addDomEvent(questions[y], 'click', function(event) {
            openParentAnswer( getEventTargetNode(event) );
        });
    }
    //Attach onclick to expand all
    var expand_faqs = document.getElementById('expand_faqs');
    addDomEvent(expand_faqs, 'click', function(event) {
        openParentAnswer( expand_faqs );
        expand_faqs.innerHTML = expand_faqs.innerHTML == '+ Expand All' ? '+ Close All' : '+ Expand All';
    });
    //Expand question if an anchor path is present in the url
    var anchors = document.getElementsByTagName('a');
    if (window.location.hash) {
        for (var z=0,zz=anchors.length;z<zz;z++) {
            var rh = new RegExp("^"+window.location.hash.substring(1)+"$", 'i');
            if(rh.test(anchors[z].name)) {
                var hash = anchors[z];
            }
        }
        openParentAnswer( hash );
    }
    //Expand question if parameter q is present in the url
    if (window.location.search) {
        for (var j=0,jj=anchors.length;j<jj;j++) {
            var questionNum = window.location.search.match("[q]=(q[^&#]*)");
            if (questionNum) {
                var rs = new RegExp("^"+questionNum[1]+"$", 'i');
                if(rs.test(anchors[j].name)) {
                    var search = anchors[j];
                }
            }
        }
        try {
            window.location.hash = "#"+search.name;
            openParentAnswer( search );
        } catch (e) {
            //ignore e
        }
    }
});