
/* Laurent Peter All rights reserved laurent at versateladsl dot be */ 

/* TABLES : alterne les couleurs de lignes */
/* usage: zebre("maclasse"), maclasse etant la classe de la table en question */
function zebra(myclass){				
	var tables=$$('table[class="' + myclass + '"]');
	for(var i=0;i<tables.length;i++){
		var rows=tables[i].select('tr');
		for(var j=1;j<rows.length;j++){
			if(j%2==0){
				rows[j].addClassName('even');
			}
			else{
				rows[j].addClassName('odd');
			}
		}
	}	
}
function zebre(mytable){
	Event.observe(window,'load', zebra.bind(this,mytable), false);
}	

/* FORMS : classe pour poster un formulaire (AJAX). postme.php requis */ 

var MailForm = Class.create({
	  initialize: function(required,theform,language) {
	  		this.language=(language == null) ? "fr" : language;
	    	this.required=required;
	    	this.form=$(theform);
	    	this.email="";
	    	this.error=new Array();
	    	Event.observe(this.form, 'submit', this.checkForm.bind(this));
	  },
	  checkForm: function(){
	  	var pass=0;
	  	this.error.length=0;
		for (var i=0;i<this.required.length; i++){
	        if ($F(this.required[i]).blank()){
	        	$(this.required[i]).previous().addClassName('required');
	        	pass=1;	
	        }
	        else{
	        	$(this.required[i]).previous().removeClassName('required');
	        }   
	    }
	    if (pass==1){
	    	switch (this.language) 
			{ 
			   case "nl" : 
			      this.error.push("U moet alle verplichte velden invullen, a.u.b."); 
			      break; 
			    case "uk" : 
			      this.error.push("Please, fill in all the reqired fields."); 
			      break; 
			   default:
			      this.error.push("Veuillez remplir tous les champs requis SVP");    
			}
	    	
	    }
	    
	    if (!$F(this.email).blank()){
	    	this.checkEmail();
	    }	
	    if (this.error.length==0){
	    	//alert("Le formulaire est pret pour etre envoye");
	    	new Ajax.Request('postme.php', {
			  method: 'post',
			  encoding:'ISO-8859-1',
			  parameters: this.form.serialize(true),
			  onSuccess: function(transport){
			  	this.form.up().innerHTML=transport.responseText;
			  }.bind(this)
			});
		}
		else{
			this.form.previous().innerHTML=this.error.join('<br />');
		}	
	   // return false;
	  },
	  addEmail: function(email){
	  		this.email=email;
	  		//alert($F('Email'));
	  },
	 checkEmail: function(){			  	
			  	var pattern=/((^[a-zA-Z0-9])|(^[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]))@[a-zA-Z0-9_-]+\.[a-zA-Z]{2,6}(\.[a-zA-Z][a-zA-Z])?[a-zA-Z]{0,1}$/;
			  	if (!$F(this.email).match(pattern)){
			  		switch (this.language) 
					{ 
					   case "nl" : 
					      this.error.push("De email formaat is verkeerd."); 
					      break; 
					    case "uk" : 
					      this.error.push("The email format is wrong."); 
					      break; 
					   default:
					      this.error.push("Le format email n'est pas bon");;  
					}
			  		$(this.email).previous().addClassName('required');
			  	}
			  	else{
			  		$(this.email).previous().removeClassName('required');
			  	}
			  	
			  }
			});	 	 

/* Marquee : texte defilant de droite  gauche */
/* Attention, la div dans le marquee boord doit avoir la propriete : word-spacing:nowrap */

var marquee = Class.create({
							 initialize: function(board,speed) {
								 this.board=$(board);
								 var msgdivs=this.board.getElementsBySelector('div');
								 this.msg=msgdivs[0];
								 this.msg_width=this.msg.getWidth();
								 this.board_width=this.board.getWidth();
								 this.msg.style.left=this.board_width + "px";
								 this.duration=parseInt(this.msg_width/(speed*10));
							},
							scroll:function(){
								Effect.MoveBy(this.msg,0,-(this.msg_width + this.board_width), 
						  					{duration: this.duration,
						  					 transition: Effect.Transitions.linear, 
						  					afterFinish:this.reinitialize.bind(this)
						  					});
							},
							reinitialize: function(){
							 	 this.msg.style.left=this.board_width + "px";
							 	 this.scroll();
							 }
						});	
