// Copy and paste the initWrapOrStuff function and addEvent onto the course page and 
// change the parameters accordingly.

function initWrapOrStuff() {
	inst = new wrapOrStuff('test', 	// id or collection of target(s) to stuff or wrap
												 'wrap', 	// either wrap or stuff
												 3,				// number of tags to wrap or stuff
												 'span',	// type of tag to wrap or stuff
												 'class=ButtonText'); // class and/or id to add to tags, starting with the outermost tag, and separated with commas between tags
}
/*addEvent(window,"load",initWrapOrStuff);*/
/*
Function: wrapOrStuff
	Object that wraps nested tags around, or stuffs nested tags inside, a specified element or collection of elements. The type and number of tags to be wrapped or stuffed can be specified, along with a class or id to attach to the outermost nested tag.

Parameters: 
	targetEl - the element or collection of elements to wrap or stuff.  Can pass in an id or the element itself
	nestType - Can be 'wrap' or 'stuff'
	numTags - the number of tags to create and nest
	tag - the type of tag to create and nest
	tagHook (optional) - the class or id to attach to the outermost nested tag.  Should be in the form 'class=XXXX' or 'id=XXXX' 
	
Methods:
	wrapIt - the function that wraps the newly created nested tags around the specified element(s).
	stuffIt - the function that stuffs the newly created nested tags inside the specified element(s).
	newNest - the function that creates the nested tags.
	addHook - the function that adds a class or id to the outermost nested tag.
	remove - the function that removes nested tags from around or inside the specified elemnt.  Must pass it three parameter: targetEl, nestType, and numTags
	init - the function that determines whether to wrap or stuff the specificed element(s).

Returns:
	None.

Example Usages:
(start code)
// To stuff the element with id=testId with three spans, adding a class="ButtonText" and 
// id="new" to the outermost of the new spans.
function initWrapOrStuff() {		
	add = new wrapOrStuff('testId','stuff',3,'span','class=ButtonText&id=new').init(); 
}

// To wrap the element with id=testId with five divs, adding a class="fl" to 
// the outermost div, id="fr" to the middle div, and class="off" to the innermost div.
function initWrapOrStuff() {		
	add = new wrapOrStuff('testId','wrap',5,'div','class=fl,id=fr,class=off').init(); 
}

// To remove the first three nested children inside of the element with id=testId 
function initWrapOrStuff() {		
	del = new wrapOrStuff().remove('testId','stuff',3); 
}
(end code)

Bugs:
	None known.
	
To do:
	- Modify so that it can wrap or stuff tags that are not closed, such as IMG.
	
Change Log:
	2006.06.30	ALP	- Initial version.
*/
function wrapOrStuff(targetEl_arg,nestType_arg,numTags_arg,tag_arg,tagHook_arg) {
	this.targetEl = $(targetEl_arg);
	this.nestType = nestType_arg;
	this.numTags = numTags_arg;
	this.tag = tag_arg;
	this.tagHook = tagHook_arg || false;
	this.wrapIt = function(targetEl) {
		var insides = targetEl.innerHTML;
		var parent = targetEl.parentNode;
		var sib = targetEl.nextSibling;
		var target = document.createElement(this.tag);
		var newNest = this.newNest(target,this.numTags-1);
		parent.removeChild(targetEl);
		parent.insertBefore(target,sib);
		newNest.appendChild(targetEl);
		targetEl.innerHTML = insides;
		if (this.tagHook != false) {
			for (var i=0; i<this.numTags; i++) {
				newNest = targetEl.parentNode;
				targetEl = newNest;
			}
			this.addHook(newNest);
		}
	}
	this.stuffIt = function(targetEl) {
		var insides = targetEl.innerHTML;
		targetEl.innerHTML = "";
		this.newNest(targetEl,this.numTags,insides);
		if (this.tagHook != false) {
			this.addHook(targetEl.firstChild);
		}
	}
	this.newNest = function(target,loopNum,insides) {
		var currEl = target;
		for (var i=0; i<loopNum; i++) {
			var newerEl = document.createElement(this.tag);
			currEl.appendChild(newerEl);
			currEl = currEl.lastChild;
		}
		if (!insides) {
			return currEl;
		} else {
			currEl.innerHTML = insides;
		}
	}
	this.addHook = function(target) {
		var hookArray = this.tagHook.split(",");
		for (var i=0; i<hookArray.length; i++) {
			var tempArray = hookArray[i].split("&");
				for (var j=0; j<tempArray.length; j++) {
					var place = tempArray[j].indexOf("=");
					var prop = tempArray[j].substring(0,place);
					var name = tempArray[j].substring(place+1);
					if (prop == "class") {
						target.className = name;
					} else if (prop == "id") {
						target.id = name;
					}
				}
			target = target.firstChild;
		}
	}
	this.remove = function (targetEl,nestType,numTags) {
		var target = $(targetEl);
		var newTarget = target;
		if (nestType == "wrap") {
			var insides = target.innerHTML;
			target.innerHTML = "";
			for (var i=0; i<numTags; i++) {
				newTarget = newTarget.parentNode;
			}
			var parent = newTarget.parentNode
			var sib = newTarget.nextSibling;
			parent.removeChild(newTarget);
			parent.insertBefore(target,sib);
			target.innerHTML = insides;
		} else if (nestType == "stuff") {
			for (var i=0; i<numTags; i++) {
				newTarget = newTarget.firstChild;
			}
			var insides = newTarget.innerHTML;
			target.removeChild(target.firstChild);
			target.innerHTML = insides;
		}
	}
	this.init = function() {
		if (!this.targetEl.length) {
			if (this.nestType == "wrap") {
				this.wrapIt(this.targetEl);
			} else if (this.nestType == "stuff") {
				this.stuffIt(this.targetEl);
			} else {
				alert("You must enter a valid nestType: wrap or stuff");
			}
		} else {
			for (var i=0; i<this.targetEl.length; i++) {
				if (this.nestType == "wrap") {
					this.wrapIt(this.targetEl[i]);
				} else if (this.nestType == "stuff") {
					this.stuffIt(this.targetEl[i]);
				} else {
					alert("You must enter a valid nestType: wrap or stuff");
				}
			}
		}
	}
/*	this.init();*/
}

// Deletes a substring and returns the string minus the substring.
function delSubstr(string, substrToCut) {
	var substrLength = substrToCut.length;
	var start = string.indexOf(substrToCut);
	if (start != -1) {
		var end = start + substrLength;
		var part1 = string.substring(0,start-1);
		var part2 = string.substring(end);
		string = part1 + part2;
	}
	return string;
}