/*******************************************************************************
FILE NAME    :window.js
DEPENDENCIES :browser.js
********************************************************************************
____________________________ API DOCUMENTATION BEGIN ___________________________
````````````````````````````````````````````````````````````````````````````````

````````````````````````````````````````````````````````````````````````````````
_____________________________ API DOCUMENTATION END ____________________________
*******************************************************************************/

//------------------------------------------------------------------------------
// BROWSER BODY/DOCUMENT FUNCTIONS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- returns height of body
function getBodyHeight() {
 if(gBrowser.ns6 || (gBrowser.isMac && gBrowser.ie)) return document.body.offsetHeight;
 else return document.body.scrollHeight;
}

//FUNCTION-- returns width of body
function getBodyWidth() {
 if(gBrowser.ns6 || (gBrowser.isMac && gBrowser.ie)) return document.body.offsetWidth;
 else return document.body.scrollWidth;
}

//FUNCTION-- returns vertical scroll position
function getScrollPositionV() {
 if(gBrowser.ie) return document.body.scrollTop;
 else return window.pageYOffset;
}

//------------------------------------------------------------------------------
// BROWSER BODY/DOCUMENT FUNCTIONS END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// BROWSER WINDOW FUNCTIONS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- returns height of browser window
function getWindowHeight() {
 if(gBrowser.ns || gBrowser.safari) return window.innerHeight;
 else return document.body.clientHeight;
}

//FUNCTION-- returns width of browser window
function getWindowWidth() {
 if(gBrowser.ns || gBrowser.safari) return window.innerWidth;
 else return document.body.clientWidth;
}

//------------------------------------------------------------------------------
// BROWSER WINDOW FUNCTIONS END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// SCREEN FUNCTIONS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- returns screen height
function getScreenHeight() {
 return parseInt(screen.height);
}

//FUNCTION-- returns screen width
function getScreenWidth() {
 return parseInt(screen.width);
}

//------------------------------------------------------------------------------
// SCREEN FUNCTIONS END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// POSITION FUNCTIONS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- positions a popup window from the same domain
function popUpPositionWindow(newWinFunCall, winObj, URL, alignment, xPos, yPos, popWinW, popWinH, offSetY) {
 if(((gBrowser.ie || gBrowser.ns) && gBrowser.majorVersion >= 5) || gBrowser.firefox || gBrowser.safari)
 {
  var theScreenW = getScreenWidth();
  var theScreenH = getScreenHeight();
  switch(alignment)
  {
   case "left":
    newWinFunCall(URL);
    if(checkWindowCoords(xPos, yPos, theScreenW, theScreenH, popWinW, popWinH)) eval(winObj+".moveTo(xPos, yPos)");
    break;
   case "centerX":
    var xPos = getCenterX(theScreenW, popWinW);
    newWinFunCall(URL);
    if(checkWindowCoords(xPos, yPos, theScreenW, theScreenH, popWinW, popWinH)) eval(winObj+".moveTo(xPos, yPos)");
    break;		
   case "right":
    var xPos = parseInt((theScreenW-popWinW)-xPos);
    newWinFunCall(URL);
    if(checkWindowCoords(xPos, yPos, theScreenW, theScreenH, popWinW, popWinH)) eval(winObj+".moveTo(xPos, yPos)");
    break;
   case "centerXY":
    var xPos = getCenterX(theScreenW, popWinW);
    var yPos = (getCenterY(theScreenH, popWinH)-offSetY);
    newWinFunCall(URL);
    if(checkWindowCoords(xPos, yPos, theScreenW, theScreenH, popWinW, popWinH)) eval(winObj+".moveTo(xPos, yPos)");
    break;
   default:
    alert("Error: Illegal alignment parameter");
  }
 }
 else newWinFunCall(URL); //if not specified browser
}

//FUNCTION-- returns x position for horizontal centering of an object
function getCenterX(theScreenW, objW) {
 var halfScreenW = parseInt(theScreenW/2);
 var halfObjW = parseInt(objW/2); 
 return (halfScreenW-halfObjW);
}

//FUNCTION-- returns y position for vertical centering of an object
function getCenterY(theScreenH, objH) {
 var verticalScrollPos = getScrollPositionV();
 if(objH >= theScreenH) {return verticalScrollPos;}
  
 var halfScreenH = parseInt(theScreenH/2);
 var halfObjH = parseInt(objH/2); 
 return ((halfScreenH-halfObjH)+verticalScrollPos);
}

//FUNCTION-- validate x and y position
function checkWindowCoords(xPos, yPos, theScreenW, theScreenH, objW, objH) {
 if(xPos < 0 || yPos < 0 || (xPos + objW) > theScreenW || (yPos + objH) > theScreenH) return false;
 else return true;
}

//FUNCTION-- validate x position
function checkWindowCoordsX(xPos, theScreenW, objW) {
 if(xPos < 0 || (xPos + objW) > theScreenW) return true;
 else return false;
}

//FUNCTION-- validate y position
function checkWindowCoordsY(yPos, theScreenH, objH) {
 if(yPos < 0 || (yPos + objH) > theScreenH) return true;
 else return false;
}

//------------------------------------------------------------------------------
// POSITION FUNCTIONS END
//------------------------------------------------------------------------------

//---END

