/* jQuery add-ons & plugins */
	$.fn.toggleFade = function(settings){
		if(settings==undefined){
			settings={ speedIn:'slow'};
		}
		settings = jQuery.extend(
			{
				speedIn: "normal",
				speedOut: settings.speedIn
			}, settings
		);
		return this.each(function(){
			var isHidden = jQuery(this).is(":hidden");
			jQuery(this)[ isHidden ? "fadeIn" : "fadeOut" ]( isHidden ? settings.speedIn : settings.speedOut);
		});
	};

	$.fn.delay = function(time,func){
		this.each(function(){ 
			setTimeout(func,time); 
		});
		return this;
	};

	$.fn.clearForm = function(){
		return this.each(function(){
			var type = this.type, tag = this.tagName.toLowerCase();
			if (tag == 'form')
				return $(':input',this).clearForm();
			if (type == 'text' || type == 'password' || tag == 'textarea')
				this.value = '';
			else if (type == 'checkbox' || type == 'radio')
				this.checked = false;
			else if (tag == 'select')
				this.selectedIndex = -1;
		});
	};

	var duration = 4500;
	var transition = 1000;
	var setCarousel = '';
	function startSlideshow(settings,el){
		if(settings == 'init'){
			var index = 1;
			if(setCarousel != ''){
				startSlideshow('stop');
			}
			if($('.carousel').length > 0 && $('.carousel .item').size() > 1){
				$('.carousel').append('<div class="nav"><ul style="width:'+ ($('.carousel .item').size()*21) +'px;"></ul></div>');
				$('.carousel .item').each(function(){
					$(this).addClass('item'+index);
					index = index+1;
					$('.carousel .nav ul').append('<li class="item"><a class="'+$(this).attr('class')+'" href="">'+$(this).find('img').attr('alt')+'</a></li>');
				});
				$('.carousel .item').first().addClass('active').fadeIn(transition);
				$('.carousel .nav ul li').first().find('a').addClass('active');
				$('.carousel .nav ul li a').click(function(){
					return false;
				});
				$('.carousel .nav ul').append('<li class="left"><a href="">Previous</a></li><li class="right"><a href="">Next</a></li>');
				$('.carousel .nav ul li.item a').hover(function(){
					startSlideshow('stop',$(this));
				},function(){
					startSlideshow('start');
				});
				$('.carousel .nav ul li.left a').click(function(){
					if($('.carousel .nav ul li a.active').parent().prev('.item').length > 0){
						startSlideshow('stop',$('.carousel .nav ul li a.active').parent().prev().find('a.item'));
					}else{
						startSlideshow('stop',$('.carousel .nav ul li.item').last().find('a.item'));
					}
					startSlideshow('start');
					return false;
				});
				$('.carousel .nav ul li.right a').click(function(){
					if($('.carousel .nav ul li a.active').parent().next('.item').length > 0){
						startSlideshow('stop',$('.carousel .nav ul li a.active').parent().next().find('a.item'));
					}else{
						startSlideshow('stop',$('.carousel .nav ul li.item').first().find('a.item'));
					}
					startSlideshow('start');
					return false;
				});
				startSlideshow('start');
			}else{
				$('.carousel .item').fadeIn(transition);
			}
		}else if(settings == 'anim'){
			if($('.carousel div.item.active').next('.item').length > 0){
				$('.carousel div.item.active').fadeOut(transition).removeClass('active').next('.item').addClass('active');/*.delay(transition,function(){*/
					$('.carousel .nav ul li a.active').removeClass('active').parent().next('li').find('a').addClass('active');
					$('.carousel div.item.active').fadeIn(transition);
				/*});*/
			}else{
				$('.carousel div.item.active').fadeOut(transition).removeClass('active');
				$('.carousel div.item').first().addClass('active').delay(transition,function(){
					$('.carousel .nav ul li a.active').removeClass('active');
					$('.carousel .nav ul li').first().find('a').addClass('active');
					$('.carousel div.item.active').fadeIn(transition);
				});
			}
		}else if(settings == 'start'){
			if(setCarousel == ''){
				setCarousel = window.setInterval("startSlideshow('anim');",duration);
			}else{
				startSlideshow('stop');
			}
		}else if(settings == 'stop'){
			if(setCarousel != ''){
				window.clearInterval(setCarousel);
				setCarousel = '';
			}
			if(!$(el).hasClass('active')){
				$('.carousel .active').each(function(){
					$(this).removeClass('active');	
				});
				$(el).addClass('active');
				$('.carousel div.item').removeClass('active').fadeOut(200).delay(300,function(){
					$('.'+$(el).attr('class').replace(' active','').replace('item ','')).addClass('active').fadeIn(200);
				});
			}
		}
	}

	function showVideo(url){
		
	}

	function ajax(fichier,lediv){
		var xhr=null;
		if (window.XMLHttpRequest)
			xhr = new XMLHttpRequest();
		else if (window.ActiveXObject)
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		xhr.onreadystatechange = function() { alert_ajax(xhr,lediv); };
		xhr.open("GET", fichier, true);
		xhr.send(null);
	}
	
	function alert_ajax(xhr,lediv){
		var lasection = document.getElementById(lediv);
		if (xhr.readyState==4) {
			var items = xhr.responseText;
			lasection.innerHTML=items;
		}
	}

/* on DOM ready events */
	$(function(){
		/* basic on load actions */
			$('body').removeClass('noJs');
			$('html').addClass($('html').attr('lang'));
			$('.ext').each(function(){
				$(this).attr('target','_blank');
			});
			setTimeout("startSlideshow('init');",600);

		/* set active page in main nav */
			$('nav ul li').each(function(){
				if($(this).attr('class') == $('.wrapMain').attr('class').replace('wrapMain ','')){
					$(this).addClass('active');
				}
			});
			$('nav>ul>li').hover(function(){
				$(this).find('ul.accessAlt').removeClass('accessAlt').addClass('active');
			},function(){
				$(this).find('ul.active').removeClass('active').addClass('accessAlt');
			});

		/* photos & videos */
			$('div.col1 article.photoVideo p.thumb').each(function(){
				$(this).hide();
				$('div.col1 article.photoVideo p.thumb').first().show();
			});
			$('a.video').click(function(){
				$(this).lightbox
				return false;
			});
	});
	
	
  
  function clearThis(ceci) {
	if (ceci.value='Votre adresse courriel')
	  ceci.value='';
  }
  
  function EmailValid(e) {
    var carateres_permis = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-@";
	var valide = true;
	for (j=0; j < e.length; j++) {
	  if (carateres_permis.search(e.substr(j,1)) == -1) {
	    valide = false;
		break;
	  }
	}
	if (e.indexOf("..")>0 ||
		e.indexOf("@")==0 ||
		e.lastIndexOf("@")==(e.length-1) ||
		e.indexOf("@") != e.lastIndexOf("@") ||
		e.indexOf(".")<1 || 
		e.lastIndexOf(".")==(e.length-1) ||
		e.indexOf(".") == (e.indexOf("@")-1) ||
		e.indexOf(".") == (e.indexOf("@")+1)){
		  valide = false;
	}
	return valide;
  }
	
  function VerifInscription(f,lg) {
	if (!EmailValid(f.courriel.value)) {
	  f.courriel.focus();
	  f.courriel.select();
	  if (lg == 'fr')
	    alert("SVP, entrez une adresse de courriel valide");
	  else
	    alert("Please enter a valid Email address");
	} else
	  ajax('_newsletter.php?courriel='+f.courriel.value,'newsletter');
	return false;
  }
