(function($) {


  Drupal.behaviors.init_theme = function (context) {
    // Growl-style system messages
    $('#messages-and-help > div.messages:not(.processed)')
      .addClass('processed')
      .each(function() {
        // If a message meets these criteria, we don't autoclose
        // - contains a link
        // - is an error or warning
        // - contains a lenghthy amount of text
        if ($('a', this).size() || $(this).is('.error') || $(this).is('.warning') || $(this).text().length > 100) {
          $(this).prepend("<span class='close'>X</span>");
          $('span.close', this).click(function() {
            $(this).parent().slideUp('fast');
          });
        }
        else {
          // This essentially adds a 3 second pause before hiding the message.
          $(this).animate({opacity:1}, 5000, 'linear', function() {
            $(this).slideUp('fast');
          });
        }
      });
  };

  function init(){
    console.log('Clameurs Theme');
    initHeaderAnime();
    // initAutoScroll();
  };

  function initHeaderAnime(){
    var $header = $('#header a'),
        header_height = $header.height(),
        $slogan = $('#header h2'),
        fontsize = parseInt($slogan.css('font-size')), tmpfs,
        scrolltop, limit = 300;

    console.log('font-size', fontsize);

    $(window).on('scroll', function(event) {
      scrolltop = limit - $(this).scrollTop() * .5;
      scrolltop = Math.max(limit*0.35,scrolltop);
      // console.log('scrolltop', scrolltop);
      alpha = scrolltop/limit;
      console.log('scrolltop '+scrolltop+' | allpha '+alpha);

      $header.height(header_height*alpha);

      tmpfs = fontsize*alpha;
      $slogan.css('font-size', tmpfs+"px");
    });
  };

  function initAutoScroll(){
    console.log('initAutoScroll');
    if($('body').is('.front')){
      var anchor = $('a.anchor.publie', "#thematique-anchor-links").last().attr('href');
      console.log(anchor);
      (function(anchor){
        setTimeout(function(){
          console.log('Timeout');
          window.location.href = anchor;
        }, 1000);
      })(anchor);
    }
  };

  init();

})(jQuery);
