var lock = false;
var timeout;
var opened = {};

function animate(item, second) {
	var el = document.getElementById(items[item.mark].content);
	var height = el.clientHeight;
	lock = true;
	if (item.shrink) {
		if (height <= items[item.mark].speed) {
			lock = false;
			clearTimeout(timeout);
			item.shrink = false;
			el.style.display = 'none';
			//el.style.visibility = "hidden";
			if(opened == item) opened = {};
			el.style.height = '0px';
			if (second) {
				document.getElementById(items[second.mark].content).style.display = "block";
				//document.getElementById(items[second.mark].content).style.visibility = "visible";
				timeout = setTimeout(function() { animate(second); }, items[second.mark].time);
			}
			return;
		}
		height -= items[item.mark].speed;
		el.style.height = height + 'px';
	} else {
		if (height >= item.content_height) {
			lock = false;
			clearTimeout(timeout);
			item.shrink = true;
			opened = item;
			return;
		}
		height += items[item.mark].speed;
		el.style.height = height + 'px';
	}
	timeout = setTimeout(function() { animate(item, second); }, items[item.mark].time);
}

function slide(e) {
	var el = this;
	if (lock || (typeof items[el.mark] == 'undefined')) return;
	if (!el.shrink && el.slide_group && el.slide_group == opened.slide_group) {
		timeout = setTimeout(function() { animate(opened, el); }, items[el.mark].time);
	} else {
		document.getElementById(items[el.mark].content).style.display = "block";
		//document.getElementById(items[el.mark].content).style.visibility = "visible";
		timeout = setTimeout(function() { animate(el); }, items[el.mark].time);
	}
}

function initSlideShowHide() {
	// hook onclick on button divs
	for(var i in items) {
		var button  = document.getElementById(items[i].button);
		var content = document.getElementById(items[i].content);
		if (button) {
			button.onclick = slide;
			button.mark = i;
			button.shrink = false;
			if (items[i].group) button.slide_group = items[i].group;
			button.content_height = content.offsetHeight;
			if (!items[i].open) {
				content.style.height = '0px';
				content.style.display = 'none';
				//content.style.visibility == 'hidden';
			} else {
				opened = button;
				button.shrink = true;
			}
		}
	}
}
