$(document).ready(function(){
	var register = false;
	var pass = false;
	var info = $("#userContent").html();
	var currentUserLink = $("#dummy");

	$("#email").focus();
	$("#userMenu").slideDown("normal");
	
	if(validate == true){
		$("#validationRow").toggle("normal");
		$("#welcomeSpan").html("<p>Haz click <a href='php/sendValidation.php?email="+email+"' id='sendMail' title='Re-enviar email'>aqui</a> para que te re-enviemos tu clave de validación");
		$("#userInfo").animate({height:'64'},500,"easein");
	};
	
	$("#registrate").click(function(e){
		e.preventDefault();
		if(register){
			$("#registerForm").slideUp("normal");
			register = false;
		}else{
			$("#registerForm").slideDown("normal");	
			register = true;
		}
	});
	// $('#loginForm').submit(function(e){
		// e.preventDefault();
		// $("#loadingCircle").html("<img src='img/loadingCircle.gif' alt='cargando'/>");
		// $("#loadingCircle").show();
		// $.post('php/valLogin.php',{email: $("#email").val(),pass: $("#pass").val(),validation: $("#validation").val()},function(datos){
			// $("#loadingCircle").hide();
			// switch(datos){
				// case "validar":
					// if(!validate){
						// $("#validationRow").toggle("normal");
						// $("#welcomeSpan").html("<p>No haz validado tu cuenta, haz click <a href='php/sendValidation.php?email="+ $("#email").val()+"' id='sendMail' title='Re-enviar email'>aqui</a> para que te re-enviemos tu clave de validación");
						// $("#welcomeSpan").addClass("error");
						// $("#validation").addClass("wrong");
						// $("#userInfo").animate({height:'64'},500,"easein");
						// $("#registerForm").slideUp("normal");	
						// validate = true;
						// register = false;
					// }else{
						// $("#welcomeSpan").html("<p>No haz validado tu cuenta, haz click <a href='php/sendValidation.php?email="+ $("#email").val()+"' id='sendMail' title='Re-enviar email'>aqui</a> para que te re-enviemos tu clave de validación</p>");
						// $("#welcomeSpan").addClass("error");
						// $("#validation").addClass("wrong");
					// }
				// break;
				// case "si":
					// document.location.reload();
				// break;
				// case "usuario":
					// $("#email").addClass("wrong");
					// $("#welcomeSpan").html("<p>No tenemos ese usuario en nuestro sistema</p>");
					// $("#welcomeSpan").addClass("error");
					// $("#email").focus();
					// $("#registrate").addClass("suggest");
				// break;
				// case "pass":
					// $("#email").removeClass("wrong");
					// $("#validationRow").hide();
					// $("#pass").focus();
					// $("#pass").val("");
					// $("#pass").addClass("wrong");
					// $("#welcomeSpan").html("<p>Contraseña incorrecta. <a href='?forgotPass="+$("#email").val()+"'>¿Olvidaste tu contraseña?</a></p>");
					// $("#welcomeSpan").addClass("error");
					// $("#registrate").addClass("suggest");
				// break;
				// case "confPass":
					// document.location = 'index.php';
				// break;
				// default:
					// alert(datos);
					// alert("error desconosido intente otra vez");
				// break;
			// }
		// });
	// });
	$(".category").click(function(e){
		e.preventDefault();
		$(this).next().toggle('fast');
	});
	$(".category, .userLink,.producto").hover(function(e){
		document.body.style.cursor='pointer';
	},function(e){
		document.body.style.cursor='auto';
	});
	$(".userLink").click(function(e){
		function showUserContent(link){
			if(link.attr("id") == "info"){
				$("#userEdit").slideDown("fast");
			}else if(link.attr("id") == "cart"){
				$("#cartDiv").slideDown("fast");
			}else{
				$.post("user/"+link.attr("id")+".php",function(datos){
					$("#userEdit2").html(datos);
					$("#userEdit2").slideDown("fast");
				});			
			}
		}
		e.preventDefault();
		$(this).toggleClass("userLinkCurrent");
		$(this).toggleClass("userLink");
		currentUserLink.removeClass("userLinkCurrent");
		currentUserLink.addClass("userLink");
		if(currentUserLink.attr("id") != "dummy"){
			if(currentUserLink.attr("id") == $(this).attr("id")){
				if(currentUserLink.attr("id") == "info"){
					$("#userEdit").slideUp("fast");
					$("#userEditForm").slideUp("fast")
				}else if(currentUserLink.attr("id") == "cart"){
					$("#cartDiv").slideUp("fast");
				}else{
					$("#userEdit2").slideUp("fast");
				}
				currentUserLink = $("#dummy");
			}else{
				if(currentUserLink.attr("id") == "info"){
					$("#userEditForm").slideUp("fast");
					$("#userEdit").slideUp("fast",showUserContent($(this)));
				}else if(currentUserLink.attr("id") == "cart"){
					$("#cartDiv").slideUp("fast",showUserContent($(this)));
				}else{
					$("#userEdit2").slideUp("fast",showUserContent($(this)));
				}
				currentUserLink = $(this);
			}
		}else{
			showUserContent($(this));
			currentUserLink = $(this);
		}
	});
	$(".subCat").click(function(e){
		var id = $(this).attr("id").substring(3);
		if($(this).parent(":has(.subList, .subListS)").length>0){
			e.preventDefault();
			$(this).parent().children('.subList, .subListS').toggle('fast');
		}
	});
	$('#catSearch').keyup(function(e){
		var searchString = $("#catSearch").val().capitalizeAll();
		var searchElements = '.subCat';
		if (searchString.length > 0){
			$(".subList, .activeCat").show();
			$(searchElements).hide();
			$(searchElements+':contains(' +searchString+ ')').show();
		}
		else {
			$(searchElements).show();
			$(".subList").hide();
		}
	});
	// $("#registerForm").submit(function(e){
		// e.preventDefault();
		// $("#loadingReg").html("<img src='img/loadingCircle.gif' alt='cargando'/>");
		// $("#loadingReg").show();
		// if($("#pass2").val() == $("#pass3").val()){
			// $.post("php/createUser.php",{
				// nombre: $("#nombre").val(),
				// apellidos: $("#apellidos").val(),
				// email1: $("#email1").val(),
				// ciudad: $("#ciudad").val(),
				// telefono: $("#telefono").val(),
				// calle:$("#calle").val(),
				// cp:$("#cp").val(),
				// pass2:$("#pass2").val()
			// },function(datos){
				// $("#loadingReg").hide();
				// switch(datos){
					// case "bien":
						// $("#registerForm").slideUp("normal");
						// $("#welcomeSpan").removeClass("error");
						// $("#welcomeSpan").html("<p>Se le a enviado un email con las instrucciónes para validar su cuenta</p>");
					// break;
					// case "email":
						// $("#welcomeSpan").addClass("error");
						// $("#welcomeSpan").html("<p>Ese email ya esta  registrado con nosotros</p>");
					// break;
					// case "vEmail":
						// $("#registerForm").slideUp("normal");
						// $("#welcomeSpan").addClass("error");
						// $("#welcomeSpan").html("<p>Error al mandar el mail de confirmación</p>");
					// break;
					// case "error":
						// $("#welcomeSpan").addClass("error");
						// $("#welcomeSpan").html("<p>Error desconosido intente otra vez</p>");
					// break;
					// default:
						// $("#welcomeSpan").addClass("error");
						// $("#welcomeSpan").html("<p>Error desconosido intente otra vez</p>");
					// break;
				// };
			// });
		// }else{
			// $("#pass2").addClass("wrong");
			// $("#pass3").addClass("wrong");
			// $("#pass2").val("");
			// $("#pass3").val("");
			// $("#welcomeSpan").html("<p>No coinciden las contraseñas</p>");
		// }
	// });
	$(".unlockInfo").click(function(e){
		e.preventDefault();
		$("#userEdit").hide();
		$("#userEditForm").show();
	});
	$(".lockInfo").click(function(e){
		e.preventDefault();
		$("#userEdit").show();
		$("#userEditForm").hide();
	});
	$("#updateUserForm").submit(function(e){
		e.preventDefault();
		$.post("user/update.php",{
			nameUp:$("#nameUp").val(),
			apellidoUp:$("#apellidoUp").val(),
			emailUp:$("#emailUp").val(),
			calleUp:$("#calleUp").val(),
			cpUp:$("#cpUp").val(),
			ciudadUp:$("#ciudadUp").val()
		},function(datos){
			
			switch(datos){
				case "bien":
					document.location = "index.php?update=t";
				break;
				case "error":
					document.location = "index.php?=f";
				break;
				case "logout":
				
				break;
				default:
				
				break;
			}
		});
	});
	$(".addCart").click(function(e){
		e.preventDefault();
		$("#addToCartForm").toggle("fast");
	});
	$("#logout").click(function(e){
		e.preventDefault();
		$.post("user/logout.php",function(datos){
			switch(datos){
				case "yes":
					document.location.reload();
				break;
				default:
				
				break;
			}
		});
	});
	$(".updateItem").submit(function(e){
		e.preventDefault();
		var quan = $(this).children().children('.quantity').val();
		$(this).parent().children('.loadingUpCart').html("<img src='img/loadingCircle.gif' alt='loading' />");
		$.post($(this).attr("action"),{
			quantity: quan
		},function(datos){
			$(this).parent().children('.loadingUpCart').html("");
			switch(datos){
				case "yes":
					document.location = document.location;
				break;
				case "no":
					alert(datos);
				break;
				default:
					alert(datos);
				break;
			}
		});
	});
	$(".destroyItem").click(function(e){
		e.preventDefault();
		var loadingImage = $(this).parent().children('.loadingDestroy');
		var quan = $(this).children('.quantity').val();
		loadingImage.html("<img src='img/loadingCircle.gif' alt='loading' />");
		$.post($(this).attr("href"),function(datos){
			loadingImage.html("");
			switch(datos){
				case "yes":
					document.location.reload();
				break;
				case "no":
					alert("error");
				break;
				case "user":
					document.location.reload();
				break;
				default:
				
				break;
			}
		});
	});
	$("#addToCartForm").submit(function(e){
		e.preventDefault();
		var quan = $('#quantity').val()
		$('#loadingUpCart').html("<img src='img/loadingCircle.gif' alt='loading' />");
		$.post($("#addToCartForm").attr("action"),{
			quantity: quan
		},function(datos){
			$('#loadingUpCart').html("");
			switch(datos){
				case "yes":
					document.location = document.location+"&cart=t";
				break;
				case "no":
				
				break;
				default:
					
				break;
			}
		});
	});
	
});