jQuery(document).ready( function(){
	/***************************************
	* SELETION QTY : Page Listing Produits *
	***************************************/
	// Clic sur Ajouter au panier => Affiche la boite de sélection de quantité
	jQuery('#select-qty button[id^=add-to-cart-]').click(function(){
		var product_id = this.id.replace(/add-to-cart-/gi, '');
		showQtyList(product_id);
	});
	
	// Clic sur le boutton "Ok" de la sélection de quantité => Redirection vers le panier avec la quantité voulue
	jQuery('#select-qty .select-qty-button input[id^=ok-]').click(function(){
		var product_id = this.id.replace(/ok-/gi, '');
		if( jQuery('#select-qty div[id=select-qty-products-' + product_id + '] select').html() != null) {
			select_qty = jQuery('#select-qty select[id=field-' + product_id + '] option:selected').val();
		} else {
			select_qty = jQuery('#select-qty input[id=field-' + product_id + ']').val();
		}
		
		// Redirection vers la page panier
		if( select_qty > 0 ) {
			var url = '/checkout/cart/add?product=' + product_id +'&qty=' + select_qty;
			jQuery(location).attr('href',url);
		}
		
	});
	
	// Clic sur le boutton "Annuler" de la sélection de quantité => Masque la boite de sélection de quantité
	jQuery('#select-qty .select-qty-button input[id^=cancel-]').click(function(){
		var product_id = this.id.replace(/cancel-/gi, '');		
		hideQtyList(product_id);
	});
	
	
	/********************************
	*	SELETION QTY : Page Produit	*
	*********************************/
	// Clic sur Ajouter au panier => Redirection (Qty en fonction de la liste déroulante ou de la boite input
	jQuery('#add-to-cart button[id^=add-to-cart-]').click(function(){
		var product_id = this.id.replace(/add-to-cart-/gi, '');
		
		if( jQuery('#add-to-cart select').html() != null) {
			select_qty = jQuery('#add-to-cart select[id=select-product] option:selected').val();
		} else {
			select_qty = jQuery('#add-to-cart input[id=select-product]').val();
		}
		
		// Redirection vers la page panier
		if( select_qty > 0 ) {
			var url = '/checkout/cart/add?product=' + product_id +'&qty=' + select_qty;
			jQuery(location).attr('href',url);
		}
	});
	
	/***********************************************
	*	AFFICHER / REDUIRE Quanté Acheter Malin Panier	*
	***********************************************/
	jQuery( '#label-optimize-plus' ).addClass('optimize-reduice');
	
	jQuery( '#label-optimize-moins' ).click( function () {
		jQuery( '#label-optimize-moins' ).addClass('optimize-reduice');
		jQuery( '#label-optimize-plus' ).removeClass('optimize-reduice');
		jQuery( '#block-optimize-shopping-content-bottom' ).addClass('optimize-reduice');
		jQuery( '#block-optimize-shopping-content' ).addClass('optimize-reduice');
	} );
	
	jQuery( '#label-optimize-plus' ).click( function () {
		jQuery( '#label-optimize-moins' ).removeClass('optimize-reduice');
		jQuery( '#label-optimize-plus' ).addClass('optimize-reduice');
		jQuery( '#block-optimize-shopping-content-bottom' ).removeClass('optimize-reduice');
		jQuery( '#block-optimize-shopping-content' ).removeClass('optimize-reduice');
	} );

});
 
/**
 * Au clic sur le bouton "Ajouter au panier"
 * - Affiche une boite contenant la quantité à sélectionner
 * - Cache les autres boites de selection de quantité des autres produits
 * */
function showQtyList(id) 
{
	// Cache les autres boites de quantité
	hideAllQtyList();
	
	// Cache le boutton Ajouter au panier du produit en cours
	jQuery('#product-attribute-bottom-'+id).addClass('show-qty');
	
	// Affiche la boite de sélection de quantité du produit en cours
	jQuery('#select-qty-products-'+id).removeClass('show-qty');
	
	// Donne le focus à la sélection de quantité
	jQuery('#field-'+id).focus();
}

/**
  * Cache la boite de sélection de quantité voulu
  * Re-affiche le bouton "Ajouter au panier" du produit voulu
  */
function hideQtyList(id)
{
	// Cache la boite de sélection
	jQuery('#select-qty-products-'+id).addClass('show-qty');
	// Re-affiche le boutton Ajouter au panier
	jQuery('#product-attribute-bottom-'+id).removeClass('show-qty');
}

/**
  * Cache toutes les boites de sélection de quantité
  * Re-affiche les boutons "Ajouter au panier"
  */
function hideAllQtyList()
{
	jQuery('#select-qty .select-qty-products').addClass('show-qty');
	jQuery('#select-qty .product-attribute-bottom').removeClass('show-qty');
	
}	
