/*
 Copyright M. Vesey 2000 - maptop.js
*/

// Make sure this is in the top frame
if (self != top) {
  top.location.href = location.href;
}
var IsMapOn;
var mapHleft;
var mapHtop;
var mapHwidth;
var mapHheight;
var mapVleft;
var mapVtop;
var mapVwidth;
var mapVheight;
var mapleft;
var maptop;
var mapwidth;
var mapheight;

function InitVars() {

  InitialiseData();

  ChangeCursor('wait','Loading Map Info');
 
// base directory for communications between mapwindow and index

//  base = location.href.substring(0,location.href.lastIndexOf('/') + 1);

// If mapwindow has been previously opened - re-establish contact
// This takes too long !

//  AssignMap('horizontal');

// If opened before use existing parameters

  if (top.mapwindow && top.mapwindow.orientation) {
    top.maptype = top.mapwindow.orientation;

    NewSize(top.maptype);
    NewPosition(top.maptype);
  }
// ... or set up new ones
  else {
    top.maptype = 'horizontal';
  
    top.mapVheight = 0;
    top.mapVwidth = 0;
    top.mapVleft = 0;
    top.mapVtop = 0;
    top.mapHheight = 0;
    top.mapHwidth = 0;
    top.mapHleft = 0;
    top.mapHtop =0;
  }
// Close at top level
  CloseMapWindow(top);
  ChangeCursor('auto');
  return;
}

function ChangeCursor(thisCursor,thisText) {

  if (document.body) {
    document.body.style.cursor=thisCursor;
  }
  else {
    document.cursor=thisCursor;
  }
  switch ( thisCursor ) {
    case 'wait'  : window.status=thisText + '. Please wait...'; break;
    default : window.status='';
  }
  return;
}

function WriteSpaces(thisNo) {

  var HTMLString = '';
  for(var i=1; i <= thisNo; i++) {
    HTMLString += '&nbsp;';
  }
  return HTMLString;
}

function DrawForm() { // add options for JavaScript screens

  var HTMLString;

  HTMLString = 'Draw Family Tree Map <input type="checkbox" name="MapOn" onClick="top.MapOnClicked();">' + WriteSpaces(9);
  HTMLString += 'Vertical   <input type="radio" name="tableType" value="vertical" onClick="top.OpenMapWindow(this.value,false);">';
  HTMLString += 'Horizontal <input type="radio" name="tableType" value="horizontal" onClick="top.OpenMapWindow(this.value,false);">';
//  HTMLString += WriteSpaces(9) + '<input type="button" name="MapKey" title="Map Key" value="Key" onClick="top.OpenInfoWindow();" class="button">';

  top.main.document.write(HTMLString);
  InitForm(top.IsMapOn);
  return;
}

function InitForm(thisStatus) { // initialise ( or reset ) options

  if ( thisStatus == null ) {
    if (top.mapwindow && !top.mapwindow.closed ) {
      top.IsMapOn = true;
    }
    else {
      top.IsMapOn = false;
    }
  }
  else {
   top.IsMapOn = thisStatus;
  }

  if ( top.maptype == null ) {
    top.maptype = 'horizontal';
  }
  if ( top.main.document.forms[0] ) {
    with (top.main.document.forms[0]) {

      MapOn.checked=top.IsMapOn;

      for ( var i=0; i < tableType.length; i++) {
        if (tableType[i].value == top.maptype) {
          tableType[i].checked = true;
        }
        else {
          tableType[i].checked = false;
        }
      }
    }
  }
  return;
}

function findYcoord(thisImage) { 

  var y;
  var yParent;

  if ( document.all ) { // ie

    y = eval(thisImage).offsetTop;
    yParent = eval(thisImage).offsetParent;

    while (yParent != null) {  
      y += yParent.offsetTop;
      yParent = yParent.offsetParent;
    }
  }
  else { // netscape allows this easily !
    y = thisImage.y;
  }
  return y;
}

function findXcoord(thisImage) {

  var x;
  var xParent;

  if ( document.all ) { // ie

    x = eval(thisImage).offsetLeft;
    xParent = eval(thisImage).offsetParent;

    while (xParent != null) {  
      x += xParent.offsetLeft;
      xParent = xParent.offsetParent;
    }
  }
  else { // netscape allows this easily !
    x = thisImage.x;
  }
  return x;
}

