//This file contains JavaScript functionality related to index page functionality of http://www.projecttree.com
//This code maybe reused and modified so long as this note remains here
//Copyright 2003-2004 Mike Radin

var letters = ["", "a", "b", "c", "d"]; // up to 4 controls, but first will accomodate section only with a resize control
var toc_built = 0;
var resized = 0;
//note that JavaScript does not read CSS style information
//but only style information included in line with the element

function initpage(){
	buildtoc();
	loadstylepref(); //IE is a steaming heap of shit that cannot switch stylesheets anywhere in the onload sequence called from an attribute of an html element in the page, not even using setTimeout, it also does not set the disabled attribute properly for the currently active css file, they're all set to true when the page loads; this refers to the use of "alternate stylesheet" link tags
    if(parsequery() != 0 && getqueryparam(parsequery(), "toc") == 1){ buildtoc(1); } // add ability to build a reverse toc
	return;
}
function collapsesection(e, id, force){
	if(e){
		var exp = new RegExp("\w*([0123456789]{1,})([abcd]{0,1})$");//note that the list of letters here has to match the list in the letters array above
		var result = exp.exec(e.id);
	    if(result){ el = document.getElementById("section_content" + result[1]); id = result[1]; }
		else{ el = document.getElementById("toc_content"); }
	}else if(id != null){
		el = document.getElementById("section_content" + id);
		e =  document.getElementById("section_control" + id);
		if(e == null){//for the times when collapsesection is called from links in the table of contents
			for(var j = 0; j < letters.length; j++){
				control = document.getElementById("section_control" + id + letters[j]);
				if(control == null){ continue; }
				if(control.style.display == "none"){ continue; }
				exp = new RegExp("&[l|g]t;");
				var result = exp.exec(control.innerHTML);
				if(result){
					e = control;
					break;
				}
			}
		}
	}else{ return; }
	if(force == null){ force = 0; }
	if(el.style.display == ""){
		el.style.display = "none";
		e.innerHTML = "&gt;";
		if(document.getElementById("photo" + id)){ document.getElementById("photo" + id).style.display = "none"; }
	}else{
		el.style.display = "";
		e.innerHTML = "&lt;";
	}
	if(force == 1){
		el.style.display = "";
		e.innerHTML = "&lt;";
	}
	return;
}
function resizesectioncontrols(newsize){
	var i = 0;
	while(true){
		var control_num = 0;
		var control = null;
		var title_e = document.getElementById("section_title" + i);
		if(title_e == null){
			title_e = document.getElementById("toc_title");
			control = document.getElementById("toc_control");
			var last = 1;
		}
		if(title_e == null){ break; }

		if(control == null){
			for(var j = 0; j < letters.length; j++){
				control = document.getElementById("section_control" + i + letters[j]);
				if(control == null){ continue; }
				if(control.style.display != "none"){//do not resize hidden controls
					control.style.paddingLeft = "0px"; //this way we dont end up with 0px padding when it is set with ((controlheight - controlwidth)/2)
					control.style.paddingRight = "0px"; //read above
					if(newsize){
						control.style.fontSize = newsize + "px";
						control.style.paddingLeft = "0px";
						control.style.paddingRight = "0px";
					}
					var controlheight = control.offsetHeight;
					var controlwidth = control.offsetWidth;
					control.style.paddingLeft = ((controlheight - controlwidth)/2) + "px";
					control.style.paddingRight = ((controlheight - controlwidth)/2) + "px";
					//var controlwidth = control.offsetWidth; //why?
					control_num++;
				}
			}
		}else{//resize toc header
			if(control.style.display != "none"){//do not resize hidden controls
				control.style.paddingLeft = "0px"; //this way we dont end up with 0px padding when it is set with ((controlheight - controlwidth)/2)
				control.style.paddingRight = "0px"; //read above
				if(newsize){
					control.style.fontSize = newsize + "px";
					control.style.paddingLeft = "0px";
					control.style.paddingRight = "0px";
				}
				var controlheight = control.offsetHeight;
				var controlwidth = control.offsetWidth;
				control.style.paddingLeft = ((controlheight - controlwidth)/2) + "px";
				control.style.paddingRight = ((controlheight - controlwidth)/2) + "px";
				//var controlwidth = control.offsetWidth; //why?
				control_num++;
			}
		}

		if(title_e.style.display != "none"){//do not resize hidden sections
			if(newsize){
				title_e.style.fontSize = newsize + "px";
			}
			title_e.style.paddingRight = "0px";
			if(parseInt(title_e.offsetWidth) < 600){
				title_e.style.paddingRight = (600 - 10 - title_e.offsetWidth - (controlheight + 2)*control_num) + "px";
			}
		}

		if(last){ break; }
		else{ i++; }
	}
	resized = 1;
	return;
}
function buildtoc(force, reverse){
	var toc = document.getElementById("toc_content");
	var toc_head = document.getElementById("toc_head");
	if(!toc){ return; }
	if(toc.style.display == "none" && !force){ return; }
	if(toc_head.style.display != "none" && toc.style.display != ""){ collapsesection(document.getElementById("toc_control")); }
	if(toc_built){ return; }

	var tocHTML = "<ul>" //because assigning this straight to toc.innerHTML adds the UL end tag automatically for some reason
	for(var i = 0; el = document.getElementById("section_title" + (i++));){
		var tocelement = "<li><a href=\"#section" + (i - 1) + "\" onclick=\"collapsesection(null," + (i - 1) + ",1)\">" + el.innerHTML + "<\/a><\/li>";
        if(reverse){ tocHTML = tocelement + tocHTML; }
        else{ tocHTML += tocelement; }
	}
	tocHTML +="<\/ul>";
	toc.innerHTML = tocHTML;
	document.getElementById("toc_head").style.display = "";
	document.getElementById("toc_title").style.display = "";
	document.getElementById("toc_control").style.display = "";
	document.getElementById("toc_content").style.display = "";
	toc_built = 1;
	if(resized){
		title_e = document.getElementById("toc_title");
		control = document.getElementById("toc_control");
		var controlheight = control.offsetHeight;
		var controlwidth = control.offsetWidth;
		control.style.paddingLeft = ((controlheight - controlwidth)/2) + "px";
		control.style.paddingRight = ((controlheight - controlwidth)/2) + "px";
		controlwidth = control.offsetWidth;
		title_e.style.paddingRight = (600 - 10 - title_e.offsetWidth - (controlheight + 2)) + "px";
	}
	return;
}
function clearonce(el){
	if(el.value == "Name"){ el.value = ""; }
	if(el.value == "Email"){ el.value = ""; }
	if(el.value == "Subject"){ el.value = ""; }
	if(el.value == "Message"){ el.value = ""; }
	if(el.value == "Who are you?"){ el.value = ""; }
	if(el.value == "Why should I believe you?"){ el.value = ""; }
    if(el.value == "Ransom demand"){ el.value = ""; }
	return;
}
