
var picArea;
var t;
var photoList;
var selector;
var biosEnabled = false;

function menuSelectPhoto ()
{
	//txt = propertiesAsHTML(selector, "formSelector", "");
	//domSetContent(picArea, txt);
	selectPhoto(selector.selectedIndex);
}


function selectPhoto (index)
{
	selector.selectedIndex = index;

	if (index <= 1)
		document.getElementById("lArrow").parentNode.className = "hiddenArrow";
	else
		document.getElementById("lArrow").parentNode.className = "arrow";

	if (! photoList)
		return;

	if (index >= photoList.length)
		document.getElementById("rArrow").parentNode.className = "hiddenArrow";
	else
		document.getElementById("rArrow").parentNode.className = "arrow";

	showImage(index);
}


function slideImage (off)
{
	var cur = selector.selectedIndex;
	cur += off;

	if (cur > 0 && cur <= photoList.length)
		selectPhoto(cur);
}


function showImage(index)
{
	if (index == "0") {
		domSetContent(picArea, "Choose a player name from the menu.");
		return;
	}

	// skip 0; it's the reset option and does not appear in photoList
	index--;

	var bioText = "";
	if (biosEnabled)
		bioText = photoList[index][2];

	domSetContent(picArea, '<img class="playerPic" src="photos/'
	                   + photoList[index][1] + '" alt="'
	                   + photoList[index][0] + '"/>'
	                   + '<p class="bio">'
	                   + '<b>'+ photoList[index][0] + '</b><br/>'
	                   + bioText + '</p>');
}


function linkifyList (list)
{
	var newList = new Array(0);
	var i, j, n;

	/* ugh-ly */
	for (i in photoList) {
		for (j in list) {
			if (list[j] == photoList[i][0]) {
				n = i; n++;
				newList.push('<a href="javascript:selectPhoto('
				             + n + ');">' + list[j] + '</a>');
				list[j] = null;
			}
		}
	}

	/* push anything left over */
	for (j in list) {
		if (list[j]) {
			newList.push(list[j]);
		}
	}

	return newList;
}


function buildList (xml, xpath, reductions, cb)
{
	try {
fuck;
		XPathResult;
		return buildListXPath(xml, xpath, cb);
	}
	catch (e) {
		return buildListDOMSearch(xml, reductions, cb);
	}
}


function buildListXPath (xml, xpath, cb)
{
	var nodes, element, list;
	var i;

	nodes = document.evaluate(xpath, xml, null,
	                          XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
	i = 0;
	list = new Array(0);
	while ((element = nodes.snapshotItem(i++)) != null) {
		list.push(cb(element));
	}
	return list;
}


function buildListDOMSearch (xml, reductions, cb)
{
	var nodes, newNodes, element, tag;
	var i, j;

	tag = reductions.shift();
	nodes = xml.getElementsByTagName(tag);

	// nodes might be a non-Array type with some Array features, so coerce
	// into a regular Array for return.
	newNodes = new Array(0);
	for (i = 0; i < nodes.length; i++) {
		if (nodes[i])
			newNodes.push(nodes[i]);
	}
	nodes = newNodes;

	while (reductions.length > 0) {
		tag = reductions.shift();
		newNodes = new Array(0);
		for (i = 0; i < nodes.length; i++) {
			var set = nodes[i].getElementsByTagName(tag);
			for (j = 0; j < set.length; j++) {
				newNodes.push(set[j]);
			}
		}
		nodes = newNodes;
	}

	for (i in nodes) {
		nodes[i] = cb(nodes[i]);
	}
	return nodes;
}


function setOptions(xml)
{
	var nSet;
	var n;

	nSet = xml.getElementsByTagName("toggle");
	for (n = 0; n < nSet.length; n++) {
		if (nSet[n].getAttribute("bios") == "yes")
			biosEnabled = true;
	}
}


function processPiclist (uri, http)
{
	var xml = http.responseXML;
	var eNew = document.getElementById("recNew");
	var eUpd = document.getElementById("recUpd");
	var nodes, element, list;
	var i;


	/* Update the picArea status message. */
	domSetContent(picArea, "Choose a player name from the menu.");

	/* Update the left/right button state by selecting 0 explicitly */
	selectPhoto(0);

	/* Load options */
	setOptions(xml);

	/* Load values into form and prep photoList. */
	photoList = buildList(xml,
	                      "//piclist/pic",
	                      ['piclist', 'pic'],
	                      function (e) { 
	                          return [e.getAttribute("name"),
	                                  e.getAttribute("img"),
	                                  domGetContent(e)];
	                      });
	photoList.sort(sortByArrayIndex0);
	for (i in photoList) {
		element = document.createElement("option");
		element.setAttribute("index", "" + i);
		domSetContent(element, photoList[i][0]);
		selector.appendChild(element);
	}


	/* Extract new pics and store them in the update table */
	var list = buildList(xml,
	                     "//piclist/recent/new",
	                     ['piclist', 'recent', 'new'],
	                     function (e) { return domGetContent(e); });
	list.sort();
	list = linkifyList(list);
	domSetContent(eNew, list.join(", "));


	/* Extract updated pics and store them in the update table */
	var list = buildList(xml,
	                     "//piclist/recent/updated",
	                     ['piclist', 'recent', 'updated'],
	                     function (e) { return domGetContent(e); });
	list.sort();
	list = linkifyList(list);
	domSetContent(eUpd, list.join(", "));

	return;
}


function sortByArrayIndex0 (a, b)
{
	if (a[0] < b[0])
		return -1;
	if (a[0] > b[0])
		return  1;
	return 0;
}


function init ()
{
	t = new Telemon(document);

	picArea = document.getElementById("photoArea");
	if (picArea == null)
		return;

	selector = document.getElementById("formSelector");
	if (selector == null)
		return;

	domSetContent(picArea, "Loading player photo data...");
	t.httpGet("piclist.xml", processPiclist);
}
