function closeFlyin() {
	if (!document.getElementById) return false;
	var flyin = document.getElementById('flyin');
	while (flyin.hasChildNodes()) {
		flyin.removeChild(flyin.firstChild);
	}
}

function showFlyin() {
	if (getSeenCookie() != null) return false;
	if (!document.getElementById || !document.createElement) return false;
	var flyin = document.getElementById('flyin');
	if (!flyin) return false;

	var closeButtonDiv = document.createElement('div');
	var windowDiv = document.createElement('div');

	closeButtonDiv.className = "flyinCloseButton";
	flyin.appendChild(closeButtonDiv);

	windowDiv.className = "flyinWindow";
	flyin.appendChild(windowDiv);
	
	var closeLink = document.createElement('a');
	closeLink.setAttribute('href', 'javascript:closeFlyin();');
	var closeText = document.createTextNode('X schließen');
	closeLink.appendChild(closeText);
	closeButtonDiv.appendChild(closeLink);
	
	windowIFrame = document.createElement('iframe');
	windowIFrame.setAttribute('src', 'http://www.kreuzfahren.de/news/form/flyin/flyin.php');
	windowDiv.appendChild(windowIFrame);
	setSeenCookie();
}

function setCookie(name, value, validUntil) {
	document.cookie = name + "=" + escape(value)
		+ "; expires=" + validUntil.toGMTString() + "; path=/";
}

function getCookie(name) {
	var regex = /\s*([^=]*)=(.*)/;
	var cookies = document.cookie.split(';');
	for (var i = 0; i < cookies.length; ++i) {
		var cookie = cookies[i];
		if (regex.test(cookie) && RegExp.$1 == name) {
			return unescape(RegExp.$2);
		}
	}
	return null;				
}

function clearCookie(name) {
	var distantPast = new Date(1970, 0 ,1);
	setCookie(name, distantPast, distantPast);
}

function setSeenCookie() {
	var validUntil = new Date();
	validUntil.setTime(validUntil.getTime() + 15 * 24 * 60 * 60 * 1000);
	setCookie("nlFlyinSeen", "1", validUntil);
}

function getSeenCookie() {
	return getCookie("nlFlyinSeen");
}

function clearSeenCookie() {
	clearCookie("nlFlyinSeen");
}

function addToOnload(func) {
	if (typeof window.onload != "function" ) {
		window.onload = func;
	} else {
		var oldOnload = window.onload;
		window.onload = function () {
			oldOnload();
			func();
		}
	}
}

function centerFlyin() {
	var flyin = document.getElementById('flyin');
	var winWidth = window.innerWidth;
	var flyinWidth = flyin.offsetWidth;
	var left = winWidth/2 - flyinWidth/2;
	flyin.style.left = left + "px";
}

addToOnload(showFlyin);

