Array.prototype.inArray = function (s) {
	for(var i=0; i<this.length; i++) {
		if(this[i] == s) {
			return true;
		}
	}
	return false;
}

Array.prototype.findFirst = function (s) {
	var found = false;
	for(var i=0; i<this.length; i++) {
		 if(this[i] == s) {
			 found = i;
		 }
	}
	return found;
}

var openedBoxArray = new Array();
var openedBoxImages = new Array();

function addBox(box, img) {
	openedBoxArray.push(box);
	openedBoxImages.push(img);
}

function removeBox(box, img) {
	openedBoxArray.splice(openedBoxArray.findFirst(box), 1);
	openedBoxImages.splice(openedBoxImages.findFirst(img), 1);
}

function toggleIMG(state, iID) {
	var img = document.getElementById(iID);
	if (state == "open") img.src = "slideMenuImages/collapse_light.gif";
	else img.src = "slideMenuImages/expand_light.gif"
}

function closeAll() {
	for (i = 0; i < openedBoxArray.length; i++) {
		iID = openedBoxImages[i];
		openedBoxArray[i].toggle();
		toggleIMG("close", iID);
		removeBox(openedBoxArray[i], iID);
	}
}

function toggleCollapse(cID, iID) {
	if (openedBoxArray.inArray(cID)) {
		cID.toggle();
		toggleIMG("close", iID);
		removeBox(cID, iID);
	} else {
		if (openedBoxArray.length > 0) closeAll();
		cID.toggle();
		toggleIMG("open", iID);
		addBox(cID, iID);
	}
}