/* Scripts du site ircf.fr */

/* Déclaration */

/* Fonctions 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.type=="checkbox")	if (!obj.checked) errors+='- La case '+nm+' doit être cochée.\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 += '- '+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 == '');
}

/* Captcha V1 */
var Captcha = new Class({
	Implements : Options,
	options : {
		form : null,
		error : 'Error',
		action : null
	},
	initialize : function(element,options){
		this.element = element;
		this.setOptions(options);
		this.a = Math.ceil(Math.random() * 10);
		this.b = Math.ceil(Math.random() * 10);
		this.c = this.a + this.b;
		this.label = new Element('span',{text:this.a + ' + ' + this.b + ' = '}).inject(this.element);
		this.input = new Element('input',{type:'text'}).inject(this.element);
		if (!this.options.form) this.options.form = this.element.getParent('form');
		if (this.options.form){
			this.options.form.oldsubmit = this.options.form.onsubmit;
			this.options.form.addEvent('submit', function(){
				if (this.input.value == this.c){
					this.options.form.action = this.options.action;
					return this.options.form.oldsubmit();
				}else{
					alert(this.options.error);
					return false;
				}
			}.bind(this));
		}
	}
});

/* Gallery V1HACK - NE PAS UPGRADER ! */
var Gallery = new Class({
	Implements : Options,
	options : {
		buttons : '.button',
		auto : true,
		step : 1,
		elemsPerView : 2,
		delay : 6000,
		containerEvents : null,
		itemEvents : null
	},
	initialize : function(element,options){
		this.element = $(element);
		this.setOptions(options);
		this.container = this.element.getElement('div');
		this.items = this.element.getElements('li.reference');
		this.buttons = this.element.getElements(this.options.buttons);
		this.list = this.container.getElement('ul');
		this.itemWidth = this.items[0].getStyle('width').toInt()+this.items[0].getStyle('padding-right').toInt()+this.items[0].getStyle('padding-left').toInt();
		this.i = 0;
		this.maxStep = Math.ceil((this.items.length/this.options.step)/this.options.elemsPerView);
		if (this.maxStep>0){
			if (this.options.auto){
				this.start();
				this.element.addEvents({'mouseenter':this.stop.bind(this),'mouseleave':this.start.bind(this)});
			}
			this.buttons[0].addEvent('click',this.scroll.bind(this,[0]));
			this.buttons[1].addEvent('click',this.scroll.bind(this,[1]));
		};
	},
	scroll : function (direction){			
		this.i = (this.i+(direction*2-1)+this.maxStep) % this.maxStep;
		this.list.tween('margin-left',-this.itemWidth * this.i * this.options.step);
		return direction;
	},
	start : function(){
		this.buttons[1].fireEvent('mouseenter');
		this.timer = this.scroll.bind(this,[1]).periodical(this.options.delay);
	},
	stop : function(){
		this.buttons[1].fireEvent('mouseleave');
		this.timer = $clear(this.timer);
	}
});

/* Plan d'accès */
var PlanAcces = {
	id : null,
	latitude : null,
	longitude : null,
	adresse : null,
	init : function(_id,_latitude,_longitude){
		id = _id;
		latitude = _latitude;
		longitude = _longitude;
		// Créé l'icône
		var baseIcon = new GIcon();
		baseIcon.image = "/images/logo_ircf_googlemap.gif";
		//baseIcon.shadow = "";
		baseIcon.iconSize = new GSize(57, 40);
		//baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(28, 40);
		baseIcon.infoWindowAnchor = new GPoint(28, 40);
		baseIcon.infoShadowAnchor = new GPoint(28, 40);
		// Récupère le contenu du div
		var div = document.getElementById(id);
		adresse = document.createElement("P");
		adresse.innerHTML = div.innerHTML;
		// Créé la carte
		var point = new GLatLng(latitude, longitude);
		var map = new GMap2(div);
		map.addControl(new GSmallMapControl());
		map.setCenter(point, 12);
		// Crée le marker
	    var marker = new GMarker(point, new GIcon(baseIcon));
	    GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(adresse.innerHTML);
	    });
		// Insère le marker sur la carte
	    map.addOverlay(marker);
		//map.openInfoWindow(point,adresse);
		window.onunload = GUnload;
	}
}

/* Initialisation */

window.addEvent("domready",function(){
	// Formulaire de contact
	document.contactFormFx = new Fx.Slide($('form_contact'), {
	    duration: 1000,
	    transition: Fx.Transitions.Pow.easeOut
	}).hide();
	document.id('form_contact').setStyle('display','block');
	$$('#fermerForm,#contact,#lienContact,#contactbas').each(function(e,i){
		e.addEvent("click",function(event){
			document.contactFormFx.toggle();
		});
	});
	// Captchas
	$$('.captcha').each(function(c){
		new Captcha(c,{error:'Test antispam incorrect !',action:'http://pro.ircf.fr/communaute/mail.php'});
	});
	// Galerie
	var masque = document.id('masque');
	var accueil = document.id('accueil');
	var showcase = document.id('showcase');
	if (masque){
		if (accueil) new Gallery(masque,{buttons:'.fleche'});
		if (showcase) new Gallery(masque,{buttons:'.fleche',elemsPerView:4});
	}
	// Menu
	$$("#menu>li>a").each(function(e,i){
		var myFx = new Fx.Tween(e,{link:"chain",duration:"200"});
		e.addEvents({
			'mouseenter' : function(){
				var pos = e.getStyle('background-position');
				myFx.start('background-position', pos, '0 0');
			},
			'mouseleave' : function(){
				var pos = e.getStyle('background-position');
				myFx.start('background-position', pos, '0 70px');
			}
		});
	});
	// Menu ProActiv
	document.id('lien_sites_internet').addEvents({
		"mouseenter" : function(e,i){
			document.id('menu_sites_internet').setStyle("display","block");
		},
		"mouseleave" : function(e,i){
			document.id('menu_sites_internet').setStyle("display","none");
		}
	});
	// Formulaires
	document.id("voile").setStyle("height",document.getSize().x);
	document.id("voile").addEvent("click",function(event){
		document.id("voile").setStyle("display","none");
		$$(".formulaire").each(function(e,i){
			if($chk($(e))) e.setStyle("display","none");
		});
	});
	var afficheur_form = ["pro","mail","go_form_proactiv","go_form_proactivplus","go_form_proactivcommerce"];
	afficheur_form.each(function(e,i){
		if(document.id(e)) document.id(e).addEvent("click", function(event){
			document.id("voile").setStyle("display","block");
			$$(document.id(e).getProperty('href')).setStyle("display","block");
			$$(document.id(e).getProperty('href')).each(function(fm){
				fm.getElement("input[type=reset]").addEvent("click",function(){
					$('voile').fireEvent("click");
				});
			});
			new Fx.Scroll($(document.body)).start(0, 0);
			return false;
		});
	});
	// Afficher le formulaire correspondant à l'ancre dans l'URL
	if (location.hash.length>0){
		var f= document.id(document.body).getElement(".formulaire"+location.hash);
		if (f){
			f.setStyle("display","block");
			new Fx.Scroll(document.id(document.body)).start(0, 0);
		}
	}
	// Faire clignoter le lien menant au showcase
	if(document.id("pub_showcase")){
		var clignotement = new Fx.Tween("pub_showcase", {property: 'opacity'});
		(function(){
			clignotement.start(1,0.1).chain(
				function(){ this.start(0,1); },
			    function(){ this.start(1,0); },
			    function(){ this.start(0,1); }
			);
		}).periodical(10000);
	}
});

