// phMain.js
//
// Implements the header, menu, and footer around my webpages.
// Relies on phMenuDat.js, which is built by buildMenu.cgi.
// Uses the following cookies
//
// 		session: menu_state=list_of_ids_that_are_forced_open
//      session: outline=closed
//
// certain limited functionality is unavailable when
// pages are viewed as file:// urls, specifically cookies,
// which drive menu persistence, etc.

var debug_level = 0;
var tri_right = "common/Images/tri_right.gif";
var tri_down = "common/Images/tri_down.gif";
var tri_empty = "common/Images/tri_empty.gif";
var mi = [];	// the menu from phMenuDat.js


var menu_state = "";
	// cookie storing the state of open folders in menu
var menu_prev;
var menu_next;
var menu_up;
	// vars storing references to rows objects 
	// that are next, up, and previous 
var outer_level_items = [];
	// the outer most items
	

function populateMenu()
{
	var parents = [];
	parents[0] = null;
	menu_state = getCookie("menu_state");
	dbg(2,"menu_state="+menu_state);
	
	if (!menu_state) menu_state = "";
	var menu_td = document.getElementById("zmtd");
	var table = document.createElement("table");
	table.id = "menu_table";
	var selected_item = "";

	// table.style.border = "2px solid yellow";
	var prev_real;
	var take_next_real = false;
		
	// loop thru phMenuDat.js items, building Javascript menu
	// state for containers: 0=closed, 1=open, 2=opened by user
				
	for (var i=0; i<mi.length; i++)
	{
		var parts = mi[i].split(",");
		var menu_level = parts[0];
		var menu_isreal = parts[1];
		var menu_id = parts[2];
		var menu_parent = parts[3];
		var menu_text = parts[4];
		var menu_numkids = parts[5];
		var next_real_is_next = false;
		
		// create the table row, which is the main info item
		
		var tr = table.insertRow(-1);
		tr.state = 0;		
		if (menu_state.search(menu_id+",")>=0)
			tr.state = 2;
		tr.menu_id = menu_id;
		tr.id = "menu_"+menu_id;
		parents[menu_level] = tr;
		tr.menu_parent = parents[menu_level-1];
		tr.menu_children = [];
		if (menu_level > 1)
			parents[menu_level-1].menu_children.push(tr);
		else
			outer_level_items.push(menu_id);
				
		// create the triangle td, which may contain an
		// invisible gif for spacing
		
		var td = tr.insertCell(-1);
		td.style.overflow = "hidden";
		td.nowrap = "true";
		td.style.paddingLeft = "" + ((menu_level-1) * 9) + "px";
		if (menu_level <= 1)
		{
			td.style.paddingTop = "10px";
			td.className = "menu_item_home";
		}
		else
		{
			tr.style.display = "none";
			td.className = "menu_item";
		}

		var tri = tri_empty;
		if (menu_numkids > 0)
		{
			if (tr.state > 0)
				tri = tri_down;
			else
				tri = tri_right;
		}
		
		var img = document.createElement('img');
		img.className = 'menu_tri';
		img.src = root_dir+tri;
		tr.triangle = img;
		var item = img;
		if (menu_numkids > 0)
		{
			item = document.createElement('a')
			item.href = 'javascript:toggleItem("' + tr.id + '",-1)';
			item.appendChild(img);
		}
		td.appendChild(item);

		// if it's the selected item
		// highlight it and set the up, next, and prev ids
				
		var menu_text_class = td.className;
		if (menu_id == docroot)
		{
			tr.is_selected_item = true;
			menu_text_class = "menu_selected";
			selected_item = menu_id;

			// skip one level of empty containers to get up button

			if (tr.menu_parent)
			{
				menu_up = tr.menu_parent;
				if (tr.menu_parent.is_empty_href)
					menu_up = tr.menu_parent.menu_parent;
			}
	
			// set the previous and be on lookout for next
					
			menu_prev = prev_real;
			next_real_is_next = true;
		}
		
		// create the menu_text portion
		
		var a2 = document.createElement('a');
		a2.id = "href_" + menu_id;
		a2.className = menu_text_class;
		a2.innerHTML = menu_text;
		a2.href = root_dir + "html/" + menu_id+ ".html";
		if (menu_id == "index")
			a2.href = root_dir + menu_id + ".html";
		
		if (menu_isreal == "") 
		{
			// a2 = document.createElement("span");
			a2.href = 'javascript:toggleItem("' + tr.id + '",-1)';
			tr.is_empty_href = true;
			a2.is_empty_href = true;
			a2.style.color = "black";
		}
		else if (take_next_real)
		{
			menu_next = tr;
			take_next_real = false;
		}
		else
		{
			prev_real = tr;
		}
		
		// cuz of logic had to use extra var here
		
		if (next_real_is_next)
			take_next_real = true;
		td.appendChild(a2);
	}
	
	// done, open up the self and parents
	
	menu_td.appendChild(table);	
	menuOpenSelfAndParentsOf(selected_item);
	
}	// populateMenu



