/*
Script:			chunkSet.js
Author:			(c) 2002 rob cherny (rcherny[at]navarts[dot]com/rob[at]cherny[dot]com)

Usage:			ChunkSet.init(parentClass,parentClassOn,parentE,childClass,childClassOn,childE,dispErr);
				ex: ChunkSet.init("quest","questOn","p","answer","answerOn","div",1)
				
Params:			(all are required)
				parentClass - class of parent item on page.
				parentClassOn - class of parent when child is toggled on
				parentE - element/tag of parent (example: "div", "p")
				childClass - child class
				childClassOn - toggled on child class
				childE - element/tag of child (see parentE)
				dispErr - boolean 1/0 on vocal failover on bad html -- mainly if # of parents != # of children
				
Warnings:		1. Currently the code does not add style rules appropriate for this script (this is a "to-do"). Must manually add rules like:
					.childClass { display: none; }
					.childClassOn { display: block }
				2. relies on no other className being assigned to a parent Element or a child Element
				3. children must immediately follow parents in source

Compat:			shoud work with most all DOM compatible browsers; "please use W3C Standards and help evolve the web"
				
License:		please feel free to use freely and/or alter, provided author line is kept intact; no warranty of any kind expressed or implied
*/

var ChunkSet = {

	init: function(parentClass,parentClassOn,parentE,childClass,childClassOn,childE,dispErr)
	{
		if (!document.getElementsByTagName && !document.getElementById) return false;
		this.parentClass = parentClass;
		this.parentClassOn = parentClassOn;
		this.parentE = parentE;

		this.childClass = childClass;
		this.childClassOn = childClassOn;
		this.childE = childE;

		// errors 
		this.failure = 0;
		this.dispErr = dispErr;
		
		// start chunks
		this.collect();
	},

	getElementsWithClassName: function(elementName,className)
	{
		var allElements = document.getElementsByTagName(elementName);
		
		var elemColl = new Array();
		var i = 0;
		while (i < allElements.length)
		{
			if (allElements[i].className == className)
				elemColl[elemColl.length] = allElements[i];
			i++;
		}
		return elemColl;
	},
	
	act: function(idx)
	{
		this.childCache[idx].className = (this.childCache[idx].className == this.childClass) ? this.childClassOn : this.childClass;
		this.parentCache[idx].className = (this.parentCache[idx].className == this.parentClass) ? this.parentClassOn : this.parentClass;
	},
	
	// graceful recovery -- this will "show" all children it finds (right or not);
	fail: function()
	{
		if (this.dispErr) alert("Error: # of parents doesn\'t match # of children.");
		var recoverChild = ChunkSet.getElementsWithClassName(this.childE, this.childClass);
		
		// clean up
		var recoverX = 0;
		while(recoverX < recoverChild.length)
		{
			recoverChild[recoverX].className = this.childClassOn;
		recoverX++
		}
		return false;
	},
	
	collect: function()
	{
		this.parentCache = ChunkSet.getElementsWithClassName(this.parentE, this.parentClass);
		this.childCache = ChunkSet.getElementsWithClassName(this.childE, this.childClass);
		
		// check errors
		if (this.parentCache.length != this.childCache.length) this.failure = 1;
		if (!this.failure)
		{
			var ct = 0;
			while (ct < this.parentCache.length)
			{
				this.parentCache[ct].onclick = new Function("ChunkSet.act(" + ct + ")");
			ct++;
			}
		}
		else ChunkSet.fail()
	}
}

