/* EMPECHE LE FLICKING DE TYPEKIT SIMPLEMENT ENLEVER LES COMMENTAIRES POUR ACTIVER */
/**************************************************************************
(function(){
  // if firefox 3.5+, hide content till load (or 3 seconds) to prevent FOUT
  var d = document, e = d.documentElement, s = d.createElement('style');
  if (e.style.MozTransform === ''){ // gecko 1.9.1 inference
    s.textContent = 'body{visibility:hidden}';
    var r = document.getElementsByTagName('script')[0];
    r.parentNode.insertBefore(s, r);
    function f(){ s.parentNode && s.parentNode.removeChild(s); }
    addEventListener('load',f,false);
    setTimeout(f,3000); 
  }
})();
**************************************************************************/



jQuery(function($) {
	/* METTRE TOUS LES SCRIPTS POUR LE SITE ICI PLUTOT QUE DIRECTEMENT DANS LA PAGE! */
	
	
    /* ***************************************************************
        OUVERTURE DES LIENS EXTERNE DANS UN NOUVEAU TAB / WINDOW 
        Note : ajouter class="in" si vous voulez qu'il s'ouvre 
               dans la même fenêtre.
    *************************************************************** */
	jQuery('a[href^=http]:not(.in), a[href$=pdf], a[href$=doc], a[href$=docx]').attr({'target':'_blank'});
	
	$('.slider-nav').each(function() {
			$(this).html($(this).html().replace(/\>\s+\</gi,'><')).find('*').removeAttr('style');
	});
	// Banner anims..
	var tmBanner;
	//var bannerSize = ($('#imageslider').hasClass('large')) ? 950 : 710;
	var animTime = 400;
	if ($('.home .slider').length) {
		$('.home .slider li:not(:first)').hide();
		$('.home .slider-nav li a').click(function() {
			if ($(this).hasClass('active')) return false;
			$('.slider-nav li a.active').removeClass('active');
			$(this).addClass('active');
			$("#desc").html($('.slider-nav li a.active').data('desc'));
			$('.home .slider li:visible').fadeOut(animTime);
			var ident = $(this).parent().index();
			$('.home .slider li:eq('+ident+')').fadeIn(animTime);
			try { clearTimeout(tmBanner); } catch(e) {}
			tmBanner = setTimeout(fnBanner,5000);
			return false;
		});
		var fnBanner = function() {
			var $next = ($('.slider-nav li a.active').parent().next().length) ? $('.slider-nav li a.active').parent().next().find('a') : $('.slider-nav li:first a');
			$next.click();
		};
		tmBanner = setTimeout(fnBanner,5000);
	}
	

	$('#f-intranet a.popup:not(.no-link)').fancybox({
		'type'				: 'iframe',
		'width'				: 350,
		'height'			: 225,
		'hideOnOverlayClick': false,
		'autoScale' : false
	});
	$('a.popup,li.popup a').fancybox({
		'type' : 'iframe',
		'title' : false,
		'showNavArrows' : false,
		'titleShow' : false,
		'width'				: 330,
		'height'			: 280,
		'hideOnOverlayClick': false,
		'autoScale' : false
	});
	$('a.popnews,li.popnews a').fancybox({
		'type' : 'iframe',
		'title' : false,
		'showNavArrows' : false,
		'titleShow' : false,
		'width'				: 330,
		'height'			: 490,
		'hideOnOverlayClick': false,
		'autoScale' : false
	});
	$('.popadherez').fancybox({
		'type' : 'iframe',
		'title' : false,
		'showNavArrows' : false,
		'titleShow' : false,
		'width'				: 330,
		'height'			: 420,
		'hideOnOverlayClick': false,
		'autoScale' : false
	});
	$('.popparlez').fancybox({
		'type' : 'iframe',
		'title' : false,
		'showNavArrows' : false,
		'titleShow' : false,
		'width'				: 330,
		'height'			: 460,
		'hideOnOverlayClick': false,
		'autoScale' : false
	});
		$('.contact-item h3 a,.gmap').fancybox({
		'type' : 'iframe',
		'title' : false,
		'showNavArrows' : false,
		'titleShow' : false,
		'width'				: 500,
		'height'			: 500,
		'autoScale' : false
	});
	$('.newsimg').fancybox({
		'titlePosition' : 'over'
	});
	
	/* PAGE RÉCLAMATION */
	$('.reclamation-auto-link').click(function() {
		$('#reclamation-habit-container').hide();
		$('#reclamation-auto-container').show();
		return false;
	});
	$('.reclamation-habit-link').click(function() {
		$('#reclamation-auto-container').hide();
		$('#reclamation-habit-container').show();
		return false;
	});
	
	var setCounterVal = function(num) {
		var str = number_format(num,2,',','')+'$';
		//console.log(str);
		/*for(var x=0;x<=str.length;x++) {
			if ($('#f-counter li:eq('+x+')') != str[x]) {
				$('#f-counter li:eq('+x+')').html(str[x]);
			}
		}*/
		$('#f-counter li').each(function() {
			var $this = $(this);
			index = $this.index();
			
			var num = str[index];
			if ($this.html() == num) return;
			$this.addClass('anim');
			setTimeout(function() {
				$this.removeClass('anim').html(num);
			},200);
		});
	}	
	
	var counterval = round(parseFloat($('#f-counter').data('value')),2);
	setCounterVal(counterval);
	setInterval(function() { 
		counterval = round(parseFloat(counterval) + 0.02,10);
		setCounterVal(counterval);
	}, 250);
	
	function round (value, precision, mode) {
   
		var m, f, isHalf, sgn; // helper variables    precision |= 0; // making sure precision is integer
		m = Math.pow(10, precision);
		value *= m;
		sgn = (value > 0) | -(value < 0); // sign of the number
		isHalf = value % 1 === 0.5 * sgn;    f = Math.floor(value);
	 
		if (isHalf) {
			switch (mode) {
			case 'PHP_ROUND_HALF_DOWN':            value = f + (sgn < 0); // rounds .5 toward zero
				break;
			case 'PHP_ROUND_HALF_EVEN':
				value = f + (f % 2 * sgn); // rouds .5 towards the next even integer
				break;        case 'PHP_ROUND_HALF_ODD':
				value = f + !(f % 2); // rounds .5 towards the next odd integer
				break;
			default:
				value = f + (sgn > 0); // rounds .5 away from zero       
			}
		}
	 
		return (isHalf ? value : Math.round(value)) / m;
	}

	function number_format (number, decimals, dec_point, thousands_sep) {
    
		number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
		var n = !isFinite(+number) ? 0 : +number,
			prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
			dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
			s = '',
			toFixedFix = function (n, prec) {
				var k = Math.pow(10, prec);            return '' + Math.round(n * k) / k;
			};
		// Fix for IE parseFloat(0.55).toFixed(0) = 0;
		s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
		if (s[0].length > 3) {        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
		}
		if ((s[1] || '').length < prec) {
			s[1] = s[1] || '';
			s[1] += new Array(prec - s[1].length + 1).join('0');    }
		return s.join(dec);
	}
	
	$('#nj-submit').click(function() {
		jQuery.post('/nous-joindre',{nobot:1,prenom:$('#prenom').val(),nom:$('#nom').val(),dossier:$('#dossier').val(),phone:$('#phone').val(),courriel:$('#courriel').val(),message:$('#message').val()},function() {
			$('#nj-form').hide();
			$('#nj-thanks').show();
		});
		return false;
	});
	
});
