//
// Javascript functions for window operations
//
// dependencies: dreamFuncs.js, browserFuncs.js
//

// popup window variable
var jsWindow = null;

// resize status variable
var lastWindowWidth = 0;
var lastWindowHeight = 0;
var newWindowWidth = 0;
var newWindowHeight = 0;

function getScreenInfo()
{
	this.availHeight = screen.availHeight; 
	this.availWidth = screen.availWidth; 
	this.screenHeight = screen.height; 
	this.screenWidth = screen.width; 
	this.colorDepth = screen.colorDepth;
	// only valid for IE
	this.fontSmoothingEnabled = (screen.fontSmoothingEnabled == true ? 1 : 0);
	
	return this; 
}

function getWindowInfo(windowToCheck)
{
	if (windowToCheck.innerHeight) {
		this.innerHeight = windowToCheck.innerHeight; 
		this.innerWidth = windowToCheck.innerWidth; 
		this.outerHeight = windowToCheck.outerHeight; 
		this.outerWidth = windowToCheck.outerWidth; 
		this.trueInnerHeight = windowToCheck.innerHeight; 
		this.trueInnerWidth = windowToCheck.innerWidth; 
		this.trueOuterHeight = windowToCheck.outerHeight; 
		this.trueOuterWidth = windowToCheck.outerWidth; 
	} else {
		this.offsetWidth = document.body.offsetWidth;
		this.offsetHeight = document.body.offsetHeight;
		this.clientWidth = document.body.clientWidth;
		this.clientHeight = document.body.clientHeight;
		this.trueInnerHeight = document.body.clientHeight; 
		this.trueInnerWidth = document.body.clientWidth; 
		this.trueOuterHeight = document.body.offsetHeight; 
		this.trueOuterWidth = document.body.offsetWidth; 
	}
	
	return this; 
}

function getWindowWidth()
{
    var ns = navigator.appName == "Netscape";
    var ns4 = (ns && parseInt(navigator.appVersion) == 4);
    var ns5 = (ns && parseInt(navigator.appVersion) > 4);

  if (!ns) {
    screenWidth = document.body.offsetWidth;
  } else if (ns4) {
    screenWidth = window.innerWidth;
  } else if (ns5) {
    screenWidth = window.innerWidth;
  }
  
  return screenWidth;
}

function getWindowHeight()
{
    var ns = navigator.appName == "Netscape";
    var ns4 = (ns && parseInt(navigator.appVersion) == 4);
    var ns5 = (ns && parseInt(navigator.appVersion) > 4);

  if (!ns) {
    screenHeight = document.body.offsetHeight;
  } else if (ns4) {
    screenHeight = window.innerHeight;
  } else if (ns5) {
    screenHeight = window.innerHeight;
  }
  
  return screenHeight;
}

function reloadPage(url) 
{
	window.location.href = unescape(url);

	browserInfo = getBrowserInfo();
	if (browserInfo.browserName == "MSIE") {
		window.navigate(unescape(url));
	}
}
 
function scrollWindow(windowToScroll,positionLeft,positionTop) 
{
	windowToScroll.scrollTo(positionLeft, positionTop);
}
 
function placeWindow(windowToPlace,windowLeft,windowTop) 
{
	windowToPlace.moveTo(windowLeft, windowTop);
}
 
function resizeWindowBy(windowToResize,windowWidthDiff,windowHeightDiff) 
{
	windowToResize.resizeBy(windowWidthDiff, windowHeightDiff);
}
 
function resizeWindow(windowToResize,windowWidth,windowHeight) 
{
	// Identify OS, Browser Type and Version 
	//browserInfo = getBrowserInfo();

	//if (browserInfo.browserName == "MSIE") {
	//	if (browserInfo.osCategory != "Mac") {
	//		windowHeight -= 16;
	//	}
	//}

	windowToResize.resizeTo(windowWidth, windowHeight);
}
 
