(function ($) {

	var defaultCollapsedClass = 'collapsed',
	    collapseOnInitClass   = 'collapse';

	$.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.parentNode).hasClass(collapseOnInitClass)) {
					$(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);