var ZFolder = Class.create();

ZFolder.prototype = {
	initialize: function( oUl ) {
		this.oUl = oUl;
		this._recurseTree( oUl, 0 );
	},
	
	_recurseTree: function( oUl, iLevel ) {
		var me = this;
		$A( oUl.childNodes ).each(
			function( oElem, iIdx ) {
				if ( oElem.tagName && oElem.tagName.match( /li/i ) ) {
					oLi = oElem;
					$A( oLi.childNodes ).each(
						function( oElem, iIdx ) {
							if ( oElem.tagName && oElem.tagName.match( /ul/i ) ) {
								Event.observe( oLi, 'click', me.toggleFolder.bindAsEventListener( oLi ), true );
								oLi.subList = oElem;
								Element.hide( oElem );
								me._recurseTree( oElem, iLevel + 1 );
							}
						}
					)
				}
			}
		);
	},
	
	toggleFolder: function( oEvent ) {
		if ( !this.subList ) {
			return oEvent.cancelBubble != undefined ? true : false; 
		}
		
		new Effect.toggle( this.subList, 'appear' );
	}
};

Event.observe(
	window,
	'load',
	function( oEvent ) {
		$A( document.getElementsByClassName( 'replace-with-zfolder' ) ).each(
			function( oElem, iIdx ) {
				if ( oElem.tagName && oElem.tagName.match( /ul/i ) ) {
					new ZFolder( oElem );
				}
			}
		);
	},
	false
);