// JavaScript Document


$(document).ready(function() {

	$(".adicionar-veiculo").bind("click", function() {
		var href = $(this).attr("href");
		var qs = getQueryStringFromText(href);
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"data" : {
				"page" : "admin-veiculo",
				"view" : "adicionar"
			},
			"beforeSend" : putLoading,
			"success" : function(data) {
				removeLoading();
				loadAddEditVeiculo(data, "#form-veiculo", "Adicionar Veículo");
				setButtonsAdicionar("#form-veiculo");
			}
		});
		return false;
	});
	
	$(".abrir-filial").bind("click", function() {
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"data" : {
				"page" : "admin-filial",
				"ajax" : "yes",
				"view" : "listar"
			},
			"beforeSend" : putLoading,
			"success" : function(data) {
				removeLoading();
				loadFilial(data);
			}
		});
		return false;
	});
	
	$(".abrir-config").bind("click", function() {
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"data" : {
				"page" : "admin-config",
				"view" : "all"
			},
			"beforeSend" : putLoading,
			"success" : function(data) {
				removeLoading();
				loadConfiguracoes(data);
			}
		});
		return false;
	});
	
	$(".marcar-destaque").live("click", function() {
		qs = getQueryStringFromText($(this).attr("href"));
		$this = $(this);
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"data" : {
				"page" : "admin-veiculo",
				"action" : "toggleFav",
				"veiculoId" : qs['veiculoId']
			},
			"curr" : $this,
			"beforeSend" : putLoading,
			"success" : function() {
				$("#admin-veiculo").trigger("prepareUpdate");
				removeLoading();
				if (this.curr.attr("destaque") == "1") {
					this.curr.attr("destaque", "0").html("<img src=\"/imagens/admin/desmarcado.jpg\" />");
				}
				else {
					this.curr.attr("destaque", "1").html("<img src=\"/imagens/admin/marcado.jpg\" />");
				}
				$("#admin-veiculo").trigger("updateAll");
			}
		});
		return false;
	});
	
	$(".editar-veiculo").live("click", function() {
		qs = getQueryStringFromText($(this).attr("href"));
		$this = $(this);
		$.ajax({
			"type" : "post",
			"url" : "/ajax.php",
			"data" : {
				"page" : "admin-veiculo",
				"view" : "detalhes",
				"veiculoId" : qs['veiculoId']
			},
			"beforeSend" : putLoading,
			"success" : function(data) {
				removeLoading();
				loadAddEditVeiculo(data, "#form-veiculo", "Editar Veículo");
				setButtonsEditar("#form-veiculo");
			}
		});
		return false;
	});
	
	
	$("#admin-veiculo").tablesorter({
		"headers" : {
			0 : {
				"sorter" : false
			}
		}
	}).tablesorterPager({
		"container" : $("#pager"),
		"matchTR" : 8
	});
});


function putLoading() {
	$("body").append("<div class=\"loading\">Carregando...</div>");
}
function removeLoading() {
	$(".loading").remove();
}
