// This is the code that will allow manipulation of document groups

// First, create the array that will hold the document group information
var document_domgrouparray = new Array();

// Second, poulate the array when the document loads
// get all of the document elements and store in array
var temp_domitemarray = document.getElementsByTagName("DIV");

// loop through all of the document element array
for ( i = 0 ; i < temp_domitemarray.length ; i++ ) {
	// get the attributes for the current document element in the array
	// if there are attributes for the current element...
	if (temp_domitemarray[i].attributes.length > 1) {
		for (j = 0; j < temp_domitemarray[i].attributes.length; j++) {
			// and its name is "id"...
			if (temp_domitemarray[i].attributes[j].name == 'id') {
				temp_domitemid = temp_domitemarray[i].attributes[j].value;
				temp_domitemidsplit = temp_domitemid.split('___');

				// and it has a prefix... identified by "___", then we are in business!
				if (temp_domitemidsplit.length > 1) {
					temp_domgroupindex = -1;
					// if there are already group prefixes identified, see if prefix array entry exists
					if (document_domgrouparray.length > 0) {
						for (k = 0; k < document_domgrouparray.length; k++) {
							if (document_domgrouparray[k][0] == temp_domitemidsplit[0]) {
								// since there is a match, this is the index to be used
								temp_domgroupindex = k;
							}
						}
					}
					// since no index found, create new entry and save new value as index
					if (temp_domgroupindex < 0) {
						// first save the existing length as the index to be used
						temp_domgroupindex = document_domgrouparray.length;

						// create the new entry
						document_domgrouparray[temp_domgroupindex] = new Array();

						// save current prefix as the new entry's first value
						document_domgrouparray[temp_domgroupindex][0] = temp_domitemidsplit[0];

						// create an empty array in the second value which will hold the handles
						// for each instance of the prefix
						document_domgrouparray[temp_domgroupindex][1] = new Array();
					}
					// save the current handle into the current group's second value,
					// which is an array of handles
					temp_nextitemhandleindex = document_domgrouparray[temp_domgroupindex][1].length;
					document_domgrouparray[temp_domgroupindex][1][temp_nextitemhandleindex] = temp_domitemidsplit[1];
				}
			}
		}
	}
}

function domgroup_toggle(var_domhandle,var_prefixlist_content,var_prefixlist_teaser) {
	// this function toggles items in the document based on:
	// --- a handle
	// --- a list of one or more content dom groups (the one of the current handle to be opened, the  rest closed)
	// --- a list of zero, one, or more teaser dom groups (the one of the current handle to be closed, the rest opened)

	if (document_domgrouparray.length == 0) {
		domgrouparray_init();
	}

	// verify there are document prefix groups identified for this webpage
	if (document_domgrouparray.length == 0) {
		// if not, provide an alert and then exit
		alert("No dom prefix groups have been identified for this document.");
		return false;
	}

	// create arrays that will hold the actions and respective toggle groups
	var temp_actionarray = new Array();
	var temp_domgrouptypearray = new Array();

	// "content" toggle groups
	temp_domgrouptypearray[0] = var_prefixlist_content.split(',');
	// "teaser" toggle groups
	temp_domgrouptypearray[1] = var_prefixlist_teaser.split(',');

	for (temp_domgrouptypeindex = 0; temp_domgrouptypeindex < temp_domgrouptypearray.length; temp_domgrouptypeindex++) {
		if (temp_domgrouptypearray[temp_domgrouptypeindex].length > 0) {
			switch (temp_domgrouptypeindex) {
			case 0:
				// "content" toggle groups
				temp_actionarray[0] = "Block";
				temp_actionarray[1] = "None";
				break;
			case 1:
				// "teaser" toggle groups
				temp_actionarray[0] = "None";
				temp_actionarray[1] = "Block";
				break;
			}

			for (temp_domgroupindex = 0; temp_domgroupindex < temp_domgrouptypearray[temp_domgrouptypeindex].length; temp_domgroupindex++) {
				for (document_domgroupindex = 0; document_domgroupindex < document_domgrouparray.length; document_domgroupindex++) {
					if (document_domgrouparray[document_domgroupindex][0] == temp_domgrouptypearray[temp_domgrouptypeindex][temp_domgroupindex]) {
						for ( document_domitemindex = 0 ; document_domitemindex < document_domgrouparray[document_domgroupindex][1].length ; document_domitemindex++ ) {
							var temp_domobject = document.getElementById(document_domgrouparray[document_domgroupindex][0] + '___' + document_domgrouparray[document_domgroupindex][1][document_domitemindex]);
							if (document_domgrouparray[document_domgroupindex][1][document_domitemindex] == var_domhandle) {
								temp_domobject.style.display = temp_actionarray[0];
							} else {
								temp_domobject.style.display = temp_actionarray[1];
							}
						}
					}
				}
			}
		}
	}
}
