window.onresize = adaptPositions;
window.onload = setStoryBoxHeight;

function adaptPositions() {
	setStoryBoxHeight();
	centerComments();
}

function getViewportHeight() {
	// mozilla/netscape/opera/IE7
	if (typeof window.innerWidth != 'undefined')
	{
		viewportheight = window.innerHeight;
	}
	// IE6  - standards compliant mode
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
	{
		viewportheight = document.documentElement.clientHeight;
	}
	// ältere IE und andere
	else
	{
		viewportheight = document.getElementsByTagName('body')[0].clientHeight;
	}
	return viewportheight;
}

function getViewportWidth() {
	// mozilla/netscape/opera/IE7
	if (typeof window.innerWidth != 'undefined')
	{
		viewportwidth = window.innerWidth;
	}
	// IE6  - standards compliant mode
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
	{
		viewportwidth = document.documentElement.clientWidth;
	}
	// ältere IE und andere
	else
	{
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
	}
	return viewportwidth;
}

function getPositionX(container){
	var leftValue= 0;
	while(container){
		leftValue += container.offsetLeft;
		container = container.offsetParent;
	}
	return leftValue;
}

function getPositionY(container){
	var topValue= 0;
	while(container){
		topValue+= container.offsetTop;
		container= container.offsetParent;
	}
	return topValue;
}

function isIE()
{
  return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

function setStoryBoxHeight() {
	storyheight = getViewportHeight()-300;
	if (storyheight > 375) {
		document.getElementById('stories').style.height = storyheight+'px';
	}
	else {
		document.getElementById('stories').style.height = '';
	}
}

function fadeInLayer(layername,opacity) {
	maxopacity = 0.5;

	if (opacity == null) {
		opacity = 0;
		if (navigator.userAgent.indexOf('MSIE') > -1) {
			document.getElementById(layername).style.filter = 'alpha(opacity=0)';
		} else {
			document.getElementById(layername).style.opacity = 0;
		}
	}
	document.getElementById(layername).style.visibility='visible';

	opacity = (Math.round(opacity * 10000) / 10000) + 0.1;
	if (navigator.userAgent.indexOf('MSIE') > -1) {
		document.getElementById(layername).style.filter = 'alpha(opacity=' + (opacity * 100) + ')';
	} else {
		document.getElementById(layername).style.opacity = opacity;
	}

	if (opacity <= maxopacity) {
		setTimeout('fadeInLayer(\''+layername+'\','+opacity+');', 50);
	}
}

function fadeOutLayer(layername,opacity) {
	maxopacity = 0.5;

	if (opacity == null) opacity = maxopacity;
	opacity = (Math.round(opacity * 10000) / 10000) - 0.1;
	if (navigator.userAgent.indexOf('MSIE') > -1) {
		document.getElementById(layername).style.filter = 'Alpha(opacity=' + (opacity * 100) + ')';
	} else {
		document.getElementById(layername).style.opacity = opacity;
	}

	if (opacity > 0) {
		setTimeout('fadeOutLayer(\''+layername+'\','+opacity+');', 50);
	}
	else
	{
		document.getElementById(layername).style.visibility='hidden';	
	}
}

function showVideoLayer(includeFile) {
	fadeInLayer('VideoLayer');
	document.getElementById('VideoBox').style.visibility='visible';
	getHttpRequest('videolayer.asp?'+includeFile,'VideoBox'); 
}

function hideVideoLayer() {
	if (document.getElementById('FlashVideo')) {
		if (navigator.mimeTypes && navigator.mimeTypes.length != 0) {
			if (navigator.mimeTypes["application/x-shockwave-flash"]) {
				try {
					document.getElementById('FlashVideo').SetVariable("player:jsPause", "");
				}
				catch (e) {	
				}
			}
		}
	}
	document.getElementById('VideoBox').style.visibility='hidden';
	fadeOutLayer('VideoLayer');
	document.getElementById('VideoBox').innerHTML = ''; 
}

function getHttpRequest(url,div) { 
	
nocacheurl = url.concat(/\?/.test(url) ? "&" : "?", "noCache=", (new Date).getTime(), ".", Math.random()*1234567);

var xmlhttp = null; 
// Mozilla 
if (window.XMLHttpRequest) { 
	xmlhttp = new XMLHttpRequest(); 
} 
// IE 
else if (window.ActiveXObject) { 
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.open("GET", nocacheurl, true);
xmlhttp.onreadystatechange = function() { 
	if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
		document.getElementById(div).innerHTML = xmlhttp.responseText; 
	} 
} 
xmlhttp.send(null); 
} 

function PersonenPopUp(PersID){
	var PopUpUrl = ""
	PopUpUrl = 'PersInfo.asp?PersID=' + PersID;
	window.open(PopUpUrl, 'PersonInfo', 'width=300,height=300,left=500,top=50,scrollbars=yes');
}

function SubmitComment(BeitragID) {
	var params = "Beitrag="+BeitragID+"&Video="+document.getElementById('Kommentar_Video').value+"&Name="+escape(document.getElementById('Kommentar_Name').value)+"&Email="+escape(document.getElementById('Kommentar_Email').value)+"&Kommentar="+escape(document.getElementById('Kommentar').value)+"&Frage="+document.getElementById('Kommentar_Frage').value+"&Antwort="+document.getElementById('Kommentar_Antwort').value+"&noCache="+(new Date).getTime()+"."+Math.random()*1234567;
	var xmlhttp = null;
	// Mozilla 
	if (window.XMLHttpRequest) { 
		xmlhttp = new XMLHttpRequest(); 
	} 
	// IE 
	else if (window.ActiveXObject) { 
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
	} 
	xmlhttp.open("POST", "comment.asp", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			response = xmlhttp.responseText;
			if (response == "CommentOK!"){
				alert("Vielen Dank für Ihren Kommentar! Er wird in Kürze auf der Seite des Beitrags veröffentlicht werden.");
				commentSlideOut();
				document.getElementById('Kommentar_Name').value = "";
				document.getElementById('Kommentar_Email').value = "";
				document.getElementById('Kommentar_Video').value = 0;
				document.getElementById('Kommentar').value = "";
				document.getElementById('Kommentar_Antwort').selectedIndex = 0;
				if (document.getElementById('VideoBox').style.visibility != 'visible') {
					window.location.href = "beitrag.asp?ID="+BeitragID;
				}
				
			}
			else {
				alert(response);	
			}
		}
	}
	xmlhttp.send(params);
}

