//  Copyright © 1999-2005 Red Hen Systems, Inc.
//              All Rights Reserved

var linkToId = 'video';
var useMapLayerId = 'videoMap';
var listId;
var isListShown = 'false';

var passedString = window.location.search;
var startOfLink = passedString.indexOf("curlink=");
if (startOfLink != -1) {
  linkToId = passedString.substring(startOfLink + 8, passedString.indexOf("+", startOfLink + 8));
  useMapLayerId = linkToId + 'Map';
}

if (document.layers) {
  var docCode = 'document.';
  var visCode = '.visibility';
  var styleLeft = '.style.left';
  var styleTop = '.style.top';
}
if (document.all) {
  var docCode = 'document.all.';
  var visCode = '.style.visibility';
  var styleLeft = '.style.left';
  var styleTop = '.style.top';
}

function showLayer(layerId, leftPos, topPos) {
  eval('frameMap.' + docCode + layerId + visCode + ' = "visible"');
  eval('frameMap.' + docCode + layerId + styleLeft + ' = ' + leftPos);
  eval('frameMap.' + docCode + layerId + styleTop + ' = ' + topPos);
}

function hideLayer(layerId) {
  eval('frameMap.' + docCode + layerId + visCode + ' = "hidden"');
}

function initUseMap() {
  hideLayer('allMap');
  hideLayer('videoMap');
  hideLayer('attachMap');
  showLayer(useMapLayerId, 0, 0);
}

function changeUseMap(linkTo) {
  hideLayer(useMapLayerId);
  linkToId = linkTo;
  useMapLayerId = linkTo + 'Map';
  showLayer(useMapLayerId, 0, 0);
}

function changeBrowserMap() {
  if (parent.nodeRoot)
    parent.buildBrowser(parent.nodeRoot);
  window.frameBrowser.location.href = window.frameBrowser.location.href;
}

function initLinks() {
  initUseMap();
  changeBrowserMap();
}

function changeLinks(linkTo) {
  hideList();
  changeUseMap(linkTo);
  changeBrowserMap(linkTo);
}

function rememberLink(htmlpage) {
  window.location.href = htmlpage + '?curlink=' + linkToId + '+';
}

function setLinkButton() {
  eval('frameLinkTo.' + docCode + 'linkto' + linkToId + '.checked = true');
  setSize('fs0');
}

var bColMap = 0;
var bRowMap = 0;

function setSize(fsId) {
  if (document.all(fsId).cols != "") {
    setWidths(fsId);
  } else if (document.all(fsId).rows != "") {
    setHeights(fsId);
  }
}

function setWidths(fsId) {
  var TotalWidth = 0;
  var oObj;
  var strWidth = "";
  var strId;
  var saveWCnt;
  for(widthCnt = 0; widthCnt < document.all(fsId).children.length; widthCnt++) {
    TotalWidth = 0;
    if (document.all(fsId).children(widthCnt).tagName == "FRAME") {
      //Get the frame ID
      strId = document.all(fsId).children(widthCnt).id;
      if (strId != "frameMap") {
        //Get all DIV elements in the frame document
        oObj = document.frames(strId).document.all.tags("DIV");

        if (oObj != null) {
          //Accumulate the widths of the objects
          for(i = 0; i < oObj.length; i++) {
            TotalWidth = oObj(i).offsetWidth + TotalWidth;
          }

          TotalWidth = TotalWidth + 40; //pad spacing

          //Add to the Col property string
          strWidth = strWidth + TotalWidth;

          //If this is not the last child in the frameset, add a comma to the string
          if (widthCnt != document.all(fsId).children.length - 1) {
            strWidth = strWidth + ",";
          }
        } //oObj != null

      } else {
        //If the frame is "frameMap", just add a "*" to the string
        strWidth = strWidth + "*";
        if (widthCnt != document.all(fsId).children.length - 1) {
          strWidth = strWidth + ",";
        }
        bColMap = 1;
      } //frameMap

    } else if (document.all(fsId).children(widthCnt).tagName == "FRAMESET") {
      //Save Counter
      saveWCnt = widthCnt;

      setSize(document.all(fsId).children(widthCnt).id);

      //Retrieve saved counter
      widthCnt = saveWCnt;

      if (bRowMap) {
        strWidth = strWidth + "*";
        bRowMap = 0;
      } else {
        //Get the instance of the first element in the frame in the nested frameset
        if (document.all(fsId).children(widthCnt).children(0).tagName == "FRAMESET") {
          strWidth = strWidth + "*";
        } else {
          var tempObj = document.frames(document.all(fsId).children(widthCnt).children(0).id).document.all.tags("DIV");
          var maxWidth = 0;
          for (j = 0; j < tempObj.length; j++) {
            if (maxWidth < tempObj(j).offsetWidth) {
              maxWidth = tempObj(j).offsetWidth + 10; //padding
            }
          }

          strWidth = strWidth + maxWidth;
        }
      }

      if (widthCnt != document.all(fsId).children.length - 1) {
        strWidth = strWidth + ",";
      }
    } //frame
  } //for children

  document.all(fsId).cols=strWidth;
}