function SetPosition(thisType) { // defaults - if this is the first map

  var ChildProps;

  if ( thisType == 'vertical' && (( top.mapVwidth == null )||( top.mapVwidth == 0 ))) {
    top.mapVheight = ((screen.availHeight<1000) ? screen.availHeight : 1000);
    top.mapVwidth = ((screen.availWidth<260) ? screen.availWidth : 260);
    top.mapVleft = screen.availWidth - top.mapVwidth;
    top.mapVtop = 0;
    if (!document.all) { // not ie
      if ( top.mapVleft > 17 ) {
       top.mapVleft -= 18;
      }
     top.mapVheight -=30;
    }
  }

  if ( thisType == 'horizontal' && (( top.mapHwidth == null )||( top.mapHwidth == 0 ))) {
    top.mapHheight = ((screen.availHeight<260) ? screen.availHeight : 260);
    top.mapHwidth = ((screen.availWidth<1000) ? screen.availWidth : 1000);
    top.mapHleft=0;
    top.mapHtop = 0;
    if ( !document.all ) { // not ie
      top.mapHheight -=30;
      top.mapHwidth -= 18;
    }
  }

  if ( thisType == 'vertical' ) {
    mapleft = top.mapVleft;
    maptop = top.mapVtop;
    mapwidth = top.mapVwidth;
    mapheight = top.mapVheight;
  }
  if ( thisType == 'horizontal' ) {
    mapleft = top.mapHleft;
    maptop = top.mapHtop;
    mapwidth = top.mapHwidth;
    mapheight = top.mapHheight;
  }

  if ( document.all ) { // ie
    ChildProps = 'height=' + top.mapheight + ',width=' + top.mapwidth + ',offsetLeft=' + top.mapleft + ',offsetTop=' + top.maptop;
  }
  else {
    ChildProps = 'innerHeight=' + top.mapheight +' ,innerWidth=' + top.mapwidth + ',screenX=' + top.mapleft + ',screenY=' + top.maptop;
  }
  ChildProps += ',resizable,dependent,scrollbars';
  
  return ChildProps;
}

function NewPosition() {

  if ( document.all ) { // ie
    if ( top.maptype == 'vertical' ) {
      top.mapVleft=top.mapwindow.offsetLeft;
      top.mapVtop=top.mapwindow.offsetTop;
    }
    if ( top.maptype == 'horizontal' ) {
      top.mapHleft=top.mapwindow.offsetLeft;
      top.mapHtop=top.mapwindow.offsetTop;
    }
  }
  else {
    if ( top.maptype == 'vertical' ) {
      top.mapVleft=top.mapwindow.screenX;
      top.mapVtop=top.mapwindow.screenY;
    }
    if ( top.maptype == 'horizontal' ) {
      top.mapHleft=top.mapwindow.screenX;
      top.mapHtop=top.mapwindow.screenY;
    }
  }
  return true;
}

function NewSize() {

  if ( document.all ) { //ie
    with ( top.mapwindow.document ) {
      if ( top.maptype == 'vertical' ) {
        top.mapVheight=offsetHeight + 26;
        top.mapVwidth=offsetWidth + 10;
      }
      if ( top.maptype == 'horizontal' ) {
        top.mapHheight=offsetHeight + 26;
        top.mapHwidth=offsetWidth + 10;
      }
    }
  }
  else {
    if ( top.maptype == 'vertical' ) {
      top.mapVheight=top.mapwindow.innerHeight;
      top.mapVwidth=top.mapwindow.innerWidth;
    }
    if ( top.maptype == 'horizontal' ) {
      top.mapHheight=top.mapwindow.innerHeight;
      top.mapHwidth=top.mapwindow.innerWidth;
    }
  }
  return true;
}

function MapOnClicked() {
  
  if ( top.main.document.forms[0].MapOn.checked ) {
    top.IsMapOn=true;
    OpenMapWindow();
  }
  else {
    top.IsMapOn=false;
    CloseMapWindow(top);
   }
  return;
}

function MapOnChecked() {
  
  if (top.IsMapOn == true ) {
    OpenMapWindow();
    return true;
  }
  else {
    if ( !top.mapwindow.closed ) {
      return true;
    }
    else {
      return false;
    }
  }
}

