// Escape from any referring site's frame, but preserve one click back
if (parent.frames.length > 0) top.location.replace(document.location);

window.name = 'f_menu';

var tnt = {
  menuWin: null,
  mainWin: null,
  isFrameSet: true,
  mainMenu: null,
  currentMenuItem: null
}

/* Initialise a menu (tree) */
function menuInit(menu, parent_menu) {
  var menu = $(menu || 'menu');
  var parent_menu = (parent_menu ? $(parent_menu) : null);

  if (menu.id == 'menu') {
    tnt.mainMenu = menu;
  }

  menu._menu = parent_menu;
  menu._depth = (parent_menu ? parent_menu.depth + 1 : 0);
  menu._selected = null;

  var items = menu.getChildren();
  var max_i = items.length;
  for (var i = 0; i < max_i; i++) {
    var item = items[i];
    if (item.tagName != 'LI') {
      continue;
    }
    item._link = item.getFirst('a');
    //item._link.onclick = function() { return false; };
    item._menu = menu;
    item._submenu = item.getFirst('ul');
    item._pageId = item.id.replace('menu-', '');
    item.addClass('closed');
    //item._link.addEvent('click', menuClick.bindWithEvent(item))
    if (item._submenu) {
      menuInit(item._submenu, item._menu);
    }
  }
}

function menuClick(event) {
  // Close any open sibling
  if (this._menu._selected) {
    this._menu._selected.className =
      this._menu._selected.className.replace('open', 'closed');
  }

  // Close any open submenus
  if (this._submenu && this._submenu._selected) {
    this._submenu._selected.className = 
      this._submenu._selected.className.replace('open', 'closed');
  }

  this.className = this.className.replace('closed', 'open');

  this._menu._selected = this;

  tnt.currentMenuItem = this;

  return true;
}

function menuShow(id) {
  //alert('menuShow('+id+') [tnt.currentMenuItem='+(tnt.currentMenuItem ? tnt.currentMenuItem.id : 'none')+']');

  if (tnt.currentMenuItem && tnt.currentMenuItem._pageId == id) {
    return;
  }

  var items = $$('#menu li').each(function(item) {
    item.className = item.className.replace('open', 'closed');
  });

  var m = id.split('-');
  var n = 'menu';
  for (var i = 0, max_i = m.length; i < max_i; i++) {
    n += ('-' + m[i]);
    var li = $(n);
    if (li) {
      li._menu._selected = li;
      li.className = li.className.replace('closed', 'open');
      tnt.currentMenuItem = li;
    }
  }
}

// Called by iframe when section changes
function pageShown(id) {
    //alert('pageShown('+id+')');
    
    tnt.waiter.stop();

    menuShow(id);

    // Save this location in the browser history
    saveHistory(id);
}

function pageShow(id) {
  //alert('pageShow('+id+')');

  var m = id.split('-');
  if (m.length == 0) {
    m[0] = 'home';
  }
  if (m.length == 1) {
    m[1] = 'index';
  }

  if (page == 'index') {
    // Never load the index frameset page into the frame
    page == 'home';
  }

  var page = m.shift();
  var sect = m.join('-');

  try {
    var win = window.top.frames['f_main'];
    if (win.document.body.id == page) {
      // Instruct the main window to go there
      //alert('showing section: '+sect);
      sect = win.showSect(sect);
    } else {
      //alert('navigating to: '+page+'.php#'+sect);
      tnt.waiter.start();
      win.location = page+'.php#'+sect;
    }

  } catch(e) {};

  // We rely on the mainWindow calling our pageShown() method when it
  // shows a page section
}

// Do the actual page turning
function anchorClick(event) {
  //alert('anchorClick: '+this.href);

  // For an internal page link no target should be specified
  if (this.target != '') {
    //alert('not internal target: '+this.target);
    return true;
  }

  var m1 = window.top.location.href.match(/^(.*)\/([^\/]*)$/);
  var m2 = this.href.match(/^(.*)\/([^\/]*)$/);
  if (!m1 || !m2 || (m1[1] != m2[1])) {
    // Different roots
    //alert('different roots: '+m1[1]+' != '+m2[1]);
    return true;
  }

  // Are we going to a named anchor on the same page
  if ((matches = this.href.match(/([a-z]*)\.php#(.*)/))) {
    var page = matches[1]+'-'+matches[2];
    //alert('page='+page);
    pageShow(page);
    event.preventDefault();
    return false;
  }

  return true;
}


function onHistoryChange(values) {

  var newLocation = values[0];

  // if there is no location then display
  // the default, which is the home-index
  if (newLocation == "") {
    newLocation = "home-index";
  }

  pageShow(newLocation);
}

function saveHistory(newLocation) {
  tnt.history.setValue(0, newLocation);
}

function domReady() {
  //alert('domReady()');
  
  tnt.menuWin = window.top;
  tnt.mainWin = window.top.frames['f_main'];

  tnt.waiter = new Waiter($('f_main'), {
    baseHref: 'scripts/floatbox-2.43/images/',
    containerProps: { styles: { width: 520 } },
    img: { src: 'loading_black.gif', styles: { height: 124, width: 124 } },
    layer: { styles: { background: '#000000' } } 
    });

  // rewrite all the anchors that cause page turning
  initAnchors();

  // initialize the DHTML History framework
  HistoryManager.initialize();

  tnt.history = HistoryManager.register(
      // key - unique key of registered module,
      'page',

      // defaults - array with default values for the history
      ['home-index'],

      // onMatch - callback when state changed
      onHistoryChange,

      // onGenerate - callback that returns the hash string
      function(values) { return values[0]; },

      // regexp - to decode the hash string,
      '([^-]*-.*)'
      );

  HistoryManager.start();
}

window.addEvent('domready', domReady);


/* vim: set expandtab tabstop=2 shiftwidth=2: */
