// JavaScript Document
function loadAddEditVeiculo(data, form, title) {
	$.alerts._show(title, data);
	
	$(form+" .tab").hide();
	$(form+" #tab1").fadeIn();
	$.alerts._reposition();
	
	$(form+" #tipo").bind("change", function() {	
		tipoId = $(this).val();
		$(form+" #marca option").remove();
		$(form+" #nome option").remove();
		$.ajax({
			"type" : "post",
			"url" : "ajax.php",
			"data" : {
				"page" : "admin-veiculo",
				"ajax" : "yes",
				"action" : "ajaxGetMarca",
				"tipoId" : tipoId
			},
			"dataType" : "json",
			"beforeSend" : putLoading,
			"success" : function(data) {
				removeLoading();
				$.each(data, function() {
					$(form+" #marca").append("<option value=\""+this.id+"\">"+this.marca+"</option>");
				});
				selected = $(form+" #marca").attr("marca");
				$(form+" #marca").val(selected);
				$(form+" #marca").trigger("change");
			}
		});
	}).trigger("change");
	
	$(form+" #marca").bind("change", function() {		
		marcaId = $(this).val();
		$(form+" #nome option").remove();
		$.ajax({
			"type" : "post",
			"url" : "ajax.php",
			"data" : {
				"page" : "admin-veiculo",
				"ajax" : "yes",
				"action" : "ajaxGetNome",
				"marcaId" : marcaId
			},
			"dataType" : "json",
			"beforeSend" : putLoading,
			"success" : function(data) {
				removeLoading();
				$.each(data, function() {
					$(form+" #nome").append("<option value=\""+this.id+"\">"+this.nome+"</option>");
				});
				selected = $(form+" #nome").attr("nome");
				$(form+" #nome").val(selected);
			}
		});
	});
	
	
	
	$(form+" #upload-fotos").fileUpload({
		"uploader" : "/lib/uploader.swf",
		"script" : "/ajax.php",
		"scriptData" : {
			"page" : "admin-veiculo",
			"action" : "ajaxAddFotos",
			"ajax" : "yes",
			"veiculoId" : $(form+" #hidd-veiculoId").val()
		},
		"multi" : true,
		"auto" : true,
		"displayData" : "percentage",
		"fileExt" : "*.jpg; *.jpeg; *.gif",
		"fileDesc" : "Arquivos de imagem",
		"simUploadLimit" : 2,
		"buttonText" : "Selecionar fotos",
		"onProgress" : function() {
			$("input:button").attr("disabled", "disabled");
		},
		"onComplete" : function(event, queueID, fileObj, response, data) {
			response = response.split(",");
			$(form+" #album-preview").append("<div class=\"uploaded-foto\"><input class=\"hidd-foto\" type=\"hidden\" name=\"fotos[]\" value=\""+response[0]+"\" /><label style=\"width: 92px\" for=\"img-"+response[0]+"\"><img width=\"92\" src=\"/userImagens/thumbs/150_"+response[1]+"\" /></label><input type=\"radio\" id=\"img-"+response[0]+"\" value=\""+response[0]+"\" name=\"radio-foto\" class=\"album-radio\" /></div>");
			$.alerts._reposition();
		},
		"onAllComplete" : function() {
			$("input:button").attr("disabled", "");
		}
	});
	
	$(form+" #album-preview label").live("click", function() {
		$(form+" #album-preview label").removeClass("albumImgSelected");
		$(this).addClass("albumImgSelected");
	});
	
	$(form+" #foto-deletar").bind("click", function() {
		$this = $(form+" #album-preview .album-radio:checked");
		id = $this.val();
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"data" : {
				"page" : "admin-veiculo",
				"ajax" : "yes",
				"action" : "deletarFoto",
				"fotoId" : id
			},
			"beforeSend" : putLoading,
			"success" : function(data) {
				removeLoading();
				if (data == "ok") {
					this.curr.parent().fadeOut(function() {
						$(this).remove();
					});
				}
				else {
					alert("error");
				}
			},
			"curr" : $this
		});
	});
}

