var cible=0;
var source=0;
var profilOpen=0;

function majCommande(l){
	var str = $("#form_catalogue").serialize();
	var msg = "";
	$(".formCtnControl").removeClass("error");
	$.ajax({
		type: 'POST',
		data: 'noMenu=1&'+str,
		url: '/init.php/shop/majCommande',
		cache: false,
		dataType: 'json',
		
		success: function(data){
			
			if(!data.success){
				$("#produits").addClass("noAlert");
				$("#msgError").html(data.message);	
				$("#msgError").fadeIn("slow");
				$.each(data.error, function(key, value){
					$("#" + key).addClass("error");
					$("#" + key).removeClass("noAlert");
				}) 
			}
			else{
				recap();
			}
		}
	});	
	
}

function recap(){
	$.ajax({
		type: 'POST',
		data: 'noMenu=1',
		url: '/init.php/shop/confirmation',
		cache: false,
		success: function(html){
			$("#catalogue").fadeOut("slow", function(){
				$("#catalogue").html(html);	
				$("#catalogue").fadeIn("slow");
			});			
			
		}
	});		
}

function sendCommande(){
	$.ajax({
		type: 'POST',
		data: 'noMenu=1',
		url: '/init.php/shop/sendCommande',
		cache: false,
		success: function(html){			
			$("#catalogue").html(html);	
		}
	});		
}

function confDel(msg){
	if(confirm(msg)){
		return true;	
	}
	return false;
}


function disconnect(l){
	$.ajax({
		type: 'POST',
		data: 'noMenu=1',
		url: '/init.php/addonshopAbonne/disconnect',
		cache: false,

		success: function(html){
			// alert(html);
			$("#conteneurshopLoginForm").html(html);	
			if($("#editProfil")) $("#editProfil").hide("slow");
		}		
	});	
	
	
}

function majProfil(l){
	var str = $("#formEditionProfil").serialize();
	var msg = "";
	
	// Rendre visible le message d'attente
	$("#message").show("slow");
	$.ajax({
		type: 'POST',
		data: 'noMenu=1&'+str,
		url: '/init.php/addonshopAbonne/majProfil',
		cache: false,
		dataType: 'json',
		success: function(data){
			if(!data.success){
				$.each(data.error, function(n) {
					msg += data.error[n] + '<br />';					
					$("#message").html(msg);
					$("#message").show("slow");				
				})
			}
			else{
				// Met à jour le champ caché id_user
				$("#id_user").val(data.data.id_user);
				$("#message").html(data.success);
				$("#message").show("slow", function(){
									
									// On vire la procedure d'achat formulaire
									$("#workFlowshop").fadeOut(2000, function(){
																		// On le met à jour
																		$("#workFlowshop").html(data.success);
																		// On le réaffiche
																		$("#workFlowshop").show(2000);		
																				}); // Fin de la function imbriguée dans fadeOut
																		
								  });	
				
			}
		}
	});	
}

function authentification(){
	var str = $("#shopLoginForm").serialize();
	
	$.ajax({
		type: 'POST',
		data: 'noMenu=1&'+str,
		url: '/init.php/addonshopAbonne/authentification',
		cache: false,
		
		success: function(html){
			if(html == "error"){
				$("#msgError").	show("slow");	
			}
			else{
				$("#conteneurshopLoginForm").html(html);				
			}
		}		
	});
}

function editProfil(l){
	if(profilOpen == "0"){
		$.ajax({
			type: 'POST',
			data: 'noMenu=1&l='+l,
			url: '/init.php/addonshopAbonne/formProfil',
			cache: false,
			success: function(html){
				$("H1").after(html);
			}
		});
		profilOpen=1; // Tag comme ouvert
	}
	
}

jQuery.fn.move = function(id, module, langue) {
	if(source == 0){
		// On ajoute la class selected à l'objet
		$(this).parent().css('border', '1px solid BLUE');
		source = id;
	}
	else{
		if(id == source){					
			$(this).parent().css('border', '0');
			source = 0;
		}
		else{
			cible = id;
			$(this).parent().css('border', '1px solid RED');
			document.location.href="/admin/index.php/"+module+"/movePosition/?cible="+cible+"&source="+source+"&l="+langue;	   
		}
	}

};

$(document).ready(function(){
	$(".move").each(function(){
		$(this).click(function(){
			$(this).move($(this).attr("id"), $(this).attr("c"), $(this).attr("l"));
		});
	});
	
	$("#form_catalogue").submit(function(){		
		majCommande();
		return false;
	});
});
