$(document).ready(function()	{
	initMainMenu();
	initDiaporama();
	initAccueilConseil();
	initProduitDetailGallerie();
	initListeProduitSimple();
	initListeProduitBig();
	initDiagnosticGallerie();
	
	
	$('.liste-avis').jScrollPane({
		verticalDragMinHeight: 10,
		showArrows: true
	});
	
	$('#liste_telechargement').jScrollPane({
		verticalDragMinHeight: 10,
		showArrows: true
	});
	
	$('#annexe_50ans .col_l').jScrollPane({
		verticalDragMinHeight: 10,
		showArrows: true
	});
	
	$('#liste_question').jScrollPane({
		verticalDragMinHeight: 10,
		showArrows: true
	});
	
	$('#annexe_eco .texte').jScrollPane({
		verticalDragMinHeight: 10,
		showArrows: true
	});
	
	$('#inscription_newsletter').submit(function(){
		var email = $('#newsletters_mail').val();
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(email)) {
			showPopin('Addresse email incorrect');
		}else{
			$.post(baseUrl+"sources/newsletter.php",{inscription:1, email:email},function(data){
				if(data.error != ""){
					showPopin(data.error);
				}else{
					showPopin(data.message);
					$('#newsletters_mail').val('votre e-mail ici');
				}
				
			},'json');
		}
		return false;
	});

	$('#newsletters_mail').focus(function(){
		if($(this).val() == "votre e-mail ici")
			$(this).val("");
	});
	
	$('#footer .lien-annexe').unbind('click').click(function(){
		var id = $(this).attr('id').replace('annexe-','');
		$.post(baseUrl+"sources/getAnnexeContent.php",{id_annexe:id},function(data){
			showPopin(data);
		});
	});
	
});

function closeBox(){
	$(document).trigger('close.facebox');
}

function showPopin(data){
	$.facebox(data);
	//$('#facebox .close').hide();
}

function initAccueilConseil(){
	$('.categorie .liste').jScrollPane({
		verticalDragMinHeight: 10,
		showArrows: true
	});
	
	var maxLi = $('.accueil_conseils .saviez_vous .encadre .content div').length-1;
	var currentLi = 0
	currentLi = 0;

	$('.accueil_conseils .saviez_vous .encadre .bouton_next').unbind('click').click(function(){
		$('#saviez-'+currentLi).hide();
		
		currentLi = currentLi+1;
		if(currentLi>maxLi)
			currentLi = 0;

		$('#saviez-'+currentLi).show();
	});
	
	$('.accueil_conseils .saviez_vous .encadre .bouton_previous').unbind('click').click(function(){
		$('#saviez-'+currentLi).hide();
		
		currentLi = currentLi-1;
		if(currentLi<0)
			currentLi = maxLi;

		$('#saviez-'+currentLi).show();
	});

}

var maxPageListe = new Array();
var currentPageListe = new Array();

function initListeProduitSimple(){
	
	$('.liste_produit_simple').each(function(){
		if(typeof pngfix == 'function')
			pngfix($(this).find('.content .image a img, .bouton_previous img, .bouton_next img'));
		id = $(this).attr('id');
		id = id.replace('liste-','');
		nbProduit = $(this).find('.content ul li').length;
		maxPageListe[id] = parseInt(Math.ceil(nbProduit/3))-1;
		currentPageListe[id] = parseInt(0);
		if(maxPageListe[id] == 0){
			$(this).find('.bouton_next span').hide();
		}
	});
	
	$('.liste_produit_simple .bouton_next span').unbind('click').click(function(){
		container = $(this).parents('.liste_produit_simple');
		ul = container.find('.content ul');
		
		id = container.attr('id');
		id = id.replace('liste-','');
		
		currentPageListe[id] = currentPageListe[id]+1;

		
		
		if(currentPageListe[id]>maxPageListe[id])
			currentPageListe[id] = maxPageListe[id];
			
		newPos = currentPageListe[id]*621;
		
		
		
		ul.animate( {'left': -newPos}, 'slow', function(){	
			container.find('.bouton_previous span').show();
			if(currentPageListe[id] == maxPageListe[id]){
				container.find('.bouton_next span').hide();
			}
		});
	});
	
	$('.liste_produit_simple .bouton_previous span').unbind('click').click(function(){
		container = $(this).parents('.liste_produit_simple');
		ul = container.find('.content ul');
		
		id = container.attr('id');
		id = id.replace('liste-','');
		
		currentPageListe[id] = currentPageListe[id]-1;
		
		if(currentPageListe[id]<0)
			currentPageListe[id] = 0;
			
		newPos = currentPageListe[id]*621;
		
		ul.animate( {'left': -newPos}, 'slow', function(){	
			container.find('.bouton_next span').show();
			if(currentPageListe[id] == 0){
				container.find('.bouton_previous span').hide();
			}
		});
	});
}

