/**
 * Fichier commun.js
 * Ce fichier contient les fonctions javascript client
 */

// Fonctions Standard Macromedia Dreamweaver
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
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 == '');
}

/* Menu V4 */
var Menu = new Class({
	Implements : Options,
	options : {
		showMinLevel : 0,
		hideDelay : 300,
		morph : {
			showStyles : {opacity:0.95},
			hideStyles : {opacity:0},
			options : {link:'cancel',duration:200}
		}
	},
	initialize: function(menu,options){
		this.setOptions(options);
		this.items = $(menu).getChildren('li');
		this.items.each(function(item,i){
			item.i = i;
			item.link = item.getElement('a');
			item.link.addEvent('focus',this.showItem.bind(this,[item]));
			item.submenu = item.getElement('ul');
			if (this.options.showMinLevel>0){
				if (item.submenu) item.submenu.setStyle('display','block');
			}else{
				item.addEvents({mouseenter:this.showItem.bind(this,[item]),mouseleave:this.hideItem.bind(this,[item])});
			}
			if (item.submenu){
				item.submenu.morph = new Fx.Morph(item.submenu,this.options.morph.options);
				var options = $merge(this.options);
				options.showMinLevel = options.showMinLevel-1;
				new Menu(item.submenu,options);
			}
		}.bind(this));
	},
	showItem : function(item){
		this.timer = $clear(this.timer);
		item.link.addClass('hover');
		if (item.submenu){
			item.submenu.morph.start(this.options.morph.showStyles).chain(function(){item.submenu.setStyle('display','block');}.bind(this));
		}
		this.items.each(function(otherItem,j){
			if (item.i!=j) this.hideItemNow(otherItem);
		}.bind(this));
	},
	hideItem : function(item){
		this.timer = $clear(this.timer);
		this.timer = this.hideItemNow.bind(this,[item]).delay(this.options.hideDelay);
	},
	hideItemNow : function(item){
		item.link.removeClass('hover');
		if (item.submenu){
			item.submenu.morph.start(this.options.morph.hideStyles).chain(function(){item.submenu.setStyle('display','none');}.bind(this));
		}
	}
});

// Marquee V1
var Marquee = new Class({
	Implements : Options,
	options : {
		isVertical : false,
		speed : 1,
		pause : true,
		delay : 30
	},
	initialize : function(element,options){
		this.element = $(element);
		this.setOptions(options);
		this.content = this.element.getFirst();
		this.element.size = this.element.getSize();
		this.content.size = this.content.getSize();
		if (this.options.isVertical){
			this.content.setStyle('top',0);
		}else{
			this.content.setStyle('left',0);
		};
		if (this.options.pause){
			this.element.addEvents({
				mouseenter : this.stop.bind(this),
				mouseleave : this.start.bind(this)
			});
		}
		this.start();
	},
	start : function (){
		this.thread = this.scroll.periodical(this.options.delay,this);
	},
	stop : function (){
		$clear(this.thread);
	},
	scroll : function (){
		if (this.options.isVertical){
			if (this.content.getStyle('top').toInt()>-this.content.size.y)
				this.content.setStyle('top',this.content.getStyle('top').toInt()-this.options.speed);
			else
				this.content.setStyle('top',this.element.size.y);
		}else{
			if (this.content.getStyle('left').toInt()>-this.content.size.x)
				this.content.setStyle('left',this.content.getStyle('left').toInt()-this.options.speed);
			else
				this.content.setStyle('left',this.element.size.x);
		}
	}
});

/* Gallery V1 */
var Gallery = new Class({
	Implements : Options,
	options : {
		buttons : '.button',
		auto : true,
		step : 3,
		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');
		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);
		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);
	}
});

// Méthode pour calculer l'intersection de deux tableaux (utilisé pour le trombinoscope)
Array.implement({
	intersect: function(other) {
		var cpy = this.slice();
		this.each(function(el) {
			if (other.indexOf(el) < 0) {			
				cpy.splice(cpy.indexOf(el), 1);
			}
		}, this);
		return cpy;
	}
});

// Initialisation de la page
window.addEvent('domready',function(){
	// Menu
	var menu = $('menu');
	if (menu) new Menu(menu);
	// Marquees
	$$('.marquee').each(function(m){
		new Marquee(m);
	});
	// Animations Flash
	$$('.flash').each(function(flash){
		new Swiff('/swf/'+flash.id+'.swf',{
			container:flash,
			width:flash.getStyle('width'),
			height:flash.getStyle('height'),
			params : {
				menu:false,
				wMode:'transparent'
			}
		});
	});
	// Gallery
	$$('.gallery').each(function(gallery){
		new Gallery(gallery,{step:5});
	});
	// Cartes et trombinoscopes
	var areas = $$('.carte area');
	var trombinoscopes = $$('.trombinoscope');
	if (areas.length>0){
		areas.each(function(area){
			area.classes = area.get('class').split(' ');
			area.trombinoscopes = trombinoscopes.filter(function(trombinoscope){
				trombinoscope.rels = trombinoscope.get('rel').split(' ');
				return (area.classes.length>0 && trombinoscope.rels.length>0 && trombinoscope.rels.intersect(area.classes).length>0);
			});
			area.addEvent('mouseover',function(){
				trombinoscopes.setStyle('display','none');
				area.trombinoscopes.setStyle('display','block');
			});
		});
	}
});