function setButtonsEditar(form) {
	
	$(form+" #tab5 p.deletar input.deletar").bind("click", function() {
		conf = confirm("ISTO IRÁ DELETAR ESTE VEÍCULO PERMANENTEMENTE");
		if (!conf) return false;
		
		veiculoId = $(form+" #hidd-veiculoId").val();
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"data" : {
				"page" : "admin-veiculo",
				"ajax" : "yes",
				"action" : "edit-veiculo",
				"submit" : "Deletar",
				"veiculoId" : veiculoId
			},
			"beforeSend" : putLoading,
			"success" : function(data) {
				removeLoading();
				if (data == "ok") {
					$("#admin-veiculo").trigger("prepareUpdate");
					$("#admin-veiculo tr#"+veiculoId).remove();
					$("#admin-veiculo").trigger("updateAll");
					$.alerts._hide();
				}
				else {
					alert("error");
				}
			}
		});
	});
	
	$(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+" input.cancelar").bind("click", function() {
		$.alerts._hide();
	});
	
	$(form+" input.ok").bind("click", function() {
		tipo = $(form+" #tipo option:selected").html();
		marca = $(form+" #marca option:selected").html();
		nome = $(form+" #nome option:selected").html();
		filial = $(form+" #filial").val();
		filialName = $(form+" #filial option:selected").html();
		
		fotoId = $(form+" .album-radio:checked").val();
		imgName = $(form+" .albumImgSelected").children().attr("src");
		
		placa = $(form+" #placa").val();
		modelo = $(form+" #modelo").val();
		condicao = $(form+" #condicao").val();
		ano = $(form+" #ano").val();
		quilometros = $(form+" #quilometros").val();
		preco = $(form+" #preco").val();
		desconto = $(form+" #desconto:checked").val();
		combustivel = $(form+" #combustivel").val();
		cor = $(form+" #cor").val();
		portas = $(form+" #portas").val();
		direcao = $(form+" #direcao").val();
		motor = $(form+" #motor").val();
		cambio = $(form+" #cambio").val();
		velocidade = $(form+" #velocidade").val();
		caracteristica = "";
		$(form+" .caracteristica:checked").each(function() {
			caracteristica += $(this).val()+"::";
		});
		
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"data" : {
				"page" : "admin-veiculo",
				"action" : "edit-veiculo",
				"submit" : "Mudar",
				"ajax" : "yes",
				"veiculoId" : $(form+" #hidd-veiculoId").val(),
				"txt-placa" : placa,
				"sel-tipo" : tipo,
				"sel-marca" : marca,
				"sel-nome" : nome,
				"txt-modelo" : modelo,
				"sel-condicao" : condicao,
				"txt-ano" : ano,
				"txt-quilometro" : quilometros,
				"txt-preco" : preco,
				"sel-combustivel" : combustivel,
				"sel-cor" : cor,
				"sel-porta" : portas,
				"sel-direcao" : direcao,
				"txt-motor" : motor,
				"sel-transmissao" : cambio,
				"txt-velocidade" : velocidade,
				"sel-filial" : filial,
				"radio-foto" : fotoId,
				"chk-desconto" : desconto,
				"caracteristica" : caracteristica
			},
			"beforeSend" : putLoading,
			"dataType" : "json",
			"success" : function(data) {
				$("#admin-veiculo").trigger("prepareUpdate");
				removeLoading();
				$this = $("#"+data.id);
				flagDestaque = (data.destaque == "1") ? "1" : "0";
				destaque = (data.destaque == "1") ? "<img src=\"/imagens/admin/marcado.jpg\" />" : "<img src=\"/imagens/admin/desmarcado.jpg\" />";
				$this.html("<td><img height=\"80\" src=\""+imgName+"\" /></td> <td>"+filialName+"</td> <td>"+condicao+"</td> <td>"+tipo+"</td> <td>"+marca+"</td> <td>"+nome+"</td> <td>"+modelo+"</td> <td>"+ano+"</td> <td>"+placa+"</td> <td>$"+preco+"</td> <td><a destaque=\""+flagDestaque+"\" class=\"marcar-destaque\" href=\"/admin.php?page=admin-veiculo&action=toggleFav&veiculoId="+data.id+"\">"+destaque+"</a><a class=\"editar-veiculo\" href=\"/admin.php?page=admin-veiculo&view=detalhes&veiculoId="+data.id+"\"><img src=\"/imagens/admin/editar.jpg\" /></a></td>");
				$.alerts.alert("Veículo editado com sucesso!", "OK");
				$("#admin-veiculo").trigger("updateAll");
			}
		});
		return false;
	});
}

