/* wordbox.js
 *
 *
 */


WDB = function() {
    var contents = null;
    var index = null;
    var current = null;

    var toArray = function(nodeList) {
        return Array.prototype.slice.call(nodeList, 0);
    };

    var zip = function() {
        var out = [];
        var len = Math.max.apply(this,
                                 Array.prototype.map.call(
                                     arguments,
                                     function(a) { return a.length; }));
        for(var i = 0; i < len; i++) {
            var item = [];
            for(var a = 0; a < arguments.length; a++) {
                item.push(arguments[a][i]);
            }
            out.push(item);
        }
        return out;
    };

    var split = function(a, l) {
        var i = 0;
        var out = [];
        while(i < a.length) {
            out.push(a.slice(i, i + l));
            i = i + l;
        }
        return out;
    };

    return {
        init: function() {
            contents = document.getElementById('wordbox_contents');
            var index_div = document.getElementById('wordbox_index');
	    if(!index_div) return;

            index = document.createElement('table');
            index.setAttribute('class', 'wordbox_table');
            index_div.appendChild(index);

            var sections = toArray(contents.getElementsByTagName('div'));
            var that = this;
            var sortfn = function(a, b) {
                return that.sectionText(a).localeCompare(that.sectionText(b));
            };
            sections.sort(sortfn);

            var num_rows = Math.ceil(sections.length / 3);
            var rows = zip.apply(this, split(sections, num_rows));

            for(var i = 0; i < sections.length; i++) {
                this.insertControls(sections[i]);
            }

            for(var r = 0; r < rows.length; r++) {
                this.insertRow(index, rows[r]);
            }
        },

        insertControls: function(section) {
            var d = document.createElement('div');
            d.setAttribute('class', 'wordbox_header');
            var a = document.createElement('a');
            a.setAttribute('href', '#');
            var that = this;
            var close = function(e) {
                that.showElement(section, false);
            };
            a.addEventListener('click', close, false);
            a.appendChild(document.createTextNode('close'));
            d.appendChild(a);
            section.insertBefore(d, section.firstChild);
        },

        showElement: function(element, show) {
            if(current) {
                current.style.setProperty('display', 'none', '');
            }
            element.style.setProperty('display', show ? 'block' : 'none', '');
            current = element;
        },

        createIndexItem: function(section) {
            var entry = document.createElement('a');
            entry.setAttribute('href', '#' + section.getAttribute('id'));
            var text = document.createTextNode(this.sectionText(section));
            entry.appendChild(text);
            var that = this;
            var listener = function(event) {
                that.showElement(section, true);
            };
            entry.addEventListener('click', listener, false);
            return entry;
        },

        insertRow: function(index, items) {
            var tr = document.createElement('tr');
            for(var i = 0; i < items.length; i++) {
                if(items[i]) {
                    var td = document.createElement('td');
                    var entry = this.createIndexItem(items[i]);
                    td.appendChild(entry);
                    tr.appendChild(td);
                }
            }
            index.appendChild(tr);
        },

        sectionText: function(section)  {
            var headers = section.getElementsByTagName('h1');
            if(headers.length > 0) {
                return headers[0].firstChild.data;
            } else {
                return 'unknown';
            }
        }
    };
}();

function WDB_hide(event) {
    if(window.location != window.parent.location) {
	var header = document.getElementById("header");
	header.style.setProperty('display', 'none');
    }
}

window.addEventListener('load',
                        function(event) { WDB.init(); },
                        false);
window.addEventListener('load', WDB_hide, false);

