/**
 * Script client 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;
			//alert(nm);
    		if (val.options){
    			if (val.options[val.selectedIndex].value=="")
    				errors+='- Vous devez choisir une valeur dans la boite de sélection '+nm+'.\n';
    		}else{
				if ((val=val.value)!="") {
					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 == '');
}

/* Menu V8 */
var Menu = new Class({
	Implements : Options,
	options : {
		showMinLevel : 0,
		hideDelay : 300,
		morph : {
			showStyles : {opacity:1},
			hideStyles : {opacity:0},
			options : {link:'cancel',duration:200}
		},
		toggleOnClick : false
	},
	initialize: function(menu,options){
		this.setOptions(options);
		this.menu = document.id(menu);
		if (this.menu){
			this.items = document.id(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 if (this.options.toggleOnClick){
					item.addEvent('click',this.toggleItem.bind(this,item));
				}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){
		if (item.link){
			item.link.removeClass('hover');
			if (item.submenu){
				item.submenu.morph.start(this.options.morph.hideStyles).chain(function(){item.submenu.setStyle('display','none');}.bind(this));
			}
		}
	},
	toggleItem : function(item){
		// TODO Fix
		//if (item.hasClass('hover')){
		//	this.hideItemNow(item);
		//}else{
			this.showItem(item);
		//}
	}
});

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

/* Follow V2 */
var Follow = new Class({
	Implements : Options,
	options : {
		container : 'content',
		debug : false,
		delta : 0// TODO Permet de décaler l'élément verticalement (corriger le programme pour éviter ça) 
	},
	initialize : function(element,options){
		this.element = $(element);
		this.setOptions(options);
		this.marginOffset = this.element.getStyle('margin-top').toInt();
		this.topOffset = this.element.getPosition().y;
		this.bottomOffset = $(this.options.container).getStyle('height').toInt();
		this.screenHeight = window.getSize().y;
		window.addEvent('scroll',this.follow.bind(this,true));
		if (this.options.debug)	this.debug = new Element('div',{styles:{position:'fixed',top:0,left:0,background:'#fff'}}).inject($(document.body));
	},
	follow : function(toTop){// toTop force à caler l'élément en haut de l'écran
		var elementHeight = this.element.getStyle('height').toInt();
		var screenTop = window.getScrollTop()-this.topOffset+this.options.delta;
		var screenBottom = screenTop+this.screenHeight;
		var elementTop = this.element.getStyle('margin-top').toInt()+this.element.getStyle('border-top-width').toInt();
		if (this.options.debug) this.debug.innerHTML = 'marginOffset='+this.marginOffset+', topOffset='+this.topOffset+', bottomOffset='+this.bottomOffset+', screenHeight='+this.screenHeight+', elementTop='+elementTop+', elementHeight='+elementHeight+',screenTop='+screenTop+',screenBottom='+screenBottom;
		if (toTop || elementHeight<this.screenHeight || elementTop>screenTop){
			this.element.tween('margin-top',Math.max(Math.min(screenTop,this.bottomOffset-elementHeight),0)+this.marginOffset);
		}else if (elementTop<screenBottom-elementHeight && elementTop<this.bottomOffset-elementHeight){
			this.element.tween('margin-top',Math.max(Math.min(screenBottom-elementHeight,this.bottomOffset-elementHeight),0)+this.marginOffset);
		}
	}
});

// Initialisation de la page
window.addEvent('domready',function(){
	// Menu
	new Menu('menu');
	// 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'
			}
		});
	});
	// Initialisation des liens sur le rubriques
	$$("div.rubrique").addEvents({
		mouseover : function(){this.addClass('hover');},
		mouseout : function(){this.removeClass('hover');},
		click : function(){location.href = this.getElement('a').href;}
	});
	// Affecte les lignes paires des tableaux riches
	$$("table.riche tr:nth-child(even)").addClass('pair');
	// Les éléments les plus importants sont pulsés
	$$('.important').each(function(important){
		new Pulse(important,{morph:true,from:{opacity:0.2},to:{opacity:1}});
	});
	// Follow sidebar
	var sidebar = document.id('sidebar');
	if (sidebar) new Follow(sidebar);
});

