var animatingMenu = false;

function startWelcomeMovie()
{
  var delay = 0;
  var images = $('welcome').select('img');
  images.each(function(image) {
    new Effect.Appear(image, {
      delay: delay,
      fps: 50
    });
    delay += image.className == 'text' ? 1.2 : 2;
  });
}

/**
 * Show the previous slide.
 */
function previousSlide()
{
  // The project and any associated photos.
  var project = $('project_details');
  var photos = $('project_gallery').select('.frame');
  // Get the (only) visible photo.
  var photo = photos.find(function(p) {
    return p.visible();
  });
  // Do we have a photo?
  if(photo) {
    // Get any photos before this one.
    var siblings = photo.previousSiblings();
    // Show previous.
    new Effect.Fade(photo, {
      afterFinish: function() {
        if(siblings.size() > 0) {
          new Effect.Appear(siblings.first());
        } else {
          new Effect.Appear(project);
          new Effect.Appear(project.down());
        }
      }
    });
  } else {
    document.location = '/previous_project/' + projectName;
  }
}

/**
 * Show the next slide.
 */
function nextSlide()
{
  // The project and any associated photos.
  var project = $('project_details');
  var photos = $('project_gallery').select('.frame');
  // If the project is visible, then we're at the first slide.
  if(project.visible()) {
    // So show the first photo.
    new Effect.Fade(project);
    new Effect.Fade(project.down(), {
      afterFinish: function() {
        new Effect.Appear(photos.first()); 
      }
    });
  } else {
    // Otherwise, get the current photo.
    var photo = photos.find(function(p) {
      return p.visible();
    });
    // and show the next one, if it exists.
    var siblings = photo.nextSiblings();
    if(siblings.size() > 0) {
      new Effect.Fade(photo, {
        afterFinish: function() {
          new Effect.Appear(siblings.first()); 
        }
      });    
    } else {
      // Otherwise, jump to the next project.
      document.location = '/next_project/' + projectName;
    }
  }
}

/**
 * Show menu.
 * @param {Object} event
 */
function showMenu(event)
{
  if(!$('menu').visible() && !animatingMenu) {
    animatingMenu = true;
    new Effect.Fade('overlay');
    new Effect.SlideDown('menu', {
      afterFinish: function() {
        animatingMenu = false;
      }
    });
  }    
}

/**
 * Hide menu.
 * @param {Object} event
 */
function hideMenu(event)
{
  if(event.element().id != 'nav' && !event.element().up('#nav')) {
    if($('menu').visible() && !animatingMenu) {
      animatingMenu = true;
      new Effect.Appear('overlay');
      new Effect.SlideUp('menu', {
        afterFinish: function() {
          animatingMenu = false;
        }        
      });
    }
  }
}

/**
 * Show submenu.
 * @param {Object} submenu
 */
function showSubmenu(submenu)
{
  // The submenu.
  submenu = $(submenu);
  
  /*
  // Only animate if nothing else is animating.
  if(!submenu.visible() && !animatingMenu) {
    // Set animation flag.
    animatingMenu = true;
    // Show selected submenu.
    new Effect.SlideDown(submenu, {
      afterFinish: function() {
        animatingMenu = false;
      },
      duration: 0.5
    });
    // Hide other submenus.
    $('menu').select('.submenu').each(function(s) {
      if(s.id != submenu.id && s.visible()) {
        new Effect.SlideUp(s, {
          duration: 0.5
        });        
      }
    });
  }
  */
}

// Activate menu hiding.
Event.observe(document, 'dom:loaded', function() {
  if($('nav')) {
    $('nav').observe('mousemove', showMenu);
    Event.observe(document, 'mousemove', hideMenu);
  }
})
