var numOfTopics = 1;

//var contentTop = 40;
//var contentHeight = 400;
var contentWidth = 550

var scrollbarHeight = 6;
var scrollArrowHeight = 20;
var showScrollX = new Array;
showScrollX['content'] = 0;
showScrollX['navig'] = 0;

var NS = (document.layers) ? 1:0;
var IE = (document.all) ? 1:0;
var W3C = (document.getElementById) ? 1:0;
//alert ("NS:" + NS + " W3C:" + W3C + " IE:"+IE); 
var h = new Array;
var w = new Array;

//h['navig'] = 0;

var scrollInt;
var isScrolling;

var lastpage;

var firstpage = "";
var firstnavpage = "";
 
function loadPage(name,url,fromnavig) {
	stopScroll();

	if (fromnavig == 'setpage') {
		firstpage = url;
	}
	if (W3C) {
		hiddenC = window.document.getElementById('ns6'+name);
		myDiv = window.document.getElementById(name);
		if (hiddenC.src != url) {
			myDiv.innerHTML = "";
			if (url) {
				myDiv.innerHTML = "<span class='loading'>loading page ...</span>";
			}
			if (name == 'content') {
				//hideLayer('linered');
				lastpage = hiddenC.src;
				if (fromnavig == '') {
					hideLayer('backbutton');
				}
				if (url.indexOf('Document') != -1) {
					//showLayer('linered');
				}
			}
			hiddenC.src = url;
		}
		resetDiv(name);
/*
		if (IE) {
			//hx = window.document.all['content'].style.offsetHeight;
			//wx = window.document.all['content'].style.width;
			hx = hiddenC.offsetHeight;
			//alert ("H: " + hx + " .... oder " + h[name]);
		}
*/
	} else if (IE) {
		// hierher hoffentlich nicht :-)
//		alert ("IE");
	} else if (NS) {
		// alert ("Loading " + url + " in " + name);
		myDiv = window.document.layers[name];
		if (myDiv.src != url) {
			if (name == 'content') {
//				hideLayer('backbutton');
//				hideLayer('linered');
				lastpage = myDiv.src;
				if (fromnavig == '') {
					hideLayer('backbutton');
				}
				if (url.indexOf('Document') != -1) {
//					showLayer('linered');
				}
			}
			myDiv.src = url;
		}
		resetDiv(name);
/*
		myDiv.top = contentTop;
		myDiv.height = contentHeight;
		myDiv.clip.top = 0;
		myDiv.clip.bottom = contentHeight;

		window.document.layers[name+'BarX'].top = contentTop+scrollArrowHeight;	
*/

	}
}

function printPage() {
	if (W3C) {
		hiddenC = window.document.getElementById('ns6content');
		myUrl = hiddenC.src;
		myUrl = myUrl.replace(/style=\w.+;/, "");
		myUrl = myUrl.replace(/style=;/, "");
		myUrl = myUrl.replace(/;;/, ";");
		if (myUrl.indexOf('?') == -1) {
			myUrl += "?x=1";
		}
		myUrl += ";style=print";
	}
	if (NS) {
		myDiv = window.document.layers['content'];
        myUrl = myDiv.src;
		myURL = myURL.replace(/style=\w*\.?;/, "");
		myUrl = myUrl.replace(/;;/, ";");
		if (myUrl.indexOf('?') == -1) {
			myUrl += "?x=1";
		}
		myUrl += ";style=print";
	}
	printWin = window.open(myUrl, "printWin", "width=580,height=300");
}

function vars (el) {
	w = window.open("about:blank", "_blank");
	var my_div = document.getElementById(el);
	for (var i in my_div)
    	w.document.write(i + " = " + my_div[i] + "<br>");

}
/*
function copyHtml () {
//		var x = document.getElementById('ns6content').document.innerHTML;
//		document.getElementById('content').innerHTML = document.getElementById('hiddencontent').innerHTML;
		
		//alert ("copy!!" + x);
}
*/
function Init() {

	// hide Layers
	myLay = new Array('content', 'navig');
	for  (var i=0; i<myLay.length; i++) {
		hideLayer(myLay[i] + 'BarX');
		hideLayer(myLay[i] + 'ScrollX');
//		if (NS) {
//			getDocSize(myLay[i]);
//		}
	}
	hideLayer('backbutton');
//	hideLayer('linered');
//	hideLayer('themenpulldown');

	// load first page
	if (firstpage != "") {
//		alert ("Loading: " + firstpage);
		loadPage('content',firstpage);
	}
	if (firstnavpage != "") {
//		alert ("Loading Navig: " + firstnavpage);
		if (NS) {
			setTimeout("loadPage('navig','"+firstnavpage+"');", 500);
		} else {
			loadPage('navig',firstnavpage);
		}
	}

	if (NS) {
//		document.layers['themenpulldown'].visbility = "hide";
//		getDocSize('content');
//		getDocSize('navig');
	}



	// hoehe des pulldowns
	if (W3C) {
		//pdheight['themenpulldown'] = parseInt(window.document.getElementById('themenpulldown').style.height);
		//pdheight['ausstellungsnavig'] = parseInt(window.document.getElementById('ausstellungsnavig').style.height);
//		alert (pdheight);
	}	
}

