var minW = 400;
var minH = 240;

function getPageWH(xPart)
{  
  if(typeof window.innerWidth  == 'number' ) 
  {
    pageW = window.innerWidth;
    pageH = window.innerHeight;
  } 
  else if(document.documentElement && document.documentElement.clientWidth ) 
  {
    pageW = document.documentElement.clientWidth;
    pageH = document.documentElement.clientHeight;
  } 
  else{
    pageW = document.body.clientWidth;
    pageH = document.body.clientHeight;
  }
  
  if(xPart=="w")
  {
    return pageW;
  }
    else return pageH;
}

/*When called will resize the window according to the passed in aspect ratio(w/h) based on the windows current height.
  If the calulated width of the new window will be wider than the users screen size then the window will be resized based on 
  width so that the new window will fit in the screen. 
  
  */
function aspectResize(ratio)
{
  var scrW = screen.width; //get users screen resolution
  var scrH = screen.height; //get users screen resolution
  /*var winW = parseInt(getPageWH("w")); //get document width
  var winH = parseInt(getPageWH("h")); //get document height*/
  
  var winW = scrW; //get document width
  var winH = scrH; //get document height
  
  //Firefox will not calculate right if window height is less than 500px
  if(winH < 500)  
    winH = 500;
  
  window.resizeTo(winW, winH); //resize window to known values
  var docW = parseInt(getPageWH("w"));  //get new document width
  var docH = parseInt(getPageWH("h"));  //get new document height
  var dw = winW - docW;  //difference of window and document width
  var dh = winH - docH; //difference of window and document height
  winW = Math.round(docH*ratio)+dw;  //new width based on aspect ration, height window height difference  
  
  //if calulated screen width is wider than the screen
  if(winW > scrW)
  {
    //calculate new window size based on the width so widow won't be wider the the screen. Window won't resize right if it is wider than screen
    winH = Math.round(docW/ratio)+dh;
    winW = scrW
  }

  window.resizeTo(winW, winH);  //resize to the new aspect ration
  docW = parseInt(getPageWH("w"));  //get new document width
  docH = parseInt(getPageWH("h"));  //get new document height
  
  /*Testing Code*/
  document.getElementById("blah").innerHTML = "Window = " + winW + ", " + winH + "<br>Document = " + docW + "," + docH + "<br>Diff = " + parseInt(dw) + "," + parseInt(dh) + "<br>Aspect Ratio = " + (docW/docH) + "<br>Screen Width=" + scrW + ", Screen Height=" + scrH;
}


function VerifySize(ratio)
{
  var resizeMe = true;
  var winW = parseInt(getPageWH("w"));  //get page width
  var winH = parseInt(getPageWH("h"));  //get page height
  
  //check for minimum height
  if(minH>winH)
    winH=minH;

  alert('ratio = ' + Math.round(ratio));
    
  if((winW/winH) == ratio)
  {
    resizeMe=false;
  }
  else
  {
    winW = winH*ratio;
    resizeMe=true;
  }
  
 
  if(resizeMe)
  {
    window.resizeTo(winW,winH);
    var winW2 = winW - parseInt(getPageWH("w"));
    var winH2 = winH - parseInt(getPageWH("h"));
    window.resizeTo(winW + winW2,winH + winH2);
  }
  document.getElementById("blah").innerHTML = "(" + parseInt(getPageWH("w")) + "," + parseInt(getPageWH("h")) + ")";
}

 
 

/*function resize(ratio)
{
  if(trigger==null)
  {
    trigger = 0;
    //alert("Trigger undefined!");
  }

  var dy = window.innerHeight, dx;
  if(dy == undefined)
  {
    dy = document.body.clientHeight+125;
    dx = dy*ratio;
    if(trigger == 0)
    {
      trigger=1;
      window.resizeTo(dx, dy);
      //alert("Resized!");
      
    }
    else
    {
      //alert("Trigger Reset");
      trigger=0;
    }
  }
  else
  {
    dx = dy*ratio;
    window.innerWidth = dx;
  }
}*/