// $Revision: 2676 $
// Toggle the folded state of a set of folding objects, referred to by array of master ids to fold and/or unfold.
function ToggleFolded(foldIds, unfoldIds, doButFirst, doSaveFoldState, event) {
    if (typeof event == "undefined" || event == null) {
        event = window.event;
    }
	var bodyId;
	var footerId;
	var spacerId;
	var openable = (unfoldIds != null && unfoldIds.length > 0);
	var closable = (foldIds != null && foldIds.length > 0);
	if (closable) {
		bodyId = foldIds[0] + "_body";
		footerId = foldIds[0] + "_footer";
		spacerId = foldIds[0] + "_spacer";
	} else if (openable) {
		bodyId = unfoldIds[0] + "_body";
		footerId = unfoldIds[0] + "_footer";
		spacerId = unfoldIds[0] + "_spacer";
	} else {
		errorReporter.Report("OpenClose.js", "ToggleFolded: no foldIds and no unfoldIds.");
		return;
	}
	var body = getElement(bodyId);
	var footer = getElement(footerId);
	var spacer = getElement(spacerId);
	var isClosed = (body.style.display == "none");
	if (isClosed && !openable) {
		return;
	} else if (!isClosed && !closable) {
		return;
	}
	var ids = ((isClosed && openable) ? unfoldIds : foldIds);
	for (var i = 0; i < ids.length; i++) {
		var id = unEscape(ids[i]);
		bodyId = id + "_body";
		body = getElement(bodyId);
		if (body == null && footer == null) {
			return;
		} 
		var closeObj = getElement(id + "_fold");
		var openObj = getElement(id + "_unfold");
		if (isClosed) {
			// open
			if (openObj && closeObj) {
				openObj.style.display = "none";
				closeObj.style.display = "";
			}
			if (openObj && doButFirst) {
				var butfirst = openObj.getAttribute("butfirst");
				if (butfirst != null) {
					// butfirst must call ToggleFolded with doButFirst = false!!
					eval(butfirst);
					return;
				}
			}
			ShowContents(body, true);
			ShowContents(footer, true);
			body.style.display = "";
			if (footer != null) {
				footer.style.display = "";
			}
			if (spacer != null) {
				spacer.style.display = "none";
			}
			// Adjust width of tree node label
			if (ContainsStyleClass(body.parentNode, "treenode")) {
			    SetTreeLabelWidth(id);
			}
			var header = getElement(id + "_header");
			if (header != null) {
				DoRemoveStyleClass(header, CLOSED);
				DoAddStyleClass(header, OPEN);
			}
			var parent = body.parentNode;
			if (openObj) {
				var andafterward = openObj.getAttribute("andafterward");
				if (andafterward != null) {
					eval(andafterward);
				}
			}
			if (doSaveFoldState != null) {
				MarkOpen(true, doSaveFoldState, ids);
			}
		} else {
			// close
			if (openObj && closeObj) {
				openObj.style.display = "";
				closeObj.style.display = "none";
			}
			ShowContents(body, false);
			ShowContents(footer, false);
			body.style.display = "none";
			if (footer != null) {
				footer.style.display = "none";
			}
			if (spacer != null) {
				spacer.style.display = "";
			}
			var header = getElement(id + "_header");
			if (header != null) {
				DoRemoveStyleClass(header, OPEN);
				DoAddStyleClass(header, CLOSED);
			}
			if (closeObj) {
				var andafterward = closeObj.getAttribute("andafterward");
				if (andafterward != null) {
					eval(andafterward);
				}
			}
			if (doSaveFoldState != null) {
				MarkOpen(false, doSaveFoldState, ids);
			}
		}
	}
    if (typeof event != "undefined" && event != null) {
		if (event.stopPropagation) {
			event.stopPropagation();
		} else {
			event.cancelBubble = true;
		}
		return Finish(event);
	}
}

function ShowContents(parent, doShow) {
	if (!isIE || parent == null) {
		return;
	}
	for (var i = 0; parent.childNodes != null && i < parent.childNodes.length; i++) {
		var elem = parent.childNodes[i];
		if (elem.tagName != null) {
			switch (elem.tagName.toLowerCase()) {
				case "table":
				case "td":
					DisplayElem(elem, doShow);
					break;
			}
		}
		if (elem.type != null) {
			switch (elem.type) {
				case "select-one":
				case "select-multiple":
					DisplayElem(elem, doShow);
					continue;
			}
		}
		ShowContents(elem, doShow);
	}
}

