$(function() {
	//Rewrite tree
	$(".list-block ul li").filter(":has(>ul)").each(function() {
		var isHomePage = $(this).parents('.home-wrapper').length ? true : false;
		var depth = $(this).parents("ul").length;
		var newNode = document.createElement ("div");
		if (isHomePage)
			newNode.className = "list-header expandable level00"+depth;
		else
			newNode.className = "list-header-2 expanded level00"+depth;
		$(this).contents().not("ul").appendTo(newNode);
		$(this).prepend (newNode);
		if(isHomePage)
			$(newNode).siblings().hide();
	});
})

$(function() {
	 //Rewrite tree without children
	 $("#main-container .list-block ul li").filter(":not(>ul)").each(function() {
		var depth = $(this).parents("ul").length;
		if(depth==1) {
			var node = this.firstChild;
			this.removeChild(this.firstChild);
			var newNode = document.createElement ("div");
			newNode.className = "list-header expanded level0"+depth;
			newNode.appendChild (node);
			$(this).prepend (newNode);
		};
	 });
})

$(document).ready(function () {
	/* Eye candy on list documents */
	$(".list-header").bind ("click",function (ev) {
		if (ev.target.tagName == "A" )
			return true;
		
		if ($(this).is(".expanded")) {
			$(this).siblings("").slideUp();
			$(this).removeClass("expanded").addClass("expandable");
		} 
		else {
			$(this).siblings("").slideDown();
			$(this).removeClass("expandable").addClass("expanded");
		}
	});

	$("#tree-collapse-all").bind ("click",function () {
		$(".list-header.expanded").trigger('click');
		return false;
	});

	$("#tree-expand-all").bind ("click",function () {
		$(".list-header.expandable").trigger('click');
		return false;
	});
	
	// class for ie 7.0 and lower
	if($.browser.msie && parseInt($.browser.version) <= 7) {
		$("a[type]").each (function () {
			$(this).append('<img src="/public/images/picto-' + this.type.substring(this.type.indexOf('/') + 1, this.type.length) + '.gif" />');
		});
	}	
});


