function loads() {
	$('html').append("<div class='ui-loader2'>" +
		"<span class='ui-icon-loading'></span>" +
		"<h1></h1>" +
		"</div>");
}

function removes() {
	$('.ui-loader2').remove();	
}

function TrocaPagina(pageId) {
    $.mobile.changePage("#" + pageId, { transition: "slide" });
}

function geraLkp(lkp, dirLkp) {
	if (lkp.length <= 1) {
		$.ajaxSetup({
			beforeSend: loads,
			complete: removes
		});
		$.post(dirLkp, {}, function (data) {
			lkp.append(data);
		});
	}
}

function geraAutocomplete(id, dir, idfiltro, idcod) {
	$(id).on('filterablebeforefilter', function(e, data) {
		var ul = $(this)
			input = $(data.input),
			value = input.val();
		$(idcod).val("");
		if (value && value.length > 2) {
			$.ajaxSetup({
				beforeSend: loads,
				complete: removes
			});
			$.post(dir, 
				{
					filtro: value
				},
				function (res) {
					ul.html(res);
					ul.listview("refresh");
					ul.trigger("updatelayout");
				}
			);
		} else {
			ul.html("");
		}
	});
	
	$(document).on('click', id + ' li', function() {
		var item = $(this).text();
		var index = $(this).index();
		$(idcod).val($(id + ' li:eq(' + index + ') input').val());
		$(idfiltro).val(item);
		$(id).html("");
	})
}

function checkScroll(carregarDados, pag) {	
	var activePage = $.mobile.pageContainer.pagecontainer("getActivePage"),
		screenHeight = $.mobile.getScreenHeight(), 
		contentHeight = $(".ui-content", activePage).outerHeight(),
		header = $(".ui-header", activePage).outerHeight() - 1,
		scrolled = $(window).scrollTop(),
		footer = $(".ui-footer", activePage).outerHeight() - 1,
		scrollEnd = contentHeight - screenHeight + header + footer;

	if (activePage[0].id == pag && scrolled >= scrollEnd) {
		carregarDados();
	}

}

const qtdeCarregar = 50;

$.mobile.document

	.on('click', '.logout', function(){
		$.post('login/sair.php', {},
			function(){
				window.location.href = "login/login.php";
			}
		)
	})

	.on('pagebeforeshow', '.loading-show', function(e){
		loads;
	})

	.on('pageshow', '.loading-hide', function(){
		removes;
	})

	.on( "pagecreate", ".mobipicker, .mobipicker-month", function(e) {
		if(e.target.className.indexOf("mobipicker-month") > 0){
			var picker = $( "input[class='mobipick-month']", this );
	
			picker.mobipick({
				locale: 'pt-br',
				dateFormatMonth: 'MMMM - yyyy',
				accuracy: "month"
			});	
		}else{
			var picker = $( "input[class='mobipick']", this );
			// var picker = $( ".mobipick", this );
	
			picker.mobipick({
				locale: 'pt-br',
				dateFormat: 'dd - MMMM - yyyy'
	//			accuracy: "month"
			});
		}

		picker.on( "change", function() {
				var date = $( this ).val();

				// formatted date
				var dateObject = $( this ).mobipick( "option", "date" );
		});
	})

	;