// Table of Contents
function createTOC() {
  if (document.ieMac) return;

  var c = document.createElement('div');
  c.id = 'toc-container';
  var top = document.createElement('div');
  top.id = 'toc-top';
  var y = document.createElement('div');
  y.id = 'toc';
  var bot = document.createElement('div');
  bot.id = 'toc-bot';
  var w = document.createElement('h2');
  w.id = 'toc_title';
  w.title = 'Click to expand/collapse the Table of Contents';
  addEvent(w, 'click', function () { toggleTOC(); })
  var t = document.createElement('span');
  t.title = "Places to visit";
  t.className = "ttxt";
  var la = document.createElement('label');
  la.appendChild(document.createTextNode('Places to visit'));
  t.appendChild(la);
  var p = document.createElement('span');
  p.src = "images/arrowUpPlain.gif";
  p.className = "ttop";
  var a = document.createElement('a');
  addEvent(a, 'click', function () { ScrollTo('ayiazoni') })
  a.href = "javascript:void(0);";
  a.title = "Scroll to the top of the page";
  //var i = document.createElement('img');
  //i.alt = "^";
  //i.className = "imageTop";
  //a.appendChild(i);
  //p.appendChild(a);
  a.innerHTML = "<img src='images/arrowUpPlain.gif' alt='^' />";
  w.appendChild(t)
  w.appendChild(p)

  y.appendChild(w)
  c.appendChild(top)

  var g = document.createElement('div');
  g.id = 'toc-sections';

  var u = document.createElement('ul');
  u.id = "mainList";

  var x = getElementsByClass("site", document.getElementById('contents'), "div");
  
  //this iteration goes through all the chapter titles
  for (var i = 0; i < x.length; i++) {
    var li = document.createElement('li');
	var currentLink = x[i];

    var title = currentLink.getElementsByTagName("h2");
    title = title[0].innerHTML;
    var a = document.createElement('a');
    a.rel = currentLink.id;
    addEvent(a, 'click', function () { ScrollTo(this.rel) });//currentLink.id
    a.href = "javascript:void(0);";
	a.appendChild(document.createTextNode(title));

    li.appendChild(a);

    var ul = document.createElement('ul');
    var h = getElementsByClass("place", currentLink, "div");
    for (var n = 0; n < h.length; n++) {
	  var currentSubLink = h[n];
      var subli = document.createElement('li');

      var title = currentSubLink.getElementsByTagName("h3");
      title = title[0].innerHTML;
      var a = document.createElement('a');
      a.rel = currentSubLink.id;
      addEvent(a, 'click', function () { ScrollTo(this.rel) });//currentSubLink.id
      a.href = "javascript:void(0);";
	  a.appendChild(document.createTextNode(title));

	  subli.appendChild(a);
      ul.appendChild(subli);
    }
    var lis = ul.getElementsByTagName('li');
    if (lis.length > 0) li.appendChild(ul);
    
    u.appendChild(li);
  }
  g.appendChild(u);
  y.appendChild(g);
  c.appendChild(y)
  var spacer = document.createElement('div');
  spacer.innerHTML = "<img src='images/spacer.gif' width='225' height='1' alt='' class='clear' />";
  c.appendChild(spacer);
  c.appendChild(bot);
  //alert(c.id)
  //alert(c.innerHTML)
  document.body.appendChild(c);
}

var is_working = false;
function toggleTOC() {
  var el = $('toc-sections'),
      state = el.getStyle('display'),
      origheight = parseInt(el.getStyle('height'));
  //el.setStyle({overflow: 'hidden'});

  if (navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('Mac') == -1) {
    if (state != 'none') {
      el.setStyle({display: 'none'});
	} else {
      el.setStyle({display: 'block'});
	}
    return;
  }

  if (is_working) return;

  if (state != 'none') {
    alter = function () {
	  is_working = true;
      var height = parseInt(el.getStyle('height'));
      var gradual = parseInt(height / 4);
      if (gradual < 1) return;
      newheight = height - gradual;

      el.setStyle({height: newheight + 'px'});
      if (gradual < 5) {
 	    is_working = false;
        //el.setStyle({overflow: 'auto'});
        el.setStyle({display: 'none'});
        el.setStyle({height: origheight + 'px'});
        return;
      }
      setTimeout("alter()", 10);
    }
	alter();
  } else {
    el.setStyle({height: '0px'});
    el.setStyle({display: 'block'});
    alter = function () {
	  is_working = true;
      var height = parseInt(el.getStyle('height'));
      var gradual = parseInt((origheight - height) / 4);
      if (gradual < 1) return;
      newheight = height + gradual;

      el.setStyle({height: newheight + 'px'});
      if (gradual < 5) {
 	    is_working = false;
        //el.setStyle({overflow: 'auto'});
        el.setStyle({display: 'block'});
        el.setStyle({height: origheight + 'px'});
        return;
      }
      setTimeout("alter()", 10);
    }
	alter();
  }
}

var tocDelay;
function tocFloatInPosition() {
  var startX = 0, startY = 0;
  var d = document;
  var iebody = (d.compatMode && d.compatMode != "BackCompat")? d.documentElement : d.body
  function ml(id) {
    var el = $(id);
    el.sP = function(x,y) { this.style.left = x + 'px'; this.style.top = y + 'px'; };
    el.x = startX;
    el.y = startY;

    return el;
  }
  window.tocStayInPosition = function() {
  var pX = d.body.clientWidth;
  //alert(pX)
  ftlObj.x = pX - ((pX - 760) / 2) - 270;

  var bHeight = (document.body.clientHeight ? document.body.clientHeight : window.innerHeight)
  
  var pY = d.all ? iebody.scrollTop : pageYOffset
  if (pY > 224) {
    ftlObj.y += (pY + startY - ftlObj.y) / 8;
    //alert(ftlObj.y)// = 224
  } else {
    //ftlObj.y = pY - 224
    ftlObj.y = 224;
    //ftlObj.y = 224 + ((pY + startY - ftlObj.y) / 8);
  }
  //$('xy').innerHTML = '('+pX+', '+pY+')';

    ftlObj.sP(ftlObj.x, ftlObj.y);
	//if (tocDelay) clearTimeout(tocDelay)
    //tocDelay = setTimeout("setTimeout(\"tocStayInPosition()\", 10);", 3000);
    setTimeout("tocStayInPosition()", 10);
    
  }
  ftlObj = ml("toc-container");
  tocStayInPosition();
}

function cypInit () { createTOC(); $('toc-container').style.position = 'absolute'; tocFloatInPosition(); }

addEvent(window, 'load', cypInit)
//if (document.addEventListener) {
//  document.addEventListener("DOMContentLoaded", cypInit, false);
//} else {
//  document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
//  var tag = $("contentloadtag");
//  tag.onreadystatechange = function(){if(this.readyState=="complete"){
//    cypInit();
//  }}
//}

