/* ****************************************************

	@file         global.js
	@description  Comportements globaux
	@author       remi (ixmedia.com)
	vim: set noet ts=4 fdm=marker fenc=utf-8:

***************************************************** */

(function($) {

	$(function() {

/* @section Basic {{{
******************************************************************************/
		$('a[href^="http"]').each(function() {
					$this = $(this);
					if ( $this.attr('href').replace( /^https?:\/\/(m\.)?([^.]+)/, '$2' ) != window.location.hostname.replace( /^https?:\/\/(m\.)?([^.]+)/, '$2' ) && $this.attr('href').indexOf("www.acelf.ca")<1) {
						//console.log( $this.attr('href').replace( /^https?:\/\/([^.]+)/, '$1' ) );
						$this.attr('target','_blank').attr('title',( $this.attr('title') ? $this.attr('title')+' – S’ouvre dans une nouvelle fenêtre.' : 'S’ouvre dans une nouvelle fenêtre.' ));;
					}
				});
		
		$('#se').not('.plein').focus(function() {
			if ( $(this).val() == this.defaultValue ) {
				$(this).val('');
			}
		}).blur(function() {
			if ( $(this).val() == '' ) {
				$(this).val( this.defaultValue );
			}
		});

		$('body.template-accueil #entete h2').each(function() {
			var $spans = $(this).find('span');
			$spans.css('opacity', 0).show().each(function() {
				var delai = $(this).parent().children().index(this)
				var $this = $(this);
				setTimeout(function() {
					$this.animate({
						opacity: 1
					},{
						duration: 300
					})
				}, (delai*1000)+1000);
			});
		});
			
		$('#utilitaires ul li').hover(function(){									   
			$(this).children('ul').css('left', '0');					  
		}, function(){
			$(this).children('ul').css('left', '-9999px');
		});
		
		// envoyer en haut
		$('a.haut-page').click(function(e){
				e.preventDefault;
				$(this).scrollTo(0,0);
		});
		
		/*$('.envoyer-a-un-ami a').click(function(){
			$(this).parent().children('.wrap-c').slideToggle();
			$(this).hide();
			return false;
		});*/
/*}}}*/

/* @section Histoires collectives {{{
******************************************************************************/
	if($('.template-formulaire').length>0){
		var	maxWords = 600;		
		$('#texte').bind('keyup click blur focus change paste', function() {
			var numWords = jQuery.trim($(this).val()).split(' ').length;
			if($(this).val() === '') {
				numWords = 0;
			}	
			$('#nb-chars').text(numWords);
			
			if(numWords < minWords || (numWords > maxWords && maxWords != 0)) {
				$('#nb-chars').addClass('error');
			} else {
				$('#nb-chars').removeClass('error');	
			}
		});
	}
/*}}}*/

/* @section Galleria {{{
******************************************************************************/
	if($('.gallery').length>0){

		function randomNumber(limit){
		  return Math.floor(Math.random()*limit);
		}
	
		$('ul.gallery li:eq('+randomNumber($('ul.gallery li').length)+')').addClass('active');
	
		$('ul.gallery').galleria({
			insert: '.conteneur_galleria'
		});
	
	}
/*}}}*/

	if($('body').hasClass('template-blogue-communaute')){
		$('#connexion-ajax').hide();
		$('#lien-connexion-ajax').click(function(){
			$('#connexion-ajax').slideDown();
			return false;
		});
	}

/* @Formulaire de commande {{{
******************************************************************************/
  if($('#formulaire-de-commande').length){(function(){
    $('#btn-envoi').attr('disabled', true);
    
    var grandTotal,
        whatsTheTotal = function() {
          var total = 0;
          $('.calcul').each(function(){
            total += parseInt($(this).val(), 10);
          })
          return total;
        },
        calculateEverything = function() {
          var _this = $(this);
          if(_this.val() === '' || !_this.val().match(/^[0-9]+$/)) {
            _this.val('0');
          }
          grandTotal = whatsTheTotal();
          if(grandTotal >= 50) {
            $('span#total').addClass('ok');
            $('#btn-envoi').attr('disabled', false);
          } else {
            $('span#total').removeClass('ok');
            $('#btn-envoi').attr('disabled', true);
          }
          $('#grand-total').html(grandTotal);
        };
        
    calculateEverything();
    
    $('.calcul').blur(calculateEverything);
    
    $('.calcul').focus(function(){
      var _this = $(this);
      
      if(_this.val() === '0') {
        _this.val('');
      }
      
    });
    
  }())}
	  
	  /* archives wordpress - lien actif */ 
	if($('#nav-mois li').length>0){
		$('#nav-mois li').each(function(){
										//console.log($(this).children('a').attr('href'),window.location.href);
			if($(this).children('a').attr('href') == window.location.href)
				$(this).addClass('active');
		 });
	}

})



})(jQuery);