function MapWindowHTML(thisType,isPrint) {

  var HTMLString;

  HTMLString = '<html><head>\n';
//  HTMLString += '<base href="' + base + '">\n';
  HTMLString += '<scr' + 'ipt language="JavaScr' + 'ipt" type="text/javascr' + 'ipt">\n'; // some browsers don't like this word !
  HTMLString += '<!--\n';
  HTMLString += 'window.onerror=null;\n';
  HTMLString += 'var orientation="' + thisType + '";\n';
  HTMLString += '// -->\n'; 
  HTMLString += '<\/scr' + 'ipt>\n';
  if(isPrint) {
    HTMLString += '<link href="maggi.css" title="preferred" rel="stylesheet" type="text/css">\n';
  }
  HTMLString += '<title>Site Map<\/title><\/head>\n';

  if(isPrint) {
    HTMLString += '<body>\n';
  }
  else {  
    HTMLString += '<body bgcolor="#000000" link="#FFFFFF" onLoad="if (window.opener && !window.opener.closed) {window.opener.HighlightCallingWindow();window.opener.InitForm(true);}" onUnload="if (window.opener && !window.opener.closed) {window.opener.InitForm(false);}" onMove="if (window.opener && !window.opener.closed) {window.opener.NewPosition();}">\n';
  }

  if ( thisType == 'vertical' ) {
    HTMLString += DrawTableVert(isPrint);
  }
  if ( thisType == 'horizontal' ) {
    HTMLString += DrawTableHoriz(isPrint);
  }

  HTMLString += '<\/body><\/html>';

  return HTMLString;
}

function AssignMap(thisType,isPrint) {

  top.mapwindow=window.open('','mapwindow',SetPosition(thisType)+((isPrint)?',menubar=yes':''));
  if (!top.mapwindow.opener || (top.mapwindow.opener != top)) {
    top.mapwindow.opener = top;
  }
  return;
}

function CloseMapWindow(thisOpener) {

  if (top.mapwindow && !top.mapwindow.closed ) {
    top.IsMapOn=false;
    top.mapwindow.opener = thisOpener;
    top.mapwindow.close();
  }
  return;
}

function OpenMapWindow(thisType,isPrint) {

  if ( thisType == null && top.maptype != null ) {
    thisType = top.maptype;
  }
  if ( thisType == null ) {
    thisType = 'horizontal';
  }
  if ( isPrint == null ) {
    isPrint = false;
  }
  top.maptype = thisType;
  if ( top.IsMapOn == true ) {

    ChangeCursor('wait','Opening Map Window');
    if ( top.mapwindow && !top.mapwindow.closed && top.mapwindow.orientation == thisType) {
      HighlightCallingWindow();
    }
    else {

      AssignMap(thisType,isPrint);

      with ( top.mapwindow ) {
        moveTo(mapleft,maptop);
        resizeTo(mapwidth,mapheight);

        focus();
        document.open();
        document.write(MapWindowHTML(thisType,isPrint));
        document.close();
      }
    }
    ChangeCursor('auto');
  }
  return;
}

function HighlightCallingWindow() {

  if (top.mapwindow) {
    with (top.mapwindow.document) {
      if(!document.images&&document.getElementByTagName){
        images=top.mapwindow.document.getElementByTagName('img');
      }
      for (var i=0;i<images.length;i++) {
        if (images[i].name) {
          if (IsCaller(images[i])) {
            top.mapwindow.scrollTo(findXcoord(images[i]), findYcoord(images[i]));
            top.mapwindow.focus();
          }
          images[i].id=images[i].name;
        }
      }
    }
  }
  return true;
}

function IsCaller(thisImage) {
 if (thisImage.name == top.main.CallingWindowName) {
    // caller is red
    thisImage.src = Ends[2].src;
    return true;
  }
  else {
    // green for my page, otherwise blue
    thisImage.src = ((thisImage.name == myPage) ? Ends[0].src : Ends[3].src);
    return false;
  }
}
function setAltText(thisImage) {
  if(thisImage.alt) {
    if(thisImage.alt.indexOf('Descendants of ')==-1) {
      thisImage.alt='Descendants of ' + thisImage.alt;
      thisImage.title=thisImage.alt;
    }
  }
}
function MapIn(thisImage) {
  lastSrc = thisImage.src;
  thisImage.src= Ends[1].src;
//  setAltText(thisImage)
  return true;
}

