// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

TouringMachine = {};

TouringMachine.ajaxify = function(selector) {
  $$(selector).each(function(form) {
    form.observe('submit', function(event) {
      new Ajax.Request(form.action, {
        method: form.method,
        parameters: form.serialize()
      });
      Event.stop(event);
    });
  });
};

TouringMachine.tabify = function(element) {
  element = $(element);
  new Control.Tabs(element, {
    afterChange: function(newtab) {
      if (newtab.hasClassName('deferred-tab')) {
        newtab.removeClassName('deferred-tab');
        // replace anchors in the deferred tab with what they link to
        newtab.getElementsBySelector('a').each(function(anchor) {
          href = anchor.href;
          // disable the anchor
          anchor.removeAttribute('href');
          anchor.innerHTML = 'Loading....';
          new Ajax.Updater(anchor, href, {
            method: 'get',
            insertion: Insertion.After,
            onComplete: function() {
              // check for maps that need loading in the new fragment...
              TouringMachine.Maps.loadAll(newtab);
              // then get rid of the 'Loading' node.
              anchor.remove();
            }
          });
        });
      }
    }
  });
};

TouringMachine.showFOUC = function() {
  $$('.hide-fouc').invoke('removeClassName', 'hide-fouc');
};

Event.observe(window, 'load', function() {
  $$('.tabs').each(TouringMachine.tabify);
});

Event.observe(window, 'load', function() {
  var flash = $('flash');
  flash.setStyle({position: 'fixed'});
  flash.observe('click', function(event) {
    Effect.Fade(flash);
  });
});

Event.observe(window, 'load', TouringMachine.showFOUC);
