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

//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 () {
	//this shouldn't really be here but it's for the product view
	$('div#longDescription li:even').addClass('even');
	$('div#longDescription li:odd').addClass('odd');
	//nor should this
	$('div#newDetails').hide();
	$('div#whatsNew a').toggle(function() {
		$('div#newDetails').slideDown('slow');
		return false;
		},
		function() {
		$('div#newDetails').slideUp('slow');
		return false;
		});
	//nor should this!
	$('div#paypalNotice a.paypalMore').toggle(
	function() {
		$('div#paypalNotice p').show();
		}, 
	function() {
		$('div#paypalNotice p').hide();
		}
	);
	
	//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'> </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 li').addClass('CLOSE').removeClass('OPEN').find('ul:first').hide();
		$('ul.tree.dhtml').show();

		//add follow for childless links
		$('ul.tree.dhtml li a:only-child').addClass('follow');
		
		//open the tree for the selected branch
			$('ul.tree.dhtml .selected').parents().each( function() {
				if ($(this).is('ul'))
					toggleBranch($(this).prev(), true);
				if ($(this).is('li'))
					$(this).find('a').addClass('follow');
			});
			toggleBranch( $('ul.tree.dhtml .selected').prev(), true);
		
		//add a fonction on clicks on growers
		$('ul.tree.dhtml li a').click(function(){
			toggleBranch($(this).parent(), true);
			$('li.OPEN ul li a').addClass('follow');
			if($(this).hasClass('follow')) 
				return true;
			else
				return false
		});

		$('li.follow a').click(function() {
			return true;
		});
		//mark this 'ul.tree' elements as already 'dynamized'
		$('ul.tree.dhtml').addClass('dynamized');

		$('ul.tree.dhtml').removeClass('dhtml');
	}
});

$(document).ready(function() {
		$('ul.tree li ul li ul').hide();
		$('a.selected').parents('ul').show();
		$('a.selected ~ ul').show();
	});