function DisplayElem(elem, doShow) {
	// don't display undisplayed disabled elements
	if (!doShow && elem.disabled && elem.style != null && elem.style.display == "none") {
		elem.setAttribute("alreadyundisplayed", "true");
		return;
	}
	if (doShow && "true" == elem.getAttribute("alreadyundisplayed")) {
		elem.removeAttribute("alreadyundisplayed");
		return;
	}
	elem.style.display = doShow ? "" : "none";
}

// Return true if the folding object referred to by id is open.
function IsOpen(id) {
	id = unEscape(id);
	var bodyId = id + "_body";
	var body = getElement(bodyId);
	return body == null || body.style == null || body.style.display == null || body.style.display != "none";
}

var BIB_SHADOW_LEFT_OFFSET = 6;
var BIB_SHADOW_BOTTOM_OFFSET = 11;

// Toggle the folded state of a bib.
function ToggleDisplayBib(id, doShow, doHide) {
	id = unEscape(id);
	var bibcontainer = getElement(id);
	var body = new DDiv(id + "_body");
	var shadow = getElement(id + "_shadow");
	if (shadow == null) {
		return;
	}
	if (shadow.style.display == "none" && doShow) {
		// show
		var bodyStyle = GetComputedStyle(body.div);
		var bodyTop = parseInt(bodyStyle.top);
		var parent = bibcontainer.parentNode;
		var parentStyle = GetComputedStyle(parent);
		if (parentStyle.borderBottomStyle == "none") {
			body.style.top = bodyTop + parent.offsetHeight + "px";
		} else {
			body.style.top = bodyTop + parent.offsetHeight - parseInt(parentStyle.borderBottomWidth) + "px";
		}
		if (body.iframe != null) {
			body.iframe.style.width = body.div.offsetWidth + "px";
			body.iframe.style.height = body.div.offsetHeight + "px";
		}
		shadow.style.display = "";
		AdjustBibShadow(id, true);
		var bibContainers = GetDescendentsByTagNameAndClassName(bibcontainer.parentNode, "div", "bibcontainer");
		for (var i = 0; bibContainers != null && i < bibContainers.length; i++) {
			var otherId = bibContainers[i].id
			if (id == otherId || !IsOpen(otherId)) {
				continue;
			}
			ToggleFolded([otherId], null, false);
			var otherShadow = getElement(otherId + "_shadow");
			if (otherShadow != null) {
				otherShadow.style.display = "none";
			}
		}
		var forms = body.div.getElementsByTagName("form");
		if (forms != null && forms.length > 0) {
			DoSetFocus(forms[0]);
		}

	} else if (doHide) {
		// hide
		shadow.style.display = "none";
	}
}

function AdjustBibShadow(id, isBib) {
	if (isBib) {
		id = unEscape(id);
	} else {
		var bibContainer = GetAncestorByTagNameAndClassName(getElement(id), "div", "bibcontainer");
		id = bibContainer.id;
	}
	var body = new DDiv(id + "_body");
	if (body.iframe != null) {
		body.iframe.style.width = body.div.offsetWidth + "px";
		body.iframe.style.height = body.div.offsetHeight + "px";
	}
	var shadow = new DDiv(id + "_shadow");
	shadow.Resize(body.ddiv.offsetLeft + Number(BIB_SHADOW_LEFT_OFFSET) + "px",
		body.ddiv.offsetTop + "px",
		body.ddiv.offsetWidth + "px",
		body.ddiv.offsetHeight + Number(BIB_SHADOW_BOTTOM_OFFSET) + "px");
}

function IsInBib(event) {
	if (!event) {
		event = window.event;
	}
	var element = null;
	if (event.target) {
	    element = event.target;
	} else if (event.srcElement) {
	    element = event.srcElement;
	}
	while (element) {
		if (ContainsStyleClass(element, "bib")) {
			return true;
		}
		element = element.parentNode;
	}
	return false;
}

