
if ( typeof(MsDB) == 'undefined' ) {
  MsDB = {};
}

//*** toggle functions: ***

function showDiv(idName) {
	document.getElementById (idName).style.display = 'block';
}
  
function hideDiv(idName) {
	document.getElementById(idName).style.display = 'none';
}
  
function toggleDiv(idName) {
	var e = document.getElementById(idName);
	if(e) {
		if(e.style.display=="none"){
			e.style.display="block"
		} else {
			e.style.display="none"
		}
	}
}

//*** Monosaccharide Builder functions: ***

function getFirstKetoPos() {
	var msSize = document.getElementById('size').value;
	for(var i = 1; i <= msSize; i++) {
		var status = getPositionStatus(i);
		if(status == "o") {
			return i;
		}
		if(status == "k") {
			return i;
		}
	}
	return 0;
}

function getMsSize() {
	return document.getElementById('size').value;
}

function quickSettings() {
	var settingsSelect = document.getElementById('quickset');
	var msSize = getMsSize;
	if(settingsSelect != null) {
		var selectValue = settingsSelect.options[settingsSelect.selectedIndex].value;
		var startSelect = document.getElementById('rs');
		var currentStart = parseInt(startSelect.options[startSelect.selectedIndex].value);
		var start = 0;
		var end = 0;
		var anomeric;
		var setring = false;
		if(selectValue == "D-L") {
			invertStereochemistry();
		} else if(selectValue == "o:o") {
			setring = true;
			//setRing(-1, -1, "o");
			start = -1;
			end = -1;
			if(currentStart > 1) {
				anomeric = "k";
			} else {
				anomeric = "o";
			}
			//alert("start: " + currentStart + " anomeric: " + anomeric);
		} else {
			if(currentStart > 0) {
				start = currentStart;
			} else {
				start = getFirstKetoPos();
			}
			if(start == 0) {
				start = 1;
			}
			
			if(selectValue == "p:a") {
				setring = true;
				end = start + 4;
				if(end > msSize) {
					return;
				}
				anomeric = "2";
			} else if(selectValue == "p:b") {
				setring = true;
				end = start + 4;
				if(end > msSize) {
					return;
				}
				anomeric = "1";
			} else if(selectValue == "f:a") {
				setring = true;
				end = start + 3;
				if(end > msSize) {
					return;
				}
				anomeric = "2";
			} else if(selectValue == "f:b") {
				setring = true;
				end = start + 3;
				if(end > msSize) {
					return;
				}
				anomeric = "1";
			}
		}
		if(setring) {
			setRing(start, end, anomeric);
		}
	}
}

function setRing(start, end, anomer) {
	var rs = document.getElementById('rs');
	var re = document.getElementById('re');
	var currentStart = parseInt(rs.options[rs.selectedIndex].value);
	var currentEnd = rs.options[rs.selectedIndex].value;
	if(start == -1) { //*** open chain, set stereocode of current start to keto ***
		if(currentStart > 0) {
			setPositionStatus(currentStart, anomer);
		}
	} else {
		setPositionStatus(start, anomer);
	}
	setSelectOption(rs, start);
	setSelectOption(re, end);
}

function getPositionStatus(pos) {
	var bbpos = document.getElementById('bbpos' + pos);
	if(bbpos != null) {
		return bbpos.options[bbpos.selectedIndex].value;
	}
	return "";
}

function setSelectOption(selectObj, value) {
	if(selectObj != null) {
		for(var i = 0; i < selectObj.length; i++) {
			if(selectObj.options[i].value == value) {
				selectObj.options[i].selected = true;
				return;
			}
		}
	}
}

function setPositionStatus(pos, value) {
	var bbpos = document.getElementById('bbpos' + pos);
	if(bbpos != null) {
		setSelectOption(bbpos, value);
	}
}

function invertStereochemistry() {
	var msSize = getMsSize();
	for(var i = 1; i <= msSize; i++) {
		if(getPositionStatus(i) == 1) {
			setPositionStatus(i, 2);
		} else if(getPositionStatus(i) == 2) {
			setPositionStatus(i, 1);
		}
	}
}

//*** MochiKit menu functions: ***

MsDB.windowLoadScript = function() {
    MsDB._setupSectionToggles();
}

MsDB._setupSectionToggles = function() {
    var md = MochiKit.DOM;
    var section_toggles = md.getElementsByTagAndClassName('div','section_toggle', document);
    for (var a_toggle_idx in section_toggles) {
        var a_toggle = section_toggles[a_toggle_idx];
        var section_toggle_head = md.getElementsByTagAndClassName('*','hd',a_toggle)[0];
        var section_toggle_body = md.getElementsByTagAndClassName('div','bd',a_toggle)[0];
        if ( ! section_toggle_head && ! section_toggle_body ) {
            log.error("Missing head or body element in toggle. Skipping");
            continue;
        }
        section_toggle_body.style.display = 'none';
        connect(section_toggle_head,'onclick',function() {
		    if (section_toggle_body.style.display == 'none') {
		      blindDown(section_toggle_body,{'duration': 0.25});
		    } else {
		      blindUp(section_toggle_body,{'duration': 0.25});
		    }
        });        
    }
}



