var DU;
if (!DU) DU = {};
if (!DU.Plugins) DU.Plugins = {};

DU.Plugins.Contents = {
    pluginName:       'Contents',
    pluginMenuName:   'Содержание',
    elements:         [],
    conteinersText:       [],
    sectionNumbers:   [0, 0, 0, 0, 0, 0],
    h1ContentTitle:   'Содержание',
    numbersSeparator: '.',
    isBacklink:       1,
    backlinkText:     '',
    hStart:           1,
    
    enumerator: function(node) {
        if (node.nodeType != 1) {
            return;
        }
        
        var children = node.childNodes;
        
        if (node.tagName.length == 2 && node.tagName.charAt(0) == 'H') {
            this.elements.push(node);
        }
        
        if (node.tagName.length == 3 && node.className == 'conteiner_prog_text') {
            this.conteinersText.push(node);
        }



        for (var i = 0; i < children.length; i++) {
            this.enumerator(children[i]);
        }
    },
    
    afterLoadExec: function() {
        var container = document.getElementById('matter');
        if (container !== null) {
        this.enumerator(container);
        
        if (this.elements.length <= 0) {
            return;
        }
        
        if (!container) {
            alert('Не обнаружен html-контейнер для построения оглавления.');
            return;
        }
        
        var contentDiv = document.createElement('div');
        DU.Functions.attr(contentDiv, 'id', 'DU_cont');
        
        var content_h1 = document.createElement('h1');
        //DU.Functions.attr(content_h1, 'id', 'h1ContentTitle');
        content_h1.appendChild(document.createTextNode(this.h1ContentTitle));
        contentDiv.appendChild(content_h1);
        
        container.parentNode.insertBefore(contentDiv, container);
        
        var anchor = document.createElement('a');
        DU.Functions.attr(anchor, 'name', 'DU_top');
        DU.Functions.attr(anchor, 'id', 'DU_top');
        contentDiv.parentNode.insertBefore(anchor, contentDiv);
        
        if (!this.hStart || this.hStart < 1 || this.hStart > 6) {
            return;
        }


       
        for (var s = 0; s < this.elements.length; s++) {
            var section = this.elements[s];
            var sectionConteiner = this.conteinersText[s];


            var level = parseInt(section.tagName.charAt(1));
            
            level = (level - this.hStart) + 1;
            
            if (isNaN(level) || level < 1 || level > 6) {
                continue;
            }
            
            this.sectionNumbers[level - 1]++;
            
            for (var i = level; i < 6; i++) {
                this.sectionNumbers[i] = 0;
            }
            
            var sectionNumber = '';
            for (i = 0; i < level; i++) {
                sectionNumber += this.sectionNumbers[i];
                sectionNumber += this.numbersSeparator;
                if (i < level - 1) {
                    sectionNumber += this.numbersSeparator;
                }
            }
            
            var frag = document.createDocumentFragment();
            var span = document.createElement('span');
            DU.Functions.attr(span, 'class', 'DU_SectNum');
            span.appendChild(document.createTextNode(sectionNumber));
            frag.appendChild(span);
            frag.appendChild(document.createTextNode(' '));
            section.insertBefore(frag, section.firstChild);
            
            var anchor = document.createElement('a');
            DU.Functions.attr(anchor, 'name', 'DU_' + sectionNumber);
            DU.Functions.attr(anchor, 'id', 'DU_' + sectionNumber);
            
            if (this.isBacklink) {
                var link = document.createElement('a');
                var clear = document.createElement('div');
                clear.style.clear = 'both';
                DU.Functions.attr(link, 'href', '#DU_top');
                DU.Functions.attr(link, 'class', 'DU_top_Nav');
                
                link.appendChild(document.createTextNode(this.backlinkText));
                sectionConteiner.appendChild(link);
                sectionConteiner.appendChild(clear);
            }
            
            section.parentNode.insertBefore(anchor, section);
            

            var link = document.createElement('a');
            DU.Functions.attr(link, 'href', '#DU_' + sectionNumber);
            link.innerHTML = section.innerHTML;
            
            var entry = document.createElement('div');
            DU.Functions.attr(entry, 'class', 'DU_Entry DU_HLevel' + level);
            entry.appendChild(link);
            
            contentDiv.appendChild(entry);
        }
        }
    }
}

hes.functions.addEvent(window, 'load', function() {DU.Plugins.Contents.afterLoadExec();});