slidePos = -650;

function commentSlideIn() {
	document.getElementById('CommentBox').style.display = "block";
	document.getElementById('FormPart1').style.display = "block";
	document.getElementById('FormPart2').style.display = "none";
	corrFactor = 0;
	if ((isIE() == false) && (getViewportHeight()-300 < 290)) {
		corrFactor = 9;
	}
	centerPos = (getViewportWidth() / 2) - 340 - corrFactor;
	actPos = getPositionX(document.getElementById('CommentBox'));
	if (actPos < centerPos) {
			if (actPos < (centerPos-centerPos/8)) { moveStep = 1 };
			if (actPos < (centerPos-centerPos/6)) { moveStep = 5 };
			if (actPos < (centerPos-centerPos/4)) { moveStep = 25 };
			slidePos = slidePos + moveStep;
		document.getElementById('CommentBox').style.left = (slidePos  + "px");
		setTimeout('commentSlideIn()',1);
	} else {
		centerComments();			
	}
}

function commentSlideOut() {
	actPos = getPositionX(document.getElementById('CommentBox'));
	if (actPos > -650) {
		slidePos = slidePos - 30;
		document.getElementById('CommentBox').style.left = (slidePos  + "px");
		setTimeout('commentSlideOut()',1);
	} else {
		document.getElementById('CommentBox').style.display = "none";		
	}
}

function centerComments() {
	if (document.getElementById('CommentBox')) {
		if (document.getElementById('CommentBox').style.display == "block") {
			corrFactor = 0;
			if ((isIE() == false) && (getViewportHeight()-300 < 290)) {
				corrFactor = 9;
			}
			document.getElementById('CommentBox').style.left = ((getViewportWidth() / 2) - 340 - corrFactor) + "px";
		}
	}
}

function videoComment(VideoID) {
	if (VideoID && VideoID != 0) document.getElementById('Kommentar_Video').value = VideoID;
	commentSlideIn();
}

function FormShow(FormPart) {
	if (FormPart == 1) {
		document.getElementById('FormPart1').style.display = "block";
		document.getElementById('FormPart2').style.display = "none";
	} else {
		document.getElementById('FormPart1').style.display = "none";
		document.getElementById('FormPart2').style.display = "block";
	}	
}

