$(document).ready(function() {	

	$(window).scroll(function () {
		var winH = $(window).height();
		var y_fixo = winH/2-$("#dialog_").height()/2;	
		$("#dialog_").animate({
			top: y_fixo+$(document).scrollTop()+"px"
			},{duration:500,queue:false}
		);
	});
	
	$('a[name=modal]').click(function(e) {
		$("#dialog_titulo").html("");
		window.location.href = $(this).attr('href');
		e.preventDefault();		
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();	
		$('#mask').css({'width':maskWidth,'height':maskHeight}); 
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);		
		//Get the window height and width
		var winW = $(window).width();              
		var winH = $(window).height();
		var y_fixo = winH/2-$("#dialog_").height()/2;	
		$("#dialog_").animate({
			top: y_fixo+$(document).scrollTop()+"px"
			},{duration:500,queue:false}
		);
		$("#dialog_").css('left', winW/2-$("#dialog_").width()/2);	
		$("#dialog_").fadeIn(2000); 	
	});
	
	$('.window .close').click(function (e) {
		e.preventDefault();		
		$('#mask').hide();
		$('#dialog_fotos').hide();
		$('.window').hide();
	});		
	
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
		$('#dialog_fotos').hide();
	});	


	$(".data").datepicker({
		dateFormat: 'dd/mm/yy',
		showOtherMonths: true,
		selectOtherMonths: true,
		dayNamesMin: ['D', 'S', 'T', 'Q', 'Q', 'S', 'S'],
		monthNames: ['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro']
	});	
	$(".data").mask('9?9/99/9999');	
	$(".num").mask('9?99');
});

function abrir(janela){
	$(janela).show(500);
}
function fechar(janela){
	$(janela).hide(500);
}

function album(id){
	$('html, body').css({'overflow':'hidden'}); 	
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();	
	$('#mask').css({'width':maskWidth,'height':maskHeight}); 
	$('#mask').fadeIn(1000);	
	$('#mask').fadeTo("slow",0.9);	

	var winH = $(window).height();
	var winW = $(window).width();  	
	$('#dialog_fotos').css('left', winW/2-$('#dialog_fotos').width()/2);	
	$('#dialog_fotos').css('height', winH-30);	
	$('#dialog_fotos').animate({
		top: "30px"
		},{duration:500,queue:false}
	);
	$('.dialog_foto_botao').css('top', winH/2-100);
	$('.dialog_foto_botao_l').css('left', winW/2-350);	
	$('.dialog_foto_botao_r').css('left', winW/2+320);	

	$('#dialog_fotos').fadeIn(2000);
	$("#dialog_foto_titulo").html("Carregando Álgum...");
	$("#dialog_foto_data").html("__/__/____");
	$("#dialog_foto_paginas").html("");	
	$('#dialog_foto').html("<div class=\"clear\" style=\"height:20px;\"></div><div style=\"float:left;font-weight:bold;margin:13px 0 0 1px;\">Carregando...</div>");
	$("#dialog_foto_dados").load("fotos_load.php?id="+id,function(){
		$("#dialog_foto_titulo").html($("#dialog_foto_dados #afotos_evento").html());
		$("#dialog_foto_data").html($("#dialog_foto_dados #afotos_perio").html());
		$("#dialog_foto_paginas").html($("#dialog_foto_dados #afotos_paginas").html());
		afoto(1);
	});	
}
function afoto(num){
	max = parseFloat($("#dialog_foto_dados #afotos_qtd").html());
	$('#dialog_numfoto').html(num);
	$('#dialog_foto').html("<div class=\"clear\" style=\"height:20px;\"></div><img src=\"imgs/carregando.gif\" style=\"float:left;margin-left:10px;\"> <div style=\"float:left;font-weight:bold;margin:13px 0 0 1px;\">Carregando...</div>");
	$('#dialog_foto').html("<img src='"+$("#dialog_foto_dados #afotos_url #ft"+num).html()+"'>");
	num = num+1;
	if (num > max){
		num = 1;
	}	
	$('#dialog_foto').html($('#dialog_foto').html()+"<img style=\"display:none\" src='"+$("#dialog_foto_dados #afotos_url #ft"+(num)).html()+"'>");
	num = num-2;
	if (num < 1){
		num = max;
	}	
	$('#dialog_foto').html($('#dialog_foto').html()+"<img style=\"display:none\" src='"+$("#dialog_foto_dados #afotos_url #ft"+(num)).html()+"'>");
}

function afotoacao(acao){
	num = parseFloat($('#dialog_numfoto').html());
	max = parseFloat($("#dialog_foto_dados #afotos_qtd").html());
	if (acao == "proxima"){
		num = num+1;
		if (num > max){
			num = 1;
		}
		afoto(num);
	}
	if (acao == "anterior"){
		num = num-1;
		if (num < 1){
			num = max;
		}
		afoto(num);
	}	
}

document.onkeyup=function(e){
	if ($('#dialog_fotos').attr('display') == 'none'){
		return false;
	}
	if (e.which == 37 || e.which == 38){
		afotoacao('anterior');
	}else if (e.which == 39 || e.which == 40){
		afotoacao('proxima');
	}
}

function passagem(pass,versao){
	$("#dialog_passlink").attr("href","biblia.php");
	$("#dialog_pass").html(pass);
	$("#dialog_conteudo").html("<div class=\"clear\" style=\"height:20px;\"></div><img src=\"imgs/carregando.gif\" style=\"float:left;margin-left:10px;\"> <div style=\"float:left;font-weight:bold;margin:13px 0 0 1px;\">Carregando...</div>");
	$("#dialog_dados").load("passagem.php?pass="+pass+"&versao="+versao,function(){
		$("#dialog_conteudo").html($("#dialog_dados #pass_conteudo").html());
		$("#dialog_titulo").html($("#dialog_dados #pass_livro").html());
		$("#dialog_passlink").attr("href","biblia.php?livro="+$("#pass_livroID").html()+"&capitulo="+$("#pass_capitulo").html()+"&versao="+versao);
	});
}

var URL = unescape(window.location.pathname);

function refresh(){
	window.location.href = URL;
}

function addFav(){
	var url      = "http://www.iasdmirandopolis.org";
	var title    = "Igreja Adventista do Sétimo Dia de Mirandópolis ";
	if (window.sidebar) window.sidebar.addPanel(title, url,"");
	else if(window.opera && window.print){
		var mbm = document.createElement('a');
		mbm.setAttribute('rel','sidebar');
		mbm.setAttribute('href',url);
		mbm.setAttribute('title',title);
		mbm.click();
	}
	else if(document.all){window.external.AddFavorite(url, title);}
}

function trim(str){return str.replace(/^\s+|\s+$/g,"");}

function replaceAll(string, token, newtoken) {
	while (string.indexOf(token) != -1) {
 		string = string.replace(token, newtoken);
	}
	return string;
}

function dialog_yesno(msg,acao){
	$.modaldialog.question(msg, {
		bt1: false,
		bt2: true,
		bt3: true,
		bt3link: acao
	});
}

function exibechk(chk,div){
	if ($("#"+chk).attr("checked") == true){
		$("#"+div).show();
	}else{
		$("#"+div).hide();
	}
}
