
var openhashtable = new Array();
var closehashtable = new Array();
var openbox = null;

var animTimeout = null;

function searchtrough(arr,element){
	for( x in arr ){
		if(arr[x] == element){
			return x;
		}
	}
	return false;
}

function startAnimateText(that){
	closeOpened();
	openhashtable.push(that);
	that.childNodes[2].style.visibility = 'visible';

	if(i = searchtrough(closehashtable, that)){
		delete closehashtable[i];
	}

	if(!animTimeout){
		animTimeout = window.setTimeout('animateText()', 0);
	}
}

function startCloseText(that){
	openbox = null;
	closehashtable.push(that);
	if(!animTimeout){
		animTimeout = window.setTimeout('animateText()', 0);
	}
}

function closeOpened(){
	if(openbox){
		closehashtable.push(openbox); // close the open text, if there is one
	}
	delete openbox;
	for(x in openhashtable){ // start closing all open or currently opening textes
		closehashtable.push(openhashtable[x]);
	}
	openhashtable = new Array(); // reset hash table
}

function animateText(){
	remove = new Array();
	count = 0;
	for(block in openhashtable){
		if(!isNaN(block)){
			curHeight = parseInt(openhashtable[block].childNodes[2].style.height);
			curHeight += 10;
			if(curHeight < openhashtable[block].animMaxHeight){
				openhashtable[block].childNodes[2].style.height = curHeight+'px';
				count++;
			}
			else {
				openhashtable[block].childNodes[2].style.height = (curHeight+5)+'px'; 
				openhashtable[block].onclick = function(){startCloseText(this);};
				openbox = openhashtable[block];
				remove.push(block); // mark for removal
			}
		}
	}

	for( i in remove ){
		if(!isNaN(i)){
			delete openhashtable[remove[i]];
		}
	}
	remove = new Array();

	for(block in closehashtable){
		if(!isNaN(block)){
			curHeight = parseInt(closehashtable[block].childNodes[2].style.height);
			curHeight -= 10;
			if(curHeight > 0){
				closehashtable[block].childNodes[2].style.height = curHeight+'px';
				count++;
			}
			else {
				closehashtable[block].onclick = function(){startAnimateText(this);};
				closehashtable[block].childNodes[2].style.visibility = 'hidden';
				remove.push(block); // mark for removal
			}
		}
	}

	for( i in remove ){
		if(!isNaN(i)){
			delete closehashtable[remove[i]];
		}
	}

	if(count > 0){
		animTimeout = window.setTimeout('animateText()', 0);
	}
	else {
		animTimeout = null;
	}

}
