var titleButtons = new Array();
var contentElements = new Array();
var innerContentElements = new Array();
var contentInitialHeight = new Array();
var buttons = 0;
var showTimer = null;
var hideTimer = null;
var showIdOld = -1;
var showIdNew = -1;
var showId = -1;
var hideId = -1;
var speed = 1; /* lager is sneller */
var startId = false;
var step = 10;

 function getDivHeight( div ){
	if(div.offsetHeight) {
		return div.offsetHeight;
	}
	if (div.clip) {
		return div.clip.height;
	}
	return 0;
}

function registerTitleButton( id ){
	titleButtons[id] = document.getElementById("titlebutton" + id);
	buttons++;
	var button = titleButtons[id];
	if (startId){
		if(showId != id){
			button.style.background = 'url(/images/titlebutton.png)';
			button.setAttribute("onmouseover", "this.style.background = 'url(/images/titlebutton-hover.png)';");
			button.setAttribute("onmouseout", "this.style.background = 'url(/images/titlebutton.png)';");
		}
	} else {
		if(buttons == 1){
			showId = id;
		}
		if(buttons > 1){
			button.style.background = 'url(/images/titlebutton.png)';
			button.setAttribute("onmouseover", "this.style.background = 'url(/images/titlebutton-hover.png)';");
			button.setAttribute("onmouseout", "this.style.background = 'url(/images/titlebutton.png)';");
		}
	}
}

function registerContent( id ){
	if(navigator.appName != "Microsoft Internet Explorer"){
		contentElements[id] =  document.getElementById("content" + id);
		innerContentElements[id] =  document.getElementById("innercontent" + id);
	}
}

function registerIEContent(){
	if(navigator.appName == "Microsoft Internet Explorer"){
		buttons = 0;
		for( var id = 0; id < 7; id++ ){
			if( titleButtons[id] != null){
				buttons++;
			}
			if( document.getElementById("content" + id) != null ){
				contentElements[id] =  document.getElementById("content" + id);
				innerContentElements[id] =  document.getElementById("innercontent" + id);
			}
		}
	}
}

function changeActiveButton(){
	if(hideId != -1){
		titleButtons[hideId].style.background = 'url(/images/titlebutton.png)';
		titleButtons[hideId].setAttribute("onmouseover", "this.style.background = 'url(/images/titlebutton-hover.png)';");
		titleButtons[hideId].setAttribute("onmouseout", "this.style.background = 'url(/images/titlebutton.png)';");
	}
	titleButtons[showId].style.background = 'url(/images/titlebutton-active.png)';
	titleButtons[showId].removeAttribute("onmouseover");
	titleButtons[showId].removeAttribute("onmouseout");
}

function showContent( id, height, maxHeight ){
	height = height + step;
	contentElements[id].style.height = height + "px";
	if( height < maxHeight ){
		showTimer = setTimeout("showContent(" + id + "," + height + "," + maxHeight + ")",speed); 
	} else {
		if(navigator.appName != "Microsoft Internet Explorer"){
			contentElements[id].style.height = "auto";
			if (getDivHeight(contentElements[id]) != maxHeight){
				getDivHeight(innerContentElements[id]) = getDivHeight(contentElements[id]);
			}
		}
		if(titleButtons[id+1] == null && id+1 < contentElements.length ){
			showContent( id+1, 1, getDivHeight(innerContentElements[id+1]) ); 
			contentElements[id+1].style.visibility = "visible";		
		}
	}
}

function hideContent( startid, endid, height){
	height = height - step;
	if (height <= 1){
		height=1;
		if (startid != showId){
			contentElements[startid].style.visibility = "hidden";
			if (endid < startid){
				hideContent( startid-1, endid, getDivHeight(contentElements[startid-1])  );
			}
		}
	}
	contentElements[startid].style.height = height +"px";
	if( height > 1 &&  startid != showId){
		hideTimer = setTimeout("hideContent(" + startid + "," + endid + "," + height + ")",speed); 
	}
}


function show( id ){
	if (showId != id){
		clearTimeout(showTimer);

		hideId = showId;
		showId = id;

		changeActiveButton();
		showContent( showId, getDivHeight(contentElements[showId]), getDivHeight(innerContentElements[showId]) );
		contentElements[id].style.visibility = "visible";

		if(hideId != -1){
			var hideLastId = hideId;
			for ( var i = hideId+1; i < contentElements.length; i++){
				if (titleButtons[i] == null){
					hideLastId = i;
				} else {
					break;
				}
			}
			hideContent( hideLastId, hideId, getDivHeight(contentElements[hideLastId]));
		}
	} else {
		clearTimeout(showTimer);

		hideId = showId;
		showId = -1;

		titleButtons[hideId].style.background = 'url(/images/titlebutton.png)';
		titleButtons[hideId].setAttribute("onmouseover", "this.style.background = 'url(/images/titlebutton-hover.png)';");
		titleButtons[hideId].setAttribute("onmouseout", "this.style.background = 'url(/images/titlebutton.png)';");

		
		var hideLastId = id;
		for ( var i = id+1; i < contentElements.length; i++){
			if (titleButtons[i] == null){
				hideLastId = i;
			} else {
				break;
			}
		}
		hideContent( hideLastId, id, getDivHeight(contentElements[hideLastId]));
	}
} 
