// Returns true if standard flow is needed, ie, the browser check FAILS
// Returns false if standard flow is not needed, ie, the broser check PASSES
// That way, the caller can set the onClick to "return login()" or "return migrate()"

function getIFrameHeight(doc) {
    var height = 0;
    if (doc.height) {
        height = doc.height - 20;
    }
    else {
        if (doc.body.scrollHeight) height = Math.max(height, doc.body.scrollHeight);
        if (doc.body.offsetHeight) height = Math.max(height, doc.body.offsetHeight);
    }
    return height;
}

function getIFrameWidth(doc)
{
  var width = 0;
  if (doc.width) {
    width = doc.width;
  } else {
    if (doc.body.scrollWidth) width = Math.max(width, doc.body.scrollWidth);
    if (doc.body.offsetWidth) width = Math.max(width, doc.body.offsetWidth);
  }
  return width;
}

function setIFrameHeight(doc)
{
  var node = document.getElementById('loginframe');
  node.style.height = "auto";
  var height = getIFrameHeight(doc);
  node.style.height = height + "px";
  return height;
}

function setIFrameWidth(doc)
{
  var node = document.getElementById('loginframe');
  node.style.width = "auto";
  var width = getIFrameWidth(doc);
  node.style.width = width + "px";
  return width;
}

function getIFrameDoc()
{
  var node = document.getElementById("loginframe");

  // get the iframe document
  var doc;
  if (node.contentDocument) {
    // NS6
    doc = node.contentDocument;
  } else if (node.contentWindow) {
    // IE5.5+
    doc = node.contentWindow.document;
  } else if (node.document) {
    // IE5
    doc = node.document;
  }

  return doc;
}

function centerIFrame(minW,minH,maxW,maxH)
{
  var node = document.getElementById("loginframe");
  var doc = getIFrameDoc();

  // calculate frame content height
  setIFrameHeight(doc);
  setIFrameWidth(doc);

  // resize if necessary
  var w = node.style.width.replace(/px/,'');
  var h = node.style.height.replace(/px/,'');
  if (minW && w < minW) {w = minW; node.style.width  = w + 'px';}
  if (minH && h < minH) {h = minH; node.style.height = h + 'px';}
  if (maxW && w > maxW) {w = maxW; node.style.width  = w + 'px';}
  if (maxH && h > maxH) {h = maxH; node.style.height = h + 'px';}

  // center the frame
  recenterIFrame();
}

function recenterIFrame()
{
  var node = document.getElementById("loginframe");
  var doc = getIFrameDoc();
  var scrOff = getScrollOffset();
  var w = node.style.width.replace(/px/,'');
  var h = node.style.height.replace(/px/,'');

  // calculate and position the frame in the center of the display area
  var width = window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth);
  var height = window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight);
  var left = Math.max(5, Math.round(width / 2 - w / 2 + scrOff[0]));
  var top = Math.max(5, Math.round(height / 2 - h / 2 + scrOff[1]));
  node.style.left = left + 'px';
  node.style.top = top + 'px';
}

function adjustIFrame(top, left, width, height)
{
  var node = document.getElementById("loginframe");
  node.style.top = top + 'px';
  node.style.left = left + 'px';
  node.style.width = width + 'px';
  node.style.height = height + 'px';
}

function centerAndDisplayIFrame(width,height,maxW,maxH)
{
  centerIFrame(width,height,maxW,maxH);
  var node = document.getElementById("loginframe");
  node.style.display = 'inline';
}

function loadIFrame(url, paramList)
{
  if((isMinIE5_5 && isWin) || isMinMoz1 || isMinSafari1_2 || isMinOpera8 || isMinKonq3_2) {
    if(!url || url == '') {url = '/MemberSignIn';}
    var params = new UrlParams();
    params.add('skin', 'iframe');
    if(paramList) {
      for (var i=0; i<paramList.length-1; i+=2) {
        params.add(paramList[i], paramList[i+1]);
      }
    }
    if(url.match(/\?/)) url += '&'; else url += '?';
    url = url + params.toString();

    var div  = document.getElementById('iframediv');
    var node = document.getElementById('loginframe');
    if(node) {
      node.src = url;
      node.style.display='inline';
    } else {
        div.innerHTML += '<iframe id="loginframe" name="loginframe" src="' + url + '" frameborder="0" scrolling="no" width="590" height="600" style="position: absolute; z-index: 9999;"></iframe>';
    }
    window.onresize = recenterIFrame;
    if(div) div.style.display='block';
    //window.onscroll = moveIFrame;
    return false;
  }
  return true;
}

// This is needed to prevent javascript security problems when subdomain pages open the
// iframe (e.g. click edit on a nexus page), a relative path is no good as it honors the <base href=""/>
function getUrlPrefix()
{
	var loc = document.location;
	var prefix = loc.protocol + "//" + loc.host;
	if (loc.port) prefix += ":" + loc.port;
	return prefix;
}

function login(paramList) {
  return loadIFrame(getUrlPrefix() + '/MemberSignIn', paramList);
}

function migrate(paramList) {
  return loadIFrame(getUrlPrefix() + '/MemberScreenName', paramList);
}

function hideIFrame()
{
  var node = parent.document.getElementById('loginframe');
  if(node) {
    node.style.display = 'none';
    node.src='';
  }
  node = document.getElementById('iframediv');
  if(node) node.style.display='none';
  if(parent.loginCancel) parent.loginCancel();
}
