$(function() {
	// hide all the sub-menus
	$("span.folder2").next().hide();
	
	// set the cursor of the toggling span elements
	$("span.folder2").css("cursor", "pointer");
	
	closeFolders2 = function(){
		$("li.open2").each(function(){
			$(this).removeClass('open2');
			$(this).addClass('closed2');
			if ($(this).children('.docs2').is(':visible')){
				$(this).children('.docs2').hide(1000);
			}
		});
		$("li.open_folders2").each(function(){
			$(this).removeClass('open_folders2');
			$(this).addClass('closed_folders2');
			if ($(this).children('.sub2').is(':visible')){
				$(this).children('.sub2').hide(1000);
			}
		});
		$("li.open_sub2").each(function(){
			$(this).removeClass('open_sub2');
			$(this).addClass('closed_sub2');
		});
		$("li.open_sub_child2").each(function(){
			$(this).removeClass('open_sub_child2');
			$(this).addClass('closed_sub_child2');
			if ($(this).children('.sub_folders2').is(':visible')){
				$(this).children('.sub_folders2').hide(1000);
			}
		});
		$("li.open_sub_folder2").each(function(){
			$(this).removeClass('open_sub_folder2');
			$(this).addClass('closed_sub_folder2');
		});
	}

	closeChild2 = function(){
		$("li.open_sub2").each(function(){
			$(this).removeClass('open_sub2');
			$(this).addClass('closed_sub2');
		});
		$("li.open_sub_child2").each(function(){
			$(this).removeClass('open_sub_child2');
			$(this).addClass('closed_sub_child2');
			if ($(this).children('.sub_folders2').is(':visible')){
				$(this).children('.sub_folders2').hide(1000);
			}
		});
		$("li.open_sub_folder2").each(function(){
			$(this).removeClass('open_sub_folder2');
			$(this).addClass('closed_sub_folder2');
		});
	}

	closeFolders = function(){
		$("li.open").each(function(){
			$(this).removeClass('open');
			$(this).addClass('closed');
			if ($(this).children('.docs2').is(':visible')){
				$(this).children('.docs2').hide(1000);
			}
		});
		$("li.open_folders").each(function(){
			$(this).removeClass('open_folders');
			$(this).addClass('closed_folders');
			if ($(this).children('.sub').is(':visible')){
				$(this).children('.sub').hide(1000);
			}
			if ($(this).children('.docs').is(':visible')){
				$(this).children('.docs').hide(1000);
			}
		});
		$("li.open_sub").each(function(){
			$(this).removeClass('open_sub');
			$(this).addClass('closed_sub');
			if ($(this).children('.docs').is(':visible')){
				$(this).children('.docs').hide(1000);
			}
		});
		$("li.open_sub_child").each(function(){
			$(this).removeClass('open_sub_child');
			$(this).addClass('closed_sub_child');
			if ($(this).children('.sub_folders').is(':visible')){
				$(this).children('.sub_folders').hide(1000);
			}
		});
		$("li.open_sub_folder").each(function(){
			$(this).removeClass('open_sub_folder');
			$(this).addClass('closed_sub_folder');
		});
	}

	closeChild = function(){
		$("li.open_sub").each(function(){
			$(this).removeClass('open_sub');
			$(this).addClass('closed_sub');
			if ($(this).children('.docs').is(':visible')){
				$(this).children('.docs').hide(1000);
			}
		});
		$("li.open_sub_child").each(function(){
			$(this).removeClass('open_sub_child');
			$(this).addClass('closed_sub_child');
			if ($(this).children('.sub_folders').is(':visible')){
				$(this).children('.sub_folders').hide(1000);
			}
		});
		$("li.open_sub_folder").each(function(){
			$(this).removeClass('open_sub_folder');
			$(this).addClass('closed_sub_folder');
		});
	}
	
	// add a click function that toggles the sub-menu when the corresponding
	// span element is clicked
	$("span.folder2").click(function() {
		Shadowbox.clearCache();
		Shadowbox.setup();
		if ($(this).parent().hasClass('closed2')){
			closeFolders2();
			closeFolders();			
			$(this).parent().removeClass('closed2');
			$(this).parent().addClass('open2');
			var files2 = $(this).next("ul.files2").html();
			var docs2 = $(this).next("ul.docs2").html();
			if(files2 != null){
				if($('#filelist').is(':hidden')){
					$('#filelist').html(files2);
					$('#filelist').show(1000);
				}else{
					$('#filelist').html('');
					$('#filelist').html(files2);
				}
			}else{
				if(docs2 == null){
					if($('#filelist').is(':hidden')){
						$('#filelist').html('<li>This folder is empty');
						$('#filelist').show(1000);
					}else{
						$('#filelist').html('');
						$('#filelist').html('<li>This folder is empty');
					}
				}
			}
			
			if (docs2 != null){
				$(this).next("ul.docs2").show(1000);
			}
			
			if(docs2 != null && files2 == null){
				if($('#filelist').is(':visible')){
					$('#filelist').html('');
					$('#filelist').hide(1000);
				}
			}
		}else if ($(this).parent().hasClass('open2')){
			$(this).parent().removeClass('open2');
			$(this).parent().addClass('closed2');
			$('#filelist').hide(1000);
			$('#filelist').html('');
			if ($(this).parent().children('.docs2').is(':visible')){
				$(this).parent().children('.docs2').hide(1000);
			}

		}else if ($(this).parent().hasClass('closed_folders2')){
			closeFolders2();
			closeFolders();
			$(this).next().show(1000);
			$(this).parent().removeClass('closed_folders2');
			$(this).parent().addClass('open_folders2');
			var files2 = $(this).next('ul.sub2').children("ul.files2").html();
			if (files2 != null){
				if($('#filelist').is(':visible')){
					$('#filelist').html('');
					$('#filelist').html(files2);
				}else{
					$('#filelist').html(files2);
					$('#filelist').show(1000);
				}
			 }else{
				if($('#filelist').is(':visible')){
					$('#filelist').hide(1000);
					$('#filelist').html('');
				}
			}
		}else if ($(this).parent().hasClass('open_folders2')){
			$(this).next().hide(1000);
			$(this).parent().removeClass('open_folders2');
			$(this).parent().addClass('closed_folders2');
			if($('#filelist').is(':visible')){
				$('#filelist').hide(1000);
				$('#filelist').html('');
			}
		}else if ($(this).parent().hasClass('closed_sub2')){
			closeChild2();
			closeChild();
			$(this).parent().removeClass('closed_sub2');
			$(this).parent().addClass('open_sub2');
			var files2 = $(this).next("ul.files2").html();
			if (files2 != null){
				if($('#filelist').is(':visible')){
					$('#filelist').html('');
					$('#filelist').html(files2);
				}else{
					$('#filelist').html(files2);
					$('#filelist').show(1000);
				}
			}
		}else if ($(this).parent().hasClass('open_sub2')){
			$(this).parent().removeClass('open_sub2');
			$(this).parent().addClass('closed_sub2');
			$('#filelist').hide(1000);
			$('#filelist').html('');
		}else if ($(this).parent().hasClass('closed_sub_child2')){
			closeChild2();
			closeChild();
			$(this).next().show(1000);
			$(this).parent().removeClass('closed_sub_child2');
			$(this).parent().addClass('open_sub_child2');
			var files2 = $(this).next('ul.sub_folders2').children("ul.files2").html();
			if (files2 != null){
				if($('#filelist').is(':visible')){
					$('#filelist').html('');
					$('#filelist').html(files2);
				}else{
					$('#filelist').html(files2);
					$('#filelist').show(1000);
				}
			}
		}else if ($(this).parent().hasClass('open_sub_child2')){
			$(this).parent().removeClass('open_sub_child2');
			$(this).parent().addClass('closed_sub_child2');
			$('#filelist').hide(1000);
			$('#filelist').html('');
		}else if ($(this).parent().hasClass('closed_sub_folder2')){
			$(this).parent().removeClass('closed_sub_folder2');
			$(this).parent().addClass('open_sub_folder2');
			var files2 = $(this).next("ul.files2").html();
			if (files2 != null){
				if($('#filelist').is(':visible')){
					$('#filelist').html('');
					$('#filelist').html(files2);
				}else{
					$('#filelist').html(files2);
					$('#filelist').show(1000);
				}
			 }
		}else if ($(this).parent().hasClass('open_sub_folder2')){
			$(this).parent().removeClass('open_sub_folder2');
			$(this).parent().addClass('closed_sub_folder2');
			$('#filelist').hide(1000);
			$('#filelist').html('');
		}

		Shadowbox.clearCache();
		$('#filelist > li').each(function(){
			$(this).children("a.image").attr('rel', 'shadowbox[mixed]');
			Shadowbox.setup();
		});
	});
});