// Looper Object
// Generate a set of repeating elements
var BehaviorLooper = GenericBehavior.create('BehaviorLooper', {
    onAttach: function(event, elem, args) {
        args.holder = elem;
        this.tellSuper('onAttach', event, elem, args);
        return true;
    },
    
    onDraw: function(event) {
        var args = this.args;
        
        this.action = args.action;
        this.data = $val(args.data);
        this.order = $val(args.order)
        this.repeat = this.e.el(args.repeat.id || this.repeat_id);
        this.repeat_id = this.repeat.id;

        this.snapshot();
                
        //if (!this.origHTML) { this.origHTML = this.holder.innerHTML; }
        var action = this.action;
        var holder = this.e;
        var repeat = this.repeat;
        var repeat_id = this.repeat_id = repeat.id;

        var orig_disp;
        if (args.draw_hidden) {
            orig_disp = this.e.style.visibility;
            this.e.style.visibility = 'hidden';
        }
            
        // prepair data for looping
        var data = $val(this.data);
        var order = this.order;
        var array;
        if (typeof data == 'array') {
            array = data;
        } 
        else if (order) {
            array = new Array();
            for (var i=0; i<order.length; i++) {
                array.push(data[order[i]]);
            }
        }
        else {
            var hash = $H({ });
            data.each(function(item, index) {
                hash.set(index, item);
            });
            array = hash.values();
            order = hash.keys();
        }

        // create duplicates of repeat element
        // this method has been found to be the fastest
        var repeater = document.createElement('div');
        repeater.appendChild(repeat.cloneNode(true));
        var repeat_html = repeater.innerHTML;
        holder.innerHTML = '';
        var addon_html = new Array();
        for (var i=0; i<(array.length); i++) {
            addon_html.push(repeat_html);
        }
        holder.innerHTML = addon_html.join("\n");
        
        // memory cleanup
        //repeater.innerHTML = '';
        repeater = null;
        repeat = null;
        
        // find and id the nodes
        var nodes = new Array();
        for (var i=0; i<holder.childNodes.length; i++) {
            var key = order ? order[i] : i;
            var node = holder.childNodes[i];
            if (node.id == repeat_id) { 
                if (!args.without_auto_id) { node.id = repeat_id+'_'+key; }
                nodes.push(el(node)); 
            }
        }

        // fill in each node with its action
        for (var i=0; i<array.length; i++) {
            var item = array[i];
            var key = order ? order[i] : i;
            var node = nodes[i];
            action({
                node: node, 
                value: item, 
                key: key, 
                index: i,
                behavior: this
            });
        }
        
        if (args.draw_hidden) { this.e.style.visibility = orig_disp; }
        
        return true;
    },
    
    onWillClear: function(event) {
        var repeat_id = this.args.repeat_id || this.repeat_id || this.repeat.id;
        this.repeat = null;
    },
    
    setargs: function(args) {
        this.args = args;
        this.args.repeat_id = el(args.repeat).id;
    }
});