function openWindow(url,name,winLeft,winTop,winWidth,winHeight,decoration) 
{
   // Identify OS, Browser Type and Version 
   browserInfo = getBrowserInfo();
   screenInfo = getScreenInfo();

   scrollbarsFlag="no";
   if (screenInfo.screenWidth < winWidth) {
       winWidth = screenInfo.screenWidth - 40;
       scrollbarsFlag="yes";
   }
   if (screenInfo.screenHeight < (winHeight+90)) {
       winHeight = screenInfo.screenHeight - 90;
       scrollbarsFlag="yes";
   }

   windowParameters = "";
   if (browserInfo.browserName != "MSIE") {
        windowParameters = 'screenX=' + winLeft + ',screenY=' + winTop + ',width=' + winWidth + ',height=' + winHeight;
   } else {
       if (browserInfo.osCategory == "Mac") {
          windowParameters = 'left=' + winLeft + ',top=' + winTop + ',width=' + winWidth + ',height=' + winHeight;
       } else {
          windowParameters = 'left=' + winLeft + ',top=' + winTop + ',width=' + winWidth + ',height=' + (winHeight-16);
	   }
   }
   
   windowParameters = windowParameters + ', scrollbars=' + scrollbarsFlag;
   if (decoration != null || decoration != "") {
       windowParameters = windowParameters + ',' + decoration;
   }
   if (scrollbarsFlag == "yes" && browserInfo.browserName != "IE") {
       resizeIndex = windowParameters.indexOf(",resizable");
       if (resizeIndex >= 0) {
           windowParameters = windowParameters.substring(0, resizeIndex);
       }
       windowParameters = windowParameters + ',resizable';
   }
   
   //alert(windowParameters);
   jsWindow = window.open(url,name,windowParameters);
   jsWindow.focus();
}

function openCentredWindow(url,name,winWidth,winHeight,decoration) 
{
   screenInfo = getScreenInfo();

   winLeft = (screenInfo.screenWidth - winWidth) / 2;
   if (winLeft < 0)
        winLeft = 0;
   winTop = (screenInfo.screenHeight - winHeight) / 2;
   if (winTop < 0)
        winTop = 0;

   openWindow(url,name,winLeft,winTop,winWidth,winHeight,decoration);
}

function preloadPageIntoWindow(winName, defaultURL, winWidth, winHeight)
{
	preloadPageIntoWindow(winName, defaultURL, winWidth, winHeight, false);
}

function preloadPageIntoWindow(winName, defaultURL, winWidth, winHeight, resizable)
{
	index = document.URL.lastIndexOf("#");
	if (index >= 0) {
		newURL = document.URL.substring(index+1, document.URL.length); 
	} else {
		newURL = defaultURL;
	}

	if (resizable == false) {	
		openCentredWindow(newURL,winName,winWidth,winHeight,'menubar,toolbar,resizable=no');
	} else {
		openCentredWindow(newURL,winName,winWidth,winHeight,'menubar,toolbar,resizable=yes,scrollbars=no');
	}
}

function handleWindowResize(layoutWidth, layoutHeight)
{
	screenInfo = getScreenInfo(window);
	windowInfo = getWindowInfo(window);
	
	desiredWidth = windowInfo.trueInnerWidth;
	if (desiredWidth < layoutWidth) {
		desiredWidth = layoutWidth;
	}
	if (desiredWidth > (screenInfo.screenWidth - 40)) {
		desiredWidth = screenInfo.screenWidth - 40;
	}

	desiredHeight = Math.ceil(desiredWidth * layoutHeight / layoutWidth);
	if (desiredHeight < layoutHeight) {
		desiredHeight = layoutHeight;
	}
	if (desiredHeight > (screenInfo.screenHeight - 40)) {
		desiredHeight = screenInfo.screenHeight - 40;
	}

	if (windowInfo.trueInnerWidth != desiredWidth || windowInfo.trueInnerHeight != desiredHeight) {
/*
		alert(
		"trueInnerWidth: " + windowInfo.trueInnerWidth + "\n" +
		"trueInnerHeight: " + windowInfo.trueInnerHeight + "\n" +
		"trueOuterWidth: " + windowInfo.trueOuterWidth + "\n" +
		"trueOuterHeight: " + windowInfo.trueOuterHeight + "\n" +
		"scrollWidth: " + windowInfo.scrollWidth + "\n" +
		"scrollHeight: " + windowInfo.scrollHeight + "\n" +
		"desiredWidth: " + desiredWidth + "\n" +
		"desiredHeight: " + desiredHeight + "\n"
		);
*/	
		resizeWindowBy(window,desiredWidth-windowInfo.trueInnerWidth,desiredHeight-windowInfo.trueInnerHeight);
	}
}

function enforceWindowSize(layoutWidth, layoutHeight)
{
	screenInfo = getScreenInfo(window);
	windowInfo = getWindowInfo(window);
	
	diffWidth = 0;
	diffHeight = 0;
	
	desiredWidth = layoutWidth;
	if (desiredWidth > (screenInfo.screenWidth - 40)) {
		desiredWidth = screenInfo.screenWidth - 40;
	}
	diffWidth = desiredWidth - windowInfo.trueInnerWidth;

	desiredHeight = layoutHeight;
	if (desiredHeight > (screenInfo.screenHeight - 40)) {
		desiredHeight = screenInfo.screenHeight - 40;
	}
	diffHeight = desiredHeight - windowInfo.trueInnerHeight;

	if (diffWidth != 0 || diffHeight != 0) {
		resizeWindowBy(window,diffWidth,diffHeight);
	}
}
