var winHandle;

function onMainWindowGetFocus() {
	if ((typeof winHandle != "undefined") && (winHandle != null)) {
		if (!winHandle.closed) {
			winHandle.focus();
			return false;
		}
	}
	return true;
}

function PopupEditor(from, id, mode, iWidth, iHeight, sFull) {
	if (typeof(iWidth)  =='undefined') iWidth  = screen.availWidth - 40;
	if (typeof(iHeight) =='undefined') iHeight = screen.availHeight - 80;
	if (typeof(sFull)   =='undefined') sFull  = 'no';
	
	iWidth  = screen.availWidth - 40;
	iHeight = screen.availHeight - 80;

	var features = "scrollbars=no,status=yes,resizable=yes,width=" + iWidth + ",height=" + iHeight + ",top=" + (((screen.availHeight-iHeight)/2)-30) + ",left=" + (((screen.availWidth-iWidth)/2)-4) + ",fullscreen=" + sFull;
	var url = "/common/components/htmleditor/default.asp?t=" + from + "&i=" + id + "&m=" + mode + "&rnd=" + Math.random();

	winHandle = window.open(url, "", features);
	window.onfocus = onMainWindowGetFocus;

	// Does not work: IFRAME will not get editable :( -> yeah it sucks, kc
	//window.showModalDialog(url,'','dialogHeight:' + iHeight + 'px; dialogWidth:' + iWidth + 'px; scroll:No; center:Yes; help:No; resizable:Yes; status:Yes;');
}

function WinPopup(sURL, sWinName, iWidth, iHeight, sScroll, sFull) {
	if (typeof(iWidth)  =='undefined') iWidth  = 560;
	if (typeof(iHeight) =='undefined') iHeight = 250;
	if (typeof(sWinName)=='undefined') sWinName= 'WinPopup';
//	if (typeof(sScroll) =='undefined') sScroll= 'no';
	if (typeof(sScroll) =='undefined') sScroll= 'yes';
	if (typeof(sFull)   =='undefined') sFull  = 'no';
	var out = "+&+";       // vervang dit
	var add = "%26";       // door dit
	var temp = "" + sURL;
	
	iHeight+=30;
	
	var features = "scrollbars="+sScroll+",status="+sScroll+",resizable="+sScroll+",width=" + iWidth + ",height=" + iHeight + ",top=" + ((screen.height-iHeight)/2) + ",left=" + ((screen.width-iWidth)/2) + ",fullscreen=" + sFull;
	while (temp.indexOf(out)>-1) {
		pos = temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + temp.substring((pos + out.length), temp.length));
	}
	
	sURL = temp;
	if ((winHandle != null) && (!winHandle.closed))
		winHandle.focus()
	else
		winHandle = window.open(sURL,sWinName,features);

	try {
		winHandle.origWidth = iWidth;
	} catch (e) {	}
	
	var w = window;

	try {
		while (w != top) {
			addEventHandler(w, "focus", onMainWindowGetFocus);
			w = w.parent;
		}
		addEventHandler(w, "focus", onMainWindowGetFocus);
	} catch (e) {	}

	return winHandle;
}

function ModalWinPopup(sURL, iWidth, iHeight, scroll) {
	if (iWidth == undefined) iWidth  = 560;
	if (iHeight== undefined) iHeight = 250;
	
	iHeight+=30;
//	if (false && document.all) 
//		window.showModalDialog(sURL,window,'dialogHeight:' + iHeight + 'px; dialogWidth:' + iWidth + 'px; scroll:No; center:Yes; help:No; resizable:No; status:No;');
//	else {
		winHandle = WinPopup(sURL, '', iWidth, iHeight, scroll, 'no');
		return winHandle;
//	}
}