function setButtonsAdicionar(form) {
	var curr = 1;
	
	$(form+" .next").bind("click", function() {
		curr++;
		$(form+" .tab").hide();
		$(form+" #tab"+curr).fadeIn();
		$.alerts._reposition();
	});
	$(form+" .prev").bind("click", function() {
		curr--;
		$(form+" .tab").hide();
		$(form+" #tab"+curr).fadeIn();
		$.alerts._reposition();
	});

	$(form+" #submit").bind("click", function() {
		tipo = $(form+" #tipo option:selected").html();
		marca = $(form+" #marca option:selected").html();
		nome = $(form+" #nome option:selected").html();
		filial = $(form+" #filial").val();
		filialName = $(form+" #filial option:selected").html();
		
		fotoId = $(form+" .album-radio:checked").val();
		imgName = $(form+" .album-radio:checked").prev().children().attr("src");
		
		placa = $(form+" #placa").val();
		modelo = $(form+" #modelo").val();
		condicao = $(form+" #condicao").val();
		ano = $(form+" #ano").val();
		quilometros = $(form+" #quilometros").val();
		preco = $(form+" #preco").val();
		desconto = $(form+" #desconto:checked").val();
		combustivel = $(form+" #combustivel").val();
		cor = $(form+" #cor").val();
		portas = $(form+" #portas").val();
		direcao = $(form+" #direcao").val();
		motor = $(form+" #motor").val();
		cambio = $(form+" #cambio").val();
		velocidade = $(form+" #velocidade").val();
		caracteristica = "";
		$(form+" .caracteristica:checked").each(function() {
			caracteristica += $(this).val()+"::";
		});
		fotos = "";
		$(form+" .hidd-foto").each(function() {
			fotos += $(this).val()+" ";
		});
		
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"data" : {
				"page" : "admin-veiculo",
				"action" : "adicionar",
				"ajax" : "yes",
				"txt-placa" : placa,
				"sel-tipo" : tipo,
				"sel-marca" : marca,
				"sel-nome" : nome,
				"txt-modelo" : modelo,
				"sel-condicao" : condicao,
				"txt-ano" : ano,
				"txt-quilometro" : quilometros,
				"txt-preco" : preco,
				"sel-combustivel" : combustivel,
				"sel-cor" : cor,
				"sel-porta" : portas,
				"sel-direcao" : direcao,
				"txt-motor" : motor,
				"sel-transmissao" : cambio,
				"txt-velocidade" : velocidade,
				"sel-filial" : filial,
				"radio-foto" : fotoId,
				"chk-desconto" : desconto,
				"caracteristica" : caracteristica,
				"fotos" : fotos
			},
			"beforeSend" : putLoading,
			"success" : function(data) {
				if (data == "error") {
					removeLoading();
					$.alerts.alert("O veículo não foi cadastrado!", "erro");
				}
				else {
					$("#admin-veiculo").trigger("prepareUpdate");
					removeLoading();
					$("#admin-veiculo tbody").append("<tr id=\""+data+"\"> <td><img height=\"80\" src=\""+imgName+"\" /></td> <td>"+filialName+"</td> <td>"+condicao+"</td> <td>"+tipo+"</td> <td>"+marca+"</td> <td>"+nome+"</td> <td>"+modelo+"</td> <td>"+ano+"</td> <td>"+placa+"</td> <td>$"+preco+"</td> <td><a destaque=\"0\" class=\"marcar-destaque\" href=\"/admin.php?page=admin-veiculo&action=toggleFav&veiculoId="+data+"\"><img src=\"/imagens/admin/desmarcado.jpg\" /></a><a class=\"editar-veiculo\" href=\"/admin.php?page=admin-veiculo&view=detalhes&veiculoId="+data+"\"><img src=\"/imagens/admin/editar.jpg\" /></a></td> </tr>");
					$.alerts.alert("Veículo cadastrado com sucesso!", "OK");
					$("#admin-veiculo").trigger("updateAll");
						
				}
			}
		});
		return false;
	});
}