function initListeProduitBig(){
	$('.liste_produit_big').each(function(){
		id = $(this).attr('id');
		id = id.replace('liste-','');
		nbProduit = $(this).find('.content ul li').length;
		maxPageListe[id] = parseInt(Math.ceil(nbProduit/3))-1;
		currentPageListe[id] = 0;
		
		if(maxPageListe[id] == 0){
			$(this).find('.bouton_next span').hide();
		}
	});
	
	$('.liste_produit_big .bouton_next span').unbind('click').click(function(){
		container = $(this).parents('.liste_produit_big');
		ul = container.find('.content ul');
		
		id = container.attr('id');
		id = id.replace('liste-','');
		
		currentPageListe[id] = currentPageListe[id]+1;
		
		if(currentPageListe[id]>maxPageListe[id])
			currentPageListe[id] = maxPageListe[id];
		
		
		newPos = currentPageListe[id]*621;
		
		ul.animate( {'left': -newPos}, 'slow', function(){	
			container.find('.bouton_previous span').show();
			if(currentPageListe[id] == maxPageListe[id]){
				container.find('.bouton_next span').hide();
			}
		});
	});
	
	$('.liste_produit_big .bouton_previous span').unbind('click').click(function(){
		container = $(this).parents('.liste_produit_big');
		ul = container.find('.content ul');
		
		id = container.attr('id');
		id = id.replace('liste-','');
		
		currentPageListe[id] = currentPageListe[id]-1;
		
		if(currentPageListe[id]<0)
			currentPageListe[id] = 0;
			
		newPos = currentPageListe[id]*621;
		ul.animate( {'left': -newPos}, 'slow', function(){	
			container.find('.bouton_next span').show();
			if(currentPageListe[id] == 0){
				container.find('.bouton_previous span').hide();
			}
		});
	});
}

function initProduitDetailGallerie(){

	maxPageListe["detail-"+0] = $('.galerie_produit .content ul li').length-1;
	currentPageListe["detail-"+0] = 0;
	
	if(maxPageListe["detail-"+0] == 0){
		$('.galerie_produit .bouton_next span').hide();
	}
	
	maxPageListe["detail-"+1] = $('.galerie_produit_mini .content ul li').length-1;
	currentPageListe["detail-"+1] = 0;
	
	if(maxPageListe["detail-"+1] == 0){
		$('.galerie_produit_mini .bouton_next span').hide();
	}

	$('.galerie_produit .bouton_next span').unbind('click').click(function(){
		container = $(this).parents('.galerie_produit');
		ul = container.find('.content ul');

		currentPageListe["detail-"+0] = currentPageListe["detail-"+0]+1;

		if(currentPageListe["detail-"+0]>maxPageListe["detail-"+0])
			currentPageListe["detail-"+0] = maxPageListe["detail-"+0];
			
		newPos = currentPageListe["detail-"+0]*284;

		ul.animate( {'left': -newPos}, 'slow', function(){	
			container.find('.bouton_previous span').show();
			if(currentPageListe["detail-"+0] == maxPageListe["detail-"+0]){
				container.find('.bouton_next span').hide();
			}
		});
	});
	
	$('.galerie_produit .bouton_previous span').unbind('click').click(function(){
		container = $(this).parents('.galerie_produit');
		ul = container.find('.content ul');
		
		currentPageListe["detail-"+0] = currentPageListe["detail-"+0]-1;
		
		if(currentPageListe["detail-"+0]<0)
			currentPageListe["detail-"+0] = 0;
		
		newPos = currentPageListe["detail-"+0]*284;
		
		ul.animate( {'left': -newPos}, 'slow', function(){	
			container.find('.bouton_next span').show();
			if(currentPageListe["detail-"+0] == 0){
				container.find('.bouton_previous span').hide();
			}
		});
	});
	
	$('.galerie_produit_mini .bouton_next span').unbind('click').click(function(){
		container = $(this).parents('.galerie_produit_mini');
		ul = container.find('.content ul');

		currentPageListe["detail-"+1] = currentPageListe["detail-"+1]+1;

		if(currentPageListe["detail-"+1]>maxPageListe["detail-"+1])
			currentPageListe["detail-"+1] = maxPageListe["detail-"+1];
			
		newPos = currentPageListe["detail-"+1]*282;

		ul.animate( {'left': -newPos}, 'slow', function(){	
			container.find('.bouton_previous span').show();
			if(currentPageListe["detail-"+1] == maxPageListe["detail-"+1]){
				container.find('.bouton_next span').hide();
			}
		});
	});
	
	$('.galerie_produit_mini .bouton_previous span').unbind('click').click(function(){
		container = $(this).parents('.galerie_produit_mini');
		ul = container.find('.content ul');
		
		currentPageListe["detail-"+1] = currentPageListe["detail-"+1]-1;
		
		if(currentPageListe["detail-"+1]<0)
			currentPageListe["detail-"+1] = 0;
		
		newPos = currentPageListe["detail-"+1]*282;
		
		ul.animate( {'left': -newPos}, 'slow', function(){	
			container.find('.bouton_next span').show();
			if(currentPageListe["detail-"+1] == 0){
				container.find('.bouton_previous span').hide();
			}
		});
	});
}

