////////////////////////////////////////////////////////////////////////////
// Functions to Manage Subtopic Boxes
////////////////////////////////////////////////////////////////////////////

var x, y; // Global variables to track mouse position

domData.flag = false; // Stops boxes from being triggered until page is loaded.
var mNumAll = 0; // Number of Subtopic boxes
var mNames = new Array(); // Names of Subtopic boxes
var subNum = 0; // Element number of first Subtopic checkbox
mCurr = false; // No Subtopic box is active

// Images for image swapping
var img_subs_off = "../all_images/button_plus_off.gif";
var img_subs_on = "../all_images/button_plus_on.gif";

// Methods to get current scrolling
domData.scrollX = getScrollX; // method to get X offset
domData.scrollY = getScrollY; // method to get Y offset

// Define name of the search form
// var thisForm = "document.asearch.";
// This is done in the HTML file so that different HTML files can use different form names and still have access to these functions.

// Define Number of Subtopic Elements for each Topic
ch = new Array(); 
ch['ta'] = true;
ch['tb'] = true;
ch['tc'] = true;
ch['td'] = true;
ch['te'] = true;
ch['tf'] = true;
ch['tg'] = true;
ch['th'] = true;
ch['ti'] = true;
ch['tj'] = true;
ch['tk'] = true;
ch['tl'] = true;
ch['tm'] = true;
ch['tn'] = true;
ch['to'] = true;
ch['tp'] = true;
ch['tq'] = true;
ch['tr'] = true;
ch['ts'] = true;
ch['tt'] = true;

// BEGIN Initialize DHTML Objects
function init() {
	if (!domData.isOther) {
		createObjects(); // Function that creates DHTML objects
		domData.flag = true;
// Find out where the subtopic elements start	
		form = eval(thisForm);
		for (var i = 0; i < form.length; i++) {
			obj = form.elements[i]; 
			if ((obj.id.indexOf(firstSub) == 0) && (obj.id.length > 2)) { 
				subNum = i;
				break;
			}
		}
// Find if any subtopics elements are checked	
// Don't bother since this can be done faster by the Java engine
//		for (var m in ch) {
//			upCount(thisForm, m);
//		}
// Begin tracking mouse position to know where to position subtopic boxes
		if (domData.isNN6Up) { 
			document.body.addEventListener("mousedown", whereAmI, true);
		} 
		document.onmousedown = whereAmI; 
	}	
} // END init()



// BEGIN Define x,y position of mouse relative to document
function whereAmI(e) {

	if (domData.isNN6Up) { 
		x = e.clientX + domData.scrollX();
		y = e.clientY + domData.scrollY();;
	} else if (domData.isIE4Up) { 
		x = event.clientX + domData.scrollX(); 
		y = event.clientY + domData.scrollY(); 
	}

} // END mouseXY()	



// BEGIN Show or hide object
function toggleSubs(mID) {
	if (!domData.flag) { return; }
	if (mCurr == mID) {
		closeSub(mCurr);
		mCurr = false;
	} else {
		if (mCurr != false) {
			closeSub(mCurr);
		}
		openSub(mID);
		mCurr = mID;
	}
} // END toggle



// BEGIN Hide Topic Menu
function closeSub(m) {
	if (!domData.flag) { return; }
	var mName = "dynamic-subtopics-" + m;
	var nName = "dynamic-" + m;
	obj = theObjs[mName];
	obj.objHide();
	obj = theObjs[nName];
	obj.objSetBGC("#cdcfed");
	upCount(thisForm, m);
} // END closeSub(m)



// BEGIN Show Topic Menu
function openSub(m) {
    //alert("x: " + x + " y: " + y)
	if (!domData.flag) { return; }
	var mName = "dynamic-subtopics-" + m;
	var nName = "dynamic-" + m;
	obj = theObjs[mName];
	if (x < 386) {
		obj.objSetLeft(370);
	} else if (x < 562) {
		obj.objSetLeft(80);
	} else {
		obj.objSetLeft(255);
	}
	obj.objSetTop(y - 283);
	obj.objShow();
	obj = theObjs[nName];
	obj.objSetBGC("#ffffff");
} // END openSub(m)



// BEGIN Update Checkbox sum
function upCount(whatForm, whatKey) {
	if (!domData.flag) { return; }
	form = eval(whatForm);
	for (var i = subNum; i < form.length; i++) {
		obj = form.elements[i];
		if (obj.id.indexOf(whatKey) == 0) {
			if (obj.checked) { 
				document.images["img-" + whatKey].src = img_subs_on;
				return;
			}
		}
	}
// No subtopics boxes are checked
	document.images["img-" + whatKey].src = img_subs_off;
} // END updateCheck(whatForm, whatKey)



// BEGIN Assign scrollX method to domData to get current X offset of page
function getScrollX() {

	if (domData.isIE6) { return document.all.canvas.scrollLeft; 
	} else if (domData.isIE4Up) { return document.body.scrollLeft; 
	} else if (domData.isNN6Up) { return window.pageXOffset; }

} // END getScrollX



// BEGIN Assign scrollY method to domData to get current Y offset of page
function getScrollY() {

	if (domData.isIE6) { return document.all.canvas.scrollTop; 
	} else if (domData.isIE4Up) { return document.body.scrollTop; 
	} else if (domData.isNN6Up) { return window.pageYOffset; }

} // END getScrollY



////////////////////////////////////////////////////////////////////////////
// Functions to check and uncheck blocks of checkboxes
////////////////////////////////////////////////////////////////////////////

// BEGIN Check or uncheck a group of checkboxes
function checkGroup(whatForm, whatKey, whatValue) {
	var i, obj, form;
	form = eval(whatForm);
	for (i = 0; i < form.length; i++) {
		obj = form.elements[i]; 
		if (obj.id.indexOf(whatKey) == 0) {
			obj.checked = whatValue;
		}
	}
} // END checkGroup
