//var picWin = null;
var returnVal = true;
var leftPosition = 0;
var topPosition = 0;
var scrollValue = "no";	
var resizableValue = "no";
var winWidth = 0;
var winHeight = 0;


function popWindow(page, popUpWidth, popUpHeight, scrollValue) {
	returnVal = true;
		
	if(scrollValue == "yes") resizableValue = "yes";
	else resizableValue = "no";
	
	leftPosition = 0;
	topPosition = 0;
	needResize = false;
	
	winWidth = popUpWidth; winHeight = popUpHeight;
	
	if(!is_ie4) {
		//Versuchen eventuelle offenes Fenster wieder zu schliessen
		try {
			picWin.close();
		} catch (e) {
			//alert(e)
		}
	}
	
	needResize = testAvailableSpace();
	
	//Position des Popups
	if(is_opera) {
		leftPosition = (window.innerWidth-winWidth)/2;
		topPosition = (window.innerHeight-winHeight)/2;
	} else {
		leftPosition = (screen.width) ? (screen.width-winWidth)/2 : 0;
		topPosition = (screen.height) ? (screen.height-winHeight-66)/2 : 0;
	}
	if(topPosition < 0) topPosition = 0;
	
	settings =  'height='+winHeight+',width='+winWidth+',top='+topPosition+',left='+leftPosition+',resizable='+resizableValue+',scrollbars='+scrollValue;
	picWin = window.open(page,'',settings);
		
	if(needResize) maximizeWindow(winWidth, winHeight, picWin);	
	if(!is_ie4) picWin.focus(); 
	returnVal = false;
}

function testAvailableSpace() {
	needResizeVal = false;
	
	if(is_opera) {
		if(window.innerWidth < winWidth) {
			scrollValue = "yes"; resizableValue = "yes";
			winWidth = window.innerWidth;
			needResizeVal = true;
		}
		if(window.innerHeight < winHeight) {
			scrollValue = "yes"; resizableValue = "yes";
			winHeight = window.innerHeight;
			needResizeVal = true;
		}
	} else {
		if(screen.availWidth < winWidth) {
			scrollValue = "yes"; resizableValue = "yes";
			winWidth = screen.availWidth;
			needResizeVal = true;
		}
		if(screen.availHeight < winHeight) {
			scrollValue = "yes"; resizableValue = "yes";
			winHeight = screen.availHeight;
			needResizeVal = true;
		}
	}
	return needResizeVal;
}

function maximizeWindow(winWidth, winHeight, winReference) {
	if(winWidth == screen.availWidth && winHeight == screen.availHeight) {
		winReference.moveTo(0,0);
	} else if(winWidth == screen.availWidth) {
		topPos = (screen.height-winHeight-66)/2; if(topPos<0) topPos = 0;
		winReference.moveTo(0,topPos);
	} else if(winHeight == screen.availHeight) {
		winReference.moveTo((screen.width-winWidth)/2,0);
		winWidth = winWidth + 16;
	}
	
	if(is_nav || is_gecko) {				
		winReference.outerWidth = winWidth;
		winReference.outerHeight = winHeight;
	} else {
		winReference.resizeTo(winWidth, winHeight);		
	}
}