var FADE_OUT_INTERVAL = 10; // in ms
var FADE_OUT_AMOUNT_PER_INTERVAL = 0.02; // 0.01 is one percent
var START_OPACITY = 0.4 // 0.01 is one percent
var SITE_WIDTH = 977; // for ie6 fixes
var SITE_HEIGHT = 3700; // for ie6 fixes
var SITE_ID = 'site'; // for ie6 fixes

var ie4 = document.all;
var effects = true;
var overlayOpacity;
var overlay = false;

function showOverlay() {
	if(!this.overlay)
		this.overlay = document.getElementById('overlay');
	
	this.overlayOpacity = this.START_OPACITY;
	if(this.ie4) {
		if(typeof(window.XMLHttpRequest) == 'undefined') {
			this.overlay.style.height = this.SITE_HEIGHT;
			this.overlay.style.width = document.documentElement.clientWidth;
			document.getElementById(this.SITE_ID).style.left = ((document.documentElement.clientWidth - this.SITE_WIDTH) / 2) + 'px';
		}
		setOverlayOpacityIe();
	}
	else
		setOverlayOpacity();
	this.overlay.style.display = "block";
}
function clickOverlay() {
	closeMagazinePopup();
	if(closeAngebotPopup) {
		closeAngebotPopup();
	}
}
function hideOverlay() {
	if (this.effects) {
		if(this.ie4) {
			overlayFadeOutIe();
		}
		else {
			overlayFadeOut();
		}
	} else {
		document.getElementById('overlay').style.display = "none";
		if(this.ie4) {
			document.getElementById(this.SITE_ID).style.left = '';
		}
	}
}

function setOverlayOpacity() {
	this.overlay.style.opacity = this.overlayOpacity;
}
function setOverlayOpacityIe() {
	this.overlay.style.filter = 'Alpha(opacity=' + (this.overlayOpacity * 100) + ')';
}
function overlayFadeOut() {
	if (this.overlayOpacity > 0) {
		this.overlayOpacity -= this.FADE_OUT_AMOUNT_PER_INTERVAL;
		setOverlayOpacity();
		setTimeout(overlayFadeOut, this.FADE_OUT_INTERVAL);
	}
	else {
		this.overlay.style.display = 'none';
	}
}

function overlayFadeOutIe() {
	if (this.overlayOpacity > 0) {
		this.overlayOpacity -= (this.FADE_OUT_AMOUNT_PER_INTERVAL * 3); // IE is slower in doing this, so tripple the amount faded out per interval ...
		setOverlayOpacityIe();
		setTimeout(overlayFadeOutIe, (this.FADE_OUT_INTERVAL * 2)); // ... and double the time
	}
	else {
		document.getElementById(this.SITE_ID).style.left = '';
		this.overlay.style.display = 'none';
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if(typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		};
	}
}