$.window = function(options) {
  var defaults = {
    width: 800,
    height: 600,
    margin: 10,
    modeless : false,
    overlayOpacity : 0.25,
    overflow: 'hidden',
    backgroundColor: '#FFFFFF',
    caption : '',
    closeButton : true
	};

	options = jQuery.extend(defaults, options);

  if ($("body > .overlay").length == 0) {
    $("body").overlay({opacity : options.overlayOpacity, bgColor : options.overlayColor});
  }

  var html = "";

  html += '<div class="window" style="margin-top: -'+ (options.height / 2 + 1) +'px; margin-left: -'+ (options.width / 2 + 1) +'px; ">';
  html += '<div class="container" style="width: '+options.width+'px;">';

  if (!options.modeless) {
    html += '<div class="header' + ((options.modeless)?' modeless':'') + '"><div class="close" style="margin-left: '+(options.width - (32))+'px;"><img src="/images/jquery/window/close.png"  width="16" height="16" /></div>'+ options.caption +'</div>';
  }

  html += '<div class="body' + ((options.modeless)?' modeless':'') + '">';
  html += '<iframe'+ ((options.backgroundColor == "transparent")?' ALLOWTRANSPARENCY':'') +' scrolling="no" frameborder="0" src="'+ options.src +'" style="background-color: '+ options.backgroundColor +'; overflow: '+ options.overflow +'; width: '+ options.width +'px; height: '+ options.height +'px;"></iframe>';
  html += '</div>';
  html += '</div>';
  html += '</div>';

  $("body").prepend(html);

  $(".header div img").bind("click", function() {
    $.window.hide();
  });
};

$.window.hide = function() {
  var destination = top.document ? top.document : document.body;

  $("body > .overlay", destination).overlayHide({fade : true, fadeSpeed : 500});

  $.window.remove();
}

$.window.checkAnimation = function() {
  var destination = top.document ? top.document : document.body;

  return $(":animated", destination).length;
}

$.window.remove = function() {
  var destination = top.document ? top.document : document.body;

  if ($.window.checkAnimation() == 0) {
    clearTimeout(timeout);

    $("body > .window", destination).remove();
  } else {
    var timeout = setTimeout(function() {
      $.window.remove();
    }, 10);
  }
}

$.window.parent = top.document;

/* SEARCH ALL A TAGS THAT LINK TO A WINDOW */
$(document).ready(function() {
  $("a").each(function() {
    if ($(this).attr("rel").length > 0) {
      $(this).click(function() {

        $.window({
          src : $(this).attr("rel"),
          width : 800,
          height : 600,
          caption : 'Adresboek'
        });

        return false;
      });
    }
  });
});