// Defines
// extensions to protoype's Element

// Rename prototype's Element class for easy access
var EL = Element;

var el = function(args) {
	var results = [], element;
	for (var i = 0; i < arguments.length; i++) {
		element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
//		element = GenericPage.getRegisteredElement(element);
        var extended = Element.extend(element);
        // ie will sometimes strip functions off of the element, this make sure they always get readded when stripped
        extended._extended = function() {return true;};
		results.push(extended);
	}
	return results.length < 2 ? results[0] : results;
};

var nino = function(node, id) {
	return EL.getChildById(node, id);
};

// Extend prototype's Element class
Object.extend(Element.Methods, {

	// Attaches a behavior object to the current element
	addBehavior: function (elem, behavior, args) {
        return GenericBehavior.addBehavior(elem, behavior, args);
	},

    tell: function(elem, method, args) {
        return elem[callback] ? elem[callback].call(elem, args) : null;
    },
    
    tellChildren: function(elem, method, args) {
        elem.eachChild(function(child) {
           var childelem = el(child);
           childelem[method] ? childelem[method].call(childelem, args) : null;
        });
    },
    
    tellParents: function(elem, method, args) {
        var args = $A(arguments), elem=args.shift(), callback = args.shift();
        elem.eachParent(function(child) {
           var childelem = el(child);
           childelem[method] ? childelem[method].call(childelem, args) : null;
        });
    },
    
    tellBehaviors: function(elem, method, args) {
        GenericBehavior.tellElementBehaviors(elem, method, args);
    },


    eachChild: function (elem, action) {
        var children = EL.getAllChildren(elem);
        var result = $A(children).each(action);
        return result;
    },
 
    
	getAllChildren: function (node, result) {
        return getAllChildren(node);
        
        var result = result || new Array();
        var children = node.childNodes;
        for (var i=0; i < children.length; i++) {
            var child = children[i];
            if (!child.getAttribute) { continue; }
            result.push(child);
            arguments.callee(child, result);
        }
        return result;
	},
    
    eachParent: function(node, action, result) {
        try {
            var result = result || new Array();
            var parent = node.parentNode;
            if (!parent) { return result; }
            var theresult;
            try{
                theresult = action(parent);    
                if (theresult != null) { result.push(theresult); }
                arguments.callee(parent, action, result);
            }
            catch (e) {
                if (e.result != null) {
                    result.push(e.result);
                }
                throw e;
            }
        }
        finally { return result; }
    },
    
    
    getChildById: function(elem, id) {
        var children = elem.getAllChildren();
        for (var i = 0; i < children.length; i++) {
            if (children[i].id == id) {
                return el(children[i]);
            }
        }
        return null;
    },
    el: function(elem, id) {
        return typeof id != 'string' ? el(id) : Element.getChildById(elem,id);
    },
    	
	createDocumentFragment: function() {
		var newnode;
		try { newnode = document.createDocumentFragment(); }
		catch(e) { newnode = document.createElement('span'); } // IE 5.0
		return el(newnode);
	},
    
    
    cloneInto: function(elem, id, dest) {
        var newelem = document.createElement(elem.tagName);
        var attrs = elem.attributes;
        newelem.setAttribute('id', id);
        for (var i = 0; i < attrs.length; i++) {
            var value = attrs[i].value;
            if (value == null) { continue; }
            var name = attrs[i].name;
            if (name == 'id') { continue; }
            newelem.setAttribute(name, value);
        }
        newelem.innerHTML = elem.innerHTML;
        newelem = dest.appendChild(newelem);
        //window.alert(dest.getAllChildren().length);
        return newelem;
    }
});


Object.extend(Element, Element.Methods);



function elCSS(rule) {
    var elems = document.getElementsBySelector(rule);
    var els = elems.map(function(elem) { return el(elem) });
    return els;
}





