function WindowPopup(title,width,height,options) {
    this.options = options;
    if( this.options == undefined )
        this.options = {};
    this.title = title;
    this.width = width;
    this.height = height;
    this.parent = this.options.parent ? this.options.parent : document.body;
    if( typeof(this.parent) == 'string' )
        this.parent = document.getElementById(this.parent);
    this.left = this.options.x ? this.options.x : Math.ceil((this.parent.offsetWidth/2)-(this.width/2));
    this.top = this.options.y ? this.options.y : Math.ceil((this.parent.offsetHeight/2)-(this.height/2));

    /* FF fix: ter voorkoming dat de popup buiten beeld valt */
    this.top = this.top < 0 ? 0: this.top;
    this.left = this.left < 0 ? 0: this.left;

    this.dragging = false;
    this.dragStartX = this.dragStartY = 0;

    this.buttons = new Array();

    this.refCounter = 0;

    this.init();
}

WindowPopup.prototype.setAsAbsolute = function(obj,x,y,cx,cy) {
    obj.style.position = 'absolute';
    obj.style.left = x+'px';
    obj.style.top = y+'px';
    obj.style.width = cx+'px';
    obj.style.height = cy+'px';
}

WindowPopup.prototype.init = function() {

    var div = document.createElement('DIV');
    div.id = 'windowPopup';
    div.style.position = 'absolute';
    div.style.width = this.width+'px';
    div.style.height = this.height+'px';
    div.style.top = this.top+'px';
    div.style.left = this.left+'px';
    div.style.visibility = 'hidden';
    div.style.backgroundColor = '#ffffff';

    this.wnd = div;

    var img = document.createElement('IMG');
    img.src = '/beheer/images/melding_linksboven_w.gif';
    img.style.position = 'absolute';
    img.style.left = '0px';
    img.style.top = '0px';
    div.appendChild(img);

    img = document.createElement('IMG');
    img.src = '/beheer/images/melding_rechtsboven_w.gif';
    img.style.position = 'absolute';
    img.style.left = this.width - 15 + 'px';
    img.style.top = '0px';
    div.appendChild(img);

    img = document.createElement('IMG');
    img.src = '/beheer/images/melding_linksonder_w.gif';
    img.style.position = 'absolute';
    img.style.left = '0px';
    img.style.top = this.height - 15 + 'px';
    div.appendChild(img);

    img = document.createElement('IMG');
    img.src = '/beheer/images/melding_rechtsonder_w.gif';
    img.style.position = 'absolute';
    img.style.left = this.width - 15 + 'px';
    img.style.top = this.height - 15 + 'px';
    div.appendChild(img);

    // Top border
    var border = document.createElement('DIV');
    border.style.borderTop = '1px solid #698799';
    this.setAsAbsolute(border,15,0,this.width-30,14+is_ie);
    div.appendChild(border);

    // Left border
    var border = document.createElement('DIV');
    border.style.borderLeft = '1px solid #698799';
    this.setAsAbsolute(border,0,15,14+is_ie,this.height-30);
    div.appendChild(border);

    // Right border
    var border = document.createElement('DIV');
    border.style.borderRight = '1px solid #698799';
    this.setAsAbsolute(border,this.width-15,15,14+is_ie,this.height-30);
    div.appendChild(border);

    // Bottom border
    var border = document.createElement('DIV');
    border.style.borderBottom = '1px solid #698799';
    this.setAsAbsolute(border,15,this.height-15,this.width-30,14+is_ie);
    div.appendChild(border);

    // Content DIV
    var contentDiv = document.createElement('DIV');
    this.setAsAbsolute(contentDiv,7,7,this.width-24+(is_ie*2),this.height-24+(is_ie*2));

    this.contentDiv = contentDiv;

    // Title
    var titleObj = document.createElement('H1');
    titleObj.innerHTML = this.title;
    contentDiv.appendChild(titleObj);

    // OK button
    this.buttonContainer = document.createElement('DIV');
    this.buttonContainer.className = 'buttonContainer';
    this.buttonContainer.style.top = this.height-30+'px';
    this.buttonContainer.id = 'buttonContainer';
    div.appendChild(this.buttonContainer);
    this.alignButtons('center');

    div.appendChild(contentDiv);

    return true;
}

