/* Navigation JS --------------------------------------- */
/* these functions power the browse- and jumpTo- pulldowns on every page of the site */

function browse(field){if (field <= 0){ return; }top.location = unescape(browse_links[field]);}
function jumpTo(field){if (field <= 0){ return; }top.location = unescape(jumpTo_links[field]);}
function get_random(){return Math.round(Math.random()*10000);}

/* Listing JS --------------------------------------- */
var listings = new Array();var queries = new Array();var queryValues = new Array();
function listing(id,ctgs){this.id=id;this.ctgs=ctgs;}
function ctg(id,count){this.id=id;this.count=count;}

function listCat(listId, catId)
{
	var list = document.getElementById(listId);var curChild = list.firstChild;
	for (var i=0;curChild!=null;i++){if (i!=catId){ curChild.style.display = 'none'; } else { curChild.style.display = 'block'; }curChild = curChild.nextSibling;}
}
function listAll(listId)
{
	var list = document.getElementById(listId); var curChild = list.firstChild;
	for (var i=0;curChild!=null;i++){curChild.style.display = 'block';curChild = curChild.nextSibling;}
	document.getElementById(listId + '_noresults').style.display='none';
}
function queryLists()
{
	for (var i=0;i<listings.length;i++)
	{
		var curValue = document.getElementById(queries[i]).value; if (curValue == 'Search this list'){ curValue = ''; }
		if (curValue != queryValues[i]){ queryValues[i] = curValue; limitList(listings[i],curValue);}
	}
	setTimeout("queryLists();", 300);
}
function limitList(list,query)
{
	/* parse the search query into an array */
	if ((query.indexOf(" ") != -1) && (query.indexOf(" ") != (query.length -1))) { // if there is white space
		var searchterms = query.split(" "); // split on the white space
	} else { // if no white space
		var searchterms = new Array(); // make the search terms be a 1-elt array
		searchterms[0] = query;
	}
	whiteSpace = /[ \t]/g; // create whitespace pattern
	for (var a = 0; a < searchterms.length; a++) { searchterms[a].replace(whiteSpace, ""); } // grep for extra white space and kill it 


	/* start doing the actual searching and results tabulation */
	l = document.getElementById(list.id);
	var countTotalMatch = countTotal = 0;	/* counters for total matches and total items in the list */
	for(var i=0;i<list.ctgs.length;i++) {	/* foreach list category */
		var countByCtg = 0;					/* counter for matching items in category */
		for(var j=0;j<list.ctgs[i].count;j++){	/* loop through the expected ids of the elements, choosing whether to display based on content */
			var row = document.getElementById(list.id + '_ctg' + i + '_' + j);	/* get the item */
			var result = searchRow(row,searchterms);	/* do the query */
			if(!result){ row.style.display='none'; } else	/* show or hide matching/nonmatching elements, and increment category counter */
			{ row.style.display='block'; countByCtg++;}
		}
		var ctgdom = document.getElementById(list.id + '_ctg' + i);	/* if nothing in this category, hide the whole thing (including header) */
		if (countByCtg == 0){ctgdom.style.display='none';} else {ctgdom.style.display='block';}
		document.getElementById(list.id + '_ctg' + i + '_count').innerHTML = "(" + countByCtg + ")";	/* otherwise, set the count display in the header */
		countTotalMatch += countByCtg;	/* update total matches and total number */
		countTotal += list.ctgs[i].count;
	}
	var nores = document.getElementById(list.id + '_noresults');
	if (countTotalMatch == 0){nores.style.display='block';} else {nores.style.display='none';}
	var resultsString = countTotal + " items total";
	if (query != ""){ resultsString += "; " + countTotalMatch + " match" + ((countTotalMatch==1) ? "" : "es") + " to your query"; }
	document.getElementById(list.id + "query_results").innerHTML = resultsString;
}
function searchRow(obj,query)
{	var doesmatch = false; var rExp;
	for (var b=0; b<query.length; b++){
		rExp = new RegExp(query[b], "i");
		doesmatch = (doesmatch || getInnerText(obj).match(rExp));
	} return doesmatch;
}
/* this function taken from a sorting script by Mingyi Liu */
function getInnerText(el)
  {
  	if (typeof el == "string") return el;
  	if (typeof el == "undefined") { return el };
  	if (el.innerText) return el.innerText;	//Not needed but it is faster
  	var str = "";
  	
  	var cs = el.childNodes;
  	var l = cs.length;
  	for (var i = 0; i < l; i++) {
  		switch (cs[i].nodeType) {
  			case 1: //ELEMENT_NODE
  				str += getInnerText(cs[i]);
  				break;
  			case 3:	//TEXT_NODE
  				str += cs[i].nodeValue;
  				break;}}
  	return str;
  }
/* PhotoView JS ---------------------------- */
var photoPaths = new Array(); var photoWidths = new Array(); var photoHeights = new Array(); var photoCaptions = new Array(); var thumbPaths = new Array(); var downloadPaths = new Array();
function photolistPrev(pvid){parent.frames['photoview' + pvid + '_thumbnails'].prev();}
function photolistNext(pvid){parent.frames['photoview' + pvid + '_thumbnails'].next();}
/* Toggle Show/Hide Divs ---------------------------- */
function toggleDivDisplay(id){
	if (document.getElementById(id).style.display == 'block') document.getElementById(id).style.display = 'none';
	else document.getElementById(id).style.display = 'block';
}

var i;
var itemId;

/* XForms JS ----------------------------- */
function selectItems(onoff, item, count)
{
	for(i = 0; i < count; i++) {
		itemId = 'shodor_xforms_' + item + '_' + i;
		document.getElementById(itemId).checked = onoff; }
}

/* Error backtrace JS */
function toggleBacktrace(id) {
	var myDiv = document.getElementById(id);
	if(myDiv.style.display == 'block')
		myDiv.style.display = 'none';
	else
		myDiv.style.display = 'block';
}
