// Copyright (c) Design Matrix, 2008, all rights reserved.
// http://www.designmatrix.com
function togMenu(id) {
  var element = document.getElementById(id);
  var elementimg = document.getElementById(id +'img');
  if (element.style.display == 'none' || element.style.display == '') {
    element.style.display = 'block';
    if (elementimg != null) elementimg.src = 'graphics/minus.gif';
  } else {
    element.style.display = 'none';
    if (elementimg != null) elementimg.src = 'graphics/plus.gif';
  }
}
var wide;
var high;
function launchWin(url, name) {
  if (wide == undefined) {wide = '250';}
  if (high == undefined) {high = '400';}
  var newwin = window.open(url,name,'status=1,scrollbars=1,resizable=1,width='+wide+',height='+high+',screenX=0,screeny=0,left=0,top=0');
  newwin.focus();
  if (newwin.opener == null) newwin.opener = window;
}
if (window.opener != null) window.rootwin = window.opener;
function fetch(url) {
  if (window.rootwin == undefined || window.rootwin.closed == true) {
    if (window.opener == null) {
      window.rootwin = window.open(url,'_external','toolbar=yes,location=yes,menubar=yes,directories=yes,resizable=yes,scrollbars=yes');
    } else {
      window.opener = window.open(url,'_external','toolbar=yes,location=yes,menubar=yes,directories=yes,resizable=yes,scrollbars=yes');
      window.rootwin = window.opener;
    }
  } else {
    window.rootwin.location.href = url;
  }
  window.rootwin.focus();
}
function newToc(url, name) {
  if (location.search == '?nav') {
    if (url == undefined) url = 'nav.html';
    if (name == undefined) name = 'NAV';
    launchWin(url, name);
  }
}
var tocX;
var title;
var headings;
function openToc(tocX) {
  if (headings == undefined) headings = /^H[1-6]$/;
  var url = rootwin.document.URL;
  url = url.replace(/#.*/, '');
  url = url.replace(/\\/g,'/');
  var nodes = rootwin.document.getElementsByTagName('*');
  document.write (
    '<DIV> <SPAN CLASS="menuselect" onClick="togMenu(\'' +tocX +'\');"> \n'
    +'<IMG SRC="graphics/plus.gif" ID="' +tocX +'img"> \n'
    +title +'<\/SPAN><\/DIV>' 
  );
  document.writeln('<DIV ID="' +tocX +'" CLASS="sections">');
  var tagNo = 1;
  var indent = 0;
  var htag = '';
  for (var i = 0; i < nodes.length; i++) {
    nn = nodes[i].nodeName.toUpperCase();
    if (nn.match(headings)) {
      indent = (nn.match(/[1-6]$/) - 1) +'em';
      htag = nodes[i].id;
      if (htag != '') {
	tag = '#' +htag;
      } else {
	nodes[i].id = tagNo;
	tag = '#' +tagNo;
      }
      document.write('<LI STYLE="text-indent: ' +indent +';"> \n'
	+'<A HREF="#' +tocX +tag +'"\n'
	+'onClick="fetch(\'' +url +tag +'\');"> \n'
	+nodes[i].childNodes[0].nodeValue +'<\/A> \n'
      );
      tagNo++;
    }
  }
  document.writeln('<\/DIV>');
  togMenu(tocX);
}
var currentidx;
function getDocIndex() {
  if (window.rootwin == undefined || window.rootwin.closed == true) {
    fetch(docarr[0]['url'] +'?nav');
  }
  var url = rootwin.document.URL;
  url = url.replace(/\.html.*/, '.html');
  url = url.replace(/\\/g,'/');
  url = url.replace(/.*\//, '');
  for(var idx=0; idx < docarr.length; idx++) {
    if (docarr[idx]['url'] == url){
      return idx;
    }
  }
  return -1;
}
function changeDoc(action) {
  currentidx = getDocIndex();
  var nextidx;
  switch (action) {
    case 'prev':
      if (currentidx < 0) {
	nextidx = 0;
      } else if (currentidx == 0) {
	nextidx = docarr.length -1;
      } else {
	nextidx = currentidx - 1;
      }
      break;
    case 'next':
      if (currentidx == docarr.length -1) {
	nextidx = 0;
      } else {
	nextidx = currentidx + 1;
      }
      break;
    default:
      alert ("changeDoc: no action match");
      break;
  }
  var nextdoc = docarr[nextidx]['url'];
  fetch(nextdoc +'?nav');
}
function writeToc() {
  if (window.rootwin == undefined) {
    fetch(docarr[0]['url'] +'?nav');
  } else { 
    currentidx = getDocIndex();
    for(var idx=0; idx < docarr.length; idx++) {
      title = docarr[idx]['title'];
      if (idx == currentidx) {
	openToc('toc' +idx);
      } else {
	document.write ('\n<DIV ID="toc' +idx +'" CLASS="menuselect"> \n'
	  +'<IMG SRC="graphics/plus.gif" ID="toc' +idx +'img" \
	  onClick="fetch(\'' +docarr[idx]['url'] +'?nav\');"> \n' 
	  +'<A HREF="#toc' +idx +'" \
	  onClick="fetch(\'' +docarr[idx]['url'] +'?nav\');">' 
	  +title +'</A> \n'
	  +'<\/DIV>\n');
      }
    }
  }
}
