var seleccionarFotosFile = "AJ_seleccionarFotos.php";
var CARROCOMPRA = new Array();
var fotos = new Array();
//var packs = new Array();
var PRECIOFOTO = 0;

CARROCOMPRA.push(fotos);
//CARROCOMPRA.push(packs);
//index 0 FOTOS
//index 1 PACKS
vFcargarPrecios();

function vFcargarPrecios(){
	$.ajax({
			type: "POST",
			url: "AJ_cargarPrecios.php",
			success: function(response){
				var datos = response.firstChild;

				for(var x=0;x<datos.childNodes.length;x++){
					if(datos.childNodes[x].nodeType == 1){
						PRECIOFOTO = parseFloat(datos.childNodes[x].firstChild.nodeValue);
					}
				}
		}
 	});
}

function vFcargarAnos(){
	//sFAno
	$.ajax({
			type: "POST",
			url: seleccionarFotosFile,
			data: "busca=anos",
			success: function(response){
				var datos = response.firstChild;

				for(var x=0;x<datos.childNodes.length;x++){
					if(datos.childNodes[x].nodeType == 1){
						$('#sFAno > option').after('<option value ="'+datos.childNodes[x].firstChild.nodeValue+'">'+datos.childNodes[x].firstChild.nodeValue+'</option>');
					}
				}
				
				hideLoading();
		}
 	});
}
function sFselectEventos(){
	$('#sFEvento option:gt(0)').remove();
	
	if($('#sFAno option:selected').val() != 0){
		$('#sFEvento option:first').html('Cargando...');
		$.ajax({
				type: "POST",
				url: seleccionarFotosFile,
				data: "busca=eventos&ano="+$('#sFAno option:selected').val(),
				success: function(response){
					var datos = response.firstChild;
					
					
					for(var x=0;x<datos.childNodes.length;x++){
						if(datos.childNodes[x].nodeType == 1){
							$('#sFEvento > option:last').after('<option value ="'+datos.childNodes[x].firstChild.nodeValue+'">'+datos.childNodes[x].firstChild.nodeValue+'</option>');
						}
					}
					$('#sFEvento option:first').html(' ');
			}
		});
	}
}
function sFselectCategorias(){
	$('#sFCategoria option:gt(0)').remove();
	
	if($('#sFEvento option:selected').val() != 0){
		
		$('#sFCategoria option:first').html('Cargando...');
		$.ajax({
				type: "POST",
				url: seleccionarFotosFile,
				data: "busca=categorias&ano="+$('#sFAno option:selected').val()+"&evento="+$('#sFEvento option:selected').val(),
				success: function(response){
					var datos = response.firstChild;
					
					
					for(var x=0;x<datos.childNodes.length;x++){
						if(datos.childNodes[x].nodeType == 1){
							$('#sFCategoria > option:last').after('<option value="'+datos.childNodes[x].firstChild.nodeValue+'">'+datos.childNodes[x].firstChild.nodeValue+'</option>');
						}
					}
					$('#sFCategoria option:first').html(' ');					
			}
		});
	}
}
function sFselectSubcategorias(){
	$('#sFSubc option:gt(0)').remove();
	
	if($('#sFCategoria option:selected').val() != 0){
		$('#sFSubc option:first').html('Cargando...');
		$.ajax({
				type: "POST",
				url: seleccionarFotosFile,
				data: "busca=subcategorias&ano="+$('#sFAno option:selected').val()+"&evento="+$('#sFEvento option:selected').val()+"&categoria="+$('#sFCategoria option:selected').val(),
				success: function(response){
					var datos = response.firstChild;
					
					
					for(var x=0;x<datos.childNodes.length;x++){
						if(datos.childNodes[x].nodeType == 1){
							$('#sFSubc > option:last').after('<option value ="'+datos.childNodes[x].firstChild.nodeValue+'">'+datos.childNodes[x].firstChild.nodeValue+'</option>');
						}
					}
					$('#sFSubc option:first').html(' ');
			}
		});
	}
}

function cargarFotografias(){

if(($('#sFAno option:selected').val() != 0)&&($('#sFEvento option:selected').val() != 0)&&($('#sFCategoria option:selected').val() != 0)&&($('#sFSubc option:selected').val() != 0)){
	
	$('#zonaVerFotosDiv').hide('fast');
	
	$('#cargandoListado').show();
	$.ajax({
				type: "POST",
				url: "AJ_cargarFotos.php",
				data: "ano="+$('#sFAno option:selected').val()+"&evento="+$('#sFEvento option:selected').val()+"&categoria="+$('#sFCategoria option:selected').val()+"&subcategoria="+$('#sFSubc option:selected').val(),
				success: function(response){
					$('#zonaVerFotosDiv div').remove();
					$('#zonaVerFotosDiv').hide();
					
					var datos = response.firstChild;
					var comprada;
					var classE;
					var display;
					var display2;
					var REFERENCIA;
					for(var x=0;x<datos.childNodes.length;x++){
						if(datos.childNodes[x].nodeType == 1){
							//comprobamos si la foto esta comprada
							REFERENCIA = datos.childNodes[x].firstChild.nodeValue;
							comprada = false;
							
							for(var u=0;u<CARROCOMPRA[0].length;u++){
								if(CARROCOMPRA[0][u] == REFERENCIA){comprada=true};							 
							}
							
							if(comprada){
								classE = "vFthumbCompradaFondo";
								display = "display:none";
								display2 = "display:inline";
							}else{
								classE = "vFthumbNoCompradaFondo";
								display = "display:inline";
								display2 = "display:none";
							}
							
							$('#zonaVerFotosDiv').append('<div class="vFthumbDiv '+classE+'"><a href="fotoThumb.php?ref='+REFERENCIA+'&alt=600" title="'+$('#sFEvento option:selected').val()+'" class="thumb"><img style="min-height:65px" src="fotoThumb.php?ref='+REFERENCIA+'&alt=95&ample=200" /></a><div><span class="vFcompraLink" onclick="comprarFoto(\''+REFERENCIA+'\',this)" style="'+display+'">comprar</span><span id="vFq'+x+'" class="vFquitarLink" onclick="quitarFoto(\''+REFERENCIA+'\',this)" style="'+display2+'">quitar</span></div></div>');
						}
					}
					$(function() {$('#zonaVerFotosDiv .thumb').lightBox();});
					$('#cargandoListado').hide()
					$('#zonaVerFotosDiv').show('normal');
					$('#carroCompra').fadeIn('fast');
					carroMostrarValores();
					carroMostrarBotones();
					hideLoading();
			}
		});
	}
}

