var current,visited,lastStep,total;

function initTree(){
	total=1;
	db=new Array();
	fillTree();
	current=getCookie("currState");
	visited=getCookie("visiState");
	lastStep=getCookie("lastStep");
	initCookies();
}

function dbAdd(mother,display,URL,indent,top,keyStep,show) {
	db[total]=new Object;
	db[total].mother=mother;
	db[total].display=display;
	db[total].URL=URL;
	db[total].indent=indent;
	db[total].top=top;
	db[total].keyStep=keyStep;
	if(typeof show=="undefined")show=1;
	db[total].show=show;
	total++;
}

function toggle(n){
	if(n!=0){
		var newString="";
		var expanded=current.substring(n-1,n);
		newString+=current.substring(0,n-1);
		newString+=expanded ^ 1;
		newString+=current.substring(n,current.length);
		setCookie("currState",newString);
	}
}

function pad(n) {
	var result="";
	for (var i=1;i<=n;i++)result+="&nbsp;";
	return result;
}

function explode(){
	current="";
	initState="";
	for(var i=1;i<db.length;i++){ 
		initState+="1";
		current+="1";
	}
	setCookie("currState",initState);
	history.go(0);
}

function contract(){
	current="";
	initState="";
	for(var i=1;i<db.length;i++){ 
		initState+="0";
		current+="0";
	}
	setCookie("currState",initState);
	history.go(0);
}

function puzzleVisiState(n) {
	var newString="";
	newString+=visited.substring(0,n-1);
	newString+="1";
	newString+=visited.substring(n,visited.length);
	setCookie("visiState",newString);
	setCookie("lastStep",n);
}

function jumpTo(url,target,n){
	if(window.opener.closed){
		window.open(url)
	}else{
		window.opener.location.href=url;
	}
	puzzleVisiState(n);
	tree_close();
}

function tree_close(){
	window.close();
}

function resetTreeCookies(){
	initState="";
	visiState="";
	for(i=1;i<db.length;i++){initState+="0";visiState+="0"}
	current=initState;
	visited=visiState;
	setCookie("currState",initState);
	setCookie("visiState",visiState);
	setCookie("lastStep","");
}

function initCookies(){
	if(current==""||current.length!=(db.length-1)){
		initState="";
		visiState="";
		for(i=1;i<db.length;i++){ 
			initState+="0";
			visiState+="0";
		}
		current=initState;
		visited=visiState;
		setCookie("currState",initState);
		setCookie("visiState",visiState);
		setCookie("lastStep","");
	}
}

function writeTree(){
	initCookies();
	var textCollapse=tocMinimize;
	var textExpand=tocMaximize;
	var prevIndentDisplayed=0
	var showMyDaughter=0
	var outline="";

	for (var i=1;i<db.length;i++){
		if(!db[i].show) continue;
		var currIndent=db[i].indent	;
		var expanded=current.substring(i-1,i);
		var top=db[i].top;
		if(top=="")top="content";
		if((currIndent==0||currIndent<=prevIndentDisplayed||(showMyDaughter==1&&(currIndent-prevIndentDisplayed==1)))){
			outline+=pad(currIndent);

			keyStep="";
			if(db[i].keyStep)keyStep="_key";

			if(!(db[i].mother)){
				outline+="<img src='"+common+"/images/tree/blank.gif' width='16' height='16' border='0' /><img src='"+common+"/images/tree/document" + keyStep + ".gif' width='16' height='16' border='0' />";
			}else{ 
				if(current.substring(i-1,i)==1){
					outline+="<a href=\"javascript:history.go(0)\" onClick=\"toggle("+i+")\">";
					outline+="<img src='"+common+"/images/tree/minus.gif' width='16' height='16' border='0' /><img src='"+common+"/images/tree/open" + keyStep + ".gif' width='16' height='16' border='0' />";
					outline+="</a>";
				}else{
					outline+="<a href=\"javascript:history.go(0)\" onClick=\"toggle(" + i + ")\">";
					outline+="<img src='"+common+"/images/tree/plus.gif' width='16' height='16' border='0' /><img src='"+common+"/images/tree/closed" + keyStep + ".gif' width='16' height='16' border='0' />";
					outline+="</a>";
				}
			}
			outline+="&nbsp;";
		 
			if(db[i].URL==""||db[i].URL==null){
				outline+=" "+db[i].display;
			}else{
				outline+=" <a href='javascript:jumpTo(\""+db[i].URL+"\",\""+top+"\","+i+")'>"+db[i].display+"</a>";
			}

			//visited ??
			if (visited.substring(i-1,i)==1){
				outline+="<img src='"+common+"/images/tree/ok.gif' width='16' height='16' border='0' />";
			}

			// Bold if at level 0
			if(currIndent==0)outline="<b>"+outline+"</b>";

			outline+="<br/>\n";
			prevIndentDisplayed=currIndent;
			showMyDaughter=expanded;
			document.write(outline);
			outline="";
		}
	}
}
