//função para fade in / out de divs
function fadeOut(target,from) {
			$(target).animate({opacity: 'hide'}, 'fast', function() {
				if (jQuery.browser.msie)  
				this.style.removeAttribute('filter');			
				$(from).animate({opacity: 'show'}, 'fast', function() {
					//alert("done");
				})
			})	
};

function startLoader() {
	$('#slideshow').find('img').batchImageLoad({
			loadingCompleteCallback: myCallback,
			imageLoadedCallback: imageLoaded
	});
}

function myCallback() {
	
	$("#loading").hide();
	$("#slideshow").show().cycle();
}

function imageLoaded(elementsLoadedCount, totalImagesCount) {
	var percent = Math.round((elementsLoadedCount / totalImagesCount) * 100);
	$('#progressbar').progressbar('option', 'value', percent);
}

$(document).ready(function() {
	
	//a.fancybox class calls
	$("a.fancybox").fancybox({autoDimensions: true});
	
	$('#datepicker').datepicker({
					changeMonth: true,
					changeYear: true,
					dateFormat: 'yy-mm-dd',
					dayNames: ['Domingo', 'Segunda', 'Terça-Feira', 'Quarta-Feira', 'Quinta-Feira', 'Sexta-Feira', 'Sábado'],
					dayNamesMin: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sáb'],
					monthNamesShort: ['Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez']				
	});

	//chama o fadeout
	$('.fadein').click(function() {
		fadeOut("#photo","#sendform");
		return false;
	 });
	$('.fadeout').click(function() {
		fadeOut("#sendform","#photo");
		return false;
	});

	
	
	//Menu lateral e do horizontal
	$('.nav').superfish({speed:0,delay:0});
	$('.navhor').superfish({speed:0,delay:0});
	
	//Jquery treeview
	$("#tree1").treeview({
			collapsed: true,
			animated: "medium",
			control:"#sidetreecontrol1",
			persist: "location"
	});
	$("#tree2").treeview({
			collapsed: true,
			animated: "medium",
			control:"#sidetreecontrol2",
			persist: "location"
	});
	$("#tree3").treeview({
			collapsed: true,
			animated: "medium",
			control:"#sidetreecontrol3",
			persist: "location"
	});
	$("#tree4").treeview({
			collapsed: true,
			animated: "medium",
			control:"#sidetreecontrol4",
			persist: "location"
	});
	
	
	//mapa das juntas de freguesia
	$(".mapBt").click(function () {
		var pos = $(this).attr('alt');
		var prev = $.cookie('last');
		
		if (prev) {
		  $('.info').hide("slow");
	
		}
		
		$("#"+pos).show("slow");
		$.cookie('last', pos);
			
		  
	});
	
	//Abre a pesquisa
	$("#openSearch").click(function () {
		  $("#searchbox").toggle();
	 });
	 
	// zebra tables
	$('tr:odd').addClass('odd');
	
		

//news ticker ----------------------
	$.ajax({
		type: "GET",
		url: "../no_access/destaques.php",
		dataType: "xml",
		success: function(xml){
                  $(xml).find("newstext").each(function(){
                        $("#ticker ul").append('<li><a href="'+$(this).find("url").text()+'">'+ $(this).find("title").text() + "</a></li>");
                  });
				  
				   var $newscycle = $('#ticker ul');
				   $newscycle.cycle({ cleartype:  0});
               }
  });
//news ticker ----------------------
//header slideshow ------------------------------------------------------------
	$.getJSON('../read_folder_json.php', function(data) {
												  
    $('#slideshow').empty();
	
		var total = data.images.length - 1;
	
		$.each(data.images, function(i, item) {
			 
			$("#slideshow").append('<img src="../images/banners/' + item + '" />');
			
			if (i == total) {
				
				startLoader();
			}
		});
	});
//header slideshow ------------------------------------------------------------

});