var slideSpeed = 20;
var slideInterval = 0;
var currentWidth = 0;
var checkTimer;

// Hochscrollen
addEventListener("load", function(event) {
    setTimeout(checkOrientAndLocation, 0);
    checkTimer = setInterval(checkOrientAndLocation, 300);
}, false);

function checkOrientAndLocation() {
    if (window.innerWidth != currentWidth)
    {   
        currentWidth = window.innerWidth;
        var orient = currentWidth == 320 ? "profile" : "landscape";
        document.body.setAttribute("orient", orient);
        setTimeout(scrollTo, 100, 0, 1);
    }
}

// Cookie auslesen, Standard: Scrolling auf AN setzen
if (document.cookie) {
	var headScrolling = getCookieVal('headScrollingOption');
	if (headScrolling != 'on' && headScrolling != 'off') var headScrolling = 'on';
} else {
	var headScrolling = 'on';
}

// Routine für Fixierung des Kopfes
var yPos = 0;
var frameHeight = 0;

function posControl() {
	if (headScrolling == 'on') {
		yPos = window.pageYOffset;
		document.getElementById('head').style.top = yPos + 'px';
		if (window.orientation != 0) {
			document.getElementById('head').style.top = '0px';
		}
		setTimeout('posControl()', 100);
	}
}

// Button / Block einblenden
var searchWindowStatus = false;

function searchSwitch()	{
	if(document.getElementById('head_suche').style.display != 'block') {
		document.getElementById('button_aufschlagen').className = 'button aktiv';
		document.getElementById('head_suche').style.display = 'block';
		document.getElementById('head_kapitel').style.display = 'none';
		if(document.getElementById('head_aufschlagen').style.display == 'block') {
			document.getElementById('head_aufschlagen').style.display = 'none';
			document.getElementById('head_aufschlagen_fake').style.display = 'none';
		}
		document.getElementById('input-aufschlagen').focus();
	} else {
		document.getElementById('button_aufschlagen').className = 'button';
		document.getElementById('head_suche').style.display = 'none';
		document.getElementById('head_kapitel').style.display = 'block';
	}
}

function headSwitch() {
	if(document.getElementById('head_aufschlagen').style.display != 'block') {
		document.getElementById('head_aufschlagen').style.display = 'block';
		document.getElementById('head_aufschlagen_fake').style.display = 'block';
		if(document.getElementById('head_suche').style.display == 'block') {
			document.getElementById('head_suche').style.display = 'none';
		}
	} else {
		document.getElementById('head_aufschlagen').style.display = 'none';
		document.getElementById('head_aufschlagen_fake').style.display = 'none';
	}
}

function showContext() {
	document.getElementById('hidden-context').style.display = 'block';
	document.getElementById('show-context').style.display = 'none';
}

function getCookieVal(cookieName) {
	var nameEQ = cookieName + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function setCookie(cookieName, cookieVal, cookieTimeout) {
	var actTimestamp = new Date();
	var timeout = new Date(actTimestamp.getTime() + cookieTimeout);
	document.cookie = cookieName + "=" + cookieVal + "; expires=" + timeout.toGMTString() + "; path=/";
}

function switchHeadScrolling() {
	var cookieTimeout = 1000 * 60 * 60 * 24 * 365;
	if (document.cookie) var headScrolling = getCookieVal('headScrollingOption');

	if (headScrolling != 'on') {
		cookieVal = 'on';
		document.getElementById('headScrollingSwitch').className = 'first actSwitch';
	} else {
		cookieVal = 'off';
		document.getElementById('headScrollingSwitch').className = 'first pasSwitch';
	} 	

	setCookie ('headScrollingOption', cookieVal, cookieTimeout);
}

function initHeadScrollingSwitch() {
	if (headScrolling != 'on') {
		document.getElementById('headScrollingSwitch').className = 'first pasSwitch';
	} else {
		document.getElementById('headScrollingSwitch').className = 'first actSwitch';
	} 	
}