$(document).ready(
	function() {
		var xml_data = new XMLHttpRequest();
		  xml_data.onreadystatechange = stateChanged;
		  xml_data.open("GET", "popupdata.html");
		  try { 
		    xml_data.send(); 
		    } catch (e) {
		    	if (typeof console != "undefined") {
		            console.log(e.message);
		        }
		  }
		
		    function stateChanged(data_loaded){
				  if(this.readyState == 4 && (this.status == 200 || this.status == 0)) {
					    if(this.responseText != null) {
					    	var popup = new popUp({
								HTMLcontent : this.responseText
							});
							setTimeout(function(){popup.hide();}, 10000);
					    } else
					    	if (typeof console != "undefined") {
					            console.log("Error! Recieved data is NULL!");
					        }
					  } else if (this.readyState == 4 && this.status != 200) {
						  if (typeof console != "undefined") {
					            console.log("Error! Status: "+this.status);
					        }
					  }
			  };
	}
);
function popUp(args) {	
	var body = $(document.body);
	body.css({margin : 0});
	var bg = $("<div id='popupContainer'>");
	var content = $("<div id='popupContent'>");
	bg.click(hide)
	.css({
		backgroundColor : '#111111',
		position : 'absolute',
		top : 0,
		left : 0,
		zIndex : 998,
		cursor : "pointer"
	})
	.fadeTo(400, 0.85);

	content.html(args.HTMLcontent)
	.css({
		backgroundColor : 'white',
		boxShadow : '3px 3px 5px #000000',
		minWidth : '500px',
		minHeight : '300px',
		maxWidth: '800px',
		padding : '10px',
		position : 'absolute',
		display : 'block',
		zIndex : 999
	})
	.css('-moz-border-radius', '10px')
	.css('-webkit-border-radius', '10px')
	.css('border-radius', '10px')
	.fadeTo(400, 1);

	$(document).bind('keydown', popupKeyDown);
	body.append(bg);
	body.append(content);
	content.css({
		top : ($(window).height() / 2 - content.height() / 2) > 10 ? $(window).height() / 2 - content.height() / 2 : 10, 
		left : ($(window).width() / 2 - content.width() / 2) > 0 ? $(window).width() / 2 - content.width() / 2 : 0
	});
	bg.css({
		width : $(window).width(),
		height : (($(document).height() > $(window).height()) && ($(document).height() > content.height())) ? $(document).height() : ($(window).height() > content.height()) ? $(window).height() : content.height()
	});
	bg.focus();
	function hide() {
		bg.fadeOut(200);
		content.fadeOut(200);
		$(document).unbind('keydown');
		window.location.replace("/index2.html"); 
	}
	function popupKeyDown(object) {
		if (object.keyCode == 27) hide();
	};
	this.popupKeyDown = function(object){popupKeyDown(object);};
	this.hide = function(){hide();};
}
