/*
	Rémy Savard, Jean-Philippe Sirois [iXmedia.com]
	global.js - Scripts
	Dernière modification: 21 juin 2011
------------------------------------------------- */
$(function(){

/* Gestion des liens externes {{{
******************************************************************************/
	window.domainWithoutSubdomain = function( domainWithSubdomain ) {
		var matches = domainWithSubdomain.match( DOMAIN_REGEX );
		return matches ? matches[0] : null;
	};

	var l = document.links.length, link;
	while (l--) {
		link = document.links[l];
		if ( !link.className.match(/fancybox/) && !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' : '';
		}
	}

/*}}}*/

/* Colorbox {{{
******************************************************************************/

	$("a[rel='vignettes']").colorbox({
		current: ''
	});
	
	$().bind('cbox_open', function(){
        $('object').parent().css('display','none');
	});
	
	$().bind('cbox_closed', function(){
        $('object').parent().css('display','block');
	});

/*}}}*/

/* Ajustement de la taille de font {{{
******************************************************************************/

	if ($.cookie('ixfont_size')) {
		$('body').addClass($.cookie('ixfont_size'));
		if($('body').hasClass('a-grand')){
			$('#a-moyen').addClass('actif');
		}
	} else {
		var defaultfont = $('#font-size a[rel=default-font]').attr("id") || '';
		$('body').addClass(defaultfont);
	}
	
	// on bâtit un array de tailles disponibles
	var ixfont_tailles = new Array("a-moyen","a-grand");
	
	$('#font-size a').click(function() {
		// on enlève les classes existantes
		if($('body').hasClass("a-moyen")){
			var newclass = "a-grand";
			$(this).addClass('actif');			
		} else {
			var newclass = "a-moyen";
			$(this).removeClass('actif');
		}
		
		$.each(ixfont_tailles,function(i, n) {
			$('body').removeClass(n);
		});
		
		// on applique la nouvelle classe
		$('body').addClass(newclass);
		
		// on place le cookie
		$.cookie('ixfont_size', newclass, { expires: 365 });
		
		return false ;
	});

/*}}}*/

/* Tooltip {{{
******************************************************************************/

	$(".tip").tooltip({
		tip: '.reponse',
		position: "center left",
		opacity: 0.7,
		offset: [0, -15]
	});

/*}}}*/

/* Valeur par défaut dans les champs {{{
******************************************************************************/

	$(":input.valeurParDefaut").each(function() { 

		var classeCSS = "couleurValeurParDefault"; // optionel 
		var valeurParDefaut = $(this).attr("title"); 

		// 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 
			} 
		}); 
	}); 

/*}}}*/

/* Calendrier {{{
    ******************************************************************************/

      if ( $('.calendrier-wrap').length ) {

        if ( ($journeeSelectionnee = $('.journee-selectionnee')).length ) {
          $('.calendrier-wrap').not( $journeeSelectionnee.parents('.calendrier-wrap') ).hide();
        } else {
          $('.calendrier-wrap:not(.mois-courant)').hide();
        }

        var $calendriers = $('.calendrier-wrap');

        $calendriers.each(function(i) {
          var $this = $(this);

          if ( i == 0 ) {
            $this.find('.lien-precedent').addClass('desactive');;
          } else {
            $this.find('.lien-precedent').click(function() {
              $this.hide();
              $('.calendrier-wrap:eq('+ (i-1) +')').show();
            });
          }
        });

        $calendriers.each(function(i) {
          var $this = $(this);

          if ( i >= $calendriers.length - 1 ) {
            $this.find('.lien-suivant').addClass('desactive');
          } else {
            $this.find('.lien-suivant').click(function() {
              $this.hide();
              $('.calendrier-wrap:eq('+ (i+1) +')').show();
            });
          }
        });
      }

    /* }}} */

});


