$(function(){
	$("#buttonOpenCesta").click(function(){
		if($("#cestaPopUp").css("display")=="none")
		{
			$("#cestaPopUp").slideDown(300);
		} else
		{
			$("#cestaPopUp").slideUp(300);			
		}
	});
	$("#flechaUp a").click(function(){ $("#cestaPopUp").slideUp(300); });
});

function modUniCesta(id, n)
{
	var unidadesAct = $("#unid_cesta_"+id).val();
	var uni;
	
	uni = (n) ? parseInt(unidadesAct)+1 : parseInt(unidadesAct)-1;
	
	if(uni>0)
	{
		$("#unid_cesta_"+id).val(uni);
	}
}

function recalcularCesta()
{
	var dataString="";
	$(".unid_cesta").each(function(){
		dataString+="basketId[]="+$(this).attr("rel")+"&basketVal[]="+$(this).val()+"&";
	});
	$("#cestaPopUp .cargar").fadeIn(200, function(){
		$.ajax ({
			type: 'POST',
			url:  'cesta_update.php',
			data: dataString,
			success: function(data){
				recalcularCestaPeq();
				$("#cestaPopUp #contCesta").fadeOut(200, function() {
					$(this).html(data);
					$(this).fadeIn(200, function(){
						$("#cestaPopUp .cargar").fadeOut(200);
					});
				});
		}
		});
	});
}

function recalcularCestaPeq()
{
	$.ajax ({
		url:  'cesta_peq.php',
		success: function(data){
			$("#cestaHeader #cestaIn").html(data);
		}
	});
}

function recalcularCestaBig()
{
	$.ajax ({
		url:  'cesta_big.php',
		success: function(data){
			$("#cestaPopUp #contCesta").html(data);
		}
	});
}

function eliminarCesta (n)
{
	$("#cestaPopUp .cargar").fadeIn(200, function(){
		$.ajax ({
			type: 'POST',
			url:  'cesta_del.php',
			data: "id="+n,
			success: function(data){
				recalcularCestaPeq();
				$("#cestaPopUp #contCesta").fadeOut(200, function() {
					$(this).html(data);
					$(this).fadeIn(200, function(){
						$("#cestaPopUp .cargar").fadeOut(200);
					});
				});
		}
		});
	});
}
