//animate the opening of the branch (span.grower jQueryElement)
function openBranch(jQueryElement, noAnimation) {
		jQueryElement.addClass('OPEN').removeClass('CLOSE');
		if(noAnimation)
			jQueryElement.parent().find('ul:first').show();
		else
			jQueryElement.parent().find('ul:first').slideDown();
}
//animate the closing of the branch (span.grower jQueryElement)
function closeBranch(jQueryElement, noAnimation) {
	jQueryElement.addClass('CLOSE').removeClass('OPEN');
	if(noAnimation)
		jQueryElement.parent().find('ul:first').hide();
	else
		jQueryElement.parent().find('ul:first').slideUp();
}

//animate the closing or opening of the branch (ul jQueryElement)
function toggleBranch(jQueryElement, noAnimation) {
	if(jQueryElement.hasClass('OPEN'))
		closeBranch(jQueryElement, noAnimation);
	else
		openBranch(jQueryElement, noAnimation);
}

//when the page is loaded...
$(document).ready(function () {
	//to do not execute this script as much as it's called...
	if(!$('ul.tree.dhtml').hasClass('dynamized'))
	{
		//add growers to each ul.tree elements
		$('ul.tree.dhtml ul').prev().before("<span class='grower OPEN' style='display:none;'> </span>");
		
		//dynamically add the '.last' class on each last item of a branch
		$('ul.tree.dhtml ul li:last-child, ul.tree.dhtml li:last-child').addClass('last');
		
		//collapse every expanded branch
		$('ul.tree.dhtml span.grower.OPEN').addClass('CLOSE').removeClass('OPEN').parent().find('ul:first').hide();
		$('ul.tree.dhtml').show();
		
		//open the tree for the selected branch
			$('ul.tree.dhtml .selected').parents().each( function() {
				if ($(this).is('ul'))
					toggleBranch($(this).prev().prev(), true);
			});
			toggleBranch( $('ul.tree.dhtml .selected').prev(), true);
		
		//add a fonction on clicks on growers
		$('ul.tree.dhtml span.grower').click(function(){
			toggleBranch($(this));
		});
		
		//visa/öppna kontorsmaterial kategorin
		$('.kontorsmaterial-kategori span.grower').removeClass('CLOSE').addClass('OPEN');
		$('.kontorsmaterial-kategori').show();
		$('.kontorsmaterial-kategori').parent().parent().show();
		
		//mark this 'ul.tree' elements as already 'dynamized'
		$('ul.tree.dhtml').addClass('dynamized');
		
		
		// Öppnar branchar
		var fnOpenBranch = function()
		{
			closeBranch($(".selected"),1);
			closeBranch($(".selected-parent"),1);
			$(".kontorsmaterial-kategori .selected-parent").parent().css("list-style-image","url(img/lisco_menypil.gif)");
			$(".selected-parent").removeClass("selected-parent");
			openBranch($('ul.kontorsmaterial-kategori'),1);
			$(this).addClass("selected-parent"); toggleBranch($(this),1); 
			if($(this).parent().find(".kontorsmaterial-kategori").length == 0)
				$(this).parent().css("list-style-image","url(img/lisco_menypil2.gif)");
		}
		
		var fnRemoveHrefs = function()
		{
			if($(this).parent().find("ul").length>0)
			  $(this).removeAttr("href").css("cursor","pointer").bind("click",fnOpenBranch);
		}
		
		$('ul.tree.dhtml a').each(fnRemoveHrefs);
		
		
		// Remove hrefs and such
		/*
		$('ul.kontorsmaterial-kategori').parent().find("a").removeAttr("href");
		$('ul.tree.dhtml > li > ul > li > a').removeAttr("href").css("cursor","pointer").bind("click",function(){ 
			$(".selected").each(function(){
				closeBranch($(this),1);
				var p = $(this);
				while(!p.is("ul")) p = p.parent();
				var equality = !p.not( $("kontorsmaterial-kategori").parent().parent() ).length;
				if(!p.hasClass("kontorsmaterial-kategori") && !equality)
				{
					closeBranch(p,1);
					p.parent().find("a").removeClass("selected-parent");
				}
				$(this).removeClass("selected");
			})
			$(this).addClass("selected"); toggleBranch($(this),1); 
			$('.kontorsmaterial-kategori').show();
			$('.kontorsmaterial-kategori').parent().find("a").show();
		});
		*/
		$('ul.tree.dhtml').removeClass('dhtml');
		
		//
		/*
		$('.kontorsmaterial-kategori span.grower').removeClass('CLOSE').addClass('OPEN');
		$('.kontorsmaterial-kategori span.grower').parent().removeClass('CLOSE').addClass('OPEN');
		$('.kontorsmaterial-kategori').show();
		$('.kontorsmaterial-kategori').parent().parent().show();
		if(!($('.selected').parent().parent().hasClass("kontorsmaterial-kategori") || $('.selected').parent().find("ul:eq(0)").hasClass("kontorsmaterial-kategori"))){
		*/
			/*$('.selected').prepend("&#187;&nbsp;");*/
			//$('.selected').parent().parent().parent().find("a:eq(0)").addClass("selected-parent").css("color","black");
		//}
		
		$(".selected").parent().parent().parent().find("a:eq(0)").addClass("selected-parent");
      $(".selected-parent").parent().css("list-style-image","url(img/lisco_menypil2.gif)");
		
		//
	}
});