function getDocSize(name) {
	if (W3C) {
		// h + w are set by child-page

	}
	if (NS) {


//		h[name] = window.document.layers[name].document.layers[name + "Y"].height;
		w[name] = window.document.layers[name].document.width;
		//alert("SIZE von " + name + ": " + h[name]); 
		//if (showScrollX[name]==0 && h[name] > contentHeight) {
		if (h[name] > contentHeight) {
			window.document.layers[name + 'ScrollX'].visibility = "show";
			window.document.layers[name + 'BarX'].visibility = "show";
			showScrollX[name] = 1;
		} 
		if (showScrollX[name]==1 && h[name] < contentHeight) {
			window.document.layers[name + 'ScrollX'].visibility = "hide";
			window.document.layers[name + 'BarX'].visibility = "hide";
			showScrollX[name] = 0;
		}
	//	resetDiv(name);
		setTimeout("getDocSize('" + name + "')", 6000);
	}


}

function resetDiv(name) {
	if (W3C) {
//		h = height;
//		w = width;

		myDiv = window.document.getElementById(name);
		myDiv.style.top = contentTop + "px";
		myDiv.style.height = contentHeight + "px";
		myDiv.style.clip = "rect(0px " + contentWidth + "px " + contentHeight + "px 0px)";

		scrollBarX = document.getElementById(name+'BarX').style;  
		scrollBarX.top = (contentTop+scrollArrowHeight) + "px";

		if (showScrollX[name] == 0 && h[name] > contentHeight) {
//			alert ("Zeigen " + name + " weil " + h[name]);
			scrollBarX.visibility = "visible";
			document.getElementById(name+'ScrollX').style.visibility = "visible";
			showScrollX[name] = 1;
		}
		if (showScrollX[name] == 1 && h[name] < contentHeight) {
//			alert ("Verstecken " + name + " weil " + h[name]);
			scrollBarX.visibility = "hidden";
			document.getElementById(name+'ScrollX').style.visibility = "hidden";
			showScrollX[name] = 0;
		}
	} else {
		//alert ("Reset DIV" + h[name]);
		/* TODO */
/*
		h[name] = window.document.layers[name].document.height;
		w[name] = window.document.layers[name].document.width;
*/
		myDiv = window.document.layers[name];
		myDiv.top = contentTop;
		myDiv.clip.top = 0;
		myDiv.clip.bottom = contentHeight;

		scrollBarX = document.layers[name+'BarX'];  
		scrollBarX.top = (contentTop+scrollArrowHeight);
/*
		//alert ("H:" + h[name]);
		if (showScrollX[name] == 0 && h[name] > contentHeight) {
			alert ("Zeigen");
			scrollBarX.visibility = "show";
			document.layers[name+'ScrollX'].visibility = "show";
			showScrollX[name] = 1;
		}
		if (showScrollX[name] == 1 && h[name] < contentHeight) {
			alert ("Verstecken " + name + " weil " + h[name]);
			scrollBarX.visibility = "hide";
			document.layers[name+'ScrollX'].visibility = "hide";
			showScrollX[name] = 0;
		}
*/
	}
}