function twist(thisImage) {
  
  if (typeof(Ends)=='undefined')
    return false;
  if (thisImage.src != Ends[4].src && thisImage.src != Ends[6].src)
    thisImage.src=Ends[4].src;
  return true;
}
function twisted(thisDoc,thisImage) {
  
  if (typeof(Ends)=='undefined')
    return false;
  for (var i=0;i<thisDoc.images.length - 1;i++)
    thisDoc.images[i].src=Ends[3].src;
  thisImage.src=Ends[6].src;
  return true;
}

function untwist(thisImage) {
  
  if (typeof(Ends)=='undefined')
    return false;
  if (thisImage.src == Ends[4].src)
    thisImage.src=Ends[5].src;
  return true;
}

function DrawTableVert() {

  var HTMLString;

  HTMLString = '<table border=0 cellspacing=0 cellpadding=0>';

  for (var i=0; i <= noBranches; i++) {
    HTMLString += '<tr>';
    for (var j=0; j <= noGens; j++) {

      HTMLString += '<td>';

      switch ( divider[i][j] ) {
       case 'bfirst'  : HTMLString += '<img src="' + Lines[0].src + '" alt="">'; break;
       case 'bsplit'  : HTMLString += '<img src="' + Lines[1].src + '" alt="">'; break;
       case 'blast'   : HTMLString += '<img src="' + Lines[2].src + '" alt="">'; break;
       case 'bdesc'   : HTMLString += '<img src="' + Lines[3].src + '" alt="">'; break;
       case 'bcross'  : HTMLString += '<img src="' + Lines[4].src + '" alt="">'; break;
       case 'bspan'   : HTMLString += '<img src="' + Lines[5].src + '" alt="">'; break;
       case 'bbranch' : HTMLString += '<img src="' + Lines[6].src + '" alt="">'; break;
       case 'bfather' : HTMLString += '<img src="' + Lines[8].src + '" alt="">'; break;
       case 'bmother' : HTMLString += '<img src="' + Lines[10].src + '" alt="">'; break;
       case 'blink'   : HTMLString += '<img src="' + Lines[12].src + '" alt="">'; break;

       default : HTMLString +='';
      }

      HTMLString += '<\/td><td>';
      if ( display[i][j] ) {
      
        // put rollovers on link, not image, so it works in Netscape

        HTMLString += '<a href="'+familyDir+'page' + display[i][j] + '.html" target="main" title="Descendants of ' + page[display[i][j]] +'" onMouseOver="if (window.opener && !window.opener.closed) {window.opener.MapIn(page' + display[i][j] + ');}" onMouseOut="if (window.opener && !window.opener.closed) {window.opener.IsCaller(page' + display[i][j] + ');}"><img src="' + lastSrc + '" alt="' + page[display[i][j]] + '" id="page' + display[i][j] + '" border=0><\/a>'; 
      }
      HTMLString += '<\/td>';
    }
    HTMLString += '<\/tr>\n';
  }
  HTMLString += '<\/table>';
  return HTMLString;
}

