$(document).ready(function()	{
	initMainMenu();
	initDiaporama();
	initListeProduitSimple();	
	
	$('.liste-avis').jScrollPane({
		verticalDragMinHeight: 10,
		showArrows: true
	});
	
	$('#liste_telechargement').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);
		});
	});
	
	$('.lienBoutique').click(function(){
		
		var content = '<div class="detail">';
		content += '	<div class="texte texte12">';
		content += '		Boutique en cours de reconstruction.';
		content += '	</div>';
		content += '</div>';
		content += '<div class="footer"><span onclick="closeBox();" class="button buttonLeft">Fermer</span></div>';
		
		showPopin(content);
		return false;
	});
	
});

function closeBox(){
	$(document).trigger('close.facebox');
}

function showPopin(data){
	$.facebox(data);
	//$('#facebox .close').hide();
}

var maxPageListe = new Array();
var currentPageListe = new Array();

function initListeProduitSimple(){
	
	$('.liste_produit_simple').each(function(){
		if(typeof pngfix == 'function')
			pngfix($(this).find('.content .image 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();
			}
		});
	});
}
