// JavaScript Document

function loadConfiguracoes(data) {
	$.alerts._show("Configurações", data);
	var form = "#form-veiculo";
	
	$(form+" .tab").hide();
	$(form+" #tab1").show();
	$.alerts._reposition();
	
	$(form+" .tabs a").bind("click", function() {
		$(form+" .tabs a").removeClass("active");
		$(this).addClass("active");
		curr = $(this).attr("href");
		$(form+" .tab").hide();
		$(form+" "+curr).show();
		$.alerts._reposition();
		return false;
	}).bind("mouseenter", function() {
		$(this).addClass("enter");
	}).bind("mouseleave", function() {
		$(this).removeClass("enter");
	});
	
	/*$(form+" #mudar-senha").bind("click", function() {
		senha = $("#password").val();
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"data" : {
				"page" : "admin-config",
				"ajax" : "yes",
				"action" : "changePassword",
				"txt-password" : senha
			},
			"beforeSend" : putLoading,
			"success" : function(data) {
				removeLoading();
				$.alerts.alert("Senha trocada com sucesso!");
			}
		});
		return false;
	});*/
	
	$(form+" #mudar-config").bind("click", function() {
		resultados = $("#resultados").val();
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"data" : {
				"page" : "admin-config",
				"ajax" : "yes",
				"action" : "changeConfig",
				"txt-resultPerPage" : resultados
			},
			"beforeSend" : putLoading,
			"success" : function(data) {
				removeLoading();
				$.alerts.alert("Informações atualizadas com êxito!");
			}
		});
		return false;
	});
	
	setConfigButtons("2", "condicao", form);
	setConfigButtons("3", "transmissao", form);
	setConfigButtons("4", "combustivel", form);
	setConfigButtons("5", "cor", form);
	setConfigButtons("6", "caracteristica", form);
	
	/*TIPO*/
	$(form+" #adicionar-tipo").bind("click", function() {
		tipo = $("#tab7 #tipo").val();
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"data" : {
				"page" : "admin-config",
				"ajax" : "yes",
				"action" : "add-tipo",
				"txt-tipo" : tipo
			},
			"beforeSend" : putLoading,
			"success" : function(data) {
				removeLoading();
				$("#tab7 .body ul").append('<li><label><a href="'+data+'" class="selecionar-tipo">Editar '+tipo+'</a></label> <a href="'+data+'" class="deletar-tipo">Deletar</a></li>');
			}
		});
		return false;
	});
	$(form+" #tab7 .body .selecionar-tipo").live("click", function() {
		tipoId = $(this).attr("href");
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"data" : {
				"page" : "admin-config",
				"ajax" : "yes",
				"view" : "marca-e-nome",
				"tipoId" : tipoId
			},
			"beforeSend" : putLoading,
			"success" : function(data) {
				removeLoading();
				$("#form-veiculo #tab8").remove();
				$("#form-veiculo").append('<div id="tab8" class="tab">'+data+'</div>');
				$(form+" .tab").hide();
				$(form+" #tab8").show();
				$.alerts._reposition();
			}
		});
		return false;
	});
	$(form+" #tab7 .body .deletar-tipo").live("click", function() {
		conf = confirm("deseja mesmo apagar?");
		if (!conf) return false;
		tipoId = $(this).attr("href");
		$this = $(this);
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"$this" : $this,
			"data" : {
				"page" : "admin-config",
				"ajax" : "yes",
				"action" : "edit-tipo",
				"submit" : "Deletar",
				"tipoId" : tipoId
			},
			"beforeSend" : putLoading,
			"success" : function() {
				removeLoading();
				this.$this.parent().remove();
			}
		});
		return false;
	});
	
	/*MARCA*/
	$(form+" #adicionar-marca").live("click", function() {
		marca = $("#tab8 #marca").val();
		tipoId = $("#tab8 .head .tipoId").val();
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"data" : {
				"page" : "admin-config",
				"ajax" : "yes",
				"action" : "add-marca",
				"txt-marca" : marca,
				"tipoId" : tipoId
			},
			"beforeSend" : putLoading,
			"success" : function(data) {
				removeLoading();
				$("#tab8 .body li ul").remove();
				$("#tab8 .body ul").append('<li class="'+data+'"><label><a href="'+data+'" class="selecionar-marca">'+marca+'</a></label> <a href="'+data+'" class="adicionar-nome">Adicionar Nome de Carro</a> <a href="'+data+'" class="deletar-marca">Deletar</a></li>');
			}
		});
		return false;
	});
	$(form+" #tab8 .body .selecionar-marca").live("click", function() {
		marcaId = $(this).attr("href");
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"marcaId" : marcaId,
			"dataType" : "json",
			"data" : {
				"page" : "admin-veiculo",
				"ajax" : "yes",
				"action" : "ajaxGetNome",
				"marcaId" : marcaId
			},
			"beforeSend" : putLoading,
			"success" : function(data) {
				removeLoading();
				$("#tab8 .body li."+this.marcaId+" ul").remove();
				$("#tab8 .body li."+this.marcaId).append("<ul></ul>");
				$.each(data, function() {
					$("#tab8 .body li."+this.marcaId+" ul").append('<li><label>'+this.nome+'</label> <a href="'+this.id+'" class="deletar-nome">Deletar</a></li>');
				});
			}
		});
		return false;
	});
	$(form+" #tab8 .body .deletar-marca").live("click", function() {
		conf = confirm("deseja mesmo apagar?");
		if (!conf) return false;
		
		marcaId = $(this).attr("href");
		$this = $(this);
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"$this" : $this,
			"data" : {
				"page" : "admin-config",
				"ajax" : "yes",
				"action" : "edit-marca",
				"submit" : "Deletar",
				"marcaId" : marcaId
			},
			"beforeSend" : putLoading,
			"success" : function() {
				removeLoading();
				this.$this.parent().remove();
			}
		});
		return false;
	});
	
	/*NOME*/
	$(form+" .adicionar-nome").live("click", function() {
		var $this = $(this);
		var marcaId = $this.attr("href");
		response = prompt("Digite o nome do Carro", "");
			if (response !== null) {
				nome = response;
				tipoId = $("#tab8 .head .tipoId").val();
				marcaId = marcaId;
				$.ajax({
					"type" : "post",
					"url" : "/ajax.php",
					"data" : {
						"page" : "admin-config",
						"ajax" : "yes",
						"action" : "add-nome",
						"txt-nome" : nome,
						"tipoId" : tipoId,
						"marcaId" : marcaId
					},
					"beforeSend" : putLoading,
					"success" : function(data) {
						removeLoading();
						$this.prev().click();
					}
				});
			}
		return false;
	});
	$(form+" #tab8 .body .deletar-nome").live("click", function() {
		conf = confirm("deseja mesmo apagar?");
		if (!conf) return false;
		
		nomeId = $(this).attr("href");
		$this = $(this);
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"$this" : $this,
			"data" : {
				"page" : "admin-config",
				"ajax" : "yes",
				"action" : "edit-nome",
				"submit" : "Deletar",
				"nomeId" : nomeId
			},
			"beforeSend" : putLoading,
			"success" : function() {
				removeLoading();
				this.$this.parent().remove();
			}
		});
		return false;
	});
	
}

function setConfigButtons(passo, name, form) {
	
	$(form+" #adicionar-"+name).bind("click", function() {
		novo = $("#"+name).val();
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"data" : {
				"page" : "admin-config",
				"ajax" : "yes",
				"action" : "add-"+name,
				"txt-novo" : novo
			},
			"beforeSend" : putLoading,
			"success" : function(data) {
				removeLoading();
				$("#tab"+passo+" .body").append('<form>'+novo+'<input type="submit" name="submit" value="Deletar" myId="'+data+'" class="'+name+'-deletar" /></form>');
			}
		});
		return false;
	});
	
	$(form+" ."+name+"-deletar").live("click", function() {
		conf = confirm("deseja mesmo apagar?");
		if (!conf) return false;
		
		$this = $(this);
		myId = $(this).attr("myId");
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"data" : {
				"page" : "admin-config",
				"ajax" : "yes",
				"action" : "edit-"+name,
				"submit" : "Deletar",
				"myId" : myId
			},
			"beforeSend" : putLoading,
			"success" : function(data) {
				removeLoading();
				$this.parent().remove();
			}
		});
		return false;
	});
	
}
