/*!
 * jQuery Tree parse @VERSION
 *
 * Copyright (c) 2011 excentrics.ru
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * Author: Yarovoy Artem (yarovoy at excentrics dot ru)
 *
 * http://www.excentrics.ru
 */
(jQuery.tree && jQuery.TreeNode && (function($){

$.extend($.tree, {
	
	parse: function(context, selector, extra){
		
		if (!extra) {
			extra = {};
		}
		
		if (typeof selector !== 'function') {	
			// Selector function
			selector = function(extra){
				return $(this).children();
			}
		}
		
		if (context.jquery) {
			if (context.length) {
				context = context[0];	
			} else {
				return null;
			}
		}
		
		var node = $.TreeNode(context),
			children = selector.call(context, extra),
			n = node.length = children.length;		
		for (var i = 0; i < n; i++) {
			var child = $.tree.parse(children[i], selector, extra);
			node[i] = child;
			child._parent = node;
		}	
		return node;
	}

});

})(jQuery)
);
