function PopupWindow(contents, title, fetch) {
    this.fetch = fetch;
    this.contents = contents;
    this.title = title;
	this.isOpen = true;
	this.isCancelable = true;
	
	this.dragging = false;

    this.cancelCallback = null;
    this.loadedCallback = null;

    this.popup = null;
    this.modalLayer = null;
	
	this.deltaX = null;
	this.deltaY = null;
	this.upEvent = null;
	this.moveEvent = null;

    this.createWindow();
    if (fetch == null) {
	this.url = contents;
	this.makeRequest();
    }
}

PopupWindow.prototype.addCancel = function (func) {
    this.cancelCallback = func;
}

PopupWindow.prototype.addLoaded = function (func) {
    this.loadedCallback = func;
}

PopupWindow.prototype.setContents = function (contents) {
    this.popup.contents.innerHTML = contents;
}

PopupWindow.prototype.setTitle = function (title) {
	titleDiv = getElement("popupTitle");
	titleDiv.replaceChild(document.createTextNode(title), titleDiv.firstChild);
}

PopupWindow.prototype.makeRequest = function () {
    var req = getXMLHttpRequest();
    req.open("GET", this.url, true);
    req.setRequestHeader('Cookie', document.cookie);

    var deferred = sendXMLHttpRequest(req);
    deferred.addCallback(bind(this.contentLoaded, this));
    deferred.addErrback(bind(this.loadFailed, this));
}

PopupWindow.prototype.contentLoaded = function(req) {
    //this.popup.contents.innerHTML = req.responseText;
    var data = evalJSONRequest(req);
	if(data.error){
		this.popup.contents.innerHTML = data["errorMessage"];
		this.popup.addCloseButton();
		this.setCancelable(true);
	}else{
		this.popup.contents.innerHTML = data['HTML'];
		eval(data['script']);
		if (this.loadedCallback) {
			this.loadedCallback();
		}
	}
}

PopupWindow.prototype.addCloseButton = function(){
	var closeDiv = DIV({'class': 'windowAlertCloseButton'});
	var closeButton = INPUT({'type': 'button', 'class': 'button', 'style': 'margin 0 auto;', 'value': 'Sluiten'});
	closeDiv.appendChild(closeButton);
	this.popup.contents.appendChild(closeDiv);
	connect(closeButton, 'onclick', this, 'cancel');
}

PopupWindow.prototype.loadFailed = function(error) {
    alert('fout: ' + error);
}

PopupWindow.prototype.createWindow = function() {
	this.popup = DIV({'class': "window", 'id': "popupWindow"});
	var topBar = DIV({'class': "windowTopBar"});
	var windowCorner = DIV({'class': "windowCorner windowCloseCorner"});
	var cancel = DIV({'class': "windowCloseButton", 'id': "popupCancel"}, "X");
	connect(cancel, 'onclick', this, 'cancel');
	windowCorner.appendChild(cancel);
	topBar.appendChild(windowCorner);
	var titleBar = DIV({'class': "windowTitleBar"});
	var title = DIV({'class': "windowTitleText", 'id': "popupTitle"}, this.title);
	title.style.cursor = "move";
	this.popup.title = title;
	titleBar.appendChild(title);
	topBar.appendChild(titleBar);
	connect(titleBar, 'onmousedown', this, 'startDragging');
	var contentsFrame = DIV({'class': "windowContentsFrame"});
	var contentsArea = DIV({'class': "windowContentsArea", 'id': "popupContentsArea"});
	var contents = DIV({'class': "windowContents"}, "Bezig met laden...");
	contentsArea.appendChild(contents);
    this.popup.contents = contents;
    if (this.fetch != null) {
    	this.popup.contents.innerHTML = this.contents;
    }
	contentsFrame.appendChild(contentsArea);
	this.popup.appendChild(topBar);
	this.popup.appendChild(contentsFrame);
	this.modalLayer = DIV({'id': "modalLayer"});
	try{
		this.modalLayer.setOpacity(0);
		this.modalLayer.style.visibility = "visible";
		document.body.appendChild(this.modalLayer);
		document.body.appendChild(this.popup);
		this.modalLayer.fadeTo(70, 10);
	}catch(e){
		document.body.appendChild(this.modalLayer);
		document.body.appendChild(this.popup);
		this.modalLayer.style.backgroundColor = "transparent";
		this.modalLayer.style.backgroundImage = "url(/images/IE8opacity.png)";
		this.modalLayer.style.backgroundRepeat = "repeat";
	}
}

PopupWindow.prototype.startDragging = function(event){
	if(this.popup.offsetLeft){
		var windowX = this.popup.offsetLeft;
	}else{
		var windowX = window.getComputedStyle(this.popup, null).left;
		windowX = Number(windowX.substring(0, windowX.length - 2));
	}
	this.deltaX = event.event().clientX - windowX;
	if(this.popup.offsetTop){
		var windowY = this.popup.offsetTop;
	}else{
		var windowY = window.getComputedStyle(this.popup, null).top;
		windowY = Number(windowY.substring(0, windowY.length - 2));
	}
	this.deltaY = event.event().clientY - windowY;
	this.upEvent = connect(document, 'onmouseup', this, 'stopDragging');
	this.moveEvent = connect(document, 'onmousemove', this, 'drag');
	event.stopPropagation();
	event.preventDefault();
}

PopupWindow.prototype.stopDragging = function(event){
	disconnect(this.upEvent);
	disconnect(this.moveEvent);
}

PopupWindow.prototype.drag = function(event){
	this.popup.style.left = (event.event().clientX - this.deltaX + 250) + "px";
	this.popup.style.top = (event.event().clientY - this.deltaY) + "px";
}

PopupWindow.prototype.cancel = function(event) {
	if(this.isCancelable){
		this.isOpen = false;
		removeElement(this.modalLayer);
		removeElement(this.popup);
	
		if (this.cancelCallback) {
			this.cancelCallback();
		}
	}
}

PopupWindow.prototype.setCancelable = function(cancelable) {
	var cancelButton = getElement("popupCancel");
	this.isCancelable = cancelable;
	if(cancelable){
		cancelButton.className = "windowCloseButton";
	}else{
		cancelButton.className = "windowCloseButtonDisabled";
	}
}

function AjaxDataSender(form, method, url, callback){
	this.form = form;
	this.method = method;
	this.url = url;
	this.userCallback = callback;
}

AjaxDataSender.prototype.call = function(){
	var req = getXMLHttpRequest();
	req.open(this.method.toUpperCase(), this.url);
	req.setRequestHeader("Cookie", document.cookie);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	var deferred = sendXMLHttpRequest(req, queryString(this.form));
	deferred.addCallback(bind(this.callback, this));
	deferred.addErrback(bind(this.errback, this));
	return(true);
}

AjaxDataSender.prototype.callback = function(data){
	data = evalJSONRequest(data);
	if(data.error){
		if(data.errorMessage){
			alert(data.errorMessage);
		}else{
			alert("De gegevens konden niet verwerkt worden op de server");
		}
	}else{
		this.userCallback(data);
	}
}

AjaxDataSender.prototype.errback = function(error){
	this.cancel();
	alert("ERROR: " + error);
}