function scroll(name,x,y) {
	if (W3C) {
		if (x != 0) {
			myDiv = window.document.getElementById(name);
			var currentTop = parseInt(myDiv.style.top);
			var currentHeight = parseInt(myDiv.style.height);
			if ( ( x > 0 && currentTop < contentTop )
				|| ( x < 0 && currentTop+h[name] > contentTop+contentHeight) ) {

				// move content clip
				var clip = myDiv.style.clip;
				var clipTop = parseInt(clip.split('(')[1]) - x;
				var clipBottom = parseInt(clip.split(' ')[2]) - x;
				
				myclip = 'rect(' + clipTop + 'px ' + contentWidth + 'px ' + clipBottom + 'px 0px)';
				myDiv.style.clip = myclip;
			
				// move content
				myDiv.style.top = (currentTop + x) + "px";
				myDiv.style.height = (currentHeight - x ) + "px";



				// move scrollbar
				scrollBarX = document.getElementById(name + "BarX").style;	
				var sbTop = parseInt(scrollBarX.top);
				var toScroll = h[name] - contentHeight;
				var scrollSteps = toScroll / x;
				var sBtoScroll = contentHeight - 2*scrollArrowHeight - scrollbarHeight;
				var sbStepPxl = sBtoScroll/scrollSteps;
				if (sbStepPxl < 0) { sbStepPxl = 0-sbStepPxl }	
				maxsbb = contentTop+contentHeight-scrollArrowHeight;
//				alert ("Height: " + h[name] + "  TOP: " + sbTop + " to Scroll: " + toScroll + "  scrollSteps: " + scrollSteps + " sBtoScroll : " + sBtoScroll + "sbStepPxl " + sbStepPxl + " max " + maxsbb );
			
				var myStep = (sbTop-contentTop-scrollArrowHeight)/sbStepPxl;
				if (x < 0) {
					myStep = parseInt(myStep + 1.5);
				} else {
					myStep = parseInt(myStep - 0.5);
				}
				newTop = parseInt(contentTop+scrollArrowHeight + myStep*sbStepPxl + 0.5);
//				alert ("STEP: " + myStep + " new TOP: " + newTop);
				if (x < 0) {	
					if (newTop + scrollbarHeight < maxsbb) {
						scrollBarX.top = newTop + "px";
					} else {
						scrollBarX.top = (contentTop+contentHeight-scrollbarHeight-scrollArrowHeight) + "px";
					}
				}
				if (x > 0) {
					if (newTop > contentTop+scrollArrowHeight) {
						scrollBarX.top = newTop + "px";
					} else {
						scrollBarX.top = (contentTop+scrollArrowHeight) + "px";
					}
				}	
			} else {
				stopScroll();
			}
		}
	}
	if (NS) {
		if (x != 0) {
			myDiv = window.document.layers[name];
			if ( ( x > 0 && myDiv.top < contentTop )
				|| ( x < 0 && myDiv.top+h[name] > contentTop+contentHeight) ) {
				myDiv.top += x;
				myDiv.clip.top -= x;
				myDiv.clip.bottom -= x;
//				alert ("Vorher: " + myDiv.clip.bottom);
//				myDiv.clip.bottom = "500"; //myDiv.clip.top + contentHeight;
//				myDiv.clip.height = 500;
//				alert ("Nachher: " + myDiv.clip.bottom);
//			alert (myDiv.clip.height);


				// move scrollbar
				scrollBarX = window.document.layers[name + "BarX"];

				var sbTop = parseInt(scrollBarX.top);
				var toScroll = h[name] - contentHeight;
				var scrollSteps = toScroll / x;
				var sBtoScroll = contentHeight - 2*scrollArrowHeight - scrollbarHeight;
				var sbStepPxl = sBtoScroll/scrollSteps;
				if (sbStepPxl < 0) { sbStepPxl = 0-sbStepPxl }	
				maxsbb = contentTop+contentHeight-scrollArrowHeight;
//				alert ("Height: " + h[name] + "  TOP: " + sbTop + " to Scroll: " + toScroll + "  scrollSteps: " + scrollSteps + " sBtoScroll : " + sBtoScroll + "sbStepPxl " + sbStepPxl + " max " + maxsbb );
			
				var myStep = (sbTop-contentTop-scrollArrowHeight)/sbStepPxl;
				if (x < 0) {
					myStep = parseInt(myStep + 1.5);
				} else {
					myStep = parseInt(myStep - 0.5);
				}
				newTop = parseInt(contentTop+scrollArrowHeight + myStep*sbStepPxl + 0.5);
//				alert ("STEP: " + myStep + " new TOP: " + newTop);
				if (x < 0) {	
					if (newTop + scrollbarHeight < maxsbb) {
						scrollBarX.top = newTop;
					} else {
						scrollBarX.top = (contentTop+contentHeight-scrollbarHeight-scrollArrowHeight);
					}
				}
				if (x > 0) {
					if (newTop > contentTop+scrollArrowHeight) {
						scrollBarX.top = newTop;
					} else {
						scrollBarX.top = (contentTop+scrollArrowHeight);
					}
				}	


				// scrollbar
/*
				scrollBarX = window.document.layers[name + "BarX"];
				
				sD = x * contentHeight / ( h[name] - contentHeight + scrollbarHeight + 2*scrollArrowHeight);
				sbb = scrollBarX.top+scrollbarHeight
				maxsbb = contentTop+contentHeight-scrollArrowHeight;
//				alert ("SD: " + sD + "  sbb " + sbb + " max " + maxsbb );
			
				if (x < 0) {	
					if (sbb-sD < maxsbb) {
						scrollBarX.top -= sD;
					} else {
						scrollBarX.top = contentTop+contentHeight-scrollbarHeight-scrollArrowHeight;
					}
				}
				if (x > 0) {
					if (sbb-sD > contentTop+scrollArrowHeight+scrollbarHeight) {
						scrollBarX.top -= sD;
					} else {
						scrollBarX.top = contentTop+scrollArrowHeight;
					}

				}
//				alert ("SD: " + sD + "  sbX: " + scrollBarX.top);

				//scrollPosRel = ( ( myDiv.clip.top )/h);
				//myTop = contentTop + (contentHeight*scrollPosRel);
				//alert ("height: " + h[name] + " rel: " + scrollPosRel*100 + " " + myTop); 
				//scrollBarX.top = parseInt(myTop);
*/
			} else {
				stopScroll();
			}
		}
	}
}

