$(document).ready(function() {

	$("#content").addClass("js-expand-initial");
	collapseElements("#content .toc-include > ul.level1 > li");
	
	$("#content").addClass("js-expand").removeClass("js-expand-initial");	
	setTimeout("inactivateAll('#content')", 25);
	exSlide();
	expandContract("#selection","#content");
	exContext("#context");
	
	
	//$("#context").addClass("js-expand-initial");
	//collapseElements("#context .list01 > ul.level1 > li > ul.level1 > li");
	//$("#context").addClass("js-expand").removeClass("js-expand-initial");	
	$("#context").addClass("js-expand");
	setTimeout("inactiveAndExpandContext('#context')", 25);
	
	$(".utfchar").parent('li').addClass("li-utf"); 
	$(".authors").parent('ul').addClass("show3"); 
});

function inactiveAndExpandContext(idContext) 
{
	inactivateAll(idContext);
	selectedExpand(idContext);
		}
	
function collapseElements(el) {
	var el = $(el);
	if (el.length) {
		el.each(function() {
			if (!($(this).hasClass('active') || $(this).hasClass('inactive'))) {
				$(this).addClass('inactive');
			}
		});
	}
}
function inactivateAll(belowEl) {
	var top = $(belowEl);
	if (top.length) {
		var clickableSections = top.find(
		".nested-section:has(.nested-section), ul > li.nested-section:has(.level1)");		
		clickableSections.addClass('inactive');
		if (clickableSections.length) {
			var sectionSpans = clickableSections.find("> span");
			sectionSpans.addClass('clickable');
			
			sectionSpans.click(
				function(e) {
					e.stopPropagation();
					var p = $(this).parent();
					if (p.hasClass('active')) {
						p.removeClass('active').addClass('inactive');
					}
					else {
						p.removeClass('inactive').addClass('active');
					}
				}
			);
		}
		
		var deepestSections = top.find(".nested-section .nested-section:not(:has(.nested-section))");
		if (deepestSections.length) {
			deepestSections.find("span").addClass("move");
		}
		
		var level1Sections = top.find(".nested-section:has(> ul > li.level1)");
		level1Sections.find("> span").removeClass("move");
	} 
}

function expandContract(idContent, idContent2) {
	
	if(!$(idContent)) return true;
	var expand=$(idContent+" > ul > li:first");
	
	//expand all chapter sections
	expand.click(function(){
		$("#content .inactive").removeClass("inactive").addClass("active");
		});
	
	var contract=$(idContent+" > ul > li:last");
	
	//contract all chapter sections
	contract.click(function(){
	$("#content .active").removeClass("active").addClass("inactive");
		
});
}

function selectedExpand(idContent) {
    $(idContent+' .toplevel').removeClass('inactive').addClass('active');
	$(idContent+' .current_article').each(function(){  
	var selectedTag = $(this).find('> li.article-info');
	$(selectedTag).find('> a').css('font-weight','bold');
	$(selectedTag).find('> ul > li.article_navigation > a').css('font-weight','bold');
		var sectionParents = $(this).parents("li.nested-section");
		if (sectionParents.length) {
			sectionParents.each(function() {
				$(this).addClass('active').removeClass('inactive');
			});
		}
		$(this).parent("li.nested-section").removeClass('active');
	});
}

function exSlide(){
	if(!("#slide")) return true;
	var lis=$("#slide > ul >li");
	//alert(lis.length);
	var totalWidth=0;
	lis.each(
		function(i){
			var el=$(this);
			totalWidth+=67;//el.outerWidth(true);
		}
	);
	//set width for id slide
	$("#slide").css('width',totalWidth);
	//set event click for next
	var leftSilde=0;
	var positionEnd=-1*(totalWidth-8*67);
	//default set back opacity 0.5
	$("#back").css('opacity',0.5);
	//if positionEnd>0 then set opacity for button next 0.5
	if(positionEnd>=0) $("#next").css('opacity',0.5);
	$("#next").click(
		function(e){
			//set position left 
			if(leftSilde==positionEnd){
				return true;
			}
			//set opacity for button back =1
			$("#back").css('opacity',1);
			leftSilde=leftSilde-67;
			$("#slide").animate({left:leftSilde},500);
			//if leftSilde == positionEnd then set opacity for button next = 0.5
			if(leftSilde==positionEnd) $("#next").css('opacity',0.5);	
		}
	);
	
	$("#next").dblclick(
		function(e){
			//alert('dblclick');
		}
	);
	
	//set event click for back
	$("#back").click(
		function(e){
			//set position left 
			if(leftSilde>=0){
				return true;
			}	
			//set opacity for button next =1
			$("#next").css('opacity',1);
			leftSilde=leftSilde+67;
			$("#slide").animate({left:leftSilde},500);
			//if leftSilde==0 then set opacity for button back =0.500
			if(leftSilde==0) $("#back").css('opacity',0.5);
		}
	);
	
	$("#back").dblclick(
		function(e){
			//alert('dblclick');
		}
	);
	
}

function exContext(idContent){
		if(!$(idContent)) return true;	

var dts=$(idContent+" > dl > dt");
		var dds=$(idContent+" > dl > dd");
		dds.each(
			function(e){
				var el=$(this);
				//defalut display none
				el.css('display','none');
				//see dd children of dd
				var dtsChildren=el.find('dl > dt');
				var ddsChildren=el.find('dl > dd');
				//if has dt or dd children
				if(dtsChildren.length>0){
					//defalut display none for dt children
					ddsChildren.each(
						function(e){
							var elChildren=$(this);
							elChildren.css('display','none');
						}
					);
					dtsChildren.each(
						function(){
							var elChildren = $(this);
							//set active for the dd, if dt has active 
							if($(this).hasClass('active')){
								$(this).next().css('display','block');
							}
							elChildren.click(
								function(){
									//add class active for dt, if not yet
									if($(this).hasClass('active')){
										$(this).removeClass('active');
										$(this).addClass('inactive');
										$(this).next().css('display','none');
									}else{
										$(this).addClass('active');
										$(this).next().css('display','block');
									}	
								}
							);
						}
					);
				
				}
				//alert(dtsChildren.length);
			}
		);
		dts.each(
			function(i){
				var el = $(this);
				//set active for the dd, if dt has active 
				if($(this).hasClass('active')){
					$(this).next().css('display','block');
				}
				el.click(
					function(e){
						//add class active for dt, if not yet
						if($(this).hasClass('active')){
							$(this).removeClass('active');
							$(this).addClass('inactive');
							$(this).next().css('display','none');
						}else{
							$(this).addClass('active');
							$(this).next().css('display','block');
						}	
					}
				);
			}
		);	
		
}