/* New scroll board */
var scrollboard = Class.create({
	 initialize: function(board,speed) {
		 this.board=$(board);
		 this.speed=speed;
		 var msgdivs=this.board.getElementsBySelector('div');
		 this.msg=msgdivs[0];
		 this.msg_height=parseInt(this.msg.getHeight());	 	 
		 this.board_height=this.board.getHeight();			
		 this.msg.style.top=this.board_height + "px";
		 this.duration=parseInt(this.msg_height/(speed*10));
		 Event.observe(this.board, 'mouseover', this.pause.bind(this),false);
		 Event.observe(this.board, 'mouseout', this.restart.bind(this),false);
	},
	scroll:function(){
  		if(parseInt(this.msg.getStyle('top')) > - this.msg_height){
	  		this.msg.style.top=(parseInt(this.msg.getStyle('top')) - this.speed) + "px";
	  		//alert(this.msg.style.top);
			setTimeout(this.scroll.bind(this),100);
		}
		else{
			this.reinitialize();
		}				
	},
	reinitialize: function(){
	 	 this.msg.style.top=this.board_height + "px";
	 	 this.scroll();
	},
	pause: function(){
		this.initspeed=this.speed;
		this.speed=0;
	},
	restart: function(){
		this.speed=this.initspeed;
	}
});
/* Menu Horizontal type "drop-down" */
/* usage : var exp=new inline_menu('topmenu'); topmenu tant la div cantenant le menu */	  
var inline_menu = Class.create({
			  initialize: function(topdiv) {
			    	this.topdiv = $(topdiv);			    
			    	this.active=-1;
			    	this.menu=this.topdiv.firstDescendant();
			    	this.timer=-1;
			    	this.dts= $$('#' + topdiv + ' dt');
			    	//alert(this.dts);
					this.dts.each(function(dt) {
						if (dt.next(0)){
							dt.next(0).hide();
							Event.observe(dt, 'mouseover', this.show_sub.bind(this,dt),false);
							Event.observe(dt, 'mouseout', this.fade_active.bind(this),false);
							
							Event.observe(dt.next(0), 'mouseover',this.keep_visible.bind(this),false);
							Event.observe(dt.next(0), 'mouseout',this.fade_active.bind(this),false);
						}
					}.bind(this));
					
					
			  },
			  show_sub:function(dt){
				clearTimeout(this.timer);	
				if (this.active==dt){
					
				//return false;
				}
				else{						
					//dt.next(0).show();
					
					Effect.Appear(dt.next(0), { duration: 0.5});

					dt.addClassName('current');
					if (this.active!=-1){
						this.active.removeClassName('current');
						Effect.Fade(this.active.next(0), { duration: 0.5 });	
					}	
					this.active=dt;
					
				}
					
			  },
			  fade_active:function(){
			  /*	Effect.Fade(this.active.next(0), { duration: 1.0,
			  									   afterFinish: function() {this.active=-1;}
			  									 });	
			  	*/
			  	this.timer=setTimeout(this.fade_and_clean.bind(this),1500);								 

			  },
			  fade_and_clean:function(){
			  		this.active.removeClassName('current');
			  		//this.active.next(0).hide();
			  		Effect.Fade(this.active.next(0), {duration: 0.5});
			  		this.active=-1;
			  },
			  keep_visible:function(){
			  	if(this.timer!=1)
			  		clearTimeout(this.timer);
			  }		
		});								

/* Slide show with next pic sliding over current pic */

var slide_stack = Class.create({
			  initialize: function(images,sid,delay) {
			    	this.slideshow=$(sid);
			    	this.frames= this.slideshow.getElementsBySelector('div');
			    	this.width=this.slideshow.getWidth();
			    	this.frames[1].style.left=this.width + "px";
			    	this.frames[0].style.zIndex=0;
			    	this.frames[1].style.zIndex=1;
			    	this.currentframe = 0;
			    	this.nextframe = 1;
			    	this.focuspic = 0;
			    	this.delay=delay;
			    	this.images=images;
			    	this.nbimages=this.images.length;
			    	this.currentpic=this.frames[0].childElements();
				this.currentpic[0].src=this.images[0]; /* img tag */
				nextpic=this.frames[1].childElements();
				nextpic[0].src=this.images[1];
			    	this.next_to_see=1; // num de la prochaine image  afficher

			    	this.rotate.bind(this).delay(this.delay);  /* !!!! Bind avant delay sinon a ne marche pas !!! */
			  },			  
			  rotate: function(){
				 Effect.MoveBy(this.frames[this.nextframe], 0, -(this.width) , 
	  					{duration: 1.0,
	  					 afterFinish:this.reinitialize.bind(this)
	  					});	
			  	//bind(this) pour rester dans le scope local
			  },
			  reinitialize: function(){
			  	this.next_to_see=++this.next_to_see % this.nbimages;
			  	this.frames[this.currentframe].style.left=this.width + "px";
			  	var nextslide=this.frames[this.currentframe].childElements();
			  	nextslide[0].src=this.images[this.next_to_see];
			  	this.currentframe=Math.abs(this.currentframe-1);
			  	this.nextframe=Math.abs(this.nextframe-1);
			  	this.frames[this.nextframe].style.zIndex=1;//Math.abs(this.frames[0].style.zindex -1);
			  	this.frames[this.currentframe].style.zIndex=0;//Math.abs(this.frames[1].style.zindex -1);
			  	
			  	this.rotate.bind(this).delay(this.delay);
			  }
			  });
			