// DDiv object
function DDiv(id) {
	this.id = unEscape(id);
	this.ddiv = getElement(this.id);
	this.style = this.ddiv.style;
	var divid, iframeid;
	if (isIE) {
		divid = this.ddiv.divid;
		iframeid = this.ddiv.iframeid;
	} else {
		divid = this.ddiv.getAttribute("divid");
		iframeid = null;
	}
	this.div = getElement(divid);
	this.iframe = null;
	if (iframeid != null) {
		this.iframe = getElement(iframeid);
		this.iframe.style.zIndex = "-1";
	}
}

DDiv.prototype.Resize = function(left, top, width, height) {
	if (left != null) {
		this.ddiv.style.left = left;
	}
	if (top != null) {
		this.ddiv.style.top = top;
	}
	if (width != null) {
		if (parseInt(width) < 0) {
			width = "0px";
		}
		this.div.style.width = this.ddiv.style.width = width;
		if (this.iframe != null) {
			this.iframe.style.width = width;
		}
	}
	if (height != null) {
		if (parseInt(height) < 0) {
			height = "0px";
		}
		this.div.style.height = this.ddiv.style.height = height;
		if (this.iframe != null) {
			this.iframe.style.height = height;
		}
	}
}

var DEFAULT_LEFT_TAB_WIDTH = 250;

function GetLeftTabWidth() {
	var mainPanelLeft = getElement("mainpanelleftid");
	var mainPanelLeftWidth = mainPanelLeft.offsetWidth;
	if (mainPanelLeftWidth > 0) {
		return Math.round(Math.min((mainPanelLeftWidth * 0.75), DEFAULT_LEFT_TAB_WIDTH));
	} else {
		return DEFAULT_LEFT_TAB_WIDTH;
	}
}

function widthOffset() {
	switch (currentTextSize) {
		case "small":
			return 16;
		case "medium":
			return 21;
		case "large":
			return 27;
		default:
			return 21;
	}
}

// Toggle the folded state of a left tab.
function ToggleLeftTab(id) {
	var leftTabWidth = GetLeftTabWidth();
	id = unEscape(id);
	var tab = new DDiv(id);
	var tabshadow = new DDiv(id + "_shadow");
	var body = new DDiv(id + "_body");
	var bodyshadow = new DDiv(id  + "_body_shadow");
	var td = GetAncestorByTagName(tab.ddiv, "td");
	var table = GetAncestorByTagName(td, "table");
	
	if (tab.ddiv.offsetLeft == 0) {
		// Tab is closed - open it.
		// invoke "butfirst" script, if any
		var butfirst = tab.div.getAttribute("butfirst");
		if (butfirst != null) {
			eval(butfirst);
		}
		// invoke "instead" script, if any
		var instead = tab.div.getAttribute("instead");
		if (instead != null) {
			eval(instead);
			return;
		}
		// close the rest
		var otherTabs = GetDescendentsByTagNameAndClassName(td, "div", "tab");
		for (var i = 0; otherTabs != null && i < otherTabs.length; i++) {
			var otherId = otherTabs[i].id;
			var otherTab = new DDiv(otherId);
			if (otherId == id) {
				if (otherTab.iframe != null) {
					DoRemoveStyleClass(otherTab.iframe, "tab-inactive");
					DoAddStyleClass(otherTab.iframe, "tab-active");
				}
				DoRemoveStyleClass(otherTab.div, "tab-inactive");
				DoAddStyleClass(otherTab.div, "tab-active");
				continue;
			}
			if (otherTab.iframe != null) {
				DoRemoveStyleClass(otherTab.iframe, "tab-active");
				DoAddStyleClass(otherTab.iframe, "tab-inactive");
			}
			DoRemoveStyleClass(otherTab.div, "tab-active");
			DoAddStyleClass(otherTab.div, "tab-inactive");
			var otherBody = getElement(otherId + "_body");
			otherBody.style.display = "none";
			var otherTabShadow = getElement(otherId + "_shadow");
			otherTabShadow.style.display = "none";
			var otherBodyShadow = getElement(otherId + "_body_shadow");
			otherBodyShadow.style.display = "none";
			otherTab.Resize("0px", null, null, null);
		}
		var parent = tab.ddiv.parentNode;
		while (parent != null && parent.offsetLeft == 0) {
			parent = parent.parentNode;
		}
		
		tab.Resize("-" + leftTabWidth + "px",
			null,
			(isIE ? (tab.ddiv.offsetWidth - 1) : tab.ddiv.clientWidth) + "px",
			(isIE ? (tab.ddiv.offsetHeight - 2) : tab.ddiv.clientHeight) + "px");

		body.Resize("-" + (leftTabWidth + (isIE ? 5 : 1) - tab.ddiv.offsetWidth) + "px",
			"5px",
			(leftTabWidth - tab.ddiv.offsetWidth + widthOffset() - 0) + "px",
			table.clientHeight - 25 + "px");		

		var container = getElement(id + "_container");
		if (container != null) {
			container.style.top = parseInt(body.style.top) + 5 + "px";
			container.style.width = parseInt(body.style.width) - 30 + "px";
			container.style.height = parseInt(body.style.height) - 15 + "px";
		}

		bodyshadow.Resize(parseInt(body.style.left) + (BIB_SHADOW_LEFT_OFFSET - 0) + "px",
			parseInt(body.style.top) + (BIB_SHADOW_BOTTOM_OFFSET - 0) + "px",
			body.style.width,
			body.style.height);

		tabshadow.Resize(parseInt(tab.ddiv.offsetLeft) + (BIB_SHADOW_LEFT_OFFSET - 0) + "px",
			parseInt(tab.ddiv.offsetTop) + (BIB_SHADOW_BOTTOM_OFFSET - 0) + "px",
			parseInt(tab.ddiv.offsetWidth) + "px",
			parseInt(tab.ddiv.offsetHeight) + "px");
			
		body.ddiv.style.display = "";
		bodyshadow.ddiv.style.display = "";
		tabshadow.ddiv.style.display = "";
	} else {
		// Tab is open - close it.
		if (tab.iframe != null) {
			DoRemoveStyleClass(tab.iframe, "tab-active");
			DoAddStyleClass(tab.iframe, "tab-inactive");
		}
		DoRemoveStyleClass(tab.div, "tab-active");
		DoAddStyleClass(tab.div, "tab-inactive");
		
		tab.Resize("0px", null, null, null);
		
		body.ddiv.style.display = "none";
		tabshadow.ddiv.style.display = "none";
		bodyshadow.ddiv.style.display = "none";
	}
}