function initDiagnosticGallerie(){

	maxPageListe["diagnostic-"+0] = $('#age-1 .liste_produit_diagnostic .content ul li').length-1;
	currentPageListe["diagnostic-"+0] = 0;
	
	if(maxPageListe["diagnostic-"+0] == 0){
		$('#age-1 .liste_produit_diagnostic .bouton_next span').hide();
	}
	
	maxPageListe["diagnostic-"+1] = $('#age-1 .liste_produit_diagnostic .content ul li').length-1;
	currentPageListe["diagnostic-"+1] = 0;
	
	if(maxPageListe["diagnostic-"+1] == 0){
		$('#age-1 .liste_produit_diagnostic .bouton_next span').hide();
	}

	$('#age-1 .liste_produit_diagnostic .bouton_next span').unbind('click').click(function(){
		container = $(this).parents('.liste_produit_diagnostic');
		ul = container.find('.content ul');

		currentPageListe["diagnostic-"+0] = currentPageListe["diagnostic-"+0]+1;

		if(currentPageListe["diagnostic-"+0]>maxPageListe["diagnostic-"+0])
			currentPageListe["diagnostic-"+0] = maxPageListe["diagnostic-"+0];
			
		newPos = currentPageListe["diagnostic-"+0]*284;

		ul.animate( {'left': -newPos}, 'slow', function(){	
			container.find('.bouton_previous span').show();
			if(currentPageListe["diagnostic-"+0] == maxPageListe["diagnostic-"+0]){
				container.find('.bouton_next span').hide();
			}
		});
	});
	
	$('#age-1 .liste_produit_diagnostic .bouton_previous span').unbind('click').click(function(){
		container = $(this).parents('.liste_produit_diagnostic');
		ul = container.find('.content ul');
		
		currentPageListe["diagnostic-"+0] = currentPageListe["diagnostic-"+0]-1;
		
		if(currentPageListe["diagnostic-"+0]<0)
			currentPageListe["diagnostic-"+0] = 0;
		
		newPos = currentPageListe["diagnostic-"+0]*284;
		
		ul.animate( {'left': -newPos}, 'slow', function(){	
			container.find('.bouton_next span').show();
			if(currentPageListe["diagnostic-"+0] == 0){
				container.find('.bouton_previous span').hide();
			}
		});
	});
	
	$('#age-2 .liste_produit_diagnostic .bouton_next span').unbind('click').click(function(){
		container = $(this).parents('.liste_produit_diagnostic');
		ul = container.find('.content ul');

		currentPageListe["diagnostic-"+1] = currentPageListe["diagnostic-"+1]+1;

		if(currentPageListe["diagnostic-"+1]>maxPageListe["diagnostic-"+1])
			currentPageListe["diagnostic-"+1] = maxPageListe["diagnostic-"+1];
			
		newPos = currentPageListe["diagnostic-"+1]*284;

		ul.animate( {'left': -newPos}, 'slow', function(){	
			container.find('.bouton_previous span').show();
			if(currentPageListe["diagnostic-"+1] == maxPageListe["diagnostic-"+1]){
				container.find('.bouton_next span').hide();
			}
		});
	});
	
	$('#age-2 .liste_produit_diagnostic .bouton_previous span').unbind('click').click(function(){
		container = $(this).parents('.liste_produit_diagnostic');
		ul = container.find('.content ul');
		
		currentPageListe["diagnostic-"+1] = currentPageListe["diagnostic-"+1]-1;
		
		if(currentPageListe["diagnostic-"+1]<0)
			currentPageListe["diagnostic-"+1] = 0;
		
		newPos = currentPageListe["diagnostic-"+1]*284;
		
		ul.animate( {'left': -newPos}, 'slow', function(){	
			container.find('.bouton_next span').show();
			if(currentPageListe["diagnostic-"+1] == 0){
				container.find('.bouton_previous span').hide();
			}
		});
	});
}
