// ActionScript Document
$(function(){
	
	


	/**** BLOC ARTICLE PANIER - *****/
	$('.bloc_article_panier .ajout_panier').live('click', function(){
		var REF_FOUSSIER 	= $(this).parent().find(".art_ref_foussier").val() ;
		var QUANTITE		= $(this).parent().find(".plus_moins_qte .quantite") .val();
		

		var tab_art_commandes 	= new Array();
		var tab_quantite 		= new Array();


		tab_art_commandes.push(REF_FOUSSIER);
		tab_quantite.push(QUANTITE);

		var article_commandable = quantiteIsCommandable(REF_FOUSSIER, QUANTITE, false, true);
		if(article_commandable.IS_COMMANDABLE){	
			ajouterAuPanier(tab_art_commandes, tab_quantite);
			updateCartouchePanier();
			popupAjoutPanier(tab_art_commandes, tab_quantite);
		}
		else{
			popupQuantiteNonCommandable(article_commandable);		
		}	
		return false;	
	});



	//popup ajout au panier, afficher/cacher arts comp
	$('#cacher_art_comp').live('click', function(){
		
		var val 			= $(this).val();
		var checked 		= $(this).is(':checked');
		
		if(val == 'oui' && checked == true){
			var cacher_art_comp = 'true';
			$('#bloc_art_comp').hide();
		}
		else if(val == 'non' && checked == false){
			var cacher_art_comp = 'true';
			$('#bloc_art_comp').hide();
		}
		else{
			var cacher_art_comp	= 'false';
			$('#bloc_art_comp').show();
		}
		
		$.ajax({
			type: "POST",
			url: URL_SITE_HTTP+"ajax/cacher_art_comp.xhr.php",
			data: 'cacher_art_comp='+cacher_art_comp
		});		
	});



	//btn ajouter au panier depui popup art comp
	$('#articles_complementaires_btn_ajouter').live('click', function(){
		
		var tab_art_non_commandables 	= new Array();
		var tab_art_commandes 			= new Array();
		var tab_quantite 				= new Array();
		
		$.each($('#articles_complementaires_table input[name^="ART_REF_FOUSSIER_QTE"]'), function(){
			var QUANTITE = Number($(this).val());
			if(QUANTITE > 0){
				var REF_FOUSSIER = $(this).attr('name').replace('ART_REF_FOUSSIER_QTE[', '').replace(']', '');
				tab_art_commandes.push(REF_FOUSSIER);
				tab_quantite.push(QUANTITE);
			}
		});
		
		if(tab_art_commandes.length){
			ajouterAuPanier(tab_art_commandes, tab_quantite);
			updateCartouchePanier();
			
			var position_panier = $('#articles_complementaires_icone_ajout_panier').position();
			
			$.each(tab_art_commandes, function(){
				var input_art = $('#articles_complementaires_table input[name="ART_REF_FOUSSIER_QTE['+this+']"]');
				var position_art = input_art.position();
				$('<span class="move_to_panier"></span>')
					.insertAfter(input_art)
					.css({
						'top':		2+position_art.top+'px',
						'left':		12+position_art.left+'px'
					})
					.animate({
						'top':		1+position_panier.top+'px',
						'left':		5+position_panier.left+'px'
					}, 1000, 'easeInOutQuart')
					.fadeOut(300, function(){
						$(this).remove();
					});
			});
		}
		return false;		
	});
	
	





	//prec <-> suiv art comp
	$('#articles_complementaires_bloc_article_prec, #articles_complementaires_bloc_article_suiv').live('click', function(){
		var REF_FOUSSIER = $(this).attr('REF_FOUSSIER');			
		if(REF_FOUSSIER != '' && REF_FOUSSIER != undefined){	
		
			$('#articles_complementaires_popup').fadeOut(200, function(){
				$('#articles_complementaires_loading').fadeIn(200);
			});
		
			var tab_art_commandes 			= new Array();
			var tab_quantite 				= new Array();
		
			$.each($('.listing_article input[name^="ART_REF_FOUSSIER_QTE"]'), function(){
				var QUANTITE = Number($(this).val());
				if(QUANTITE > 0){
					var REF_FOUSSIER = $(this).attr('name').replace('ART_REF_FOUSSIER_QTE[', '').replace(']', '');
					tab_art_commandes.push(REF_FOUSSIER);
					tab_quantite.push(QUANTITE);
				}
			});
			
			var tab_param_REF_FOUSSIER = new Array();
			$.each(tab_art_commandes, function(){
				tab_param_REF_FOUSSIER.push('TAB_REF_FOUSSIER[]='+this);
			});
			
			var tab_param_QUANTITE = new Array();
			$.each(tab_quantite, function(){
				tab_param_QUANTITE.push('TAB_QUANTITE[]='+this);
			});
			
			$.ajax({
				type: "POST",
				url: URL_SITE_HTTP+"ajax/get_articles_complementaires.xhr.php",
				data: "REF_FOUSSIER="+REF_FOUSSIER+"&"+tab_param_REF_FOUSSIER.join('&')+'&'+tab_param_QUANTITE.join('&'),
				dataType: "json",
				success: function(json){

					//bloc article
					$('#articles_complementaires_bloc_article_art_img').html(json['ARTICLE'].PHOTO);
					$('#articles_complementaires_bloc_article_art_nom').html('<strong>'+json['ARTICLE'].DESIGNATION+'</strong>');
					$('#articles_complementaires_bloc_article_art_code').html('<strong>Code:</strong> '+json['ARTICLE'].REF_FOUSSIER);
					$('#articles_complementaires_bloc_article_art_qte').html('<strong>Quantité:</strong> '+json['ARTICLE'].QUANTITE);
					$('#articles_complementaires_bloc_article_art_prix').html('<strong>Prix:</strong> '+json['ARTICLE'].PRIX+' € HT');
					
					$('#articles_complementaires_bloc_article_prec').attr('REF_FOUSSIER', json['ARTICLE'].REF_FOUSSIER_PREC);
					$('#articles_complementaires_bloc_article_suiv').attr('REF_FOUSSIER', json['ARTICLE'].REF_FOUSSIER_SUIV);
					updateOreilles();
				
					//art complémentaires
					$('#articles_complementaires_table tbody').empty();	
					if(json['ART_COMP'].length){
						$.each(json['ART_COMP'], function(key, val){
							var article = '<tr>';
							article +=		'<td style="padding:0;">'+val.PHOTO+'</td>';	
							article +=		'<td style="text-align:left;">'+val.DESIGNATION+'</td>';	
							article +=		'<td>'+val.CODE+'</td>';	
							article +=		'<td>'+val.PRIX+'</td>';	
							article +=		'<td>'+val.QUANTITE+'</td>';	
							article +=		'<td><div class="plus_moins_qte"><input type="button" value="-"><input type="text" value="" name="ART_REF_FOUSSIER_QTE['+val.REF_FOUSSIER+']"><input type="button" value="+"></div></td>';		
							article +=	'</tr>';
							$('#articles_complementaires_table tbody').append(article);
						});
						$('#articles_complementaires_table_conteneur').show();
						$('#articles_complementaires_table tbody tr:odd').addClass('odd');	
					}
					else{						
						$('#articles_complementaires_table_conteneur').hide();
					}
				
					$('#articles_complementaires_loading').fadeOut(200, function(){
						$('#articles_complementaires_popup').fadeIn(200);
					});
				
				}
			});
		}
		return false;
	});

});



