// requires prototype.js

// objects to identify:
// font class=normal -- HEADINGS
// table width=390 -- DETAILS

var headings = [];
var details = [];
var title = null;
var detail_data = [];

function get_outer_html(obj) {
    result = obj.outerHTML;
    if (result == null) {
        temp_div = $('img_bottom');
        old_content = temp_div.innerHTML;
        temp_div.innerHTML = '';
        temp_div.appendChild(obj.cloneNode(true));
        result = temp_div.innerHTML;
        temp_div.innerHTML = old_content;
    }
    return result;
}

// lame hackery but good enough :
function set_outer_html(obj, new_text) {
    if (obj.outerHTML == null) {
        orig_text = get_outer_html(obj);
        p = obj.parentNode;
        p.innerHTML = p.innerHTML.replace(orig_text, new_text);
    } else {
        obj.outerHTML = new_text;
    }
}

// private
function all_expander() {
    return "<a class=\"expand_all\" href=\"#\" onclick=\"javascript: expandAll(); return false;\">EXPAND ALL</a>";
}

// private
function all_contracter() {
    return "<a class=\"expand_all\" href=\"#\" onclick=\"javascript: contractAll(); return false;\">COLLAPSE ALL</a>";
}

//private
function item_count(index) {
    items = detail_data[index].items;
    if (items == 0)
        return ' ';
    else
        return " (" + items + ") ";
}

// private
function expander(index) {
	return "<span onclick=\"javascript: expandSection(" + index + ");\">" + item_count(index) + "<img style=\"margin-top: 3px;\" src=\"/GFX/list_closed.gif\" /></span> ";
}

// private
function contracter(index) {
	return "<span onclick=\"javascript: contractSection(" + index + ");\">" + item_count(index) + "<img style=\"margin-top: 3px;\" src=\"/GFX/list_open.gif\" /></span> ";
}

// private
function reload_for_ie() {
    // this only really needs to run in ie6 but I don't think it's a big deal on the other browsers...
    mc = $('interior');
    if(mc)
      mc.innerHTML = mc.innerHTML;
    id_sections();
}

// private
function do_expand_section(index) {
	details[index].style.display = '';
	headings[index].innerHTML = contracter(index) + detail_data[index].original_header;
}

// public
function expandSection(index) {
    do_expand_section(index);
    reload_for_ie();
}

// private
function do_contract_section(index) {
	details[index].style.display = "none";
	headings[index].innerHTML = expander(index) + detail_data[index].original_header;
}

// private
function onAll(fct) {
    details.each( function(item, index) {
        fct(index);
    });
    reload_for_ie();
}

// public
function expandAll() {
    onAll(do_expand_section);
}

// public
function contractAll() {
    onAll(do_contract_section);
}

// public
function contractSection(index) {
    do_contract_section(index);
    reload_for_ie();
}

// private
function id_sections() {
    
    headings = $A(document.getElementsByTagName('font'));
    details = $A(document.getElementsByTagName('table'));
    details = details.findAll( function(value, index) { 
        return value.width == 390;
    });
    title = $A(document.getElementsByTagName('h4'));
    title = title.findAll( function(value, index) {
        return value.className == 'red';
    })[0];
}

// internal
function fold_lists() {
    id_sections();
    show_title = false;
    detail_data = details.map( function(item, index) {
        h = {
            display: item.style.display,
            height: item.style.height,
            top: item.style.top,
            original_header: '',
            items: 0
        };
        t = item.innerHTML;
        r = /<TR/;
        h.items = t.split(r).length - 1;
        show_title = true;
        // start with items visible
        // item.style.display = "none";
        return h;
    });
    headings.each( function(item, index) {
        detail_data[index].original_header = item.innerHTML;
        item.innerHTML = contracter(index) + item.innerHTML;
    });
    if (show_title) {
        set_outer_html(title, get_outer_html(title) + '<div id="expansionists">' + 
            all_expander() + ' | ' + all_contracter() +
            '</div>');
        $('interior').innerHTML += '<div id="expansionists">' + 
            all_expander() + ' | ' + all_contracter() +
            '</div>';
    }
}

fold_lists();
expandAll();

