﻿
function openSection(element){
    
    //Closes all <tr>s in the table.
    closeAll();
    
    // Finds the section to open
    var section = element.parentNode.parentNode.nextSibling
    
    // We only need to open the <tr> if it is already closed...
    if (section.style.display == "none" || section.style.display == ""){
        // Sets the display property of the <tr>
        section.style.display = "";
        
        // Swivels the arrow to point vertically
        element.style.backgroundPosition = "left 0.4em"
    }
}

function closeAll(){
    
    // Finds the table in the DOM
    var table = document.getElementById('top-10-bills');
    
    // Gets all the <tr>s inside the table
    var all = table.getElementsByTagName('tr')
    
    // Sets up a regular expression to find the word "content"
    var re = new RegExp('\\bcontent\\b');
    
    for (var i=0;i<all.length;i++){
    
        // Tests the string returned by className against the previously created regular expression
        if (re.test(all[i].className)){
            // Closes the relative <tr>
            close(all[i])
        }
    }
}

function close(element){
    //element.style.display = "block"

    // Tests if the passed element exists
    if (element){
        
        // Hides the <tr>
        element.style.display = "none"
        
        // Swivels the arrow to point horizontally
        element.previousSibling.childNodes[0].childNodes[0].style.backgroundPosition= "left -497px"
    }
}

function init(){

    // Sets the onclick event for each <a> in the table
    var table = document.getElementById('top-10-bills');
    var all = table.getElementsByTagName('a')
    var reA = new RegExp('\\bbuildownbill\\b');
    var reTR = new RegExp('\\bbuildownbill-content\\b');
    
    for (var i=0;i<all.length;i++){
    
        // Tests the string returned by className against the previously created regular expression
        if (reA.test(all[i].className)){
            all[i].onclick = function(){
                openSection(this);
                return false;
            };
            all[i].href = "#"
            var tmpElement = all[i].parentNode.parentNode.nextSibling
            if (reTR.test(tmpElement.className)){
                tmpElement.style.display = "none"
            }
        }
    }
    
    // Closes all <tr>s to begin.
    //closeAll();
    
}