$( function() {
  $("#quicksearch input[type=text]").val("Recherche rapide...");
  $("#quicksearch input[type=text]").focus(function() {if($(this).val() == "Recherche rapide...") $(this).val('')});
  $("#quicksearch input[type=text]").blur(function() {if($(this).val() == '') $(this).val("Recherche rapide...")});

  $('.spoiler').each( function() {
    $(this).removeClass('degrade');
    $(this).children('.title').html('<span>Spoiler</span><a class="img_link" href="#"><img src="/extension/ag_design/design/refuge/images/icons/lightbulb.png"/> Afficher/Masquer</a>');
    $(this).children('.content').wrapInner('<div class="show_hide">');

    $(this).find('.title a').click(function(){
      $(this).parent().next().children('.show_hide').slideToggle();
      return false;
    });

    $(this).find('.content .show_hide').hide();
  });

  $('.time_ago').each( function() {
    var current_date = new Date();
    var interval = Math.floor( current_date.getTime() / 1000) - $(this).attr('rel');
    var nb = 0;
    var string = '';
    if ( interval > 31536000 ) {
      nb = Math.floor( interval / 31536000 );
      string = 'il y a plus ' + ((nb == 1)?'d\'1 an':'de ' + nb + ' ans');
    }else if( interval > 2592000 ) {
      nb = Math.floor( interval / 2592000 );
      string ='il y a ' + nb + ' mois';
    }else if( interval > 604800 ) {
      nb = Math.floor( interval / 604800 );
      string ='il y a ' + nb + ' semaine' + ((nb > 1 )?'s':'');
    }else if( interval > 86400 ) {
      nb = Math.floor( interval / 86400 );
      interval = Math.floor( (interval - nb * 86400) / (12 * 3600));
      string ='il y a ' + nb + ' jour' + ((nb > 1 )?'s':'') + ((interval != 0)?' ½':'');
    }else if( interval > 3600 ) {
      nb = Math.floor( interval / 3600 );
      interval = Math.floor((interval - nb * 3600) / (5 * 60));
      string ='il y a ' + nb + ' heure' + ((nb > 1 )?'s':'') + ((interval != 0)?' ' + (interval * 5):'');
    }else if( interval > 60 ) {
      nb = Math.floor( interval / 60 );
      string ='il y a ' + nb + ' minute' + ((nb > 1 )?'s':'');
    }else {
      string ='il y a quelques secondes';
    }
    $(this).text(string);
  });

  $('.shout-form textarea')
    .keyup(function() {
      val = $(this).val();

      if( val.length > 250 ) {
        val = val.substr(0, 250);
        $(this).val(val);
      }

      $(this).siblings('p').find('.char_count span').text( val.length );
    })
    .siblings('p')
    .prepend('<span class="char_count"><span>0</span>/250 caractères</span>');

  SyntaxHighlighter.all()
});

