// Necessary try...catch for IE that trips on console.log()
try { console.log('init console... done'); } catch(e) { console = { log: function() {} } };
// this is to shorten the use of the logging : log('my stuff')
var log = function(message) { console.log(message) }

function ff2() {
  return navigator.userAgent.indexOf("Firefox/2") != -1;
}

/* ===========================>> PerPageScripts <<=========================== */


/* ===========================>> Library <<=========================== */

/*
 Copy input values between forms, when inputs have the same names.
 Useful for tabs
*/
FormSynchronizer = Class.create({
 initialize: function(forms) {
   this.forms = this._find_forms(forms);
   this._observe_inputs();
 },
 _find_forms: function(forms) {
   if (forms instanceof Control.Tabs) {
     return forms.containers.values().collect(function(div) {
       return div.select('form');
     }).flatten().compact();
   } else {
     return forms;
   }
 },
 _observe_inputs: function() {
   copyFieldValue = this._copyFieldValue.bindAsEventListener(this);

   this.forms.collect(function(form) {
     return Form.getInputs(form);
   }).flatten().each(function(input) {
     new Form.Element.EventObserver(input, copyFieldValue);
   });
 },
 _copyFieldValue: function ( element ) {
   this.forms.without(element.form).each(function(other_form) {
     same_element = Form.getInputs(other_form, "text", element.name).first();
     if (same_element) {
       same_element.setValue(element.value);
     }
   });
 }
});

PreMessageManager = Class.create({
  initialize: function(textInput, preMessage, cssStyle) {
    this.textInput = $(textInput);
    this.preMessage = preMessage;
    this.cssStyle = cssStyle;
    Event.observe(window, "load", this._on_load.bindAsEventListener(this));
  },
  _on_load: function() {
    if (!this.textInput.value) {
      this.textInput.value = this.preMessage;
      if (this.cssStyle) {
        this.textInput.addClassName(this.cssStyle);
      }
      this.focusHandler = this._on_focus.bindAsEventListener(this);
      this.textInput.observe('focus', this.focusHandler);
    }
  },
  _on_focus: function() {
    this.textInput.value = '';
    if (this.cssStyle) {
      this.textInput.removeClassName(this.cssStyle);
    }
    this.textInput.stopObserving('focus', this.focusHandler);
  }
});

Event.observe(window, "load", function() {
  $$('ul[class=tabs]').each(function(element) {
    /* use a.active in html as default tab */
    defaultTab = 'first';

    active_link = element.select('a.active').first();
    if (active_link) {
      defaultTab = active_link.getAttribute('href').gsub("#","");
    }

    control_tabs = new Control.Tabs(element, {defaultTab: defaultTab});
    new FormSynchronizer(control_tabs);
  });
});

function display_road_itinerary(url, container, departure, arrival, mode, departure_at) {
    new Ajax.Updater(container, url, {
        parameters: { 
            "rivoirie_itinerary[departure]": departure,
            "rivoirie_itinerary[arrival]": arrival,
            "rivoirie_itinerary[mode]": mode,
            "rivoirie_itinerary[departure_at]": departure_at
        }
    });
}