var initLoading = true;
var active = 1;
var current = "";
var live = 0;

function makeIconsActive() {
	if (active == 0) return;
	//debug("makeIconActive");

	document.getElementById("home").src = "images/dock/home-0.png";
	document.getElementById("nasa").src = "images/dock/nasa-0.png";
	document.getElementById("cern").src = "images/dock/cern-0.png";
	document.getElementById("fermi").src = "images/dock/fermi-0.png";
	document.getElementById("maya").src = "images/dock/maya-0.png";
}

function makeIconsInactive(full) {
	if (active == 0) return;
	//debug("makeIconInactive");
	clearStatus();

	document.getElementById("home").src = "images/dock/home-x.png";
	document.getElementById("nasa").src = "images/dock/nasa-x.png";
	document.getElementById("cern").src = "images/dock/cern-x.png";
	document.getElementById("fermi").src = "images/dock/fermi-x.png";
	document.getElementById("maya").src = "images/dock/maya-x.png";

	if (current != "" && full == 0) document.getElementById(current).src = "images/dock/"+current+"-0.png";
}

function loadIcons() {
//	top.frames["hiddenFrame"].location = "load-icons.php";

//	setTimeout("load('home');", 2000);
	setTimeout("loadProblem()", 12000);
}

function loadProblem() {
	if (initLoading == false) return;

	document.getElementById('content').innerHTML = "There seems to be a problem loading the page... hitting <a href=\"www.zmunk.com\">refresh</a> may correct the problem.";
}

function loadDone() {
	initLoading = false;
	load('home');
}

function getPage(iconId) {
	top.frames["hiddenFrame"].location = "get-page.php?page=" + iconId;
	loadingStatus();
}

function load(iconId) {
	//debug("load("+iconId+")");
	live = 1; active = 0;

	current = iconId;

	lift(iconId, 1);

	getPage(iconId);
}

function displayContent(sText, pageId) {
	debug("displayContent("+sText+", "+pageId+")");//#########################################

	document.getElementById("content").innerHTML = sText;
	clearStatus();

	if (pageId == "about") current = "";
	else current = pageId;

	setTimeout("halt();", 1500);
}

function halt() {
	//debug("halt");
	live = 0; active = 1;

	makeIconsInactive(0);
}

function lift(iconId, step) {
	//debug("lift("+iconId+", "+step+")");
	document.getElementById(iconId).src = "images/dock/"+iconId+"-"+step+".png";
	if (step == 5) setTimeout("drop('"+iconId+"', 5)", 50);
	else setTimeout("lift('"+iconId+"', "+(step+1)+")", 50);
}

function drop(iconId, step) {
	debug("drop("+iconId+", "+step+")"); //#########################################

	document.getElementById(iconId).src = "images/dock/"+iconId+"-"+step+".png";
	if (step == 0) {
		if (live == 0) return;
		setTimeout("lift('"+iconId+"', 1)", 50);
	}
	else setTimeout("drop('"+iconId+"', "+(step-1)+")", 30);
}

function highlightAbout(step) {

	if (step == 1) document.getElementById('about').src = "images/template/images/bot_1_2a.gif";
	else /*if (step == 0) */ document.getElementById('about').src = "images/template/images/bot_1_2.gif";
}

function highlight(iconId) {
	if (active == 0) return;
	//debug("highlight("+iconId+")");

	makeIconsInactive(1);
	setStatus(summary(iconId));
	document.getElementById(iconId).src = "images/dock/"+iconId+"-0.png";
}

function summary(iconId) {
	//debug("summary("+iconId+")");
	var str;

	if (iconId == "nasa")		str="NASA's Microgravity University";
	else if (iconId == "maya")	str="3D Graphics and Animation";
	else if (iconId == "fermi")	str="The Fermi National Accelerator Laboratory";
	else if (iconId == "cern")	str="US CMS at CERN in Geneve, Switzerland";
	else /*if (iconId == "home")*/	str="Overview of My Projects";

	return str;
}

function clearStatus() { setStatus("&nbsp;");}
function loadingStatus() { setStatus("Loading...");}
function setStatus(sText) {
	document.getElementById("status").innerHTML = sText;
}

function clearDebug() {
	document.getElementById("null").innerHTML = '<input type="hidden" id="debug" value=""/>';
}

function debug(sText) {
/*	document.getElementById("debug").innerHTML += "<br/>\n" + sText;

	setTimeout("clearDebug();", 3000);
	//top.frames["hiddenFrame"].document.getElementById("dev_null").innerHTML +=  "<br/>\n" + sText;
	*/
}


function flash(num) {
	var letter = document.getElementById("let" + num);

	setTimeout("fade("+num+", 'ffff', 0)", 5);
}

function fade(num, color, alt) {
	var delay_s = 60;
	var delay_l = 1500;

	var letter = document.getElementById("let" + num);

	// fade IN
	if (alt == 0) {
		letter.style.color = "#ff"+color;
		// fade in
		switch (color) {
			case "4800":setTimeout("fade("+num+", '4800',1)", delay_l);
			break;
			case "5708":setTimeout("fade("+num+", '4800',0)", delay_s);
			break;
			case "6db0":setTimeout("fade("+num+", '5708',0)", delay_s);
			break;
			case "8fc4":setTimeout("fade("+num+", '6db0',0)", delay_s);
			break;
			case "afd6":setTimeout("fade("+num+", '8fc4',0)", delay_s);
			break;
			case "cfe8":setTimeout("fade("+num+", 'afd6',0)", delay_s);
			break;
			case "ffff":setTimeout("fade("+num+", 'cfe8', 0)", delay_s);
			break;
		}
	}
	// sets the current color
	// fade OUT
	if (alt == 1)  {
		letter.style.color = "#ff"+color;
		switch (color) {
			case "4800":setTimeout("fade("+num+", '5708',1)", delay_s);
			break;
			case "5708":setTimeout("fade("+num+", '6db0',1)", delay_s);
			break;
			case "6db0":setTimeout("fade("+num+", '8fc4',1)", delay_s);
			break;
			case "8fc4":setTimeout("fade("+num+", 'afd6',1)", delay_s);
			break;
			case "afd6":setTimeout("fade("+num+", 'cfe8',1)", delay_s);
			break;
			case "cfe8":setTimeout("fade("+num+", 'ffff',1)", delay_s);
			break;
			case "ffff":
				//setTimeout("fade("+num+", 'ffff',0)", 1000+ Math.random() * 1000);
			break;
		}
	}
}
