/* Greybox Redux
 * Required: http://jquery.com/
 * Written by: John Resig
 * Based on code by: 4mir Salihefendic (http://amix.dk)
 * License: LGPL (read more in LGPL.txt)
 */

var GB_DONE = false;
var GB_HEIGHT = 400;
var GB_WIDTH = 400;
var GB_CALLBACK = null;

function GB_show(caption, url, height, width, callbackFunction ) {
	GB_HEIGHT = height || 400;
	GB_WIDTH = width || 400;
	GB_CALLBACK = callbackFunction;

	// Lynkx addition, add _showInPopup=true parameter
	if ( url.indexOf( '?' ) == -1 )
		url = url + '?_showInPopup=true';
	else
		url = url + '&_showInPopup=true';

	if(!GB_DONE) {
	 $(document.body)
		.append("<div id='GB_overlay' style='display: none;'></div><div id='GB_window'><div id='GB_close'>X SLUIT</div><div id='GB_caption'></div></div>");
	 $("#GB_close").click(GB_hide);
	 $(window).resize(GB_position);
	 
	 GB_DONE = true;
	}
	
	// create browser-specific max width and height vars
	var maxWidth, maxHeight;
	if ($.browser.msie) {
		 maxWidth=document.documentElement.scrollWidth;
		 maxHeight=document.documentElement.scrollHeight;
	} else {
		 maxWidth=Math.max($('html').width(), $(window).width()-30);
		 maxHeight=Math.max($('html').height(), $(window).height()-30);
	}  
  
  $("#GB_overlay").css({ 
		width:maxWidth+"px",
		height:maxHeight+"px" });
		
  $("#GB_frame").remove();    
  $("#GB_window").append("<iframe id='GB_frame' src='"+url+"' frameborder='no'></iframe>");

  $("#GB_caption").html(caption);
  $("#GB_overlay").show();
  GB_position();

  $("#GB_window").fadeIn();
}

function GB_hide() {
  $("#GB_window").hide();
  $("#GB_overlay").hide();
  if (GB_CALLBACK != null)
  	GB_CALLBACK();
}

function GB_position() {
  var de = document.documentElement;
  
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var h = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
  $("#GB_window").css({ 
		width:GB_WIDTH+"px",
		height:GB_HEIGHT+"px",
		left: ((w - GB_WIDTH)/2 + de.scrollLeft )+"px" ,
		top: ((h - GB_HEIGHT)/2 + de.scrollTop )+"px" });
  $("#GB_frame").css("height",GB_HEIGHT - 32 +"px");
}