// If the floating window referred to by id is present, close it.
// If not, invoke command to display it.
function ToggleFloatingWindow(id, openCommand, closeCommand, doNotifyServer) {
	var command = null;
	id = unEscape(id);
	if (getElement(id) != null) {
		// close floating window
		command = closeCommand;
		DisplayDialog(id, false, doNotifyServer, false);
	} else {
		// open floating window
		command = openCommand;
		Busy(true);
	}
	if (command != null) {
		var url = new Url(LOADSCRIPT_URL);
		url.Append("command", command);
		url.Append("id", id);
		for (var i = 4; Number(i + 1) < arguments.length; i = Number(i) + 2) {
			url.Append(arguments[i], arguments[Number(i) + 1]);
		}
		LoadScript(url.toString());
	}
}

// Close a panel
function Close(id, doRemoveInnerHtml) {
	id = unEscape(id);
	var elem = getElement(id);
	if (doRemoveInnerHtml) {
		elem.innerHTML = null;
	}
	elem.style.display = "none";
}

// Toggle which of a ToggleButton's buttons shows
function ToggleButton(id) {
    var buttonContainer = getElement(unEscape(id));
    var buttons = buttonContainer.getElementsByTagName("BUTTON");
    for (var i = 0; i < buttons.length; i++) {
        var button = buttons[i];
        if (button.style != null && button.style.display == "none") {
            button.style.display = "";
        } else {
            button.style.display = "none";
        }
    }
}

