(function ($) {

	var defaultCollapsedClass = 'col';

	$.fn.extend({
		filelist: function (options) {
			options = $.extend({
				collapse: false
			}, options);
			this.each(function () {
				var node = this;
				$('> a:first', this.parentNode).click(function (e) {
					e.preventDefault();
					node.filelistIsCollapsed ? $(node).filelistExpand(): $(node).filelistCollapse();
				});
				if (options.collapse) {
					$(this).filelistCollapse();
				}
				$('> li > ul', this).filelist(options);
			});
			return this;
		},
		filelistCollapse: function () {
			this.each(function () {
				$(this.parentNode).addClass(defaultCollapsedClass);
				this.filelistIsCollapsed = true;
			});
			return this;
		},
		filelistExpand: function () {
			this.each(function () {
				$(this.parentNode).removeClass(defaultCollapsedClass);
				this.filelistIsCollapsed = false;
			});
			return this;
		}
	});

})(jQuery);

jQuery(function () {
	$('.filelist > ul > li > ul').filelist({
		collapse: true
	}).filelistExpand();
});