WindowPopup.prototype.show = function() {
    this.parent.appendChild(this.wnd);

    this._alignButtons();

    this.wnd.style.opacity = 1;
    this.wnd.style.zIndex = 3;
    this.wnd.style.visibility = 'visible';

    if( this.options.disableParent ) {
        this.disabler = document.createElement('DIV');
        (new Fader(this.disabler)).setOpac(60);
        this.disabler.style.backgroundColor = '#ffffff';
        this.disabler.style.position = 'absolute';
        this.disabler.style.left = this.parent.offsetLeft+'px';
        this.disabler.style.top = this.parent.offsetTop+'px';
        this.disabler.style.width = this.parent.clientWidth+'px';
        this.disabler.style.height = this.parent.clientHeight+'px';
        this.disabler.style.zIndex = 2;
        this.parent.style.overflow = 'hidden';
        this.parent.appendChild(this.disabler);

    }

    var obj = null;
    if( this.options.setFocus && ( obj = document.getElementById(this.options.setFocus) ) )
        obj.focus();

    this.incRefCounter();
}

WindowPopup.prototype.hide = function() {
    var ret = false;

    if( this.refCounter == 1 ) {
        this.wnd.parentNode.removeChild(this.wnd);
        ret = true;

        if( this.options.disableParent ) {
            this.parent.removeChild(this.disabler);
            (new Fader(this.disabler)).setOpac(100);
            this.parent.style.overflow = 'auto';
        }
    }

    this._decRefCounter();

    return ret;
}

WindowPopup.prototype.windowDrag = function(evt) {
    ( e = evt || window.event );

    if( dragging ) {
        ( obj = e.target || e.srcElement ).innerHTML = e.clientX + ', ' + e.clientY;
        obj.style.left = objStartX + ( e.clientX - dragStartX);
        obj.style.top = objStartY + ( e.clientY - dragStartY);
    }
}

WindowPopup.prototype.windowStartDrag = function(evt) {
    ( e = evt || window.event );
    dragStartX = e.clientX;
    dragStartY = e.clientY;
    objStartX = ( e.target || e.srcElement ).offsetLeft;
    objStartY = ( e.target || e.srcElement ).offsetTop;
    dragging = true;
    ( obj = e.target || e.srcElement ).style.cursor = 'move';
    var fdr = new Fader(obj);
    fdr.setOpac(50);
}

WindowPopup.prototype.windowEndDrag = function(evt) {
    ( e = evt || window.event );
    ( obj = e.target || e.srcElement ).style.cursor = 'default';
    dragging = false;
    var fdr = new Fader(obj);
    fdr.setOpac(100);
}

WindowPopup.prototype.setHTML = function(html,useTitle) {
    if( useTitle == undefined || useTitle == true )
        this.contentDiv.innerHTML = '<h1>'+this.title+'</h1>'+html;
    else
        this.contentDiv.innerHTML = html;
}

WindowPopup.prototype.addButton = function(label,handler) {
    if(this.buttons.length)
        this.buttons[this.buttons.length-1].style.marginRight = '5px';

    var btn = new Button(label);
    btn.addToWindow(this.buttonContainer);
    if( handler != undefined )
        btn.setClickHandler(handler);
    this.buttons[this.buttons.length] = btn;

    if( this.wnd.style.visibility != 'hidden' )
        this._alignButtons();
}

WindowPopup.prototype.alignButtons = function(pos) {
    this.pos = pos;
}

WindowPopup.prototype._alignButtons = function() {
    if( this.pos == 'left' ) {
        this.buttonContainer.style.left = '';
        this.buttonContainer.style.left = '8px';
    }
    else if( this.pos == 'center' ) {
        this.buttonContainer.style.left = '';
        this.buttonContainer.style.right = Math.round((this.width/2) - (this.buttonContainer.offsetWidth/2)) + 'px';
    }
    else if( this.pos == 'right' ) {
        this.buttonContainer.style.right = '8px';
    }
    else if( this.pos == 'top' ) {
        this.buttonContainer.style.top = '70px';
        this.buttonContainer.style.left = '8px';
    }
}

WindowPopup.prototype.incRefCounter = function() {
    this.refCounter++;
}

WindowPopup.prototype._decRefCounter = function() {
    this.refCounter--;
}

var wnd = null;

function CreateWindow(url) {
    var wp = new WindowPopup('', 600, 400, {'disableParent' : 1})

    wp.setHTML('<iframe class="popupWindowIframe" src="'+url+'"></iframe>', false);

    wp.show();
}