function menuOpenSelfAndParentsOf(what)
	// open up the given menu item and it's parents
	// one time call after menu is read
{
	var item = document.getElementById("menu_"+what);
	if (item.state == 0) item.state = 1;
	toggleItem(item.id,item.state);
	
	var parent = item.menu_parent;
	while (parent != null)
	{
		if (parent.state == 0) parent.state = 1;
		toggleItem(parent.id,parent.state);
		parent=parent.menu_parent;
	}
		
	// since it's a one time call, also used to open
	// level 2 items from cookies
	
	for (var i=0; i<mi.length; i++)
	{
		var parts = mi[i].split(",");
		var menu_level = parts[0];
		var menu_id = parts[2];
		var item2 = document.getElementById("menu_"+menu_id);
		if (item2.state == 2)
		{
			toggleItem(item2.id,item2.state);
		}
	}
}


function collapseOutline()
{
	var selected_item = "";
	setCookie("menu_state","");
	var table = document.getElementById("menu_table");
	var rows = table.getElementsByTagName("tr");
	for (var i=0; (i<rows.length) && (tr=rows[i]); i++)
	{
		tr.state = 0;
		if (tr.menu_children.length > 0)
			tr.triangle.src = root_dir+tri_right;
		if (tr.menu_parent)
			tr.style.display = "none";
		if (tr.is_selected_item == true)
			selected_item = tr.menu_id;
	}
}


function toggleItem(what,state,recursed)
	// Acts as "toggleItem()' if state is -1,
	// but more generally used as "showChildren()" to
	// recurse through children show or hiding them
{
	dbg(2,"toggleItem("+what+","+state+")");
	var item = document.getElementById(what);
	if (item.menu_children.length > 0)
	{
		// keep track of whether we need to modify
		// the menu_state cookie (if a state==2 item changes)
		
		var set_menu_state = false;

		// if !state, then we were called as toggleItem.
		// so we set our own state, and loop through children
		
		if (state == -1)
		{
			if (item.state == 0)
			{
				state = 1;
				item.state = 2;
				set_menu_state = true;
			}
			else
			{
				if (item.state == 2)
					set_menu_state = true;
				state = 0;
				item.state = 0;
			}
		}
		
		// otherwise, note if we are closing a level 2
		// or get the state from the item
		
		else if (state==0) 
		{
			if (item.state == 2)
				set_menu_state = true;
			// item.state = 0;
		}
		else
		{
			state = item.state;
		}

		// change our triangle and set children's display style
		
		var display_style = "";
		if (state > 0) 
		{
			item.triangle.src = root_dir+tri_down;
		}
		else
		{
			item.triangle.src = root_dir+tri_right;
			display_style = "none";
		}
		
		// loop through children, possibly recursing
			
		for (var i=0; i<item.menu_children.length; i++)
		{
			var child = item.menu_children[i];
			child.style.display = display_style;
			if ((child.state == 2) || 
			    ((state == 0) && (child.state != 0)) ) 
				toggleItem(child.id,state,true);
		}
		
		// if a level 2 item changed, modify the cookie variable
		// and if if done with a call, actually set the cookie

		if (set_menu_state)
		{
			menu_state = menu_state.replace(item.menu_id+",","");		
			if (item.state != 0)
				menu_state += item.menu_id + ",";
		}
	}
	if (!recursed)
		setCookie("menu_state",menu_state);
		
}	// toggleItem



function addButton(td,text,href,element_id)
{
	var anch = document.createElement("a");
	anch.innerHTML = text;
	anch.href = href;
	anch.id = element_id;
	td.appendChild(anch);
}


function addToButton(td,text,tr,element_id)
{
	var item = document.getElementById("href_"+tr.menu_id);
	addButton(td,text,item.href,element_id);
}


function addDivTo(tdid,divid)
{
	var td = document.getElementById(tdid);
	var div = document.createElement("div");
	div.id = divid;
	//div.style.border = "2px solid red";
	td.appendChild(div);
	return div;
}


function showHitCounts()
{
	alert(
		"uniqueHits = " + uniqueHits + "    \n" +
		"refreshHits = " + refreshHits + "    \n" +
		"robotHits = " + robotHits + "    \n" +
		"newUniqueHits = " + newUniqueHits + "    \n" +
		"newRefreshHits = " + newRefreshHits + "    \n" +
		"newRobotHits = " + newRobotHits + "    \n" +
		"newVisitors = " + newVisitors + "    \n" 
	);
	
}


