$(function(){
	//Accordion
	if( $('#accordion').length ){
		$("#accordion").accordion({
			 collapsible: true, autoHeight: false, fillSpace: false
		});
	}
	
	//Menu d�roulant
	$('ul.jd_menu').jdMenu();
	
	//Fond lightbox
	var page = $('#wrap').height();
	$('#popup').height(page);
	
	//Ouvrir un lien dans un nouvel onglet
	$("a[class^='_blank']").click(function(){
		window.open(this.href, '_blank');
		return false;
    });
	
	//Slideshow
	if( $('.slideTab').length ){
		$('.slideTab').tabSwitch('create', {type: 'toggle', toggle: 'fade', height: 505, width: 961});
		$('.slideTab').tabSwitch('startAuto',{interval: 5000});
	}
	
	//Diaporama accueil
	if( $('#slides').length ){
		$('#slides').slides({
			effect: 'fade',
			play: 5000,
			hoverPause: false,
			fadeSpeed: 800,
			crossfade: true,
			generateNextPrev: false,
			generatePagination: false
		});
	}
	
	//Effet opacite au hover
	$('.opacityOut').hover(
		function(){$(this).stop().animate({opacity: 1}, 300);},
		function(){$(this).stop().animate({opacity: 0.60}, 300);
	});
	$('.opacityOut-85').hover(
		function(){$(this).stop().animate({opacity: 1}, 300);},
		function(){$(this).stop().animate({opacity: 0.85}, 300);
	});
	
	$('input#recherche').click(function(){
		if( ($(this).val() == 'Search') || ($(this).val() == 'Rechercher') ){
			$(this).val('');
		}
	});
	
	//Liste en 3 colonnes de 8 �lements chacun
	var noColonne = 1;
	var nbElement = 0;
	var nbElementParColonne = 9;
	$('ul#menu_niveau_1_bis li').each(function(element){
		
		$(this).addClass('colonne-'+noColonne);
		
		if( nbElement < nbElementParColonne-1 ){
			nbElement++;
			if( (noColonne > 1) && (nbElement == 1) ){
				$(this).addClass('reset');
			}
		}else{
			nbElement = 0;
			noColonne++;
		}
	});
	
	//Select custom
	$('select.select').sSelect({
		ddMaxHeight: '250px',
		defaultText: 'Choisir une gamme'
	});
	
	//Afficher un div
	$('.more').toggle(function(){
		$('#'+$(this).attr('rel')).show();
	},function(){
		$('#'+$(this).attr('rel')).hide();
	});
	
	//Boutique
	$('.ajoutProduit').click(function(){
		ajouterProduit($(this).attr('id'));
	});
	
	$('#bt-continuer').click(function(){
		$('#popup').hide();
	});
	
	$('.modifierProduit').keyup(function(){
		var id = $(this).attr('id');
		setTimeout('modifierProduit(\''+id+'\')', 1000);
	});
	
	//Etape 1 Panier/revendeur
	$('.panier-revendeur').click(function(){
		
		if( $('#panier').is(':visible') ){
			$('#panier').hide(); $('#boutons-acheter').hide();
			$('#liste-revendeur').show(); $('#boutons-revendeur').show();
		}
		else{
			$('#panier').show(); $('#boutons-acheter').show();
			$('#liste-revendeur').hide(); $('#boutons-revendeur').hide();
		}
	});
	
	$('#adr_livraison').click(function(){
		
		if( $('#adresse_livraison').is(':visible') ){
			$('#adresse_livraison').hide();
		}else{
			$('#adresse_livraison').show();
		}
	});
	
	if( $("a.youtube").length > 0 ){
		$("a.youtube").attr("rel", "prettyPhoto");
		$("a[rel^='prettyPhoto']").prettyPhoto({
			animationSpeed: 'normal',
			padding: 20, /* padding for each side of the picture */
			opacity: 0.40, /* Value betwee 0 and 1 */
			showTitle: false, /* true/false */
			allowresize: true, /* true/false */
			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
			theme: 'dark_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
			hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
			modal: false /* If set to true, only the close button will close the window */
		});
	}
	
	//Fiche produit
	$('.zoom-div').hover(function(){
		if ($(this).children('.contenu-zoom').is(':hidden')){
			$(this).children('.lien-zoom').css('z-index','400');
			$(this).children('.contenu-zoom').css('z-index','300');
			$(this).children('.contenu-zoom').dequeue().stop().slideToggle();
		}
		return false;
	}, function(){
		$(this).children('.contenu-zoom').slideToggle();
		$(this).children('.lien-zoom').css('z-index','200');
		$(this).children('.contenu-zoom').css('z-index','1');
		return false;
		
		
	});
	
	$('#accordion h3 a').click(function(){
		$('html,body').animate({scrollTop:0}, 'slow');
	});
});

/**
 * G�n�rer nouveau mot de passe
 *
 * @param	void
 * @return	string
 */
function updatePassword(){
	
	var email = $('#email').val();

	$.ajax({
		url: "/modules/boutique/ajax/recuperer-password.php",
		global: false,
		type: 'GET',
		data: {'email': email},
		success: function(data){
			if( data != '' ){
				alert(data);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert(XMLHttpRequest.status);
		}
	});
}

/**
 * Ajouter un produit dans le panier
 * 
 * @param 	id
 * @return	void
 */
function ajouterProduit(id){
	
	var prixUnitaire 	= $('#prix-unitaire').html();
	var aPrixUnitaire 	= prixUnitaire.split("&nbsp;");
	prixUnitaire 		= aPrixUnitaire[0];
	var nom 			= $('#nom-produit').html();
	var reference 		= $('#reference').html();
	
	$.ajax({
		url: "/modules/boutique/ajax/ajouter-produit.php",
		global: false,
		type: 'GET',
		data: {'id_produit': id, 'prixunit': prixUnitaire, 'nom': nom, 'ref': reference},
		success: function(data){
			if( data == true ){
				$('#popup').show();
				$('#description').html(nom+" ("+reference+")");
			}
			else{
				$('#popup').show();
				$('#message').html("Une erreur est survenue !");
				$('#description').html(data);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert(XMLHttpRequest.status);
		}
	});
}

/**
 * Modifier un produit dans le panier
 * 
 * @param 	id
 * @return	void
 */
function modifierProduit(id){
	
	var quantite = $('#'+id).val();

	$.ajax({
		url: "/modules/boutique/ajax/modifier-produit.php",
		global: false,
		type: 'GET',
		data: {'id_produit': id, 'quantite': quantite},
		success: function(data){
			if( data == true ){
				window.location = "/index.php?section=15&etape=1";
			}
			else{
				alert(data);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert(XMLHttpRequest.status);
		}
	});
}

/**
 * Supprimer un produit dans le panier
 * 
 * @param 	id
 * @return	void
 */
function supprimerProduit(id){
	
	$.ajax({
		url: "/modules/boutique/ajax/supprimer-produit.php",
		global: false,
		type: 'GET',
		data: {'id_produit': id},
		success: function(data){
			if( data == true ){
				$('#popup').show();
				$('#description').html();
			}
			else{
				$('#popup').show();
				$('#message').html("Une erreur est survenue !");
				$('#description').html(data);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert(XMLHttpRequest.status);
		}
	});
}
