// Original 2003 code from codelifter.com extensively
// adapted by Leigh Harrison http://www.else.co.nz

isIE = document.all;
isNN = !document.all && document.getElementById;
isN4 = document.layers;
isHot = false;

var ddEnabled = false;
var nowX = 0;
var nowY = 0;

document.onmousedown = mouseDown;
document.onmouseup = mouseUp; 

function mouseDown(e){
  topDog = isIE ? "BODY" : "HTML";
  whichDog = isIE ? document.all.layerPopup : document.getElementById("layerPopup");  
  hotDog = isIE ? event.srcElement : e.target;  
  while (hotDog && hotDog.id != "titleBar" && hotDog.tagName != topDog){
    hotDog = isIE ? hotDog.parentElement : hotDog.parentNode;
  }  
  if (hotDog && hotDog.id == "titleBar"){
    offsetx = isIE ? event.clientX : e.clientX;
    offsety = isIE ? event.clientY : e.clientY;
    nowX = parseInt(whichDog.style.left);
    nowY = parseInt(whichDog.style.top);
    ddEnabled = true;
    document.onmousemove = mouseMove;
  }
}

function mouseUp() {
  ddEnabled = false;
}

function mouseMove(e){
  if (ddEnabled == false) return;
  iLeft = isIE ? nowX + event.clientX - offsetx : nowX + e.clientX - offsetx;
  iTop = isIE ? nowY + event.clientY - offsety : nowY + e.clientY - offsety;
  whichDog.style.left = iLeft + 'px'; 
  whichDog.style.top = iTop + 'px';
  return false;  
}

function ddN4(whatDog){
  if (!isN4) return;
  N4 = eval(whatDog);
  N4.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP);
  N4.onmousedown = function(e){
    N4.captureEvents(Event.MOUSEMOVE);
    N4x = e.x;
    N4y = e.y;
  }
  N4.onmousemove = function(e){
    if (isHot){
      N4.moveBy(e.x - N4x, e.y - N4y);
      return false;
    }
  }
  N4.onmouseup = function(){
    N4.releaseEvents(Event.MOUSEMOVE);
  }
}

function hideMe() {
  if (isIE || isNN) whichDog.style.visibility = "hidden";
  else if (isN4) document.layerPopup.visibility = "hide";
  var oFrame = document.getElementById('layerContent');
  if (oFrame) {
    oFrame.src = "";
    oFrame.height = Math.round(getDocumentPosition().height * 0.9);
  } 
  obj("layerTitle").innerHTML = '<b>&nbsp;Loading ...&nbsp;</b>';
  obj("layerBackground").style.display = "none";
}

function showMe(sFile){
  // Removed until we have opportunity to work on it some more 
  // placeBackground();
  var oFrame = document.getElementById('layerContent');
  if (oFrame) {
    oFrame.src = sFile;
    oFrame.height = Math.round(getDocumentPosition().height * 0.9);
  }  
  placeLayer();
  if (isIE || isNN) {
    whichDog.style.visibility = "visible";
  }  
  else if (isN4) {
    document.layerPopup.visibility = "show";
  }      
}

function focusPopupButton(oObject) {
  oObject.className = 'focusButton';
}

function blurPopupButton(oObject) {
  oObject.className = 'normalButton'; 
}

function showTitle() {
  var oSpan = top.document.getElementById('layerTitle'); 
  var oFrame = top.document.getElementById('layerContent');
  oSpan.innerHTML = '';
  if (oFrame && oSpan) {
    var sTitle = (oFrame.contentDocument || oFrame.contentWindow.document).title;
    if (sTitle) {
      oSpan.innerHTML = '<b>&nbsp;' + sTitle + '</b>';      
    }
  }
}  

function getDocumentPosition() {
  var iebody = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
  var iLeft = document.all ? iebody.scrollLeft : window.pageXOffset;
  var iTop = document.all ? iebody.scrollTop : window.pageYOffset;
  var iWidth = document.all ? iebody.clientWidth : window.innerWidth;
  var iHeight = document.all ? iebody.clientHeight : window.innerHeight;
  return {
    left: iLeft,
    top: iTop,
    width: iWidth,
    height: iHeight
  };
}

function placeLayer() {
  var oLayer = document.getElementById('layerPopup');
  var ptElement = getElementOffset(oLayer);
  var ptParent = getElementOffset(oLayer.offsetParent);
  var ptViewport = getDocumentPosition();
  // Don't make this conditional - always reposition
  //if (!oLayer.style.left || ptElement.left < ptViewport.left) {
    var iLeft = Math.round(ptViewport.left + ((ptViewport.width - oLayer.clientWidth) / 2)) - ptParent.left;
    oLayer.style.left = iLeft + 'px';
  //}
  nowX = parseInt(oLayer.style.left);
  //if (!oLayer.style.top || ptElement.top < ptViewport.top || ptElement.top > (ptViewport.top + ptViewport.height)) {
    var iTop = Math.round(ptViewport.top + ((ptViewport.height - oLayer.clientHeight) / 2)) - ptParent.top;
    oLayer.style.top = iTop + 'px';
  //}
  nowY = parseInt(oLayer.style.top);
}    

function placeBackground() {
  var oBackground = document.getElementById("layerBackground");
  if (oBackground) {
    var oOffset = getElementOffset(oBackground);
    oBackground.style.left = (oOffset.left * -1) + "px";
    oBackground.style.top = (oOffset.top * -1) + "px";
    oBackground.style.width = window.innerWidth;
    oBackground.style.height = getDocHeight(document);
    oBackground.style.display = "block";
  }  
}
