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

// var is_searching = false;
// var will_search_timer = null;

onDomLoad(function() {
  // search input
  var search_input = $('q');
  if (search_input && (navigator.appVersion.indexOf('AppleWebKit')>0)) {
    search_input.setAttribute('type', 'search');
    search_input.setAttribute('placeholder', 'Recherche');
    search_input.setAttribute('autosave', 'Pierlis');
    search_input.setAttribute('results', '10');
    search_input.setAttribute('onsearch', 'javascript: form.onsubmit();');
  }
  
  // // periodical search
  // 
  // Event.observe(search_input, 'keyup', function(event) {
  //   if (will_search_timer) clearTimeout(will_search_timer);
  //   will_search_timer = setTimeout(function() {
  //     if (!is_searching) {
  //       var search_string = $F('q');
  //       if (search_string.length > 0 && search_string != 'Recherche' && search_string != 'recherche') {
  //         is_searching = true;
  //         new Ajax.Request(
  //               '/recherche', {
  //               asynchronous: true,
  //               evalScripts:  true,
  //               parameters:   'q='+encodeURIComponent($F('q')),
  //               onComplete:   function() { is_searching = false; }});
  //       }
  //     }
  //   }, 500);
  // });
  
  // google map
  if ($('map')) {
    if (GBrowserIsCompatible()) {
      var map = new GMap2(document.getElementById("map"));
      map.setCenter(new GLatLng(48.8772, 2.351), 15);
      var marker = new GMarker(new GLatLng(48.876217, 2.350367));
      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml("<b>Nos locaux</b><br />8, rue des Messageries<br />75010 Paris");
      });
      map.addOverlay(marker);
      map.addControl(new GSmallMapControl());
    }
  }
  
  // give <a> in textile a behavior of target: _blank
  $$('.text a').each(function(element) {
    // but only if their url is absolute
    if (element.getAttribute('href').indexOf('http://') == 0) {
      element.target='_blank';
    }
  });

  // give <a> in comments a behavior of target: _blank
  $$('.comment a').each(function(element) {
    // but those with "permalink" class
    if (!Element.hasClassName(element, 'permalink')) {
      element.target='_blank';
    }
  });

  // give <a> in footer a behavior of target: _blank
  $$('#footer .standards a').each(function(element) {
    element.target='_blank';
  });

  // give <a> in news feeds a behavior of target: _blank
  $$('.news_feed a').each(function(element) {
    element.target='_blank';
  });
}, false);


// Google destructor
Event.observe(window, 'unload', function() {
  if ($('map')) {
    GUnload();
  }
});