function startScroll(name,x) {
	var inter = 50;
	if (isScrolling == 0) {
		isScrolling = 1;
		scrollInt = setInterval("scroll('" + name + "'," + x + ",0)", inter); 
	}
}

function stopScroll() {
	if (scrollInt) {
		clearInterval(scrollInt);
		scrollInt = null;
	}
	isScrolling = 0;
}

function showLayer(name) {
	if (W3C) {
		if (document.getElementById(name)) {
			document.getElementById(name).style.visibility = "visible";
		}
	}
	if (NS) {
		//alert("Show:" + name);
		document.layers[name].visibility = "show";
	}
}

function hideLayer(name) {
	if (W3C) {
		if (document.getElementById(name)) {
			document.getElementById(name).style.visibility = "hidden";
		}
	}
	if (NS) {
//		alert("Hide:" + name);
		document.layers[name].visibility = "hide";
	}
}

function goback() {
//	loadPage('content', lastpage);
	loadPage('content', firstpage);
//	if (lastpage == firstpage) {
		hideLayer('backbutton');
//	}
}

var pulldown = new Array();
pulldown['ausstellungsnavig'] = 0;
pulldown['themenpulldown'] = 0;
//var pdlineheight = ( navigator.appName.indexOf('Win') != -1 ) ? 40 : 23;
var pdheight = new Array();
pdheight['themenpulldown'] = 500;
pdheight['ausstellungsnavig'] = 150;
// alert (pdlineheight);
// alert(pdheight['ausstellungsnavig']);
function hidePullDown(e) {
	if (pulldown['themenpulldown'] == 1) {
		var x = (window.event) ? window.event.x : e.pageX;
		var y = (window.event) ? window.event.y : e.pageY;
//		window.status = 'x=' + x + '; y=' + y;
	//	if (x > 235 || y > (numOfTopics*pdlineheight + 20) ) {
		if (x > 300 || y > pdheight['themenpulldown'])  {
			hideLayer('themenpulldown');
			pulldown['themenpulldown'] = 0;
		}
	}
	if (pulldown['ausstellungsnavig'] == 1) {
		var x = (window.event) ? window.event.x : e.pageX;
		var y = (window.event) ? window.event.y : e.pageY;
//		window.status = 'x=' + x + '; y=' + y + '; pdh=' + pdheight['ausstellungsnavig'];
		if (x < 480 || x > 580 || y > pdheight['ausstellungsnavig'])  {
			hideLayer('ausstellungsnavig');
			pulldown['ausstellungsnavig'] = 0;
		}
	}
	return true ;
}
document.onmousemove = hidePullDown;



function showPullDown(name) {
	if (!name) {
		name = "themenpulldown";
	}
	pulldown[name] = 1;
	showLayer(name);
}

/*
document.ommousemove = hidePullDown;
function hidePullDown(e) {
	var x = (window.event) ? window.event.x : e.pageX;
   	var y = (window.event) ? window.event.y : e.pageY;
	if (pulldown == 1) {
		if (x > 200 || y > 300) {
			hideLayer('themenpulldown');
			pulldown = 0;
		}
	}
	alert("X: " + x + " Y: " + y);
}
*/

if (NS) {
//	window.onresize = document.location.reload();
}


