// start by getting all the questions and answers
// these will be put into arrays

var questions = document.getElementsByTagName('dt');
var answers = document.getElementsByTagName('dd');


// function for the link that turns them all off
function toggleAllOff(){
	for (var i = 0; i < answers.length; i++) { 
		answers[i].className = 'hide';
	}
}

// function for the link that turns them all on
function toggleAllOn(){
	for (var i = 0; i < answers.length; i++) { 
		answers[i].className = 'show';
	}

}

//hide/show all 
function toggleSection(e, action, section) {
    if(e != false) { 
        e.preventDefault();
    }
	
    var action = action.split('-')[0];
    $("dd", section).each(function() {
        toggleIndividual(this, action, true);
    });
    
    $("dt", section).each(function() {
        toggleIndividual(this,action,true);
    });
	
	trackClick(document.location.href+'#'+section+'-'+action);
}

function toggleIndividual(elm, action, notrack) {
    switch(elm.tagName) {
        case 'DD':
            if(!elm.className.match(action)) {
                toggleVisClass(elm,notrack);
            }
            break;
            
        case 'DT':
            if(elm.className.match(action)) {
                toggleVisClass(elm,notrack);
            }
            break;
    
        default:
            return;
    }
}

function toggleVisClass(elm,notrack) {
    elm.className = ($(elm).hasClass('hide')) ? 'show' : 'hide';
	var index = $(elm.parentNode).find(elm.tagName).index(elm);
	if(!notrack) {
		trackClick(document.location.href+'#'+elm.parentNode.id+index+'-'+elm.className);
	}
}


function toggleNext(el) {
 var next=el.nextSibling;
 next.style.display=((next.style.display=="none") ? "block" : "none");
}

function next(elem) {
    do {
        elem = elem.nextSibling;
    } while (elem && elem.nodeType != 1);
    return elem;                
}



//makes the definition lists click-able
function displayToggle(){
    $( function() {
    	//event listener for when on faq page
    	//raina isn't sure why this won't work from non-faq pages
	    $(".beige-box a").click(function(e){
		    var anchor = $(this).attr('href').split('#');
			var keyword = anchor[1];
			document.getElementById('q-'+keyword).setAttribute('class', 'hide');
			document.getElementById('a-'+keyword).setAttribute('class', 'show');
	    });
		//sidebar links open specific question on faqs page for non-faq pages
	    var anchor = document.location.href.split('#');
	    var keyword = anchor[1];    
	    var url = new RegExp('http:\/\/.*?\/faqs#'+keyword);   
	    if(document.location.href.match(url)) {
	    	document.getElementById('q-'+keyword).setAttribute('class', 'hide');
			document.getElementById('a-'+keyword).setAttribute('class', 'show'); 
	    }
	});

     //get all faq control sections and add listeners to control links
     $('div.faq-control').each(function() {
        $("a", this).each(function() {
            var oLink = $(this);
           $(this).click(function(e) {toggleSection(e, oLink.attr('class'), oLink.attr('href')); }); 
        });
     });
     
     //put listeners on all data list children
     $("#prime dl").each(function() {        
        $("dt", this).each(function() {
            var _this = this;
            $(this).click(function(e){ 
                toggleIndividual(_this, _this.className, true); 
                var n = next(_this); 
                toggleIndividual(n, (n.className == 'show') ? 'hide' : 'show'); });
        });
     });

}

// initiates the click-able dt's when the page loads
window.onload=function() {
    //if there is a hash in the url, open the correct section
    var faqTarget = window.location.hash.split('#')[1];
    if(document.getElementById(faqTarget) != null) {
        toggleSection(false, 'show-all', window.location.hash);
    }
    
	displayToggle();
	}