function DrawTableHoriz(isPrint) {

  var HTMLString;

  HTMLString = '<table border=0 cellspacing=0 cellpadding=0>';
  for (var j=0; j <= noGens; j++) {
    HTMLString += '<tr>';
    for (var i=0; i <= noBranches; i++) {

      HTMLString += '<td>';

      switch ( divider[i][j] ) {
       case 'bfirst'  : HTMLString += '<img src="' + Lines[0].src + '" alt="">'; break;
       case 'bsplit'  : HTMLString += '<img src="' + Lines[7].src + '" alt="">'; break;
       case 'blast'   : HTMLString += '<img src="' + Lines[8].src + '" alt="">'; break;
       case 'bdesc'   : HTMLString += '<img src="' + Lines[5].src + '" alt="">'; break;
       case 'bcross'  : HTMLString += '<img src="' + Lines[4].src + '" alt="">'; break;
       case 'bspan'   : HTMLString += '<img src="' + Lines[3].src + '" alt="">'; break;
       case 'bbranch' : HTMLString += '<img src="' + Lines[9].src + '" alt="">'; break;
       case 'bfather' : HTMLString += '<img src="' + Lines[2].src + '" alt="">'; break;
       case 'bmother' : HTMLString += '<img src="' + Lines[10].src + '" alt="">'; break;
       case 'blink'   : HTMLString += '<img src="' + Lines[11].src + '" alt="">'; break;

       default : HTMLString +='';
      }

      HTMLString += '<\/td>';
    }
    HTMLString += '<\/tr>\n';
    HTMLString += '<tr>';

    for (var i=0; i <= noBranches; i++) {

      if(isPrint) {
        HTMLString += '<td align="center">';
      }
      else {
        HTMLString += '<td>';
      }
      if ( display[i][j] ) {
        if(isPrint) {
          HTMLString += display[i][j]; 
        }
        else {
          HTMLString += '<a href="'+familyDir+'page' + display[i][j] + '.html" target="main" title="Descendants of ' + page[display[i][j]] +'" onMouseOver="if (window.opener && !window.opener.closed) {window.opener.MapIn(document.page' + display[i][j] + ');}" onMouseOut="if (window.opener && !window.opener.closed) {window.opener.IsCaller(document.page' + display[i][j] + ');}"><img src="' + lastSrc + '" alt="Descendants of ' + page[display[i][j]] +'" name="page' + display[i][j] + '" border=0><\/a>'; 
        }
      }
      HTMLString += '<\/td>';
    }
    HTMLString += '<\/tr>\n';
  }
  HTMLString += '<\/table>';
  if(isPrint) {
    HTMLString += '<p>Each page above shows the <b>descendants<\/b> of :-<p>';
    HTMLString += '<table border=0 width="1300px" cellspacing=0 cellpadding=2>';

    for (var i=0; i < 10; i++) {
      HTMLString += '<tr>';
      for (var j=0; j <= 8; j++) {
        k = (j*10)+(i+1);
        if(k<=noPages) {
          HTMLString += '<td class="print" valign="top">'+k+' - ' +page[k]+'<\/td>';
        }
      }
      HTMLString += '<\/tr>';
    }
    HTMLString += '<\/table>';
  }
  return HTMLString;
}

function WidestFrame() { 

  if (top.document.body) {
    top.document.body.cols = '*,0';
  }
  else { // doesn't work !
    top.frames.nav.resizeTo(0,window.innerHeight);
    top.frames.main.resizeTo(window.innerWidth,window.innerHeight);
  }
  return false;
}

function show_props(obj, objName) {

   var HTMLString = '<br>';

   for (var i in obj) {
      HTMLString += objName + '.' + i + ' = ' + (new String(obj[i]).replace(/</g,"<")) + '\n';
   }
   return HTMLString;
}

function ShowTimeLeft() { // countdown for key window
  
  top.infowindow.document.forms[0].timeleft.value = timer/1000;
  timer -= 1000;
  if (timer<0) {
    top.infowindow.close();
    clearInterval(timerID);
  }
  return;
}

function OpenInfoWindow() { // key window

  var HTMLString;
  var ChildProps; 

  if ( document.all ) { // ie
    ChildProps = 'height=150,width=280';
  }
  else {
    ChildProps = 'innerHeight=150,innerWidth=300';
  }

  ChildProps += ',resizable,dependent';

  HTMLString = '<html><head><title>Key<\/title><\/head><body bgcolor=#000000 text=#FFFFFF>';
  HTMLString += '<form>Point at any dot to see page titles' + WriteSpaces(6);
  HTMLString += '<input type="text" name="timeleft" align="right" size=1 maxlength=1 readonly><\/form>';
  HTMLString += '<p>Special dots :-';
  HTMLString += '<br><img src="' + Ends[0].src + '" align="center">' + WriteSpaces(2) + 'The Author';
  HTMLString += '<br><img src="' + Ends[2].src + '" align="center">' + WriteSpaces(2) + 'Current page';
  HTMLString += '<br><img src="' + Ends[3].src + '" align="center">' + WriteSpaces(2) + 'Click to see other pages in the tree';
  HTMLString += '<\/body><\/html>';

  top.infowindow=window.open('','InfoWindow',ChildProps);

  with ( top.infowindow ) {    
    focus();
    document.open();
    document.write(HTMLString);
    document.close();
  }
  timer=5000;
  timerID=setInterval('ShowTimeLeft()',1000);

  return;
}
