// returns Keyword value of specified category
// from form "Category: Keyword;"
function getKeyword(category, string){
	re = new RegExp(category + ":(.+?);");
	if (string.match(re))
		return string.match(re)[1];
	else
		return "Uncategorized";
}

// returns list of unique catsKeywords from passed in category
function getUniqueKeywords(category){
	// instantiate result holder
	var keywords = new Array();
	// get array of all keyword spans
	var catsKeywords = $$('span.catsKeywords');
	catsKeywords.each(function(item) {
		keywords.push(getKeyword(category, item.innerHTML));
	});
	return keywords.uniq().sort();
}

var NEHGS = {
	/*
	 * stripifyTable(table)
	 * Creates odd/even classes on the body of a table within a stripe container.
	 */
	stripifyTable : function(t) {
		var containers = $$('div.table-container');
		containers.each(function(c){
			if (c.hasClassName('stripe')) {
				var rows = $A(c.getElementsBySelector('tbody tr'));
				rows.each(function(r, index){
					if (index % 2 == 0) r.addClassName('even').removeClassName('odd');
					else r.addClassName('odd').removeClassName('even');
				})
			}
		});
	},
	
	buildDatabaseList : function() {
		// initialization
		var ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		if (!$('alpha-list') || !$('alpha-tables') || !$('alpha-nav')) return;
		var dblinks = $A($('alpha-list').getElementsBySelector('a'));
		
		// let's fill the hash with anchors
		var currentLetter = "";
		var letterHash = new Hash(); // hash of key = letter, value = array of elements;
		for (var i=0; i<dblinks.length; ++i) {
			var letter = dblinks[i].innerHTML.charAt(0);
			if (!letterHash.get(letter)) letterHash.set(letter, new Array());
			letterHash.get(letter).push(dblinks[i]); //push the link on the hash
		}
		//alert("B:" + letterHash.get('B')[1].innerHTML);
		
		
				
		// create the tables for each letter
		for (var i=0; i<ALPHA.length; ++i) {
		// TODO: apply all styles necessary
		
			var navlink = document.createElement("div");
			navlink.className = "alpha-navlink";
			
			
			if (letterHash.get(ALPHA.charAt(i))) {
				var tablecontainer = document.createElement("div");
				tablecontainer.className = "table-container footer stripe";
				tablecontainer.id = "table" + ALPHA.charAt(i);
				$(tablecontainer).hide();
				var table = document.createElement("table");
				table.cellSpacing=0;
				table.cellPaddding=0;
				table.width = "100%";
				var thead = document.createElement("thead");
				var headrow = document.createElement("tr");
				var headcell = document.createElement("th");
				headcell.className = "first";
				headcell.innerHTML = ALPHA.charAt(i);
				var tbody = document.createElement("tbody");

				
				headrow.appendChild(headcell);
				thead.appendChild(headrow);
				table.appendChild(thead);
				table.appendChild(tbody);
				
				// loop through letterHash(letter) array
				var rows = $A(letterHash.get(ALPHA.charAt(i)));
				rows.each(function(r) {
					var bodyrow = document.createElement("tr");				
					var bodycell = document.createElement("td");
					bodycell.className = "first";
					bodycell.appendChild(r);
					bodyrow.appendChild(bodycell);
					tbody.appendChild(bodyrow);
				});
				
			var anc = document.createElement("a");
			anc.name = "letter" + ALPHA.charAt(i);
			tablecontainer.appendChild(anc);
			tablecontainer.appendChild(table);
			
			$('alpha-tables').appendChild(tablecontainer);
			
			// create the anchor to link to the table
			var navanc = document.createElement("a");
			Event.observe(navanc,'click',function(e) {
				var blocks = $('alpha-tables').getElementsBySelector('div.table-container');
				blocks.each(function(b) {
					b.hide();
				});
				$('table'+this.innerHTML).show();
				setCookie('letter','table'+this.innerHTML,'',document.href);
			});
			navanc.innerHTML = ALPHA.charAt(i);
			navlink.appendChild(navanc);
				
			} else { // no letter in the hash
				navlink.innerHTML = ALPHA.charAt(i);	
			}
			$('alpha-nav').appendChild(navlink);
		}		// end for loop
		var l = getCookie('letter');
		if (l) $(l).show(); else  $('tableA').show();
		
	},
	
	sortListByCategory : function() {
		if (!$('alpha-list')) return;
		if ($('Location-nav'))
			var category = "Location";
		else if ($('Database_Record_Type-nav'))
			var category = "Database Record Type";
		else return;
		
		var keywordlist = getUniqueKeywords(category);
		keywordlist.each(function(keyword){
			var newGroup = document.createElement('div');
			var newHeader = document.createElement('h3');
			newHeader.innerHTML = keyword;
			newGroup.appendChild(newHeader);
			var newList = document.createElement('ul');
			newList.setAttribute("id", keyword);
			newGroup.appendChild(newList);
			document.getElementById('alpha-list').appendChild(newGroup);
		});
	
		// loop through list items and sort into categories
		var allListItems = $$('li');
		$A(allListItems).each(function(item){
			if(item.firstDescendant().hasClassName('catsKeywords')){
				keyword = getKeyword(category, item.firstDescendant().innerHTML);
				document.getElementById(keyword).appendChild(item);
			}
		});
		
		document.getElementById('alpha-list').setStyle({display:'block'});
	}
	
};

	Event.observe(window,'load',NEHGS.buildDatabaseList);
	Event.observe(window,'load',NEHGS.stripifyTable);
	Event.observe(window,'load', NEHGS.sortListByCategory);
	
	