function WinResizePopup(sURL, sWinName, iWidth, iHeight) {
	if (iWidth == undefined) iWidth  = 560;
	if (iHeight== undefined) iHeight = 250;
	if (sWinName==undefined) sWinName= 'PopupResize';
	var out = "+&+";        // vervang dit
	var add = "%26";       // door dit
	var temp = "" + sURL;
	
	iHeight+=30;
	
	var features = "scrollbars=yes,resizable=yes,width=" + iWidth + ",height=" + iHeight + ",top=" + ((screen.height-iHeight)/2) + ",left=" + ((screen.width-iWidth)/2);

	while (temp.indexOf(out)>-1) {
		pos = temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + temp.substring((pos + out.length), temp.length));
	}
	
	sURL = temp;
	winHandle = window.open(sURL,sWinName,features);
	winHandle.focus();

	window.onfocus = onMainWindowGetFocus;
	return winHandle;
}

function ShowPopup(type, id) {
	var leftPos = (screen.availWidth-780) / 2
	var topPos = (screen.availHeight-580) / 2 
	previewWin = window.open('about:blank','','width=780,height=580,scrollbars=yes,resizable=yes,titlebar=0,top=' + topPos + ',left=' + leftPos);
	previewWin.document.write(
		'<title>Preview</title>'+
		'<frameset rows="30,*" framespacing="0" frameborder="0">' +
		'<frame name="header" src="/preview/preview_header.asp?f='+type+'&i='+id+'" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" border="0">'+
    '<frame name="content" src=""about:blank" marginwidth="0" marginheight="0" scrolling="auto" frameborder="0" border="0">'+
		'</frameset>'
	);
}

function createEvent(e) {
	var event = {};
	if (typeof(e) != "undefined" && typeof(e.target) != "undefined") {
		event.offsetX 		= e.layerX;
		event.offsetY		  = e.layerY;
		event.srcElement  = e.target;
		event.keyCode     = e.which;
		event.ctrlKey		  = e.ctrlKey;
		event.shiftKey    = e.shiftKey;
		event.altKey      = e.altKey;
	} else {
		event = window.event;
	}
	return event;
}

function centerWindow(x) {
	x.moveTo((screen.width-x.document.body.offsetWidth)/2,(screen.height-x.document.body.offsetHeight)/2);
}

function onPopupWindowLoad(w, e) {
	if (w.NORESIZE !== true)	fitWindowToContent(w);
	if (w.NOCENTER !== true)	centerWindow(w);
}

function onPopupWindowKeyDown(w, e) {
	var evt = e ? e : w.event;
	var keyCode = evt.keyCode ? evt.keyCode : evt.charCode ? evt.charCode :	evt.which ? evt.which : void 0;
	if (keyCode == 27) w.close();
}

function addEventHandler(anObject, anEvent, aFunction) {
	if (anObject.attachEvent) {
		anObject.attachEvent('on' + anEvent, aFunction);
	}	else {
		anObject.addEventListener(anEvent, aFunction, false);
	}
}

function fitWindowToContent(x) {
	// IE 			-> 252 en   x en 256
	// FF 1.0.3 -> 245 en 310 en 245
	// FF 1.5   -> 310 en 310 en 245		(x.document.body.scrollHeight = x.innerHeight)
	// alert(x.document.body.scrollHeight + " en " + x.innerHeight + " en " + x.document.body.offsetHeight);
	
	var dh = (typeof x.innerHeight != 'undefined' ? x.innerHeight : x.document.body.offsetHeight-4);

	var w  = 20 + 6 + (typeof x.innerWidth  != 'undefined' ? x.innerWidth  : x.document.body.offsetWidth);
	if (typeof x.width != "undefined") w = x.width;
	try {
		x.resizeTo(w, dh);
	} catch (e) {
		// can cause an error with https? or cross-domain scripting or something
	}

	var h = (typeof x.innerHeight != 'undefined' ? x.innerHeight : x.document.body.offsetHeight-4);

	dh -= (h - 18);
	try {
		if (navigator.product == "Gecko") {
			x.resizeTo(w, x.document.body.offsetHeight+dh);
		} else {
			x.resizeTo(w, x.document.body.scrollHeight+dh);
		}
	} catch (e) {
	}
}

function initPopup(w, width) {
  w.width = width;
  addEventHandler(w         , 'load'   , function (e) { onPopupWindowLoad   (w, e); });
  addEventHandler(w.document, 'keydown', function (e) { onPopupWindowKeyDown(w, e); });
}