var gCurrentLetter = 'A';
var gURLchecker;

function debug(msg) {
	var debugContainer = document.getElementsByTagName('h1')[0];
	debugContainer.innerHTML = msg + '<br />' +debugContainer.innerHTML
}

function changeLetter(letterTo) {
	if((document.getElementById) && (document.getElementById('c'+gCurrentLetter)) && (document.getElementById('c'+letterTo))) {
		document.getElementById('c'+gCurrentLetter).style.display = 'none';
		document.getElementById('c'+letterTo).style.display = 'block';
		gCurrentLetter = letterTo;
		location.hash = '#' + gCurrentLetter;
	}
}

// takes an array of images and the current image object
function listenForURLchange() {
	// if the hash exists and is not #top
	if((location.hash) && (location.hash.indexOf('top') == -1)) {
		var letterRequested = location.hash.replace('#', '');
		if(letterRequested != gCurrentLetter) {
			changeLetter(letterRequested);
		}
	}
}

function initiateAZ() {

	// re-configure list to show only letters with coresponding terms as a link, otherwise not
	var list = document.getElementById('alphabetListing').getElementsByTagName('ol')[0].getElementsByTagName('li');
	var i = list.length -1;
	var currentLetter
	for(i; i>= 0; i--) {
		currentLetter =list[i].childNodes[0].href.split("#")[1].replace('#', '');
		if(list[i].getElementsByTagName('a')[0]) {
			if(!(document.getElementById(currentLetter))) {
				// if the letter we are looking at has no associated terms, turn it into a span
				var newNode = document.createElement('span');
				newNode.appendChild(document.createTextNode(currentLetter));
				list[i].appendChild(newNode);
				list[i].removeChild(list[i].getElementsByTagName('a')[0]);
			} else {
				// letter has associated terms, add click listner to make transition faster
				(function() {
					var currentL = currentLetter;
					var currentN = list[i];
					Event.observe(currentN, 'click', function() {changeLetter((currentL)); return false;});
				})();
			}
		}
	}
	currentLetter = null;

	var containers = document.getElementById('atoz').getElementsByTagName('div');
	var j = containers.length -1;
	for(j; j>= 0; j--) {
		if(containers[j].className.indexOf('alphabet') != -1) {
			containers[j].id = 'c' + containers[j].id;
		}
	}

	// what page are we on? take hash and turn to index : #1 --> [0]
	if((location.hash)&&(location.hash.length<3)&&(location.hash != 'A')) {
		changeLetter(location.hash.replace('#', ''))
 	} else {
 		gCurrentSlide = 'A';
 		location.href = location.href + '#A';
	}

	// assign our listener to a global variable so we can purge it later
	gURLchecker = setInterval(listenForURLchange, 700);
}

function setCSS(css) {
	try {
  // append stylesheet to alter
		document.getElementsByTagName("head")[0].appendChild(css);
	} catch (e) {
		setTimeout(function(){setCSS(css)}, 100);
	}
}

// initiate file
Event.observe(window, 'load', initiateAZ);

// create CSS element to set up the page
var css = document.createElement("link");
css.setAttribute("href","/css/front/templates/a_z_javascript.css");
css.setAttribute("rel","stylesheet");
css.setAttribute("type","text/css");

// attempt to add the css and then keep trying till we do
setCSS(css);
css = null;

