window.onload = function() {
		
		var jetzt = new Date();
		var heute = jetzt.getFullYear()+'-'+jetzt.getMonth()+'-'+jetzt.getDate();
		
		//Zeit zählt nach Klick weiter
		//if(!readCookie('onlineTime') || readCookie('onlineTime') >= 120) createCookie('onlineTime', 0, 1);
		
		//Zeit beginnt nach jedem Klick neu
		createCookie('onlineTime', 0, 1);
		
 		count(heute);
}


var counter = 0;


function count(date){

	counter = parseInt(readCookie('onlineTime'));
	
	if(counter >= 120 && !(readCookie('PopupOpened') == date)) {
		
		html = '<div id="popup-container" style="display: none"><a href="http://www.elbcampus.de/de/Popup/2057.php?iframe=true&width=620&height=400" title="" rel="prettyPhoto"></a></div>'
		$('.page').append(html);
		$("a[rel^='prettyPhoto']").prettyPhoto({social_tools: '', show_title: false, deeplinking: false});
		$('#popup-container a').trigger('click');
					
		//openPopup('/de/Popup/2057.php');
		createCookie('PopupOpened', date);
	}
	
	counter++;
	createCookie('onlineTime', counter, 1);
	if (counter <= 120)
		window.setTimeout(function(){count(date)}, 1000);
	
}


function openPopup(url) {
	var concatinator = "&";
	if(url.indexOf("?") == -1) 
		concatinator = "?";
	var vHWindow = window.open(url,"ELBCAMPUS","width=500,height=400,status=1,resizable=0,location=0,titlebar=0,menubar=0,directories=0", true);
	vHWindow.moveTo(200, 500);
	vHWindow.focus();
}

function createCookie(name,value,days) {
	if (days) { 
		var date = new Date();
		date = new Date(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	//console.log(document.cookie);
}

function readCookie(name) {
	var nameEQ = name + "=";
	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;
}
