document.observe("dom:loaded", function() {

  checkBrowserSize();

  if (window.emergency) {
    showAlert();
  }

  if ($("notification")) {
    Event.observe(window, "resize", positionAbsolutes);
    positionAbsolutes();
  }

});


function checkBrowserSize() {
  //If the browser is not large enough for our ideal size, enlarge it where appropriate:

  var wantedWidth = 930 + 50;   //50 for padding
  var wantedHeight = 700 + 10;  // 10 for padding

  //Current screen size:
  var winSize = document.viewport.getDimensions();
  var w = winSize.width;
  var h = winSize.height;

  //Delta to grow window by
  var dw = 0;
  var dh = 0;

  var resizeFlag = false;
  if (w < wantedWidth) {
    dw = wantedWidth - w;
    resizeFlag = true;
  }

  if (h < wantedHeight) {
    dh = wantedHeight - h;
    resizeFlag = true;
  }

  if (resizeFlag) {
    //alert(dw + ' x ' + dh);
    window.resizeBy(dw,dh);
  }
}



function showAlert() {
  var alertEl = $('alertBox').show();

  var winSize = document.viewport.getDimensions();
  //TODO, what about screen resize:

  var w = 600;
  var h = 500;

  alertEl.setStyle({ width: (w) + "px", height: (h) + "px"});
  alertEl.setStyle({ left: ((winSize.width - w)/2) + "px", top: ((winSize.height - h)/2) + "px"});


  var iframe = document.createElement('iframe');
  iframe.setAttribute('id', 'alertIframe');
  iframe.setAttribute('name', 'alertIframe');
  iframe.setAttribute('src', window.alertFile);
  iframe.setAttribute('height', '100%');
  iframe.setAttribute('width', '100%');
  iframe.setAttribute('ALLOWTRANSPARENCY', 'true');
  iframe.setAttribute('frameBorder', '0');  //IE7 needs this mixed case key
  iframe.setAttribute('marginwidth', '0');
  iframe.setAttribute('marginheight', '0');
  iframe.setAttribute('scrolling', "no");

  $("alertBoxContent").update(iframe);
}


function hideAlert() {
  $('alertBox').remove();
}

function positionAbsolutes() {
  var notificationEl = $("notification");
  var winSize = document.viewport.getDimensions();
	var flashWidth = 930;
	var flashHeight = 480;

  if (notificationEl) {

    var notificationTopOffset = 65;
    var notificationLeftOffset = 0;

    var leftPadding = (winSize.width - flashWidth)/2;

    var positionLeft = 0;
    if (leftPadding > notificationLeftOffset) {
      positionLeft = leftPadding;
    }

    notificationEl.setStyle({
      left: positionLeft + "px",
      top: notificationTopOffset + "px"
    });
  }
}
