// JavaScript Document

var Registry = new function() {
	this.currentItem = 0;
	this.currentSection = 0;
	this.dayDivs = null;
	this.sectionDivs = null;
	this.sectionLis = null;
}; // Registry

var TabDiv = new function() {
	this.init = function() {
		var contentDiv = document.getElementById("content");
	
		// wash/rinse/repeat using sections (so as not to break any daydiv use)
		// new implementation: sections and divs must match in (array) order
		Registry.sectionLis = getElementsByClassName("sectionlink", "LI", contentDiv);
		Registry.sectionDivs = getElementsByClassName("section", "DIV", contentDiv);
		
//		alert('daydivs: '+Registry.dayDivs.length+' daylinks: '+dayLinks.length);
		// loop through links and reassign ids to correspond to array ordering
		for (var c = 0; c < Registry.sectionLis.length; c++) {
			/*
			dayLinks[c].onclick = function() {
				TabDiv.activate(this);
			};
			*/
			Registry.sectionLis[c].id = 'link-' + c;
		}
		
		var sectionLinksDiv = document.getElementById("sectionlinks");
		sectionLinksDiv.onclick = function(e) {
			if (!e) e = window.event;
			var target = getTargetElem(e);
			while(target.nodeName != 'LI') target = target.parentNode;
			//alert(Registry.sectionDivs[target.id.split('-')[1]].id);
			TabDiv.activateSection(target);
		}
		
		sectionLinksDiv.onmouseover = function(e) {
			if (!e) e = window.event;
			var target = getTargetElem(e);
			while(target.nodeName != 'LI') target = target.parentNode;
			//alert(Registry.sectionDivs[target.id.split('-')[1]].id);
			TabDiv.highlightOn(target);
		}
		
		sectionLinksDiv.onmouseout = function(e) {
			if (!e) e = window.event;
			var target = getTargetElem(e);
			while(target.nodeName != 'LI') target = target.parentNode;
			//alert(Registry.sectionDivs[target.id.split('-')[1]].id);
			TabDiv.highlightOff(target);
		}
		
		// display first day div
		if (Registry.sectionDivs[0]) {
			Registry.sectionDivs[0].style.display = 'block';
			TabDiv.highlightOn(Registry.sectionLis[0]);
		}
	}; // init
	
	this.activateSection = function(itemDiv) {
		// hide current item
		if (Registry.currentSection != -1) Registry.sectionDivs[Registry.currentSection].style.display = 'none';
		// display selected item
		var index = itemDiv.id.split('-')[1];
		// index--; // adjust for array
		//alert(index);
		Registry.sectionDivs[index].style.display = 'block';
		// remove highlight class from original item
		TabDiv.highlightOff(Registry.sectionLis[Registry.currentSection]);
//		Registry.sectionLis[Registry.currentSection].className = 
//			Registry.sectionLis[Registry.currentSection].className.substr(0,Registry.sectionLis[Registry.currentSection].className.length - 10);
		// set selected item as current item
		Registry.currentSection = index;
		// apply highlighting class
		TabDiv.highlightOn(Registry.sectionLis[Registry.currentSection]);
//		Registry.sectionLis[Registry.currentSection].className = Registry.sectionLis[Registry.currentSection].className + ' activelink';
	}; // activate
	
	this.highlightOn = function(target) {
		target.className = target.className + ' activelink';
	};
	
	this.highlightOff = function(target) {
		target.className = target.className.substr(0,target.className.length - 11);
	};
	
	this.cleanup = function() {
		purge(document.getElementById("wrapper"));
		Registry.dayDivs = null;
		Registry.sectionDivs = null;
		Registry.sectionLis = null;
	}; // cleanup
}; // TabDiv

window.onload = TabDiv.init;
window.onunload = TabDiv.cleanup;

