// JavaScript Document
Event.observe(window, 'load', function() {
									   
	Event.observe(document.onresize ? document : window, "resize", resizePrincipale);			
						
	// On ressentre les vignette	
	resizePrincipale();
	
	$$('.vignette').each(function(element) {
								  														 
		var _delay = rand(0, 100)/100;
		var _duration = rand(0, 200)/100;
		
		// En cas de type defini
		if(typeof(type) != 'undefined') { 
		
			if(element.up(0).readAttribute('title').include(type) == false) {
				
				new Effect.Morph(element.up(0), { style: 'width: 0px;', duration: 0.1 });
				new Effect.Appear(element.up(0), { from: 1, to: 0, duration: 0.1 });
				
			}
		
		}
		
		
		// On gere le lien sur la vignette 'projet_over'
		$(element).next(0).observe('mouseover', function() { $(element).next(0).setStyle({ cursor: 'pointer' }); }).observe('click', function() { location.href = $(element).next(0).down(1).readAttribute('href'); });
			
		if(element.next(0).hasClassName('dejavu')) {			
			
			// On enlève l image
			new Effect.Appear(element.next(0), { from: 0, to: 1, duration: 1, delay: 0 });
			new Effect.Appear(element, { from: 0, to: 1, duration: 1, delay: 0 });	
						
		} else {
		  
			new Effect.Appear(element, { from: 0, to: 1, duration: _duration, delay: _delay, afterFinish:function() {
					if(element.up(0).hasClassName('projet_vignette')) {
						element.up(0).observe("mouseover", function() { $(element).next(0).show();  } ).observe("mouseout", function() { $(element).next(0).hide();  });
					} else {
						element.next(0).show();
					}
				}
			});
			
		}
					
	});
	
});

function changeAffichage(type) {
	
	var autre = (type == 'liste') ? 'vignette' : 'liste';
	
	$('par_'+type).addClassName('active');
	$('par_'+autre).removeClassName('active');
	
	new Ajax.Updater('liste_projet', 'include/ajax/affichageProjet.php', {
				   method: 'get',
				   parameters: 'type='+type,
				   asynchronous: false,
				   evalScripts: true,
				   onComplete:function() {
					   					   
					   // En cas de liste
					   if(type == 'liste') {
						   						   						   
						   $$('.lien_h3').each(function(element) {
							   Event.observe(element, "mouseover", function() { element.up(0).next(0).setStyle({ color: '#fff'}); } );
							   Event.observe(element, "mouseout", function() { element.up(0).next(0).setStyle({ color: '#966'}); });
						   });
						   
					   } else {
						   
						 $$('.projet').each(function(el) { 
						  el.down(0).setStyle({ display: '' });
						  if(!el.down(0).next(0).hasClassName('dejavu')) {			
							el.down(0).next(0).setStyle({ display: 'none' }); 
		  					Event.observe(el, "mouseover", function() { $(el).down(0).next(0).show();  } );
							Event.observe(el, "mouseout", function() { $(el).down(0).next(0).hide();  });
						  }

						});
								   
					   }
					   
					   // On cache
					   if($F('pages') != 'tous-les-projets') {
					    $$('.projet').each(function(el) {	
						 if(!el.readAttribute('title').include($F('pages'))) {
							el.setOpacity(0).setStyle({ display: '', height: '0px', width: '0px'});
							}
						});
					   }
					   
				   }
				}
	);
	

}

function resizePrincipale() {
		var projet = 420;
		var nbProjet = Math.floor(document.viewport.getWidth()/projet);
		var width = projet*nbProjet;
		var marge = parseInt((document.viewport.getWidth()-width)/2)-16;
		$('page').setStyle({ width: width+'px', margin: '0px 0px 0px '+marge+'px'});
	}
	
function listerType(type) {
	
	if(type == 'tous-les-projets') {
		
		$$('.projet').each(function(el) {
									
			// En cas de vignette
			if(el.hasClassName('projet_vignette')) {
									
				if(el.getStyle('width') == '0px') {							
					// On l'affiche
					el.setOpacity(0).setStyle({ display: ''});
					new Effect.Morph(el, { style: 'width: 420px; height: 264px;', duration: 1, afterFinish:function() {
						new Effect.Appear(el, { from: 0, to: 1, duration: 1 });
						}
					});
				}
			
			} else {
				
				// On verifie si le projet est affiché
				if(el.getStyle('height') == '0px') {
					// On l'affiche
					el.setOpacity(0).setStyle({ display: ''});
					new Effect.Morph(el, { style: 'height: 30px; width: 1000px;', duration: 1, afterFinish:function() {
						new Effect.Appear(el, { from: 0, to: 1, duration: 1 });
						}
					});
					
				}
				
			}
			
		});
		
	} else {
	
	 $$('.projet').each(function(el) {	
								 

		// On verifie que le projet en fait parti
		if(el.readAttribute('title').include(type)) {
			
			// En cas de vignette
			if(el.hasClassName('projet_vignette')) {

				// On verifie si le projet est affiché
				if(el.getStyle('width') == '0px') {
					// On l'affiche
					el.setOpacity(0).setStyle({ display: ''});
					new Effect.Morph(el, { style: 'width: 420px; height: 264px;', duration: 1, afterFinish:function() {
						new Effect.Appear(el, { from: 0, to: 1, duration: 1 });
						}
					});
					
				}
					
			} else {
				
				// On verifie si le projet est affiché
				if(el.getStyle('height') == '0px') {
					// On l'affiche
					el.setOpacity(0).setStyle({ display: ''});
					new Effect.Morph(el, { style: 'height: 30px; width: 1000px;', duration: 1, afterFinish:function() {
						new Effect.Appear(el, { from: 0, to: 1, duration: 1 });
						}
					});
					
				}
				
			}						
			
		} else {
			
			// En cas de vignette
			if(el.hasClassName('projet_vignette')) {
			
				// On verifie si le projet est affiché
				if(el.getStyle('width') != '0px') {
					// On le cache
					new Effect.Appear(el, { from: 1, to: 0, duration: 1, afterFinish:function() {
						new Effect.Morph(el, { style: 'width: 0px; height: 0px;', duration: 1, afterFinish:function() {
							el.hide();
							}
						});
						}
					});
					
				}
				
			} else {
				
				// On verifie si le projet est affiché
				if(el.getStyle('height') != '0px') {
					// On le cache
					new Effect.Appear(el, { from: 1, to: 0, duration: 1, afterFinish:function() {
						new Effect.Morph(el, { style: 'height: 0px; width: 0px;', duration: 1, afterFinish:function() {
							el.hide();
							}
						});
						}
					});
					
				}
				
			}
			
		}
		
	 });
	 
	}
	
}