function setHeaders()
{
	// In the table metaphor I am using, absolute positioning
	// is relative to the top of the document.  So we create
	// some divs from within which we can position absolutely

	var logo_div = addDivTo("zltd","logo_div");
	var title_div = addDivTo("zttd","title_div");
	var title_left_div = addDivTo("title_div","title_left_div");
	var title_middle_div = addDivTo("title_div","title_middle_div");
	var title_right_div = addDivTo("title_div","title_right_div");
	var left_footer_div = addDivTo("zlftd","left_footer_div");
	var footer_div = addDivTo("zftd","footer_div");
	
	// LOGO DIV
		
	if (menu_prev)
		addToButton(logo_div,"Prev",menu_prev,"logo_prev_button");
	if (menu_up)
		addToButton(logo_div,"Up",menu_up,"logo_up_button");
	if (menu_next)
		addToButton(logo_div,"Next",menu_next,"logo_next_button");

	addButton(logo_div,"Close Outline","javascript:closeOutline()","logo_close_button");
	// addButton(logo_div,"Collapse Outline","javascript:collapseOutline()","logo_collapse_button");

	// TITLE DIV

	var last_id = "";
	var path = [];
	var tr=document.getElementById("menu_"+docroot);
	while (tr)
	{
		var last_id = tr.menu_id;
		dbg(2,"adding menu_id="+last_id);
		var href = document.getElementById("href_"+last_id);
		path.push(href);
		tr = tr.menu_parent
	}
	// add home if needed
	if (last_id != "index")
	{
		path.push(document.getElementById("href_index"));
	}
	var html = "<span class='title_path'>";
	for (var i=path.length-1; i>=0; i--)
	{
		var endanchor = "";
		if ((!path[i].is_empty_href) && (i>0))
		{
			html += "<a href='"+path[i].href+"'>";
			endanchor = "</a>";
		}
		html += path[i].innerHTML + endanchor;
		if (i>0)
			html += "&nbsp;&nbsp;:&nbsp;&nbsp;";
				
	}
	title_middle_div.innerHTML = 
		"<b>"+document.title+"</b><br>" + html + "</span>";
	
	addButton(title_left_div,"Open Outline","javascript:openOutline()","open_outline_button");
	document.getElementById("open_outline_button").style.display = "none";
	
	// use window.userHits to see if it's defined
	
	if (window.uniqueHits != null)
	{
		title_right_div.innerHTML = 
			"<a href='javascript:showHitCounts();'>" +
			 "Hits: "+uniqueHits+"("+newUniqueHits+")" +
			 "</a>";
	}

	// FOOTERS
	
	addButton(left_footer_div,"Collapse Outline","javascript:collapseOutline()","footer_collapse_button");

	if (menu_prev)
		addToButton(footer_div,"Prev",menu_prev,"footer_prev_button");
	if (menu_up)
		addToButton(footer_div,"Up",menu_up,"footer_up_button");
	if (menu_next)
		addToButton(footer_div,"Next",menu_next,"footer_next_button");

	var table = document.createElement("table");
	table.id = "footer_links_table";
	table.style.align = 'center';
	var row = table.insertRow(-1);
	for (var i=0; i<outer_level_items.length; i++)
	{
		var id = outer_level_items[i];
		var menu = document.getElementById("href_"+id);
		var td = row.insertCell(-1);
		var a = document.createElement("a");
		// a.className = "footer_link";
		a.innerHTML = menu.innerHTML;
		a.href = menu.href;
		td.appendChild(a);
	}
	footer_div.appendChild(table);
	
	// BODY TITLE
	
	var found = docroot.match(/(\d\d\d\d-\d\d-\d\d)(.*)/);
	var body_title = document.getElementById("body_title");
	if (body_title && found)
	{
		var span = document.createElement("span");
		span.className = "small";
		var html = "<br>"+prettyDate(found[1]);
		if (found[2] != "") html += "("+found[2]+")";
		if (debug_level > 0) html = "<br>id="+docroot;
		span.innerHTML = html;
		body_title.appendChild(span);
	}
}	// setHeaders


function closeOutline()
{
	document.getElementById("zltd").style.display = "none";
	document.getElementById("zmtd").style.display = "none";
	document.getElementById("zlftd").style.display = "none";
	document.getElementById("open_outline_button").style.display = "";
	setCookie("outline","closed");
}

function openOutline()
{
	document.getElementById("zltd").style.display = "";
	document.getElementById("zmtd").style.display = "";
	document.getElementById("zlftd").style.display = "";
	document.getElementById("open_outline_button").style.display = "none";
	setCookie("outline","");
}

function startDocument()
{
	var found = query.match(/slideShow\((.*)\)/);
	if (found)
	{
		slideShow(found[1]);
	}
	else
	{
		populateMenu();
		setHeaders();
		if (getCookie("outline") == "closed") 
			closeOutline();
		setCookie('last_page',docroot,30);
	}
}

function endDocument()
{
}



