$(document).ready(function(){
	
	$(".thumb_producto").hover(
		function(){
			$(this).stop().fadeTo(200,1);	
		},
		function(){
			$(this).stop().fadeTo(200,0.5);
		}
	).click(function(){
		var id = $(this).attr("id");
		$("#productos_tienda").load("Content/Scripts/ajax_tienda.php?seccion=tienda&accion=ver_producto&num="+id,function(data){
		  //alert(data);
/*			ajustar_zoom();
			$("#zoom_Wrapper").fadeIn();
			ajustar_zoom();
			$("body,html").css({overflow:"hidden"});*/
            
		});
	});
    
    $("#pal_carro").live("click",function(){
    	var id_cliente = $("#id_cliente").attr("value");
		if(id_cliente != ""){
			var id_producto = $(this).parent().parent().attr('id');
		 	$("#mensajes_de_resultados").load("Content/Scripts/ajax_tienda.php?accion=add_item&id_producto=" + id_producto + "&cantidad=1&id_cliente=" + id_cliente,function(){
	    		$("#cesta_resumen_Wrapper").load("Content/Scripts/ajax_tienda.php?accion=cesta_resumen&id_cliente=" + id_cliente);
	    		$("#cesta_menu_Wrapper").load("Content/Scripts/ajax_tienda.php?accion=ver_cesta_status&id_cliente=" + id_cliente);    		
			});
		} else {
			alert("Login o Register first.");
		}
    });
    
    $("#cancelar_prod").live("click",function(){
        document.location = "index.php?seccion=tienda";
    });


// LOGIN CLIENTES

	$(".b_form").click(function(e){
		e.preventDefault();		
	});
	
	var caja = "";
	
	$("#b_login").click(function(e){
		e.preventDefault();
		if(caja != "login"){
			$(".caja_dialogo:visible").hide();
			$("#caja_login").fadeIn();
			caja = "login";
		} else {
			$(".caja_dialogo:visible").hide();
			caja = "";	
		}
	});
	
	$("#b_lopd_tienda").toggle(
		function(e){
			e.preventDefault();
			$(".lopd_tienda").slideDown();
		},
		function(e){
			e.preventDefault();
			$(".lopd_tienda").slideUp();		
		}
	);
	
	$("#b_logout").live("click",function(e){
		e.preventDefault();
		$.cookie("sn_cliente_tienda_id",null);
		document.location = "index.php?seccion=tienda";		
	});
	
	$("#password1").attr("value","");
	
    $("#b_submit_registro_tienda").live("click",function(e){
		e.preventDefault();
		
		if($("#email_registro").attr("value") == ""){
			alert("El campo \"Correo electrónico\" es obligatorio");
		
		} else if($("#nombre").attr("value") == ""){
			alert("El campo \"Nombre\" es obligatorio");
		
		} else if($("#apellidos").attr("value") == ""){
			alert("El campo \"Apellidos\" es obligatorio");		
		
		} else if($("#direccion").attr("value") == ""){
			alert("El campo \"Dirección\" es obligatorio");
		
		} else if($("#cp").attr("value") == ""){
			alert("El campo \"Código postal\" es obligatorio");
		
		} else if($("#localidad").attr("value") == ""){
			alert("El campo \"Localidad\" es obligatorio");
		
		} else if($("#provincia").attr("value") == ""){
			alert("El campo \"Provincia\" es obligatorio");
		
		} else if($("#password1").attr("value") != $("#password2").attr("value")){
			alert("Las contraseñas no coinciden");
		
		} else if(!$("#ch_lopd_tienda").attr("checked")){
			alert("Hay que leer y aceptar la política de privacidad");
		
		} else {
			$("#cliente_tienda_form").submit();
		}
	});
	
	$("#b_submit_clientes_tienda").live("click",function(e){
		e.preventDefault();
		
		if($("#email_registro").attr("value") == ""){
			alert("El campo \"Correo electrónico\" es obligatorio");
		
		} else if($("#nombre").attr("value") == ""){
			alert("El campo \"Nombre\" es obligatorio");
		
		} else if($("#apellidos").attr("value") == ""){
			alert("El campo \"Apellidos\" es obligatorio");		
		
		} else if($("#direccion").attr("value") == ""){
			alert("El campo \"Dirección\" es obligatorio");
		
		} else if($("#cp").attr("value") == ""){
			alert("El campo \"Código postal\" es obligatorio");
		
		} else if($("#localidad").attr("value") == ""){
			alert("El campo \"Localidad\" es obligatorio");
		
		} else if($("#provincia").attr("value") == ""){
			alert("El campo \"Provincia\" es obligatorio");
		
		} else if($("#password1").attr("value") != $("#password2").attr("value")){
			alert("Las contraseñas no coinciden");
		
		/*} else if(!$("#ch_lopd_tienda").attr("checked")){
			alert("Hay que leer y aceptar la polÃ­tica de privacidad");
		*/
		} else {
			$("#cliente_tienda_form").submit();
		}
	});	
		
	$("#b_submit_login").live("click",function(e){
		e.preventDefault();
		login();
	});
	
	
	$("#password_tienda").live("keyup",function(e){
		var keycode;
	
		if (window.event) {
			keycode = window.event.keyCode;
		} else if (e) {
			keycode = e.which;
		} else {
			return true;
		}
		
		if(keycode == 13){			
			login();						
		}			
	});
	
	
	$(".caja_dialogo").blur(function(){
		$(this).fadeOut();
	});

	$(".msj_registro .b_form").live("click",function(e){
		e.preventDefault();
		$("#caja_registro .caja_dialogo_contenido").fadeOut("normal",function(){
			$(this).load("Content/Scripts/ajax_tienda.php?accion=form",function(){
				$(this).fadeIn();
			});			
		});
	});
	
	$("#b_borrar_registro").live("click",function(e){
		e.preventDefault();
		$("#registro_form input").attr("value","");
	});


	//####### FICHA PRODUCTO ######//
	
	$("#producto_thumbnails ul li").mouseover(function(){
		var src = $(this).find("img").attr("src");
		$("#producto_foto img").attr("src",src);
	});
	
	
	//######## CESTA #######//
	var cesta_abierta = false;
	
	$("#b_cerrar_cesta").live("click",function(e){
		e.preventDefault();
		$("#cesta_menu_Wrapper").fadeOut("normal",function(){
			cesta_abierta = false;
		});
	});
	
	$("#b_ver_cesta").live("click",function(e){
		e.preventDefault();
		if(!cesta_abierta){
			$("#cesta_menu_Wrapper").fadeIn("normal",function(){
				cesta_abierta = true;
			});
		} else {
			$("#cesta_menu_Wrapper").fadeOut("normal",function(){
				cesta_abierta = false;
			});
		}
	});
	
	$("a.producto_listado_add").live("click",function(e){
		e.preventDefault();
		
		var id_cliente = $("#id_cliente").attr("value");
		if(id_cliente != ""){
			var id = $(this).parent().attr("id");
			var cantidad = 1;
			
			$("#cesta_menu_Wrapper").load("Content/Scripts/ajax_tienda.php?accion=add_item&id_producto="+id+"&cantidad="+cantidad+"&id_cliente="+id_cliente,function(){
				$(this).load("Content/Scripts/ajax_tienda.php?accion=ver_cesta_status&id_cliente="+id_cliente);
				$("#cesta_resumen_Wrapper").load("Content/Scripts/ajax_tienda.php?accion=cesta_resumen&id_cliente="+id_cliente);
			});
			
		} else {
			if($.cookie("sn_idioma") == "es"){
				alert("Identifí­cate o regí­strate para añadir productos a la cesta.");
			} else {
				alert("Login o Register first.");			
			}		
		}
	});
	
	$("#tabla_cesta_status tbody tr").live("click",function(){
		if($(this).attr("id") != "cesta_vacia"){
			/*
			if($.cookie("sn_idioma") == "es"){
				var msj = "¿Deseas quitar el producto de la cesta?";
			} else {
			*/
				var msj = "Do you want to remove this item from the basket?";
			//}		
			if(confirm(msj)){
				var id_cliente = $("#id_cliente").attr("value");
				var id = $(this).attr("id").split("#");
				var id_producto = id[1];
				var id_pedido = id[0];
				var fila = $(this);
				$("#cesta_menu_Wrapper").load("Content/Scripts/ajax_tienda.php?accion=update_item&id_pedido="+id_pedido+"&id_producto="+id_producto+"&cantidad=0&id_cliente="+id_cliente,function(){
					$("#cesta_menu_Wrapper:visible").load("Content/Scripts/ajax_tienda.php?accion=ver_cesta_status&id_cliente="+id_cliente);
				$("#cesta_resumen_Wrapper").load("Content/Scripts/ajax_tienda.php?accion=cesta_resumen&id_cliente="+id_cliente);
				});
			}		
		}
	});
			
	$("a#producto_add").live("click",function(e){
		e.preventDefault();
		var id_cliente = $("#id_cliente").attr("value");
		if(id_cliente != ""){
			var id = $(".producto").attr("id");
			var cantidad = $("#cantidad_input").attr("value");
			
			if(cantidad > 0){
				$("#cesta_menu_Wrapper").load("Content/Scripts/ajax_tienda.php?accion=add_item&id_producto="+id+"&cantidad="+cantidad+"&id_cliente="+id_cliente,function(){
					$(this).load("Content/Scripts/ajax_tienda.php?accion=ver_cesta_status&id_cliente="+id_cliente);
					$("#cesta_resumen_Wrapper").load("Content/Scripts/ajax_tienda.php?accion=cesta_resumen&id_cliente="+id_cliente);
				});
			}
		} else {
			/*if($.cookie("sn_idioma") == "es"){
				alert("IdentifÃ­cate o regÃ­strate para aÃ±adir productos a la cesta.");
			} else {
			*/	alert("Login o Register first.");			
			//}
			
		}
	});
	
	$("#b_vaciar_cesta").live("click",function(e){
		e.preventDefault();
		
		var id_pedido = $("#id_pedido").attr("value");
		var id_cliente = $("#id_cliente").attr("value");
		/*if($.cookie("sn_idioma") == "es"){
			var msj = "Se eliminarÃ¡n todos los productos de la cesta. Â¿EstÃ¡ seguro?";
		} else {
		*/	var msj = "Do you want to clear your basket?";			
		//}
		
		if(confirm(msj)){
			$("#cesta_menu_Wrapper").load("Content/Scripts/ajax_tienda.php?accion=vaciar_cesta&id_pedido="+id_pedido,function(){
				$("#cesta_resumen_Wrapper").load("Content/Scripts/ajax_tienda.php?accion=cesta_resumen&id_cliente="+id_cliente);
				$(this).load("Content/Scripts/ajax_tienda.php?accion=ver_cesta_status&id_cliente="+id_cliente,function(){
					if($("#accion").attr("value") == "formalizar"){
						document.location = "index.php?seccion=tienda";
					} else {
						$("#cesta_menu_Wrapper").fadeOut();
					}
				});				
			});
		}		
	});
	
	$("#b_submit_cesta").live("click",function(e){
		e.preventDefault();
		document.location = "index.php?seccion=tienda&accion=formalizar";
	});
	
	$("#tabla_compra .b_quitar_item").click(function(e){
		e.preventDefault();
		/*if($.cookie("sn_idioma") == "es"){
			var msj = "¿Deseas quitar el producto de la cesta?";
		} else {
		*/	var msj = "Do you want to remove that item?";
		//}
		if(confirm(msj)){
			document.location = $(this).attr("href");
		}
	});
		
	$("#b_recordar_password").live("click",function(e){
		e.preventDefault();
		/*if($.cookie("sn_idioma") == "es"){
			var msj = "Se va a generar una contraseÃ±a nueva para su cuenta que le serÃ¡ enviada a la direcciÃ³n de correo proporcionada para su usuario.\nÂ¿Quiere continuar?";
		} else {
		*/	var msj = "A new password will be send to your email address. Are you sure?";
		//}
		
		if($("#email_login").attr("value") != "" && $("#email_login").attr("value") != "user"){
			if(confirm(msj)){
				$.ajax({
					url:"Content/Scripts/ajax_tienda.php?accion=enviar_password&email="+$("#email_login").attr("value"),
					success: function(data){
						alert(data);
					}
				});
			}
		} else {
			/*if($.cookie("sn_idioma") == "es"){
				alert("Introduzca el correo electrónico de su cuenta para recibir un nuevo password");
			} else {
			*/	alert("You must enter a valid email address to receive your new password.");
			//}
		}
	});
	
	$("#b_submit_compra").live("click",function(e){
		e.preventDefault();
		if($(".radio_pago:checked").length == 0){
			/*if($.cookie("sn_idioma") == "es"){
				alert("Hay que seleccionar un tipo de pago.");
			} else {
			*/	alert("Please choose a payment method.");
			//}			
		} else if($(".radio_factura:checked").attr("value") == "1" && $("#nif").attr("value") == ""){
			/*if($.cookie("sn_idioma") == "es"){
				alert("Hay que indicar un NIF/CIF para la factura");
			} else {
			*/	alert("Please enter a NIF/CIF number.");
			//}
			
		} else {	
			/*if($.cookie("sn_idioma") == "es"){
				var msj = "Se va a procesar el pedido. Â¿EstÃ¡ seguro?";
			} else {
			*/	var msj = "Your order will be sent. Are you sure?";
			//}
			if(confirm(msj)){
				$("#compra_form").submit();
			}
		}
	
	});
	
	$("#b_cancelar_compra").live("click",function(e){
		e.preventDefault();
		var id_pedido = $("#id_pedido").attr("value");
		
		if(confirm("Are you sure?")){
			$("#msj_compra").load("Content/Scripts/ajax_tienda.php?accion=vaciar_cesta&id_pedido="+id_pedido,function(){
				document.location = "index.php?seccion=tienda";
			});
		}
	});
	
	$("#tabla_pedidos tbody tr").live("click",function(){
		var id_pedido = $(this).attr("id");
		document.location = "index.php?seccion=tienda&accion=ver_pedido&id_pedido="+id_pedido;
	});
	
	
	$(".radio_factura").click(function(){
		if($(this).attr("value") == "1"){
			$("#nif_Wrapper").slideDown();
		} else {
			$("#nif_Wrapper").slideUp();
			$("#nif").attr("value","");
		}
	});
	
	$("#tipo_envio").change(function(){
		var tipo = $(this).attr("value");
		document.location = "index.php?seccion=tienda&accion=formalizar&tipo_envio="+tipo;
	});

    
});
