// 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 <<=========================== */

    /* =menu ================================ */
        Event.observe(window, "load", function() {
          $$('.menu ul ul').each(function(submenu) {
            var menuLink = submenu.parentNode.getElementsByTagName('A')[0];
            var isCurrentMenu = Element.hasClassName(menuLink, 'current');
            Event.observe(submenu.parentNode, 'mouseover', function(event) {
              Element.show(submenu);
              if (!isCurrentMenu) {
                Element.addClassName(menuLink, 'current');
              }
            });
            Event.observe(submenu.parentNode, 'mouseout', function(event) {
              Element.hide(submenu);
              if (!isCurrentMenu) {
                Element.removeClassName(menuLink, 'current');
              }
            });
          });

          if($("animation-tur")) {
            var so = new SWFObject("/flashs/animation-tur.swf", "animation-tur", "736", "231", "7");
            so.addParam("wmode", "transparent");
            so.write("animation-tur");
          }

          $$('.way').each(function(el) { Event.observe(el, "click", function(e) {
            Event.stop(e); alert("Recherche d’itinéraire en cours d’amélioration et disponible prochainement") }) });
        });


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

    /* =home ================================ */
        // pageInit("trip_plans/new", function() {
        //     TripPlansNew.init();
        // });




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

    /* =loadScript ================================ */
        function loadScript(url) {
          var s = document.createElement('script');
          s.type = 'text/javascript';
          s.src = url;
          document.getElementsByTagName('head')[0].appendChild(s);
        }


    /* =pageInit ================================ */
        function pageInit (urlRegExp, funct) {
            page_url_reg_exp = new RegExp(urlRegExp);
            if(window.location.toString().match(page_url_reg_exp)){
                Event.observe(window, 'load', function() { funct.call() });
            }
        }

/*
 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);
  });
});
