//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 desc = new Array();
var desc_id = new Array();
var pics = new Array();
var timer;
var shaketimer;
var main_inner = "tree_table";
var main_outer = "tree";
var loaded = 0;
var loading = 0;
var debug = 0;
var moving = 0;

function init(){
	if(loaded == 1){ return; }
	coverui();
	loadimg();
    togglebranch("project", 0);
    togglebranch("photo", 0);
	togglebranch("article", 0);
    shaketimer = setTimeout("shake()", 15000);
	return;
}
function loadimg(){
	for(var i = 0; i < 7; i++){ //baaad, hardwired to 7
		pics[i] = new Image();
		pics[i].src = "./img/" + (i+1) + ".jpg";
	}

	timer = setInterval("checkimg()", 250);
	return;
}
function checkimg(force){ // makes sure a few imgs have been loaded before proceeding with initializations
	if(force != 1){ for(var i = 0; i < pics.length; i++){ if(!pics[i].complete){ return; } } }
	clearInterval(timer);
	document.getElementById('tree').style.display = "";
	reposition();
	document.getElementById('load').style.display = "none";
	loaded = 1;
    pics = null;
	return;
}
function togglebranch(id, bit){//shows or hides a branch by id depending on value of bit
	var piece = 0;
	if(bit == null){
		if(document.getElementById(id + 0).style.visibility == "visible"){ var visibility = "hidden"; }
		else{ var visibility = "visible"; }
	}else if(bit == 0){ var visibility = "hidden"; }
	else{ var visibility = "visible"; }
	while(document.getElementById(id + piece) != null){
		document.getElementById(id + piece).style.visibility = visibility;
		piece++;
	}
	return;
}
function coverui(){
	if (loading == 1){ return; }
	loading = 1;
	var window_width = parseInt((document.all)?document.body.offsetWidth:window.innerWidth);
	var window_height = parseInt((document.all)?document.body.offsetHeight:window.innerHeight);
	document.getElementById('load').style.width = window_width;
	document.getElementById('load').style.height = window_height;
	document.getElementById('load').style.backgroundColor = "white";
	document.getElementById('load').style.clip = "rect(0," + window_width + "," + window_height + ",0)";
	reposition('load_text','load',1,1,0);
	return;
}
function reposition(inner, outer, display, noresize, top){
	if(moving == 0){ moving = 1; }
	else{ return; }
	if(inner == null && outer == null){//is this even used still?
		inner = main_inner;
		outer = main_outer;
	}
	if(display == null){ display = 1; }
	if(noresize == null){ noresize = 0; }
	if(top == null){ top = 0; }
	if(noresize == 0){
		var inner_width = document.getElementById(inner).offsetWidth;
		var inner_height = document.getElementById(inner).offsetHeight;
		var window_width = parseInt((document.all)?document.body.offsetWidth:window.innerWidth);
		var window_height = parseInt((document.all)?document.body.offsetHeight:window.innerHeight);
		document.getElementById(outer).style.position = "absolute";
		document.getElementById(outer).style.width = inner_width + "px";	
		document.getElementById(outer).style.height = inner_height + "px";
		document.getElementById(outer).style.top = (window_height - inner_height)/2 + "px";
		document.getElementById(outer).style.left = (window_width - inner_width)/2 + "px";
	}else{
		document.getElementById(inner).style.position = "absolute"; //for some reason this has to be here in order to calculate the size of the content inside the inner object

		var inner_width = document.getElementById(inner).offsetWidth;
		var inner_height = document.getElementById(inner).offsetHeight;
		var window_width = parseInt((document.all)?document.body.offsetWidth:window.innerWidth);
		var window_height = parseInt((document.all)?document.body.offsetHeight:window.innerHeight); //IE returns document.body.offsetHeight as the size of the whole internal window, that is it does not subtract the height of the interface buttons, etc - stupid IE
		var outer_width = parseInt(document.getElementById(outer).style.width);
		var outer_height = parseInt(document.getElementById(outer).style.height);
		if(!outer_width){ outer_width = window_width; document.getElementById(outer).style.width = window_width + "px"; }
		if(!outer_height){ outer_height = window_height; document.getElementById(outer).style.height = window_height + "px"; }

		if(document.all){ window_height = outer_height; } //stupid fucking IE

		document.getElementById(outer).style.position = "absolute"; //for some reason this has to be here in order to calculate the size of the content inside the inner object
		document.getElementById(outer).style.top = (window_height > outer_height)?((window_height - outer_height)/2) + "px":0 + "px";
		document.getElementById(outer).style.left = (window_width - outer_width)/2 + "px";

		document.getElementById(inner).style.top = (window_height > inner_height)?((window_height - inner_height)/2) + "px":0 + "px";
		document.getElementById(inner).style.left = (window_width - inner_width)/2 + "px";
	}
	if(top == 1){
		document.getElementById(outer).style.top = 0 + "px";
		document.getElementById(inner).style.top = 0 + "px";
	}

	if(display == 1){
		document.getElementById(outer).style.display = "";
		document.getElementById(inner).style.display = "";
	}
	moving = 0;
	return;
}
function shake(){
    togglebranch("project", 1);
    togglebranch("photo", 1);
	togglebranch("article", 1);
    return;
}
