/* Script commun à toutes les pages du site */

// Fonctions Standard Macromedia Dreamweaver
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_validateForm() { //v3.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name;obj=val; if ((val=val.value)!="") {
      if (obj.length>1){
		checked=false;
		for (j=0;j<obj.length && !checked;j++){
			checked = obj[j].checked;
		}
		if (!checked) errors+='- Sélectionnez au moins une valeur dans la rubrique '+args[i].replace("[]","")+'.\n';
	  }
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- Le champ '+nm+' doit contenir une adresse mail valide.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (val!=''+num) errors+='- Le champ '+nm+' doit contenir une valeur numérique.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- Le champ '+nm+' doit contenir une valeur numérique comprise entre '+min+' et '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- Le champ '+nm+' est obligatoire.\n'; }
  } if (errors) alert('Le formulaire n\' a pas pu être validé pour les raisons suivantes :\n'+errors);
  document.MM_returnValue = (errors == '');
}

/* Pulse V5 */
// TODO Feature : Add option to use element style as from style (autodetect from)
var Pulse = new Class({
	Implements : Options,
	options : {
		from : '.pulsed-off',
		to : '.pulsed-on',
		duration : 500,
		morph : false,
		repeat : -1 // N = repeat N times, -1 = repeat infinite times
	},
	initialize : function (element,options){
		this.element = document.id(element);
		this.setOptions(options);
		if (this.options.repeat!=0){
			this.options.repeat-=0.5;
			if (this.options.morph){
				if (typeof this.options.morph=='object'){
					this.element.set('morph',this.options.morph);
				}
				this.element.morph(this.options.to);
			}else{
				if (typeof this.options.to == 'string'){
					this.element.removeClass(this.options.from.replace('.',''));
					this.element.addClass(this.options.to.replace('.',''));
				}else{
					this.element.setStyles(this.options.to);
				}
			}
			setTimeout(function(){
				var tmp = this.options.from;
				this.options.from = this.options.to;
				this.options.to = tmp;
				this.initialize(this.element, this.options);
			}.bind(this),this.options.duration);
		}
	}
});

// Initialisation de la page
window.addEvent('domready', function() {
	/* Animations Flash */
	$$('.flash').each(function(flash){
		new Swiff('/swf/'+flash.id+'.swf', {
		    container : flash.id,
			width: flash.getStyle('width'),
		    height: flash.getStyle('height'),
		    params: {
		        wmode: 'transparent',
		        menu: 'false'
		    }
		});
	});
	/* Diaporamas */
	$$('.diaporama').each(function(d){
		var photos = d.getElements("ul > li");
		if (photos.length>0) photos.w = photos[0].getWidth();
		if (!d.getElement("li.selected")) photos[0].set('class','selected');
		photos.each(function(photo,i){
			if (photo.hasClass('selected')) photos.i = i
		});
		photos.each(function(photo,i){
			photo.setStyle('margin-left',photos.i!=i?photos.w:0);
		});
		var span = new Element("span",{"text":(photos.i+1)+" sur "+photos.length});
		var precedent = new Element("a",{"class":"precedent"});
		var suivant = new Element("a",{"class":"suivant"});
		d.adopt(span,suivant,precedent);
		var effets = new Array();
		var afficher = function(i,sens){
			photos[i].setStyle('margin-left',(2*sens-1)*photos.w);
			photos[i].tween('margin-left',0);
		};
		var cacher = function(i,sens){
			photos[i].tween('margin-left',(2*sens-1)*photos.w);
		};
		precedent.addEvent('click', function(event){
			cacher(photos.i,1);
			photos.i = (photos.i+photos.length-1) % photos.length;
			span.set("text",(photos.i+1)+" sur "+photos.length);
			afficher(photos.i,0);
		});
		suivant.addEvent('click', function(event){
			cacher(photos.i,0);
			photos.i = (photos.i+1) % photos.length;
			span.set("text",(photos.i+1)+" sur "+photos.length);
			afficher(photos.i,1);
		});
	});
	/* Pulse */
	$$('.pulse').each(function(element){
		new Pulse(element,{from:{opacity:1},to:{opacity:0},morph:{duration:2000},duration:5000});
	});
});

