/*
	Rémy Savard [iXmedia.com]
	global.js - Scripts
	Dernière modification: 17 novembre 2010
------------------------------------------------- */
$(document).ready(function(){

  // External links {{{
  var $links = $('a');

  $links.keyup(function(e) {
    if (e.which === 9) {
      $(this).addClass('focus');
    }
  });

  $links.blur(function() {
    $(this).removeClass('focus');
  });

  window.domainWithoutSubdomain = function(domainWithSubdomain) {
    var matches = domainWithSubdomain.match(DOMAIN_REGEX);
    return matches ? matches[0] : null;
  };

  var links = $links.get(),
    l = links.length;
    
  while (l--) {
    link = links[l];
    if (!link.className.match(/fancybox|videos/) && !link.href.match(/^(javascript:|mailto:)/) && (domainWithoutSubdomain(link.hostname) != domainWithoutSubdomain(location.hostname) || link.href.match(/\.(docx?|xlsx?|pptx?|pdf|eps|zip|vsd|vxd|rar|wma|mov|avi|wmv|mp3|mp4|mpg|mpeg|mpeg4|m4a|m4v|f4v|flv|csv|xml|ogg|oga|ogv|webm|jpg|jpeg|png|gif|webp|svg|ico|txt|css|js)$/))) {
      link.target = '_blank';
      link.title += link.title ? ' – S’ouvre dans une nouvelle fenêtre.' : 'S’ouvre dans une nouvelle fenêtre.';
      link.className += link.className.indexOf('externe') == -1 ? ' externe' : '';
    }
  }

  // Tooltip {{{
  xOffset = 25;
  yOffset = 25;

  $(".tooltip, option[title]").hover(function(e) {
      this.t = this.title;
      this.title = "";
      $("body").append('<p id="tooltip-i">' + this.t + "</p>");0
      var a = ($(document).width() - e.pageX > 350) ? e.pageX + xOffset : e.pageX - ($('#tooltip-i').width() < 300 ? $('#tooltip-i').width() : 300) - 15 - xOffset;
      $("#tooltip-i").css("top", (e.pageY - yOffset) + "px").css("left", a + "px").fadeIn("fast");
      if ($('#tooltip-i').width() > 300) {
          $('#tooltip-i').css('width', 300);
      }
  },
  function() {
      this.title = this.t;
      $("#tooltip-i").remove();
  });
  $(".tooltip, option[title]").mousemove(function(e) {
      var a = ($(document).width() - e.pageX > 350) ? e.pageX + xOffset : e.pageX - ($('#tooltip-i').width() < 300 ? $('#tooltip-i').width() : 300) - 15 - 55;
      if ($('#tooltip-i').width() > 300) {
          $('#tooltip-i').css('width', 300);
      }
      $("#tooltip-i").css("top", (e.pageY - xOffset) + "px").css("left", a + "px");
  });

  $(".tooltip").click(function(e) {
      e.preventDefault();
      return false;
  });
  // }}}
  
  $("option[title]").click(function(e) {
      $("#tooltip-i").remove();
  });

	$('a.rsswidget').attr('target', '_blank');
	
	if ($('#termes').length !== 0) {
		var $navTop,
			retourAutocomplete;

		$('#termes').autocomplete( '/ajax/recherche.php', {
			delay: 400,
			minChars : 2,
			scroll : false,
			autoFill : false,
			highlight : false,
			width: 165,
			selectFirst: false,
			cacheLength: 1
		});

		function updateNav() {
			$nav = $('div#fix');
			if ($(window).scrollTop() > ($navtop-30)) {
					$nav.stop().animate({'top':($(window).scrollTop()+30)+'px'},'fast');
			} else { 
				$nav.css({'position':'absolute','top':$navtop+'px'}); 
			}

			var newleft = ($('#entete h1').offset().left-38);

			if($nav.css('position')!='fixed'){
				$nav.css('right', '15px').addClass('reduite');
			}
		}
		
		if ($('div#fix').length != 0) {
			$navtop = $('div#fix').css('top').replace('px', '');
			$('#fix').css('position','absolute');
			$(window).scroll(updateNav);
			updateNav();
		}
	}

/***************************************************************************************
	[ Formulaires ] ==> Valeur par défaut
***************************************************************************************/

	$(":input.valeurParDefaut").each(function() { 
         
        var classeCSS = "couleurValeurParDefault"; // optionel 
        var valeurParDefaut = this.defaultValue; 
 
        // Au chargement de la page 
        var valeurActuel = $(this).val(); 
        if (valeurActuel == "") { 
            $(this).val(valeurParDefaut); 
            $(this).addClass(classeCSS); // optionel 
        } 
        // Sur le focus 
        $(this).focus(function() { 
            var valeurActuel = $(this).val(); 
            if (valeurActuel == valeurParDefaut) { 
                $(this).val(""); 
                $(this).removeClass(classeCSS); // optionel 
            } 
        }); 
        // Sur le blur 
        $(this).blur(function() { 
            var valeurActuel = $(this).val(); 
            if (valeurActuel == "") { 
                $(this).val(valeurParDefaut); 
                $(this).addClass(classeCSS); // optionel 
            } 
        }); 
    }); 

/***************************************************************************************
	[ Répertoires ] - Affichage des descriptions	
***************************************************************************************/

	$('ul.liste-repertoires li .desc').hide();
	$('ul.liste-repertoires li .plus').click(function() {
		$(this).parent().next().slideToggle(200);		
		return false;
	});


/***************************************************************************************
	[ Divers ] - Champs cliquables
***************************************************************************************/

	$('input.focus:not(.plein)').focus(function() {
		if ( $(this).val() == this.defaultValue ) {
			$(this).val('');
		}
	}).blur(function() {
		if ( $(this).val() == '' ) {
			$(this).val( this.defaultValue );
		}
	});

/***************************************************************************************
	[ En vedette ] - Navigation entre les 5 articles
***************************************************************************************/

	$('.vedette').each(function() {
		var $this = $(this);
		var $items = $this.find('.vedette-contenu');
		var $nav = $this.find('.nav');
		var $viewport = $this.find('.vedette-viewport');
		var position = 0;
		
		$nav.find('a').click(function() {
			if ( !$(this).parent().is('.inactive') ) {
				deplacer($(this).parent().is('.next')?1:-1);
			}
		});

		function deplacer(sens) {
			position = position + parseInt(sens,10);
			if ( position <= 0 ) { position = 0; }
			if ( position >= $items.length-1 ) { position = $items.length-1; }
			var newleft = position*460;
			$viewport.find('ul').stop().animate({
				left : (-1*newleft)+'px'
			},{
				duration : 200
			});
			update_hauteur(false);
			update_nav();
		}

		function update_nav() {
			if ( position <= 0 ) { $nav.find('.prev').addClass('inactive');
			} else { $nav.find('.prev').removeClass('inactive'); }
			if ( position >= $items.length-1 ) { $nav.find('.next').addClass('inactive');
			} else { $nav.find('.next').removeClass('inactive'); }
		}
		update_nav();

		function update_hauteur(init) {
			var duration = init ? 0 : 200;
			$viewport.stop().animate({
				height: (10+$items.eq(position).parent().height())+'px'
			},{
				duration: duration
			});
		}
		$(window).load(function() {
			update_hauteur(true);
		});
		
	});

/***************************************************************************************
	[ Commentaires ] - Politique éditoriale
***************************************************************************************/

	$('#respond .politique .toggle a').click(function() {
		$(this).parent().next().slideToggle(200);
	}).parent().next().hide();


/***************************************************************************************
	[ Recherche ] - Affiner sa recherche
***************************************************************************************/

	$('#sidebar .affiner h3').wrapInner('<a href="javascript://"></a>').find('a').click(function() {
		$(this).parent().next().toggle();		
	}).parent().next().each(function() {
		if ( !$('li.active', this).length ) {
			$(this).hide();
		}
	});
	
	
/***************************************************************************************
	Fonctions Sondage
***************************************************************************************/

	if ($('span#popup').length) {
		$.fancybox({
			'autoDimensions' : false,
			'autoScale' : false,
			'width' : 550,
			'height' : 475,
			'type' : 'iframe',
			/*'href' : '/coeureaction-sondage/',*/
			'href' : '/sondage-coeureaction/',
			'transitionIn' :  'none',
			'transitionOut' :  'none'
		});
	};

	if ($('body#le-sondage').length) {
		$('.radio').click(function () {
			$('p.erreurs').hide();
			if ($('#autre').is(':checked')){
				$('#si-autre').show();
			} else {
				$('#si-autre').hide();
			}
		});
	};
});