//Ajoute le tableau d'article au panier
function ajouterAuPanier(tab_art_commandes, tab_quantite){
	if(tab_art_commandes.length){
		
		//formation du tableau de paramètres
		var tab_param_REF_FOUSSIER = new Array();
		$.each(tab_art_commandes, function(){
			tab_param_REF_FOUSSIER.push('REF_FOUSSIER[]='+this);
		});
		
		var tab_param_QUANTITE = new Array();
		$.each(tab_quantite, function(){
			tab_param_QUANTITE.push('QUANTITE[]='+this);
		});
		
		$.ajax({
			type: "POST",
			url: URL_SITE_HTTP+"ajax/ajouter_au_panier.xhr.php",
			data: tab_param_REF_FOUSSIER.join('&')+'&'+tab_param_QUANTITE.join('&')
		});
	}
}



//affiche le popup d'ajout au panier
function popupAjoutPanier(tab_art_commandes, tab_quantite){

	

	if(tab_art_commandes.length){
	
	

		//formation du tableau de paramètres
		var tab_param = new Array();
		$.each(tab_art_commandes, function(){
			tab_param.push('REF_FOUSSIER[]='+this);
		});
		
		var tab_param_QUANTITE = new Array();
		$.each(tab_quantite, function(){
			tab_param_QUANTITE.push('QUANTITE[]='+this);
		});

		
		
		$.messageService({
			title: 			'',
			type:			'url',
			source:			'popup_ajax/popup_ajout_panier.php?'+tab_param.join('&')+'&'+tab_param_QUANTITE.join('&'),
			width:			732,
			onLoadComplete:	function(){ 
				$('#articles_complementaires_table tbody tr:odd').addClass('odd');
				updateOreilles();				
			}
		});
		
		
	}
	return false;
}





//met à jour le cartouche panier (interface)
function updateCartouchePanier(){
	$.ajax({
		type: "POST",
		url: URL_SITE_HTTP+"ajax/get_infos_panier.xhr.php",
		dataType: 'json',
		success: function(json){
			if($('#bloc_panier_nb_art').length){
				$('#bloc_panier_nb_art').html(json['PANIER'].NB_ARTICLES_TXT);
				$('#bloc_panier_TOTAL_HT').html(json['PANIER'].TOTAL_HT+' €');
				$('#bloc_panier_FRAIS_PORT').html(json['PANIER'].FRAIS_PORT+' €');
				$('#bloc_panier_MONTANT_TOTAL_HT').html(json['PANIER'].MONTANT_TOTAL_HT+' €');
			}
			else{
				$.ajax({
					type: "POST",
					url: URL_SITE_HTTP+"ajax/get_infos_panier.xhr.php",
					data: "new_panier=true",
					dataType: 'xml',
					success: function(res){
						$('#bloc_panier').html($(res).find('#bloc_panier').html());
					}
				});
			}
		}
	});
}
