/* 	popup.js v4.2 030701 (c)h */

var popupWindow; 
window.onunload = popupCloseAll;
/*	
 *	closes popupWindow automatically 
 */
function popupCloseAll() 
{
	if (popupWindow != null && !popupWindow.closed) 
		popupWindow.close();
}

/* 	opens a popup
 *	@param 	url			of page to show
 *			w			width
 *			h			height
 *			type		of opening [center|BtoT|BtoC|BtoB|RtoL|RtoC|RtoR|CtoC|CshiftL|CshiftT]
 *			noframe		ie frameless window, no type of [CtoC|CshiftL|CshiftT]
 *			extraHeight	for the frameless window, e.g.a navibar
 */
function popup(url, w, h, type, noframe, extraHeight) 
{
	//base values
	if (w >= envScreen.width) 
		w = envScreen.width - 20;
	if (h >= envScreen.height) 
		h = envScreen.height - 50;
	var posLeft = absLeft = (envScreen.width - w) / 2;
	var posTop = absTop = (envScreen.height - h) / 2;
	//zoom
	var dw, dh; //step w, h
	if ( type == "CtoC" || type == "CshiftL" || type == "CshiftT" ) {
		posLeft = envScreen.width - 1; //out of screen
		posTop = envScreen.height - 1;
		var minSize = 250;// 250 for IE7! else: 116;
		var steps = 20;
		if (ieMac) steps = 1; //does not work, god knows why...
		var targetW = w;
		var targetH = h;
		w = minSize + ((targetW - minSize) % steps);
		dw = Math.floor( (targetW - minSize) / steps);	
		h = minSize + ((targetH - minSize) % steps);	
		dh = Math.floor( (targetH - minSize) / steps);
		if (type == "CshiftL") {
			h = targetH;
			dh = 0;
		}
		if (type == "CshiftT") {
			w = targetW;
			dw = 0;
		}
	}
	//image zoom
	else if ( type == 'IzoomH') {
		;
	}
	//fixed size
	else {
		switch (type) {
		case "BtoT": 	posTop = envScreen.height;
						absTop = 10;
						break;
		case "BtoC": 	posTop = envScreen.height;
						absTop = ((envScreen.height - h) / 2) - 20;
						break;
		case "BtoB": 	posTop = envScreen.height;
						absTop = (envScreen.height - h) - 50;
						break;
		case "RtoL": 	posLeft = envScreen.width-10;
						absLeft = 25;
						break;
		case "RtoC": 	posLeft = envScreen.width-10;
						absLeft = (envScreen.width - w) / 2;
						break;
		case "RtoR": 	posLeft = envScreen.width-10;
						absLeft = (envScreen.width - w) - 100;
						break;
		}
	}
	//normal
	var options = "resizable=1,scrollbars=yes,width=" + w + ",height=" + h + ",left=" + Math.round(posLeft) + ",top=" + Math.round(posTop);
	popupCloseAll();
	popupWindow = window.open(url, "cdot" , options);
	if (popupWindow) {
	 	popupWindow.focus();
		//normal
		if ( type == "CtoC" || type == "CshiftL" || type == "CshiftT" ) {
			popup_stepsizer(popupWindow, w, h, dw, dh, steps);
		}
		if ( type = "IzoomH" ) {
			//popup_zoom(popupWindow, w, h);
		}
		if ( type == "BtoT" || type == "BtoC" || type == "BtoB" ) {
			moveToCenter(popupWindow, posLeft, posLeft, posTop, absTop); 
		}
		if ( type == "RtoL" || type == "RtoC" || type == "RtoR" ) {
			moveToCenter(popupWindow, posLeft, absLeft, posTop, posTop); 
		}
		//frameless
		if ( noframe && ie ) {
			popupWindow.resizeTo(w,h);
			popupWindow.moveTo(posLeft,posTop);
		}
		return false; 
		}
	else return true; //open didn't work...
}

/*	centers a window on the screen
 *	@param window to center
 *	@param width of window
 * 	@param height of window
 */
function popup_center(win, w, h) 
{
	var x = (envScreen.width - w) / 2
	var y = (envScreen.height - h) / 2 - 40;
	win.moveTo(x, y);
}


/*	helper: resizes a window and centers it at every step
	@param window to shift
	@param w target width
	@param h target height
	@param dw amount to resize w in one step
	@param dh amount to resize h in one step
	@param steps 
 */
function popup_stepsizer(win, w, h, dw, dh, steps) 
{
	popup_center(win, w, h);
	for	(var i = 0; i < steps; i++) {	
		win.resizeBy(dw, dh);
		w += dw;
		h += dh;
		popup_center(win, w, h);
	}
	win.resizeBy(16,16);
	win.resizeBy(-16,-16);
	return true;
	}

/*	moves a window from one point to the other
 *	@param window to move
 */
function moveToCenter (w,x1,x2,y1,y2)
{	
	var d = 4;
	if (x1 == x2) { 
		for (y1; y1 > y2; y1 -= d) {
			w.moveTo(x1, y1);
			if (y1 < 75) d = 1;
		}
	}
	if (y1 == y2) { 
		for (x1; x1 > x2; x1 -= d) {
			w.moveTo(x1, y1); 
			if (x1 < 75) d = 1;
		}
	}
}

function changeOpener(url) {
	if (window.opener && window.opener.location) window.opener.location.href = url;
	}
