function Elektra(nid) {
    this.container = document.getElementById(nid);
    this.markers = new Object();
    this.templates = new Object();
    var children = this.container.childNodes;
    var pending = new Array();
    for(var index = 0; index < children.length; index++) {
        var node = children[index];
        if(node.nodeType != 1)
            continue;
        if(node.id) {
            this.markers[node.id] = {
                node: node,
                before: new Array(),
                after: new Array()
            };
            continue;
        }
        if(node.className)
            pending.push(node);
    }
    while(pending.length > 0) {
        var element = this.container.removeChild(pending.shift());
        this.templates[element.className] = element;
    }
}

Elektra.prototype.before = function(key) {
    var host = this;
    var mark = this.markers[key];
    var target = this.container;
    return {
        add: function(name) {
            var element = host.templates[name].cloneNode(true);
            mark.before.push(element);
            target.insertBefore(element, mark.node);
            return element;
        },
        clear: function() {
            while(mark.before.length > 0)
                target.removeChild(mark.before.shift());
        }
    };
}

Elektra.prototype.after = function(key) {
    var host = this;
    var mark = this.markers[key];
    var target = this.container;
    return {
        add: function(name) {
            var element = host.templates[name].cloneNode(true);
            mark.after.push(element);
            var location = mark.node.nextSibling;
            if(location)
                target.insertBefore(element, location);
            else
                target.appendChild(element);
            return element;
        },
        clear: function() {
            while(mark.after.length > 0)
                target.removeChild(mark.after.shift());
        }
    };
}

/*
 * Software License
 * 
 * Copyright (c) 2006 Juha M. Pohjalainen.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if any, must
 *    include the following acknowledgment:
 * 
 *       "This product includes software developed by Juha M. Pohjalainen."
 * 
 *    Alternately, this acknowledgment may appear in the software itself, if and
 *    wherever such third-party acknowledgments normally appear.
 * 
 * 4. The name "Juha M. Pohjalainen" must not be used to endorse or promote
 *    products derived from this software without prior written permission.
 *    For written permission, please contact jmp@iki.fi.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JUHA M.
 * POHJALAINEN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */
