var TemplateBit = new Class ({
    /* Implements */
    Implements: Events,
    
    /* Public variables */
    element: null,
    properties: new Hash(),
    rendered: false,
    displayStyle: "block",
    
    /* Functions */
    initialize: function(element) {
        this.element = element.clone();
	/*if (document.createElement) {
		document.createElement("tmpl");
	}*/
        //var props = this.element.getElements("tmpl");
	//var props = document.getElementsByTagName ("tmpl");
	var props = $$("tmplbit", this.element);
         
        props.each(function(el) {            
	    alert (el.getProperty("id") + " had " + el.getProperty("name"));
            if (el.getProperty("name"))
            {
	    	alert ("Yay.");
                // If element has editchild attribute, use what it says (and default to el is child doesn't exist), toherwise use el.
                /*var e = el.getProperty("editchild") ? el.getChildren()[el.getProperty("editchild").toInt()] || el : el;*/
                this.properties.set(el.getProperty("name"), el);
		alert ("Set " + el.getProperty("name") + " to " + el);
            }
        }, this);
        
        this.fireEvent('onInitialize', element);
	alert ("init was OK!");
    },
    
    set: function(array, keys) {
        if (!keys && $type(array) == "hash") {
            keys = array.getKeys();
        }
        else if (!keys) {
            keys = array;
        }
        
        if ($type(array) != "hash") {
            array = new Hash(array);
        }
        
        // Set the values.
        var keys = array.getKeys();
        $each(keys, function(key) {
            if (array.get(key) != null) {
	    	alert ("Hmm.");
                var elem = this.properties.get(key);
                alert ("elem was: " + elem + "; key was" + key);

                if (elem.getProperty("autoassign")) {
                    // Find a matching element and use that as a value
                    var thisname = elem.getProperty("name");
                    
                    // First, get sub-tmpl children
                    var children = elem.getElements("tmpl");
                    children.each(function (child) {
                        if (child.getProperty("source") == thisname) {
                            // Relates to this template bit, now do val
                            var expects = child.getProperty("option");
			    alert ("Got here.");
                            if (array[key] + "" == expects) {
                                this.properties.set(thisname, child);
                                child.replaces(elem);
                            }
			    
                        }
                    }, this);
                }
                else {
                    // Set the text if we're not an autoassign tmpl
		    alert ("Got to non-autoassign templ.");
                    elem.set('text', array[key]);
                }
            }
        }, this);
        
	alert ("Set was OK!");

        this.fireEvent('onSet', [array, keys]);
    },
    
    render: function(destelem) {
        // unhide root element
        this.element.setStyle('display', this.displayStyle);
        
        // make the destination sign the adoption papers.
        destelem.adopt(this.element);
        
        // leave the country.
        rendered = true;
        alert ("Rendered OK!");
        this.fireEvent('onRender', destelem);
    }
});
