/* 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.
    *************************************************************** */
		
		$("nav > ul > li").hover(
		function() {
			$(this).toggleClass('active');
			/*if ($.browser.msie && $.browser.version < 8) $(this).find(".subnav").stop(false,true).slideToggle();
			else $(this).find(".subnav").stop(false,true).slideToggle("fast");*/
		}
	);
	$(".sidenav > li span").click(
		function() {
			$(this).toggleClass('close');
			if ($.browser.msie && $.browser.version < 8) $(this).parent().find("ul").stop(false,true).slideToggle();
			else $(this).parent().find("ul").stop(false,true).slideToggle("fast");
		}
	);

	var $curLink = $('.sidenav li li a[href="'+location.pathname+'"]:last');
	if ($curLink.length) {
		$curLink.addClass('active').parent().parent().show().parent().find('> span').addClass('close');
	}
	
	$(".sidenav > li").each(function() {
		if ($(this).hasClass('active')) $(this).find('> span').addClass('close').parent().find("ul").show();
	});
	
	//$(".sidenav > li").hasClass('active').find('> span').addClass('close').find("ul").stop(false,true).slideToggle("fast");
		
		$(".faq li > a").click(
		function(){
			$(this).next().stop(false,true).slideToggle("normal");
      if($(this).find('span').css('display') == 'none') {
        $(this).find('span').css('display','block');
      }else{
        $(this).find('span').css('display','none');
      }
      
      return false;
			}
		);
		
		
	jQuery('a[href^=http]:not(.in)').attr({'target':'_blank'});
	
	
		// Banner anims..
	var tmBanner;
	//var bannerSize = ($('#imageslider').hasClass('large')) ? 950 : 710;
	var animTime = 800;
	if ($('#promo-wrapper .nav').length) {
		//alert('HELLO');
		$('#promo ul li:not(:first)').hide();
		$('#promo-wrapper .nav li a').click(function() {
			if ($(this).hasClass('active')) return false;
			$('#promo-wrapper .nav li a.active').removeClass('active');
			$(this).addClass('active');
			$('#promo ul li:visible').fadeOut(animTime);
			var ident = $(this).parent().index();
			$('#promo ul li:eq('+ident+')').fadeIn(animTime);
			try { clearTimeout(tmBanner); } catch(e) {}
			tmBanner = setTimeout(fnBanner,5000);
			return false;
		});
		var fnBanner = function() {
			//try { } catch(e) {}
			var $next = ($('#promo-wrapper .nav li a.active').parent().next().length) ? $('#promo-wrapper .nav li a.active').parent().next().find('a') : $('#promo-wrapper .nav li:first a');
			$next.click();
		};
		tmBanner = setTimeout(fnBanner,5000);
	}
	
	$('.form input[type=checkbox],.form input[type=radio]').prettyCheckboxes();
	$('.form .inlineRadios input[type=radio]').prettyCheckboxes({'display':'inline'});
				
	$('.long').click(function() {
		if ($(this).hasClass('active')) return;
		$('.long.active').removeClass('active');
		$(this).addClass('active');
	});
	
	/*$('.sidenav li').click(function() { 
		$(this).parent().find('> ul');//.slideToggle();
		if ($(this).hasClass('active')) $(this).removeClass('active');
		$(this).find('ul').slideToggle();
		var myover = $(this).parent();
		if (myover.hasClass('active')) myover.removeClass('active');
		else myover.addClass('active');
		//return false;
	});*/
	
	$(function(){
			$('#lst-glossaire-result').masonry({
					// options
					itemSelector : 'li',
					columnWidth : 313
			});
	});
	$(function(){
			// $('#sitemap').masonry({
					// // options
					// itemSelector : '#sitemap > li',
					// columnWidth : 313
			// });
      $('#sitemap').isotope({
        getSortData : {
          name : function ( $elem ) {
            return $elem.find('a.nom').text();
          }
        },
        masonry: {
          columnWidth: 313
        },
        sortBy : 'name'
      });
	});
	
	
	$(function(){
		$('.promo-popup').fancybox({'type':'iframe', 'width': 280, 'height':240, 'titleShow' : false, 'title' : false});
	});
	
	$('.search-txt').autocomplete({
		source: "/autocomplete.php",
		minLength: 1,
		select: function( event, ui ) {
			if (ui.item) {
				//console.log(ui.item);
				location.href='/recherche?q='+escape(ui.item.label);
			}
		}
	});
	
	if ($('.formservices').length) {
		$('.formservices').attr('action','/form-merci.php').append('<input type="hidden" name="src_url" value="'+location.href+'" />');
	}
	
	
	//concours
	//reglements de concours
	$('.concours_reglements').fancybox({'type':'iframe', 'width': 580, 'height':400});
	
	//concours - possède une assurance
	$('#yesauto').click(function(){ $('#noautodate').hide(); });
    $('#noauto').click(function(){ $('#noautodate').show(); });
      		
    $('#yeshabi').click(function(){ $('#nohabidate').hide(); });
    $('#nohabi').click(function(){ $('#nohabidate').show(); });

	//concours valid
	var validMail = function(email,isrequired) {
		if (!isrequired && email=='') return true;
		var result = false
		var theStr = new String(email)
		var index = theStr.indexOf("@");
		if (index > 0) {
			var pindex = theStr.indexOf(".",index);
			if ((pindex > index+1) && (theStr.length > pindex+1)) result = true;
		}
		return result;
	}
	$('#submit-bt').click(function() {
	$('#frmConcours').submit(function(){
			var valid = true;
			
			//nom
			if ($('#nom').val() == "") {
				valid = false;
				$('#valnom').show();
			} else $('#valnom').hide();

			//prenom
			if ($('#prenom').val() == "")  {
				valid = false;
				$('#valprenom').show();
			}else $('#valprenom').hide();			

			//adresse
			if ($('#adresse').val() == "") {
				valid = false;
				$('#valadresse').show();
			}else $('#valadresse').hide();

			//ville
			if ($('#ville').val() == "") {
				valid = false;
				$('#valville').show();
			}else $('#valville').hide();

			//code postal
			if ($('#codepostal').val() == "") {
				valid = false;
				$('#valcodepostal').show();
			}else $('#valcodepostal').hide();

			//telephone
			if (!$('#tel1').val().match(/\d{3}/) || !$('#tel2').val().match(/\d{3}/) || !$('#tel3').val().match(/\d{4}/)) {
				valid = false;
				$('#valtelephone').show();
			}else $('#valtelephone').hide();

			//courriel
			if (!validMail($('#courriel').val())) {
				valid = false;
				$('#valcourriel').show();
			}else $('#valcourriel').hide();

			//meilleur moment
			if (!$('#jour').attr('checked') && !$('#soir').attr('checked')) {
				valid = false;
				$('#valmeilleurmoment').show();
			}else $('#valmeilleurmoment').hide();

			//question d'habileté
			if ($('#question').val() == "") {
				valid = false;
				$('#valquestion').show();
			}else $('#valquestion').hide();

			//compagnie
			if ($('#compagnie').val() == "") {
				valid = false;
				$('#valcompagnie').show();
			}else $('#valcompagnie').hide();

			//deja assurance auto
			if (!$('#yesauto').attr('checked') && !$('#noauto').attr('checked')) {
				valid = false;
				$('#valdejaassuranceauto').show();
			}else $('#valdejaassuranceauto').hide();

			//renouvellement auto
			if ($('#noauto').attr('checked') && ($('#renauto').val() == "" || $('#renauto').val() == dateformat)) {
				valid = false;
				$('#valrenauto').show();
			}else $('#valrenauto').hide();

			
			//deja assurance habi
			if (!$('#yeshabi').attr('checked') && !$('#nohabi').attr('checked')) {
				valid = false;
				$('#valdejaassurancehabi').show();
			}else $('#valdejaassurancehabi').hide();

			//renouvellement habi
			if ($('#nohabi').attr('checked') && ($('#renhabi').val() == "" || $('#renhabi').val() == dateformat)) {
				valid = false;
				$('#valrenhabi').show();
			}else $('#valrenhabi').hide();

			
			//terms
			if (!$('#accept').attr('checked')) {
				valid = false;
				alert(acceptcond);
			}

			return valid;
      
			
		});
    });
});
