/* Définition des comportements javascript du site */

function setCSSBehaviours() {
	if ($('sondage')) {
		var eff = new Fx.Style('sondage', 'opacity', { duration: 1000 });
		eff.start(0, 1);
		a = new Element('a', { 'href': '#' }).appendText('Fermer');
		a.eff = eff;
		a.addEvent('click', function(e) { this.eff.start(1, 0); new Event(e).stop(); });
		div = new Element('div', { 'align': 'right'});
		a.injectInside(div);
		div.injectTop('sondage');
		$('sondage').setStyle('cursor', 'move');
		$('sondage').makeDraggable();
	}

	sliders = new Array();
	current = eff = '';
	$$('#produits div.produit').each(function (div) {
		div.setStyles({ 'position': 'absolute', 'height': '33.70em', 'width': '430px'})
		id = 'p'+div.getProperty('id').substring(1);
		sliders[id] = new Fx.Style(id, 'opacity', { duration: 1000 });
		cid = window.location.hash.substring(1);
		if (cid != id && !div.hasClass('visible')) sliders[id].hide();
		else current = id;
	});
	window.addEvent('unload', function() {
		// Montrer au chargement
		cid = window.location.hash.substring(1);
		if (sliders[cid]) sliders[cid].start(0,1);
	});

	$$('#applications').each(function (div) {
		eff = new Fx.Style(div, 'opacity', { duration: 1000 });
		eff.hide();
		a = new Element('a', {
			'title': 'Cliquez pour fermer',
			'href': '#',
			'styles': {
				'display': 'block',
				'text-align': 'right'
			},
			'events': {
				'click': function(e) { eff.start(1,0); new Event(e).stop(); }
			}
		}).setHTML('X');
		a.injectTop($E('#applications caption'));
		div.makeDraggable();
	});
	$$('#produits a.application').each(function (a) {
		a.addEvent('click', function(e) {
			eff.start(0, 1);
			new Event(e).stop();
		});
	});
	$$('#produits div.produit a').filterByClass('retour').each(function(a) {
		a.setHTML('Fermer');
		a.addEvent('click', function(e) {
			sliders[current].start(1, 0);
			current = '';
			new Event(e).stop();
		});
	});
	$$('ul#liste-produits a').each(function (a) {
		a.addEvent('click', function(e) {
			id = 'p'+a.getProperty('id').substring(1);
			if (current != '' || current == id) {
				sliders[current].start(1, 0);
			}
			sliders[id].start(0, 1);
			if (current == id) current = '';
			else current = id;
			$$('ul#liste-produits a').each(function (a2) { a2.removeClass('aselect'); });
			this.addClass('aselect');
			new Event(e).stop();
		});
	});
	$$('a.popup').each(function (a) {
		a.addEvent('click', function(e) {
			if (a.getProperty('id') == 'maison') {
				w = window.open(a.href, a.getProperty('id'), 'scrollbars=yes,width=875,height=450');
			} else {
				w = window.open(a.href, a.getProperty('id'), 'scrollbars=yes,width=800,height=530');
			}
			if (w.focus) w.focus();
			if (w) new Event(e).stop();
		});
	});
	$$('form.themes select').each( function(select) {
		select.addEvent('change', function () { select.form.submit(); });
	});
	$$('form div.row').each( function (field) {
		src = field.getProperty('id');
		depends = new Array();
		depends = document.getElements('div.row[id^='+src+'_]');
		if (src && depends.length > 0) {
			depends.each( function (depend) { depend.setStyles({ 'display': 'none', 'opacity' : 0 }); } );
			$ES('select', src).each( function(select) {
				select.depends = depends;
				select.addEvent('change', function() {
					sid = select.getProperty('id');
					sval = select.getValue();
					this.depends.each( function (depend) {
						eff = new Fx.Style(depend, 'opacity', { duration: 1000 });
						if (depend.getProperty('id') == 'row_'+sid+'_'+sval) {
							depend.setStyle('display', 'block');
							eff.start(0, 1);
						} else {
							eff.start(1, 0);
							depend.setStyle('display', 'none');
						}
					} );
				} );
				select.fireEvent('change');
			} );
		}
	} );
}


window.addEvent('domready', setCSSBehaviours);

function augmenter(field) { $(field).value++; }
function reduire(field) { if ($(field).value > 1) { $(field).value--; } }


// Validation des formulaires
function validation() { //v4.0
	var i,p,q,nm,test,num,min,max,errors='',args=validation.arguments;
	for (i=0; i<(args.length-2); i+=3) {
		test=args[i+2];
		var obj=document.getElementById(args[i]);
		var error = '';
		if (obj) {
			// Avant de continuer, déterminer si l'un des parents du champ est affiché ou non
			if (is_displayed(obj)) {
				var error = '';
				nm=args[i+1];
				if (!nm) nm=obj.name;
				if ((val=obj.value)!="") {
					if (test.indexOf('isEmail')!=-1) {
						if (val.search(/^[\w\d\._-]+@[\w\d\._-]+$/) == -1) {
							error='- « '+nm+' » doit être une adresse email valide\n';
						}
					} else if (test.indexOf('isDate')!=-1) {
					} else if (test!='R') {
						num = parseFloat(val);
						if (isNaN(val)) error='- « '+nm+' » doit être un nombre.\n';
						if (test.indexOf('inRange') != -1) {
							p=test.indexOf(':');
							min=test.substring(8,p); max=test.substring(p+1);
							if (num<min || max<num) error='- « '+nm+' » doit être un nombre entre '+min+' et '+max+'.\n';
						}
					}
				} else if (test.charAt(0) == 'R') { 
					if (obj.type == 'checkbox') {
						if (!obj.checked) error= '- « '+nm+' » est requis\n';
					} else {
						error= '- « '+nm+' » est requis\n';
					}
				}
				if (error != '') {
					errors += error;
					$(obj).addClass('missing');
				} else {
					$(obj).removeClass('missing');
				}
			}
		}
	}
	return errors;
}
// Renvoie faux si l'objet ou l'un de ses parents est masqué (display=='none')
function is_displayed(obj) {
	if (obj.style && obj.style.display == 'none') {
		return false;
	}
	if (obj.parentNode) {
		return is_displayed(obj.parentNode);
	} else {
		return true;
	}
}
function alert_validation() {
	args = new Array();
	for (var i=0; i<alert_validation.arguments.length; i++) {
		args[i] = "'"+alert_validation.arguments[i].replace("'","\\'")+"'";
	}
	eval("errors = validation("+args.join(',')+")");
	if (errors != '') alert('Les erreurs suivantes sont apparues\n'+errors);
	return (errors == '');
}