function comprarFoto(ref,obj){

	CARROCOMPRA[0].push(ref)
	carroMostrarValores();
	
	$(obj).hide();
	$(obj).next().show();
	$(obj).parent().parent().removeClass("vFthumbNoCompradaFondo");
	$(obj).parent().parent().addClass("vFthumbCompradaFondo");
	
	carroMostrarBotones()
}

function quitarFoto(ref,obj){
	var tmpArray = new Array;
	for(var x=0;x<CARROCOMPRA[0].length;x++){
		if(CARROCOMPRA[0][x] != ref){tmpArray.push(CARROCOMPRA[0][x])};							 
	}
	CARROCOMPRA[0] = tmpArray;
	
	carroMostrarValores();
	if(obj){
		$(obj).hide();
		$(obj).prev().show();
		$(obj).parent().parent().removeClass("vFthumbCompradaFondo");
		$(obj).parent().parent().addClass("vFthumbNoCompradaFondo");
	
	carroMostrarBotones();
	}else{verCarroListar();}
}

function carroMostrarBotones(){
	if(CARROCOMPRA[0].length > 0){
		$('#carroCompra input').show();	
	}else{$('#carroCompra input').hide();	}
}

function carroVaciar(){
	if(confirm('Va a borrar todos los elementos del carro de la compra. Desea continuar?')){
		CARROCOMPRA[0] = new Array;
		cargarFotografias();
		verCarroListar();
	}
}

function carroMostrarValores(){
	$('#CCnumFotos').html(CARROCOMPRA[0].length);
	$('#CCprecioTotal').html(((CARROCOMPRA[0].length)*PRECIOFOTO));
}

function verCarroListar(){
	mZona('pas2',true);
	var REF;
	$('#carroCompraLista div').remove();
	for(var x=0;x<CARROCOMPRA[0].length;x++){
		REF = CARROCOMPRA[0][x];
		$('#carroCompraLista').append('<div class="vFthumbDiv"><a href="fotoThumb.php?ref='+REF+'&alt=600" title="'+$('#sFEvento option:selected').val()+'" class="thumb"><img style="min-height:65px" src="fotoThumb.php?ref='+REF+'&alt=95&ample=200" /></a><span style="cursor:pointer" onclick="quitarFoto(\''+REF+'\',false)">borrar del carro</span></div>');	
	}
	if(x==0){$('#carroCompraLista').append('<div> El carro de la compra est&aacute; vacio</div>');$('#vCbotonVaciar').hide();}
	else{$('#carroCompraLista').append('<div style="clear:both"></div>');}
	
	$(function() {$('#carroCompraLista .thumb').lightBox();});
	hideLoading();
}

function vFP4FinalizarCompra(){
	var IdFactura;
	var precioTotal = 0;
	var inputs = $('#frm :input');
	for(var i=0;i<(inputs.length-1);i++){
		if(inputs[i].value == ""){inputs[i].value = "0"}
	}
	var variables = "tipopago="+$('input[name=mpago]:checked').val()+"&nombre="+$('#nombre').val()+"&apellidos="+$('#apellidos').val()+"&dni="+$('#dni').val()+"&email="+$('#email').val()+"&telefono="+$('#telfijo').val()+"&mobil="+$('#telmovil').val();

	alert(variables);

	$.ajax({
				type: "GET",
				url: "AJ_introducirPedido",
				data: variables,
				success: function(response){
					
					IdFactura = $(response).text();
					
					var passarTPV = CARROCOMPRA[0].length;
					
					for(var x=0;x<CARROCOMPRA[0].length;x++){
							$.ajax({
								type: "POST",
								url: "AJ_introducirFotoAPedido",
								data: "idf="+IdFactura+"&referencia="+CARROCOMPRA[0][x],
								success: function(response2){
									precioTotal += parseFloat(response2);
									passarTPV--;
									
									if(passarTPV < 1){//GO TO TPV
										if($('input[name=mpago]:checked').val() == 2){
											$('#p4compraPago').val(precioTotal);
											$('#p4compraFactura').val(IdFactura);
										
											$('#vFFinalizarCompra').submit();
										}
										else{
											alert('tienes que hacer una transferencia de '+precioTotal+' euros');	
										}
									}
							}
						});
					}				
			}
		});
}

