
//--------------------------------------------------------------------------------------------
// função para encontrar um valor em um array
//--------------------------------------------------------------------------------------------
function in_array(valor,array){
	//alert('valor:'+valor);
	for(var i in array){
		//alert('valor no array:'+array[i]);
		if(array[i] == valor){
			return true;
		}
	}
	return false;
}

//--------------------------------------------------------------------------------------------
// função para alterar o tamanho do conteudo e alinhar o rodapé
//--------------------------------------------------------------------------------------------
function resizeContent(){
	var window_h = $(window).height();
	
	var h_out = $('#sup-wrapper').height() + $('#bottomshadow').height() + $('#footer').height();
	
	$('#container-wrapper').removeAttr('style');
	
	var page_h = h_out + $('#container-wrapper').height();
	
	if(page_h < window_h){
		var h_to_go = window_h - h_out;
		//$('#container-wrapper').animate({ height: h_to_go+'px' }, 250);
		$('#container-wrapper').height(h_to_go);
	}
	else{
		//$('#container-wrapper').animate({ height: $('#container-wrapper').height()+'px' }, 250);
		$('#container-wrapper').height($('#container-wrapper').height());
	}
}

//--------------------------------------------------------------------------------------------
// função para alternar para a seção do calendário de eventos e carregar os eventos da data
//--------------------------------------------------------------------------------------------
function goToEvents(date){
	$("h1.active").next('ul').slideUp('fast',function(){
		$('h1.active').removeClass('active');
		$('#Novidades h1').addClass('active');

		$('#Novidades h1').next('ul').slideDown('medium',function(){
			$('li.active').removeClass('active');
			$('#calendariodeeventos').addClass('active');
			
			$(".content").hide();
			$("#calendariodeeventos-content").fadeIn('fast',function(){
				resizeContent();
			});

			$("#calendario-eventos").datepicker( "setDate" , date )
		});
	});
	
	getDayEvents(date);
}

//--------------------------------------------------------------------------------------------
// função para verificar os eventos da data
//--------------------------------------------------------------------------------------------
var eventDays = [];
function checkMonthEvents(month,year){
	$.ajax({
		async:	false,
		type:	"POST",
		url:	"ci/index.php/eventos/getMonthEventDays/"+month+'/'+year,
		success:
			function(dateArray){
				eval(dateArray);
			}
	});
}

//--------------------------------------------------------------------------------------------
// função para carregar os eventos da data
//--------------------------------------------------------------------------------------------
function getDayEvents(date){
	$.ajax({
		type:	"POST",
		url:	"ci/index.php/eventos/getDayEvents/"+date,
		success:
			function(events){
				$('#eventos').html(events);
			}
	});
}

//============================================================================================

$(document).ready(function(){
	
	var currentTime = new Date();
	checkMonthEvents(currentTime.getMonth()+1,currentTime.getFullYear()); //mes +1 pq começa no zero
	
	//--------------------------------------------------------------------------------------------
	// binder do resizeContent no resize da janela
	//--------------------------------------------------------------------------------------------
	$(window).resize(function(){
		resizeContent();
	});	
	
	//--------------------------------------------------------------------------------------------
	// menu - accordion
	//--------------------------------------------------------------------------------------------
	$("#main-menu dl dd h1").click(function(){
		if(!$(this).hasClass('active')){
			var h1 = this;
			
			$("h1.active").next('ul').slideUp('fast',function(){
				$('h1.active').removeClass('active');
				$(h1).addClass('active');
			
				$(h1).next('ul').slideDown('medium',function(){
					resizeContent();
				});
			});
		}
	});

	//--------------------------------------------------------------------------------------------
	// menu - alterar conteudo
	//--------------------------------------------------------------------------------------------
	$("#main-menu dl dd ul li").click(function(){
		if(!$(this).hasClass('active')){
			$('li.active').removeClass('active');
			$(this).addClass('active');
			
			//--
			var activesection	= $(this).parents('dd').children('h1').html();
			var activecontent	= $(this).html();
			$("#actualposition").html(activesection+' > '+activecontent);
			//--
			
			$(".content").hide();
			if($("#"+this.id+"-content").hasClass('content')){
				$("#"+this.id+"-content").fadeIn('fast',function(){
					resizeContent();
				});
			}
			else{
				$("#no-content").fadeIn('fast',function(){
					resizeContent();
				});
			}
		}
	});

	//--------------------------------------------------------------------------------------------
	// fancybox - fotos
	//--------------------------------------------------------------------------------------------
	$(".fancybox").fancybox();
	
	//--------------------------------------------------------------------------------------------
	// calendarios
	//--------------------------------------------------------------------------------------------
	//...........................................
	// home
	//...........................................
	$('#calendario-home').datepicker({
		monthNames:		['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'],
		dayNamesMin:	['D','S','T','Q','Q','S','S'],
		dateFormat:		'yy-mm-dd',
		inline:			true,
		
		onChangeMonthYear:
			function (year, month) {
				checkMonthEvents(month,year);
			},
		beforeShowDay:
			function (date) {
				var dia		= (''+date.getDate()).length == 1 ? '0'+date.getDate() : ''+date.getDate();
				var mes		= date.getMonth()+1; //+1 pq começa no zero
					mes		= (''+mes).length == 1 ? '0'+mes : ''+mes;
				
				var data	= date.getFullYear()+'-'+mes+'-'+dia;
				
				return [in_array(data,eventDays)];
			},
		onSelect:
			function (date) {
				goToEvents(date);
			}
	});
	
	//...........................................
	// eventos
	//...........................................
	$('#calendario-eventos').datepicker({
		monthNames:		['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'],
		dayNamesMin:	['D','S','T','Q','Q','S','S'],
		dateFormat:		'yy-mm-dd',
		inline:			true,
		
		onChangeMonthYear:
			function (year, month) {
				checkMonthEvents(month,year);
			},
		beforeShowDay:
			function (date) {
				var dia		= (''+date.getDate()).length == 1 ? '0'+date.getDate() : ''+date.getDate();
				var mes		= date.getMonth()+1; //+1 pq começa no zero
					mes		= (''+mes).length == 1 ? '0'+mes : ''+mes;
				
				var data	= date.getFullYear()+'-'+mes+'-'+dia;
				
				return [in_array(data,eventDays)];
			},
		onSelect:		function(date) {
			getDayEvents(date);
		}
	});
	
	$('#ui-datepicker-div').hide();
	
	
	//--------------------------------------------------------------------------------------------
	//resize do conteudo para alinhar o rodape
	//--------------------------------------------------------------------------------------------
	resizeContent();
	
	$(function() {
		$( "#datepicker" ).datepicker({
			numberOfMonths: 1,
			monthNames:		['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'],
			dayNamesMin:	['D','S','T','Q','Q','S','S'],
			dateFormat:		'yy-mm-dd'
		});
	});
	
});
	

