var Page = {
	/* Slide Collection Sur mesure */
	surMesure: function(){
		$('.product').each(function(i){
			$(this).find('.prev').click(function(){
				$('.product').hide();
				$('#product_'+(i-1)).fadeIn('slow');
			});
			$(this).find('.next').click(function(){
				$('.product').hide();
				$('#product_'+(i+1)).fadeIn('slow');
			});
		});
	},
	
	/* Navigation dans les collections en javascript et non en html */
	navigation: function(){
		/*$('#navigation').find('.prev').click(function(){
			if(_currentproduct > 1){
				_currentproduct = _currentproduct-1;
				$('#visuel').find('img').animate({opacity: 0},1000, function(){
					$(this).attr('src', _products[_currentproduct]['image'])
							.bind('load', function(){
								$(this).animate({opacity: 1},1000, function(){
									$(this).attr('alt', _products[_currentproduct]['name']);
									
									$('#legendeTexte .nom').html( _products[_currentproduct]['name'] );
									$('#legendeTexte .titre').html( _products[_currentproduct]['libelle'] );
									$('#legendeTexte .page').html( (_currentproduct<10)? '0'+_currentproduct : _currentproduct);
									_link = $('#legendeTexte #linkToSendToFriend').attr('href');
									_reg = new RegExp("sendtofriend="+_products[_currentproduct+1]['id'], 'gi');
									$('#legendeTexte #linkToSendToFriend').attr('href', _link.replace(_reg, "sendtofriend="+_products[_currentproduct]['id']));
									$('#legendeTexte #zoom').attr('href', _products[_currentproduct]['zoom']);
									$('#legendeTexte #print_ie6').attr('href', _products[_currentproduct]['pdf']);
									
									Page.cleanSendToFriend();
									
									$('#bigVisuel').find('img').attr('alt', _products[_currentproduct]['name'])
																.attr('src', _products[_currentproduct]['zoom']);
									
									$('#navigationRight').fadeIn();
									if(_currentproduct == 1){
										$('#navigationLeft').fadeOut();
									}else{
										$('#navigationLeft').fadeIn();									
									}
								})
							})
				});
			}
		});
		$('#navigation').find('.next').click(function(){
			if(_currentproduct <= _total){
				_currentproduct = _currentproduct+1;
				$('#visuel').find('img').animate({opacity: 0},1000, function(){
					$(this).attr('src', _products[_currentproduct]['image'])
							.bind('load', function(){
								$(this).animate({opacity: 1},1000, function(){
									$(this).attr('alt', _products[_currentproduct]['name']);
									
									$('#legendeTexte .nom').html( _products[_currentproduct]['name'] );
									$('#legendeTexte .titre').html( _products[_currentproduct]['libelle'] );
									$('#legendeTexte .page').html( (_currentproduct<10)? '0'+_currentproduct : _currentproduct);
									_link = $('#legendeTexte #linkToSendToFriend').attr('href');
									_reg = new RegExp("sendtofriend="+_products[_currentproduct-1]['id'], 'gi');
									$('#legendeTexte #linkToSendToFriend').attr('href', _link.replace(_reg, "sendtofriend="+_products[_currentproduct]['id']));
									$('#legendeTexte #zoom').attr('href', _products[_currentproduct]['zoom']);
									$('#legendeTexte #print_ie6').attr('href', _products[_currentproduct]['pdf']);
									
									Page.cleanSendToFriend();
									
									$('#bigVisuel').find('img').attr('alt', _products[_currentproduct]['name'])
																.attr('src', _products[_currentproduct]['zoom']);
									
									$('#navigationLeft').fadeIn();									
									if(_currentproduct == _total){
										$('#navigationRight').fadeOut();
									}else{
										$('#navigationRight').fadeIn();
									}
								})
							})
				});
			}
		});*/
	},

	// Affiche le formulaire d'envoie à un ami
	sendToFriend: function(){
		$('#linkToSendToFriend').click(function(){
			$('#sendToFriend').before('<div id="fadeBackground"></div>');
			$('#sendToFriend').fadeIn('slow');
		});
		$('.closed').click(function(){
			$('#fadeBackground').remove();
			$('#sendToFriend').fadeOut().removeClass('success');
			Page.cleanSendToFriend();
		});
	},
	
	// Nettoye le formulaire d'envoie a un ami
	cleanSendToFriend: function(){
		$('#sendToFriend #inputSendToFriend').attr('value', _products[_currentproduct]['id']);
		$('#sendToFriend #name').attr('value', '').removeClass('error');
		$('#sendToFriend #email').attr('value', '').removeClass('error');
		$('#sendToFriend #friend_name').attr('value', '').removeClass('error');
		$('#sendToFriend #friend_email').attr('value', '').removeClass('error');
		$('#sendToFriend #message').attr('value', '');
		$('#sendToFriend #error').hide();
		$('#sendToFriend #success').hide('');
	},
	
	// Affiche le zoom de l'image
	zoom: function(){
		$('#zoom').click(function(){
			$('#bigVisuel').fadeIn('slow');
		});
		$('.closed').click(function(){
			$('#bigVisuel').fadeOut();
		});
	},
	
	// Alignement au centre d'un texte dynamique
	vAlign: function(){
		$('.verticalAlign').each(function(){
			var parentHeight = $(this).height();
			var childHeight = $(this).find('.blockToAlign').height();
			$(this).find('.blockToAlign').css('padding-top', ((parentHeight-childHeight)/2) +'px');
		});
	},
	
	// La maison - Histoire
	history: function(){
		var contentHeight = $('#content').height();
		$('.txt').each(function(){
			$(this).css('padding-top', ((contentHeight - $(this).height())/2) +'px');
		});
		$('.histoire').each(function(){
			$(this).click(function(){
				if($(this).find('.txt').css('display') == 'none'){
					$('.txt').hide();
					$('.date').removeClass("selected");
					$(this).find('.txt').fadeIn('slow');
					$(this).find('.date').addClass("selected");
				}
			});
		});
	},
	
	// La maison - Savoir faire
	savoirFaire: function(){
		$('.savoir').each(function(){
			$(this).click(function(){
				if($(this).find('p').css('display') == 'none'){
					$('p').hide();
					$('.visuels').hide();
					$('h2').removeClass("selected");
					$(this).find('p').fadeIn('slow');
					$(this).find('h2').addClass("selected");
					$(this).find('.visuels').show();
				}
			});
		});
	},
	
	// SlideShow automatique dans La maison - Savoir faire
	slideSwitch: function(){
		$('.visuels').each(function(){
			if($(this).css('display') != 'none'){
				var _active = $(this).find('img.active');
				if ( _active.length == 0 ) _active = $(this).find('img:last');
				var $next =  _active.next().length ? _active.next() : $(this).find('img:first');
				_active.addClass('last-active');
				$next.css({opacity: 0.0})
					.addClass('active')
					.animate({opacity: 1.0}, 1000, function() {
						_active.removeClass('active last-active');
					});
			}
		});
	},
	
	// Lancement du slideShow
	runSlideSwitch: function(){
    	// Indicateur de chargement sur image portant la classe 'loading'; 
        // Jay Salvat - http://blog.jaysalvat.com
	    $(".visuel").each(function() { 
	        var img = $(this); 
	        var src = img.attr("src"); 
	        img.attr("src", "_img/nolang/black.gif"); 
	        $("<img>").bind("load", function() { 
	            img.fadeOut("slow", function() { 
	                img.attr("src", src) 
	                   .fadeIn(); 
	            }); 
	        }).attr("src", src); 
	    }); 
		setInterval("Page.slideSwitch()", 5000 );
	}
	
};

$(document).ready(function() {
	Page.vAlign();
	Page.surMesure();
	Page.navigation();
	Page.zoom();
	Page.sendToFriend();
	Page.history();
	Page.savoirFaire();
	Page.runSlideSwitch();
});