// Escape from any referring site's frame, but preserve one click back
if (parent.name != 'f_menu') window.location.replace(window.location.href.replace(/(.*)\/(.*).php(?:#(.*))?/, '\1\/index.php#\2-\3'));

window.name = 'f_main';

var tnt = {
  menuWin: null,
  mainWin: null,
  isFrameSet: false,
  sections: new Hash,
  numSections: 0,
  currentSection: null,
  navigator: null,
  sectionPrefix: 'tnt_'
}

// Goto a section (either by name or index)
function showSect(sect) {
  //alert('showSect('+sect+') [current section='+(tnt.currentSection ? tnt.currentSection.id : 'none')+']');

  if (tnt.numSections == 0) {
    // No sections :-(
    return null;
  }

  if (sect == 'self') {
    sect = 'index';
  } else if (sect == 'hash') {
    // If an anchor point was specified in the URL then
    // we only show the division labeled with that anchor name
    var hash = document.location.hash;
    if (hash != '') {
      sect = hash.substring(1, hash.length);
    } else if ((idx = document.location.search.indexOf('section=')) != -1) {
      sect = document.location.search.substring(idx + 8, document.location.search.length);
    } else {
      sect = 'index';
    }
  }

  if (tnt.currentSection) {
    if (tnt.currentSection.id == sect) {
      // already there
      return sect;
    }
    if (sect == 'next') {
      if (tnt.currentSection.no < (tnt.numSections - 1)) {
        sect = tnt.sections.get(tnt.currentSection.no + 1).id;
      }
    } else if (sect == 'prev') {
      if (tnt.currentSection.no > 0) {
        sect = tnt.sections.get(tnt.currentSection.no - 1).id;
      }
    }
  }

  var newSection;
  if (!(newSection = tnt.sections.get(tnt.sectionPrefix+sect))) {
    alert('Section "' + tnt.sectionPrefix + sect + '" not found - choose from: '+tnt.sections.getKeys().join(','));
    return null;
  }

  if (tnt.currentSection) {
    tnt.currentSection.el.setStyle('display', 'none');
    tnt.currentSection = null;
  }

  tnt.currentSection = newSection;

  //alert('new section '+tnt.currentSection.id);

  tnt.currentSection.el.setStyle('display', 'block');

  if (tnt.navigator) {
    if (tnt.currentSection.no == 0) {
      tnt.navigator.navprev.addClass('disabled');
      tnt.navigator.navself.addClass('disabled');
    } else {
      tnt.navigator.navprev.removeClass('disabled');
      tnt.navigator.navself.removeClass('disabled');
    }

    if (tnt.currentSection.no == (tnt.numSections - 1)) {
      tnt.navigator.navnext.addClass('disabled');
    } else {
      tnt.navigator.navnext.removeClass('disabled');
    }
  }

  parent.setFrameHeight(window.name);

  parent.pageShown(document.body.id+'-'+tnt.currentSection.id);

  return tnt.currentSection.id;
}

function faqClick() {
  if (this._dl._dt) {
    this._dl._dt.removeClass('selected');
    this._dl._dt._dd.removeClass('selected');
  }
  this._dl._dt = this;
  this.addClass('selected');
  this._dd.addClass('selected');
  window.parent.setFrameHeight(window.name);
}

function faqInit(dl) {
  var dl = $(dl);
  var dts = dl.getChildren('dt');
  var max_i = dts.length;
  for (var i = 0; i < max_i; i++) {
    var dt = dts[i];
    dt._dd = dt.getNext();
    dt._dl = dl;
    dt.addEvent('click', faqClick.bindWithEvent(dt))
  }
  dl._dt = null; // currently selected item
}

// Find all sections
function initSections(showDefault) {
  var sections = $$('.section');

  tnt.numSections = 0;
  tnt.sections.empty();
  for (var i = 0, max_i = sections.length; i < max_i; i++) {
    //alert('found section: '+sections[i].id);
    var s = {
      no: i,
      id: sections[i].id,
      el: sections[i]
    };
    tnt.sections.set(s.no, s);
    tnt.sections.set(tnt.sectionPrefix+s.id, s);
  }
  tnt.numSections = i;
}

function initNavigator() {
  // Find navigation controls
  tnt.navigator = $('navigator');
  if (tnt.navigator) {
    tnt.navigator.navnext = $('navnext');
    tnt.navigator.navprev = $('navprev');
    tnt.navigator.navself = $('navself');
  }
}

function domReady() {
  
  tnt.menuWin = window.top;
  tnt.mainWin = window.top.frames['f_main'];

  // rewrite all the anchors that cause page turning
  initAnchors();

  // init the navigator
  initNavigator();

  // find all the page sections
  initSections();

  // Start with whatever section the hash says
  showSect('hash');
}

// Mootools domReady fires too soon on IE
window.addEvent('mydomready', domReady);

/* vim: set expandtab tabstop=2 shiftwidth=2: */

