// globals ---------------------------------------------------------------------------

var transition = 0;
var countmax = 10
var countdown = countmax;
var speed = 20; // millseconds

// -----------------------------------------------------------------------------------

function getAsNumber (astring) {
	astring = String(astring);
	var count = (arguments.length > 1) ? arguments[1] - 1 : 0;
	var matches = astring.match(/[0-9\-]+/g);
	if (!matches || (count > matches.length))
		return 0;
	return Number(matches[count]);
}

// -----------------------------------------------------------------------------------

function GetHeight (id, correction) { // from sum of child elements
	var ul = document.getElementById(id);
	if (ul) {
		var kids = ul.childNodes;
		var height = 0;
		for (var i = 0; i < kids.length; i++) {
			if (kids[i].nodeType == 1) {
				height += Math.max(kids[i].clientHeight, kids[i].offsetHeight) + correction;
			}
		}	
		return height;
	} else
		return 0;
}

// -----------------------------------------------------------------------------------

function Menu (id) {
	if (countdown == countmax) {
		var ul = document.getElementById(id);
		if (ul.style.display != 'none') {
			dimension = Math.max(ul.clientHeight, ul.offsetHeight);
			transition = setInterval('CloseUp("' + id + '", ' + dimension + ')', speed);
		} else {
			ul.style.height = '0px';
			ul.style.display = 'block';
			dimension = GetHeight(id, 0);
			transition = setInterval('OpenUp("' + id + '", ' + dimension + ')', speed);
		}
	}
}

// -----------------------------------------------------------------------------------

function OpenUp (id, dimension) {
	countdown--;
	if (countdown <= 0) {
		var parent = document.getElementById(id).parentNode;
		parent.className = parent.className.replace(/collapsed/, 'expanded');
		document.getElementById(id).style.height = 'auto';
		clearInterval(transition);
		countdown = countmax;
	} else {
		document.getElementById(id).style.height = Math.round(dimension * (countmax - countdown) / countmax) + 'px';
	}
}

// -----------------------------------------------------------------------------------

function CloseUp (id,  dimension) {
	countdown--;
	if (countdown <= 0) {
		var parent = document.getElementById(id).parentNode;
		parent.className = parent.className.replace(/expanded/, 'collapsed');
		document.getElementById(id).style.display = 'none';
		clearInterval(transition);
		countdown = countmax;
	} else {
		document.getElementById(id).style.height = Math.round(dimension * countdown / countmax) + 'px';
	}
}

// -----------------------------------------------------------------------------------

function Concertina (id) {
	if (countdown > 0) {
		var div = document.getElementById(id);
		if (div.style.display != 'none')
			return;
		var opendiv = null;
		var kids = div.parentNode.childNodes; 
		for (var i = 0; i < kids.length; i++) {
			if ((kids[i].nodeType == 1) && (kids[i].nodeName.toLowerCase() == 'div') && (kids[i].style.display != 'none')) {
				opendiv = kids[i];
				break;
			}
		}
		dimopendiv = Math.max(opendiv.clientHeight, opendiv.offsetHeight);
		div.style.height = '0px';
		div.style.display = 'block';
		dimdiv = GetHeight(id, 5);
		transition = setInterval('OpenClose("' + id + '", ' + dimdiv + ', "' + opendiv.id + '", ' + dimopendiv + ')', speed);
	}
}

// -----------------------------------------------------------------------------------

function OpenClose (idopen, dimopen, idclose, dimclose) {
	countdown--;
	if (countdown <= 0) {
		document.getElementById(idopen).style.height = 'auto';
		document.getElementById(idclose).style.display = 'none';
		clearInterval(transition);
		countdown = countmax;
	} else {
		document.getElementById(idopen).style.height = Math.round(dimopen * (countmax - countdown) / countmax) + 'px';
		document.getElementById(idclose).style.height = Math.round(dimclose * countdown / countmax) + 'px';
	}
}

// -----------------------------------------------------------------------------------
