/*
 * pageLoad.js
 *
 * IT-Sundhed
 * Lasse F. Pedersen
 * 21/Dec/2004
 */

//Setting style possible in radEWrapper iframe. After updating editor - changed from div.  
function copyParentStylesToEditor(editor)
{
 var theMainEditorTable = document.getElementById("RadEWrapper" + editor.Id);
 var theParentEl = theMainEditorTable.parentNode.parentNode.parentNode.parentNode;
 var theContentArea = editor.GetContentArea();
 for (var attr in theParentEl.currentStyle)
 {
  if (attr.toLowerCase() == "overflow" ||
   attr.toLowerCase() == "overflowx" ||
   attr.toLowerCase() == "overflowy")
   continue;
  theContentArea.style[attr] = theParentEl.currentStyle[attr];
 }

  //Backgroundcolor in RadEContent, RadEContentBordered
  //editor.ContentWindow.document.body.style.backgroundColor="#28465D";
  editor.ContentWindow.document.body.style.color="#000000";
  editor.ContentWindow.document.body.style.paddingLeft="2px";
  editor.ContentWindow.document.body.style.marginRight="-155px";
  if (editor.ContentWindow.document.getElementsByTagName("H2").length != 0)
	 {editor.ContentWindow.document.getElementsByTagName("H2")[0].style.color="#000000";
	  editor.ContentWindow.document.getElementsByTagName("H2")[0].style.fontSize="16px";
	 }
  if (editor.ContentWindow.document.getElementsByTagName("H1").length != 0)
	 {editor.ContentWindow.document.getElementsByTagName("H1")[0].style.color="#000000";}
}


// Recalculate correct page height
function recalcHeight() {

  var left    = document.getElementById("left");
  var text    = document.getElementById("text");
  var context = document.getElementById("context");

  left.style.height = "auto";
  text.style.height = "auto";
  
  if(context) {
    context.style.height = "auto";
  }
  
  calcHeight();

}

// Calculate correct page height
function calcHeight() {

  // Site container
  var left     = document.getElementById("left");
  var leftH    = parseInt(left.offsetHeight) + offsetTop(left);
  var content  = document.getElementById("content");
  var contentH = parseInt(content.offsetHeight) + offsetTop(content);
  /*
  var text     = document.getElementById("text");
  var textH    = parseInt(text.offsetHeight) + offsetTop(text);
  */
  var context  = document.getElementById("context");
  var contextH = 0;
  var height   = 0;

  if(context) {
    contextH = parseInt(context.offsetHeight) + offsetTop(context);
  }

  /*
  alert("left("+leftH+")... offset:"+offsetTop(left)+", height:"+left.offsetHeight);
  alert("content("+contentH+")... offset:"+offsetTop(content)+", height:"+content.offsetHeight);
  alert("context("+contextH+")... offset:"+offsetTop(context)+", height:"+context.offsetHeight);
  */

  if(document.documentElement) {
    height = document.documentElement.clientHeight - document.getElementById("bottom").offsetHeight;
  }

  if(leftH > height) {
    height = leftH;
  }
  if(contentH > height) {
    height = contentH;
  }
  /*
  if(textH > height) {
    height = textH;
  }
  */
  if(contextH > height) {
    height = contextH;
  }

  // Compare to left
  if(height > leftH) {
    left.style.height = (height - paddingHeight(left)) + "px";
  }

  // Compare to content
  if(height > contentH) {
    content.style.height = (height - paddingHeight(content)) + "px";
  }
  
  /*
  // Compare to text
  if(height > textH) {
    text.style.height = (height - paddingHeight(text)) + "px";
  }
  */

  // Compare to context
  if(context && height > contextH) {
    context.style.height = (height - paddingHeight(context)) + "px";
  }

}

// Get top offset of object
function offsetTop(obj) {

  var offset = 0;

  while(obj != null) {
    offset += obj.offsetTop;
    obj     = obj.offsetParent;
  }

  return offset;
  /*
   * 02/May/2005: Ovenstående burde virke bedre på kryds af projekter...
   *
  if((obj.parentNode.currentStyle ? obj.parentNode.currentStyle.position : window.getComputedStyle(obj.parentNode, null).position) == "relative") {
    return obj.parentNode.offsetTop;
  } else {
    return obj.offsetTop;
  }
  */

}

// Get height of top and bottom extras (padding, border) of object
function paddingHeight(obj) {

  var paddingTop    = 0;
  var paddingBottom = 0;
  var borderTop     = 0;
  var borderBottom  = 0;

  if(obj.currentStyle) {
    paddingTop    = parseInt(obj.currentStyle.paddingTop) + offsetTop(obj);
    paddingBottom = parseInt(obj.currentStyle.paddingBottom);
    borderTop     = parseInt(obj.currentStyle.borderTopWidth);
    borderBottom  = parseInt(obj.currentStyle.borderBottomWidth);
  } else {
    paddingTop    = parseInt(window.getComputedStyle(obj, null).paddingTop) + offsetTop(obj);
    paddingBottom = parseInt(window.getComputedStyle(obj, null).paddingBottom);
    borderTop     = parseInt(window.getComputedStyle(obj, null).borderTopWidth);
    borderBottom  = parseInt(window.getComputedStyle(obj, null).borderBottomWidth);
  }

  /*
  alert(
    "paddingTop    = " + paddingTop + "\n" +
    "paddingBottom = " + paddingBottom + "\n" +
    "borderTop     = " + borderTop + "\n" +
    "borderBottom  = " + borderBottom + "\n"
  );
  */

  return (paddingTop ? paddingTop : 0) + (paddingBottom ? paddingBottom : 0) + (borderTop ? borderTop : 0) + (borderBottom ? borderBottom : 0);

}

/*
 * A register of code to be executed after page finishes loading
 * It it used by parts of the code that need to refresh something
 * after the page finishes loading
 *
 * onLoadExec() includes the initial recalcHeight() call by default
 * and must be the value of the onload attrib of <body>... Example:
 *
 * <body onload="javascript:onLoadExec()">
 *   <!-- template body here -->
 * </body>
 */

var onLoadList = new Array();

function onLoadAdd(script) {

  onLoadList.push(script);

}

function onLoadExec() {

  for(var i = 0; i < onLoadList.length; i++) {

    //alert("exec: " + onLoadList[i]);

    try {
      eval(onLoadList[i]);
    } catch(ex) {
      alert("eval() failed: " + ex);
    }
  }
  
  // Defined earlier in this file
  recalcHeight();
  
}

