//
// Menu navigation
//

var ns4;
var ie4;
var ns6;

NUM_MAIN_MENUS = 7;
NUM_SUB_MENUS = 7;

var currentMenu = -1;

function isBrowser(b,v)
{
	/*
	** Check if the current browser is compatible
	**  b  browser name
	**  v  version number (if 0 don't check version)
	** returns true if browser equals and version equals or greater
	*/
	browserOk = false;
	versionOk = false;
	
	browserOk = (navigator.appName.indexOf(b) != -1);
	if (v == 0) versionOk = true;
	else  versionOk = (v <= parseInt(navigator.appVersion));
	return browserOk && versionOk;
}


// Test for which browser
if (isBrowser('Netscape', 4)) ns4 = true;
if (document.all) ie4 = true;
if ((document.getElementById) && (!document.all)) ns6 = true;
if (ns6 == true) ns4 = false;

// Number of seconds since last menu clear:
var menusCleared = 0;
// Number of seconds of idle time before clearing menus:
var MENU_START_TIME = 4;

function checkClearMenus()
{
	if (menusCleared > 0) {
		menusCleared--;
		if (menusCleared == 0) {
			clearAll(currentMenu);
		}
	}
}

function InitMenus(path, curMenu)
{
	// Save this for clearing purposes:
	currentMenu = curMenu;
}

var windowTimerFunc = window.setInterval("checkClearMenus()", 1000);

function showLayer(which)
{
	if (ie4) {
		document.all[which].style.visibility = "visible";
	}
	if (ns6) {
		document.getElementById(which).style.visibility = "visible";
	}
	if (ns4) {
		document.layers[which].visibility = "show";
	}
}

function hideLayer(which)
{
	if (ie4) {
		document.all[which].style.visibility = "hidden";
	}
	if (ns6) {
		document.getElementById(which).style.visibility = "hidden";
	}
	if (ns4) {
		document.layers[which].visibility = "hide";
	}
}

function hideAllSubMenus()
{
	for (i = 0;  i < NUM_SUB_MENUS;  i++) {
		hideLayer("submenu_" + i);
	}
}

function clearAll(menuNum)
{
	hideAllSubMenus();
	menusCleared = 0;
}

function clearAllWithSubmenu(menuNum)
{
	hideAllSubMenus();
	menusCleared = 0;
}

function showSubMenu(menuNum)
{
	// First, unhilite the other menus and hide the sub-menus:
	clearAllWithSubmenu(menuNum);
	menusCleared = MENU_START_TIME;
	
	// Show the selected sub-menu:
	showLayer("submenu_" + menuNum);
}

function preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


// AJAX functions:

var xmlHttp;
var picIndex = 0;
var numPics = 0;

function createXMLHttpRequest()
{
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		xmlHttp = new XMLHttpRequest();
	}
}

function startRequestParseXML(npics, incr)
{
	numPics = npics;
	newIndex = picIndex + incr;
	if (newIndex < 0 || newIndex >= numPics - 1) {
		return;
	}
	picIndex = newIndex;
	
	// Wait icon:
	/*
	if (incr == 1) {
		SetImage("img2", "ajax-loader.gif");
		SetDivContent("caption2", "");
	} else {
		SetImage("img1", "ajax-loader.gif");
		SetDivContent("caption1", "");
	}
	*/
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChangeParseXML;
	theScript = "gethighlights.php?idx=" + picIndex;
	//theScript = "gethighlights-test.xml";
	xmlHttp.open("GET", theScript, true);
	xmlHttp.send(null);
}

function handleStateChangeParseXML()
{
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			getPicInfo();
		}
	}
}

function getPicInfo()
{
	var xmlDoc = xmlHttp.responseXML;
	var picNodeName = "info";
	var picNodes = xmlDoc.getElementsByTagName(picNodeName);
	
	var picNode = picNodes[0];
	var titleNode1 = picNode.getElementsByTagName("title")[0].firstChild.nodeValue;
	var imageNode1 = picNode.getElementsByTagName("picture")[0].firstChild.nodeValue;
	var captionNode1 = picNode.getElementsByTagName("caption")[0].firstChild.nodeValue;
	var urlNode1 = picNode.getElementsByTagName("url")[0].firstChild.nodeValue;
	var pdfNode1 = picNode.getElementsByTagName("pdf")[0].firstChild.nodeValue;

	var title1 = titleNode1;
	var picture1 = "";
	if ((urlNode1 != null) && (urlNode1 != "") && (urlNode1 != "#")) {
		title1 = "<a href='http://" + urlNode1 + "' class='titlelink'>" + titleNode1 + "</a>";
		picture1 = "<a href='http://" + urlNode1 + "' class='titlelink'><img src='http://" + imageNode1 + "' border='0' class='focus' name='pic1'></a>";
	} else if ((pdfNode1 != null) && (pdfNode1 != "")) {
		title1 = "<a href='http://" + pdfNode1 + "' class='titlelink'>" + titleNode1 + "</a>";
		picture1 = "<a href='http://" + pdfNode1 + "' class='titlelink'><img src='http://" + imageNode1 + "' border='0' class='focus' name='pic1'></a>";
	} else {
		picture1 = "<img src='http://" + imageNode1 + "' border='0' class='focus' name='pic1'>";
	}
	SetDivContent("picture1", picture1);
	SetDivContent("title1", title1);
	SetDivContent("summary1", captionNode1);

	var picNode = picNodes[1];
	var titleNode2 = picNode.getElementsByTagName("title")[0].firstChild.nodeValue;
	var imageNode2 = picNode.getElementsByTagName("picture")[0].firstChild.nodeValue;
	var captionNode2 = picNode.getElementsByTagName("caption")[0].firstChild.nodeValue;
	var urlNode2 = picNode.getElementsByTagName("url")[0].firstChild.nodeValue;
	var pdfNode2 = picNode.getElementsByTagName("pdf")[0].firstChild.nodeValue;

	var title2 = titleNode2;
	var picture2 = "";
	if ((urlNode2 != null) && (urlNode2 != "") && (urlNode2 != "#")) {
		title2 = "<a href='http://" + urlNode2 + "' class='titlelink'>" + titleNode2 + "</a>";
		picture2 = "<a href='http://" + urlNode2 + "' class='titlelink'><img src='http://" + imageNode2 + "' border='0' class='focus' name='pic2'></a>";
	} else if ((pdfNode2 != null) && (pdfNode2 != "")) {
		title2 = "<a href='http://" + pdfNode2 + "' class='titlelink'>" + titleNode2 + "</a>";
		picture2 = "<a href='http://" + pdfNode2 + "' class='titlelink'><img src='http://" + imageNode2 + "' border='0' class='focus' name='pic2'></a>";
	} else {
		picture2 = "<img src='http://" + imageNode2 + "' border='0' class='focus' name='pic2'>";
	}
	SetDivContent("picture2", picture2);
	SetDivContent("title2", title2);
	SetDivContent("summary2", captionNode2);
	
	// Set the little you-are-here dots:
	for (i = 0;  i < numPics - 1;  i++) {
		focusname = "focus" + i;
		if (i == picIndex) {
			SetImage(focusname, "images/focus_here.gif");
		} else {
			SetImage(focusname, "images/focus_not_here.gif");
		}
	}

}
