// Check for existing window.onload function and run them aswell
if (window.onload) {
	var func = window.onload;
	window.onload = function() {
		func();
		addAlert();
	}
} else {
	window.onload = addAlert;
}

function addAlert() {
	new LightboxAlert(0.2);
}

function LightboxAlert(animSpeed) {
	if (!this.checkIfShowed()) {
		this.animSpeed = animSpeed;
		this.initialize();
	}
}
	LightboxAlert.prototype.animSpeed;
	
	LightboxAlert.prototype.initialize = function(e) {
		this.hideScrollers();
		this.addOverlayMarkup();
		this.addLightboxMarkup();
	}

	LightboxAlert.prototype.animate = function(elem, property, unit, from, to, seconds, doAfter) {
		var self = this;
		eval('elem.style.'+property+' = from+unit');
		var fps = 50;
		var interval;
		var dir;
		var steps = seconds*fps;
		var step = Math.abs(from-to)/steps;
		if (to >= from) {
			dir = 1; 
		} else {
			dir = -1;
		}
		function changeVal() {
			if (dir>0) {
				if (from < to ) {
					from += step;
				}
				if (from >= to ) {
					from = to;
					clearInterval(interval);
					if (doAfter != undefined) {
						eval('self.'+doAfter+'()');
					} 
				}
			}
			if (dir<0) {
				if (from > to) {
					from -= step;
				}
				if (from <= to) {
					from = to;
					clearInterval(interval);
					if (doAfter != undefined) {
						eval('self.'+doAfter+'()');
					}
				}
			}
			eval('elem.style.'+property+' = from+unit');
		}
		interval = setInterval(changeVal, 1000/fps);
	}

	LightboxAlert.prototype.addOverlayMarkup = function() {
		var bod = document.getElementsByTagName('body')[0];
		var overlay = document.createElement('div');
		overlay.id = 'AlertOverlay';
		bod.appendChild(overlay);
		this.addOverlayStyle(overlay);
		overlay.style.display = 'block';
		this.animate(overlay, 'opacity', '', 0, 0.7, this.animSpeed);
		
	}
	
	LightboxAlert.prototype.addLightboxMarkup = function(obj) {
		var bod = document.getElementsByTagName('body')[0];
		var content = document.createElement('div');
		var docName = document.title;
		content.id = 'AlertContent';
		content.className = 'oldSiteLightbox';
		content.innerHTML = '<img src="http://www.yle.fi/svenska/images_local/local_site_head.jpg" alt="svenska.yle.fi logo" /><div style="margin:1em;"><h1>Sidan <span style="font-weight:bold;">' + docName + '</span> uppdateras inte längre!</h1><p>Detta kan medföra att vissa länkar inte längre fungerar eller att sakinnehållet är föråldrat.</p><p><a href="#">Fortsätt</a></p><div>';
		//Set initaial height to zero
		content.style.height = '0%';
		this.addLightboxStyle(content);
		this.animate(content, 'height', '%', 0, 50, this.animSpeed, 'activateClose');
		// Hide selects mainly for IE6
		this.setVisibilityForTags('select', 'hidden');
		// Hide Flash
		this.setVisibilityForTags('object', 'hidden');
		bod.appendChild(content);
	}
	
	LightboxAlert.prototype.activateClose = function() {
		var self = this;
		var content = document.getElementById('AlertContent');
		content.style.cursor = 'pointer';
		content.onclick = function() {
			self.closeBox();
		}
	}
	
	LightboxAlert.prototype.closeBox = function() {
		var content = document.getElementById('AlertContent');
		content.onclick = null;
		content.style.cursor = 'auto';
		this.animate(content, 'height', '%', 50, 0, this.animSpeed, 'afterClose');
	}
	
	LightboxAlert.prototype.afterClose = function() {
		document.getElementById('AlertOverlay').style.display = 'none';
		document.getElementById('AlertContent').style.display = 'none';
		// Show scroller for body
		document.getElementsByTagName('html')[0].style.overflow = 'auto';
		document.getElementsByTagName('body')[0].style.overflow = 'auto';
		this.setVisibilityForTags('select', 'visible');
		this.setVisibilityForTags('object', 'visible');
	}
	
	LightboxAlert.prototype.hideScrollers = function() {
		document.getElementsByTagName('html')[0].style.overflow = 'hidden';
		document.getElementsByTagName('body')[0].style.overflow = 'hidden';
	}
	
	LightboxAlert.prototype.addLightboxStyle = function(obj) {
		var hOnes = obj.getElementsByTagName('h1');
		for (i = 0; i<hOnes.length; i++) {
			hOnes[i].style.font = '24px Arial,Verdana,Helvetica,sans-serif';
			hOnes[i].style.margin = '4px 0 8px 0';
			hOnes[i].style.padding = '2px 0 2px 0';
			hOnes[i].style.color = '#000';
			hOnes[i].style.background = 'none';
			hOnes[i].style.textAlign = 'left';
		}
		var as = obj.getElementsByTagName('a');
		for (i = 0; i<as.length; i++) {
			as[i].style.color = '#000';
		}
		var ps = obj.getElementsByTagName('p');
		for (i = 0; i<ps.length; i++) {
			ps[i].style.marginLeft = '0';
			ps[i].style.font = '12px Arial,Verdana,Helvetica,sans-serif';
			ps[i].style.color =  '#333';
		}
		obj.style.font = '12px Arial,Verdana,Helvetica,sans-serif';
		obj.style.color =  '#333';
		obj.style.position = 'absolute';
		obj.style.top = '15%';
		obj.style.left = '25%';
		obj.style.width = '50%';
		obj.style.height = '50%';
		obj.style.border = '1em solid #B8B8B8';
		obj.style.backgroundColor = '#FFF';
		obj.style.textAlign = 'left';
		obj.style.zIndex = '1001';
		obj.style.overflow = 'hidden';
		obj.style.backgroundRepeat = 'no-repeat';
		
	}
	LightboxAlert.prototype.addOverlayStyle = function(obj) {
		obj.style.display =  'none';
		obj.style.position = 'absolute';
		obj.style.top = '0';
		obj.style.left = '0';
		obj.style.width = '100%';
		obj.style.height = '100%';
		obj.style.zIndex = '1000';
		obj.style.backgroundColor = '#333';
		//obj.style.-moz-opacity = '0.7';
		obj.style.opacity = '.70';
		obj.style.overflow = 'hidden';
		obj.style.filter = 'alpha(opacity=70)';
	}
	LightboxAlert.prototype.setVisibilityForTags = function(tag, visibility) {
		var elements = document.getElementsByTagName(tag);
		for(i = 0; i < elements.length; i++) {
			elements[i].style.visibility = visibility;
		}
	}
	
	LightboxAlert.prototype.setCookie = function(c_name, value, expiredays) {
		var exdate = new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie = c_name+"=" +escape(value)+
		((expiredays == null) ? "" : ";expires="+exdate.toGMTString());
	}
	
	LightboxAlert.prototype.getCookie = function(c_name) {
		if (document.cookie.length > 0) {
			c_start = document.cookie.indexOf(c_name + "=");
			if (c_start != -1) {
				c_start = c_start + c_name.length+1;
				c_end = document.cookie.indexOf(";", c_start);
				if (c_end == -1) c_end = document.cookie.length;
				return unescape(document.cookie.substring(c_start, c_end));
			}
		}
		return "";
	}
	
	LightboxAlert.prototype.checkIfShowed = function() {
		if (this.getCookie('old_alert') != document.title) {
			this.setCookie('old_alert', document.title, 3);
			return false;
		}
		return true;
	}

