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

// Scoller V1
var Scroller = new Class({
	Implements:[Events,Options],
	options:{area:20,velocity:1,onChange:function(A,B){this.element.scrollTo(A,B);}},
	initialize:function(B,A){
		this.setOptions(A);
		this.element=$(B);
		this.listener=($type(this.element)!="element")?$(this.element.getDocument().body):this.element;
		this.timer=null;
		this.coord=this.getCoords.bind(this);
	},
	start:function(){
		this.listener.addEvent("mousemove",this.coord);
	},
	stop:function(){
		this.listener.removeEvent("mousemove",this.coord);
		this.timer=$clear(this.timer);
	},
	getCoords:function(A){
		this.page=(this.listener.get("tag")=="body")?A.client:A.page;
		if(!this.timer){this.timer=this.scroll.periodical(50,this);}
	},
	scroll: function(){
		var size = this.element.getSize(), scroll = this.element.getScroll(), pos = this.element.getOffsets(), scrollSize = this.element.getScrollSize(), change = {'x': 0, 'y': 0};
		for (var z in this.page){
			if (this.page[z] < (this.options.area + pos[z]) && scroll[z] != 0)
				change[z] = (this.page[z] - this.options.area - pos[z]) * this.options.velocity;
			else if (this.page[z] + this.options.area > (size[z] + pos[z]) && scroll[z] + size[z] != scrollSize[z])
				change[z] = (this.page[z] - size[z] + this.options.area - pos[z]) * this.options.velocity;
		}
		if (change.y || change.x) this.fireEvent('change', [scroll.x + change.x, scroll.y + change.y]);
	}
});

// Pulse V1.0
var Pulse = function(element,from, to){
	element.fade(to);
	setTimeout(function(){
		Pulse(element, to, from);
	},500);
}

// Initialisation de la page
window.addEvent('domready', function() {
	/* Accueil */
	var pub = $('pub');
	if (pub){
		pub.getElement('.close').addEvent('click',function(){
			pub.slide('out');
		});
	}
	/* Menu*/
	var menu = $('menu');
	var items = menu.getChildren('li');
	var links = menu.getElements('span');
	var submenus = menu.getElements('ul');
	var accordion = new Accordion(links,submenus,{duration:300});
	accordion.display(-1);
	links.each(function(link,i){
		link.addEvent('mouseenter', function(){
			link.getElement('a').addClass('selected');
			accordion.display(i);
		});
	});
	items.each(function(e){
		e.addEvent('mouseleave', function(){
			links.getElement('a').removeClass('selected');
			accordion.display(-1);
		});
	});
	/* Défilement des modèles de maisons */
	if($chk($$("#modeles .masque")[0])){
		var defil = new Fx.Scroll($$("#modeles .masque")[0], {"duration": 500} );
		var elems = $$("#modeles .masque ul li");
		var max = elems.length;
		var i = 0;
		(function(){
			if(i == max-1) i=0; else i++;
			defil.toElement(elems[i]);
		}).periodical(3000);
	}
	/* 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'
		    }
		});
	});
	/* Marquees */
	$$('.marquee').each(function(m){
		m.scroller = new Scroller(m, {area: 100, velocity: 0.5});
		m.setAutoScroll = function(isAuto){
			if (isAuto){
				this.autoScroll = (function(){
					m.scrollTo(0, m.getScroll().y + 1);
				}).periodical(50);
			}else{
				$clear(this.autoScroll);
			}
		}
		m.setAutoScroll(true);		
		m.addEvents({
			'mouseenter' : function(){
				this.setAutoScroll(false);
				this.scroller.start();
			},
			'mouseleave' : function(){
				this.scroller.stop();
				this.setAutoScroll(true);
			}
		});
	});
	/* Vignettes */
	$$('ul.vignettes li a[rel="lightbox[image]"]').each(function(a){
		a.addEvent('mouseover',function(){
			var imageDetail = $('imageDetail');
			if (imageDetail.rel){
				imageDetail.href = this.href;
				imageDetail.getElement('img').src = this.href;
			}
		});
	});
	/* Switches de formulaires */
	$$('input.switch').each(function(s){
		s.addEvent('click',function(){
			$(this.name).setStyle('display',this.value=='oui' ?'block':'none');
		});
	});
	/* Afficher les maisons au survol d'une zone du plan de masse (maisons à 15 euros) */
	var maisons = $$('ul.articles li');
	$$('map#plan-de-masse area').each(function(a,i){
		a.maison = maisons[i];
		a.maison.addEvent('mouseleave',function(){
			this.setStyle('display','none');
		});
		a.addEvents({
			'mouseenter' : function(e){
				maisons.setStyle('display','none');
				this.maison.setStyles({
					'display' : 'block',
					'left' : e.page.x + 10,
					'top' : e.page.y + 10
				});
			}
		});
	});
	/* Eléments pulsés */
	$$('.pulse').each(function(e){
		new Pulse(e);
	});
	/* Animer le slogan "Les clés de votre confort" */
	var slogan = $$('#haut h2')[0];
	var morph = new Fx.Morph(slogan,{duration:1000});
	function animerSlogan(){
		morph.set({'top':'-100px'});
		if (Browser.Engine.trident){
			//V1 : Animation sans couche alpha (IE)
			morph.start({'top':0})
				.chain(function(){morph.start({'opacity':1})})
					.chain(function(){morph.start({'opacity':1})})
						.chain(function(){morph.start({'opacity':1})})
							.chain(function(){morph.start({'top':'100px'})})
								.chain(animerSlogan);
		}else{
			//V2 : Animation + couche alpha (bogue affichage sur IE)
			morph.start({'opacity':1,'top':0})
				.chain(function(){morph.start({'opacity':0.5})})
					.chain(function(){morph.start({'opacity':1})})
						.chain(function(){morph.start({'opacity':0.5})})
							.chain(function(){morph.start({'opacity':1})})
								.chain(function(){morph.start({'top':'80px','opacity':0})})
									.chain(animerSlogan);
		}
	};
	animerSlogan();
});
