var anim_time = 0.25; // global

Ojay.onDOMReady(function() { // equalise font sizes for (96-dpi) Mac users */
  if (navigator.userAgent.indexOf('Mac') != -1) $('#page').setStyle({fontSize: '0.82em'});
});

var menu_animating = false; // global
Ojay.onDOMReady(function() {
  $('#main_menu').on('click', Ojay.delegateEvent({
    'a': function(link, e) {
      var name = link.node.id.match(/^m_(.*)/)[1]; // extract name of submenu to load from link id
      if (submenus[name] || ($('#page.' + name).node && link.node.href == location.href)) { // then, if there's a submenu associated with this link, or it's the current page link...
        e.stopEvent(); // cancel navigating
        if ($('#sub_menu.' + name).node || menu_animating) return; // abort if the relevant submenu is already displayed, or if mid-animation
        $('#main_menu a.active').removeClass('active'); // make previous main menu link 'inactive'
        link.addClass('active'); // make clicked link 'active'
        var existing = $('#sub_menu');
        var chain = new JS.MethodChain();
        if (existing.node) { // fade out and remove any existing submenu
          chain.animate({ opacity: {to: 0} }, anim_time)
            .parents()._(function() { this.node.removeChild(existing.node) });
        }
        chain._($('#main_menu')); // switch scope to main menu
        if (submenus[name]) { // if we're showing a submenu
          if ($('#main_menu.wide').node) chain.animate({ width: {to: 100} }, anim_time).removeClass('wide'); // reduce main menu width if required
          var current_page = $('#page').node.className;
          chain.insert(submenus[name], 'after')  // insert new submenu
            ._(function() { return $('#sm_' + current_page); }).addClass('active').setContent(page_name) // highlight current page if applicable
            ._(function() { return $('#sub_menu'); }).setStyle({opacity: 0}).animate({ opacity: {to: 1} }, anim_time) // fade in
            ._(function() { if (window.EXPLODER && this.node.style.removeAttribute) this.node.style.removeAttribute('filter'); }); // fixes IE7 ClearType
        } else { // otherwise, we're removing the submenu from current page
          chain.animate({ width: {to: 340} }, anim_time).addClass('wide');
        }
        chain._(function() { menu_animating = false; }); // reset flag at end
        menu_animating = true; // set flag to prevent simultaneous animations with unpredictable (nasty!) effects
        chain.fire(existing);
      }
    }
  }, true));
});

var pages_animating = false, current_section = 0, sections; // globals
Ojay.onDOMReady(function() {
  sections = $('.section');
  sections.replaceClass('section', 'paged_section'); // so we pick up the correct styles for JS section-switching
  var lis = sections.map(function (s, i) { // generate sections menu
    if (i != current_section) s.hide();
    return "<li><a id='p_" + i + "' href='#page' class='" + (i == current_section ? 'active' : '') + "'>" + (i + 1) + "</a></li>"
  }).join('');
  var div = "<div id='content_paging_links'><p class='no_css'><b>Pages</b></p><ul>" + lis + "</ul></div>"
  $('#paged').insert(div, 'top');
  $('#content_paging_links').on('click', Ojay.delegateEvent({
    'a': function(link, e) {
      e.stopEvent();
      var page_index = parseInt(link.node.id.match(/^p_(.*)/)[1]); // extract page from link id
      if (page_index == current_section || pages_animating) return; // do nothing if we're already here, or in mid-animation
      var old_section = $(sections[current_section]);
      var new_section = $(sections[page_index]);
      if (window.EXPLODER) { // IE requires the image div opacity to be specified directly
        old_section = $(old_section, old_section.descendants('.mid_content'));
        new_section = $(new_section, new_section.descendants('.mid_content'));
      }
      pages_animating = true; // set flag to prevent simultaneous animations with unpredictable (nasty!) effects
      old_section.animate({opacity: {to: 0}}, anim_time).hide() // fade out and hide old section
        ._($('#content_paging_links a')).removeClass('active')._(link).setClass('active') // switch active section link
        ._(new_section).show().setStyle({opacity: 0}).animate({opacity: {to: 1}}, anim_time) // fade in new section
        ._(function() { if (window.EXPLODER && this.node.style.removeAttribute) this.node.style.removeAttribute('filter'); }) // fixes IE7 ClearType
        ._(function() { current_section = page_index; pages_animating = false; }); // reset flag and set current section at end
    }
  }, true));
});

Ojay.onDOMReady(function() {
  $('.reveal_content').hide();
  $('#page').on('click', Ojay.delegateEvent({
    'a.reveal_title': function(link, e) {
      e.stopEvent();
      $('.reveal_title_active').setClass('reveal_title')
        .ancestors('.reveal_set').descendants('.reveal_content').hide();
      link.setClass('reveal_title_active')
        .ancestors('.reveal_set').descendants('span.reveal_content').show()
        ._(link).ancestors('.reveal_set').descendants('div.reveal_content')
        .show().animate({opacity: {from: 0, to: 1}}, anim_time)
        ._(function() { if (window.EXPLODER) this.node.style.removeAttribute('filter'); });
    },
    'a.reveal_title_active': function(link, e) {
      e.stopEvent();
    }
  }, true));
});