function setHeights(fsId) {
  var TotalHeight = 0;
  var oObj;
  var strHeight = "";
  var strId;
  var saveHCnt;
  for(eCnt = 0; eCnt < document.all(fsId).children.length; eCnt++) {
    TotalHeight = 0;
    if (document.all(fsId).children(eCnt).tagName == "FRAME") {
      //Get the frame ID
      strId = document.all(fsId).children(eCnt).id;
      if (strId != "frameMap") {
        //Get all DIV elements in the frame document
        oObj = document.frames(strId).document.all.tags("DIV");

        if (oObj != null) {
          //Accumulate the Heights of the objects
          for(i = 0; i < oObj.length; i++) {
            TotalHeight = oObj(i).offsetHeight + TotalHeight;
          }

          TotalHeight = TotalHeight + 40; //pad spacing

          //Add to the Row property string
          strHeight = strHeight + TotalHeight;

          //If this is not the last child in the frameset, add a comma to the string
          if (eCnt != document.all(fsId).children.length - 1) {
            strHeight = strHeight + ",";
          }
        } //oObj != null

      } else {
        //If the frame is "frameMap", just add a "*" to the string
        strHeight = strHeight + "*";
        if (eCnt != document.all(fsId).children.length - 1) {
          strHeight = strHeight + ",";
        }
        bRowMap = 1;
      } //frameMap

    } else if (document.all(fsId).children(eCnt).tagName == "FRAMESET") {
      //Save Counter
      saveHCnt = eCnt;

      setSize(document.all(fsId).children(eCnt).id);

      //Retrieve saved counter
      eCnt = saveHCnt;

      if (bColMap) {
        strHeight = strHeight + "*";
        bColMap = 0;
      } else {
        //Get the instance of the first element in the frame in the nested frameset
        if (document.all(fsId).children(eCnt).children(0).tagName == "FRAMESET") {
          strHeight = strHeight + "*";
        } else {
          var tempObj = document.frames(document.all(fsId).children(eCnt).children(0).id).document.all.tags("DIV");
          var maxHeight = 0;
          for (j = 0; j < tempObj.length; j++) {
            if (maxHeight < tempObj(j).offsetHeight) {
              maxHeight = tempObj(j).offsetHeight + 10; //padding
            }
          }

          strHeight = strHeight + maxHeight;
        }
      }

      if (eCnt != document.all(fsId).children.length - 1) {
        strHeight = strHeight + ",";
      }
    } //frame
  } //for children

  document.all(fsId).rows=strHeight;
}

function showList(listLayerId, leftPos, topPos) {
  hideList();
  listId = linkToId + listLayerId;
  if (eval('frameMap.' + docCode + listId + '.all[1].size') == 1)
    parent.openWindow(eval('frameMap.' + docCode + listId + '.all[1].options[0].value'),
                      eval('frameMap.' + docCode + listId + '.all[1].options[0].label'));
  else
  {
    showLayer(listId, leftPos, topPos);
    isListShown = 'true';
  }
}

function hideList() {
  if (isListShown == 'true') {
    hideLayer(listId);
    isListShown = 'false';
  }
}

function showListItem(linkToFile, linkToTitle, windowAction) {
  if (linkToFile != '') {
    hideList();
    if (windowAction == "newwindow")
      parent.openWindow(linkToFile, linkToTitle);
    else if (windowAction == "samewindow")
      rememberLink(linkToFile);
    }
  if (linkToTitle == '<Cancel>')
    hideList();
}