// Open or close all panes in a control panel, or all but the active pane.
function OpenCloseAll(outerContainerId, doOpen, allButActive, toggleButtonId) {
	var outerContainer = getElement(unEscape(outerContainerId));
	if (outerContainer == null) {
		return;
	}
	var paneContainers = GetDescendentsByTagNameAndClassName(outerContainer, "div", "controlpanecontainer");
	for (var i = 0; i < paneContainers.length; i++) {
		var paneContainerId = paneContainers[i].id;
		var header = getElement(paneContainerId + "_header");
		var didOpen = false;
		var didClose = false;
		if (IsOpen(paneContainerId)) {
			if (!doOpen) {
				var activePaneHeaders = GetDescendentsByTagNameAndClassName(paneContainers[i], "div", "open");
				if (allButActive && activePaneHeaders.length > 0) {
					continue;
				}
				for (var j = 0; j < activePaneHeaders.length; j++) {
					// close open pane, if any
					var activePane = activePaneHeaders[j].parentNode;
					var paneHeaders = GetDescendentsByTagNameAndClassName(activePane, "div", "open");
					for (var k = 0; k < paneHeaders.length; k++) {
						if (activePaneHeaders[j].id == paneHeaders[k].id) {
							continue;
						}
						paneHeaders[k].onclick();
						break;
					}
				}
				header.onclick();
				didClose = true;
			}
		} else if (doOpen) {
			header.onclick();
			didOpen = true;
		}
	}
}

var OPEN = "open";
var CLOSED = "closed";

function TogglePane(containerId, paneId) {
	var container = getElement(unEscape(containerId));
	if (container == null) {
		errorReporter.Report("OpenClose.js", "TogglePane: containerId '" + containerId + "' not found.");
		return;
	}
	var panes = GetDescendentsByTagNameAndClassName(container, "div", "controlpane");
	for (var i = 0; i < panes.length; i++) {
		var pane = panes[i];
		var headerId = pane.id + "_header";
		var header = getElement(headerId);
		var bodyId = pane.id + "_body";
		var body = getElement(bodyId);
		if (pane.id == paneId) {
			var paneTitleString = GetTitle(paneId);
			var folder = GetAncestorByTagNameAndClassName(pane, "div", "controlpanecontainer");
			var folderTitleString;
			if (folder == null) {
				folderTitleString = GetTitle(container.id);
			} else {
				folderTitleString = GetTitle(folder.id);
			}
			if (body.style.display == "none") {
				DoRemoveStyleClass(header, CLOSED);
				DoAddStyleClass(header, OPEN);
				body.style.display = "";
				ShowContents(body, true);
				MarkPaneOpen(folderTitleString, paneTitleString, true);
			} else {
				DoRemoveStyleClass(header, OPEN);
				DoAddStyleClass(header, CLOSED);
				body.style.display = "none";
				ShowContents(body, false);
				MarkPaneOpen(folderTitleString, paneTitleString, false);
			}
		} else {
			DoRemoveStyleClass(header, OPEN);
			DoAddStyleClass(header, CLOSED);
			body.style.display = "none";
			ShowContents(body, false);
		}
	}
}

function GetTitle(id) {
	var title = getElement(id + "_title");
	var titleString;
	if (typeof title.innerText != "undefined" && title.innerText != null) {
		titleString = title.innerText;
	} else {
		titleString = title.innerHTML;
	}
	titleString = titleString.replace(/^\s*(.*)\s*$/, "$1");
	if (isMozilla) {
		var zwspat = new RegExp(String.fromCharCode('8203'), "g");
		titleString = titleString.replace(zwspat, "");
	}
	return titleString;
}

function MarkOpen(isOpen, doSaveFoldState, ids) {
	if (doSaveFoldState == null || doSaveFoldState == "NONE") {
		return;
	}
	var url = new Url(LOADSCRIPT_URL);
	url.Append("command", "markopen");
	url.Append("dosavefoldstatetype", doSaveFoldState);
	switch (doSaveFoldState) {
		case "ABSOLUTE":
			for (var i = 0; i < ids.length; i++) {
				url.Append("id", escape(ids[i]));
			}
			break;
		case "RELATIVE":
			for (var i = 0; i < ids.length; i++) {
				var id = GetTitle(ids[i]);
				url.Append("id", escape(id));
			}
			break;
	}
	url.Append("Open", isOpen);
	LoadScript(url.toString());
}

function MarkPaneOpen(container, pane, isOpen) {
	var url = new Url(LOADSCRIPT_URL);
	url.Append("command", "markpaneopen");
	url.Append("opencontainer", escape(container));
	if (pane != null) {
		url.Append("openform", escape(pane));
	}
	if (isOpen == null) {
		var bodyId = container + "_body";
		isOpen = IsVisible(bodyId, false);
	}
	url.Append("Open", isOpen);
	LoadScript(url.toString());
}