/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/

// Speed of the automatic slideshow
var slideshowSpeed = 10000;

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ {
		
		"image" : "back1.jpg"
	}, {
		
		"image" : "back2.jpg"
		
	}, {
		
		"image" : "back3.jpg"
		
	}, {
		"image" : "back4.jpg"
	}
];

var interval;
var scroller  = null;
var scrollbar = null;
$(document).ready(function() {
		
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		// Check if no animation is running. If it is, prevent the action
		if(animating) {
			return;
		}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
		
	};
	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(imagens/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
				
		// Fade out the current container
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				$("#headertxt").css({"display" : "block"});
				animating = false;
			}, 500);
		});
	};
	
		
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
});
function submitForm() {
	
	var toptip = function() {$('#formLivro').validationEngine('hideAll');};
	if ($('#formLivro').validationEngine('validate')) {
		var dados = $('#formLivro').formSerialize();
		
		$.ajax({
			type: "POST",
			url:"verLivroVisitas.php",
			cache: false,
			dataType: "html",
			data: dados,
			
			success:function(result){
   		 		//$("#divformlivro").html(result);
				switch (result)
				{
				case '0':
					$("#seguranca").validationEngine('showPrompt','Atenção! Deve colocar o resultado correcto da soma!','', true);
					window.setTimeout(toptip,2000);
				break;
				case '1':
					$("#formLivro").validationEngine('showPrompt','Atenção! Verifique os dados que são obrigatórios!','', true);
					window.setTimeout(toptip,3000);
				break;
				default:
					$("#divformlivro").html(result);
					scroller  = new jsScroller(document.getElementById("Scroller-1"), 780, 240);
					scrollbar = new jsScrollbar (document.getElementById("Scrollbar-Container"), scroller, false);
				break
				}
				
			}
		});
	}
}

function lerLivro(campoCodificado,cod,lang)
{
	var dados = $('#formpesquisaLivro').formSerialize();
	document.getElementById("linkVoltar").href="index.php?"+campoCodificado+"="+cod+"&lang="+lang;
	$.ajax({
			type: "POST",
			url:"verLivroVisitas.php",
			cache: false,
			dataType: "html",
			data: dados+"&cod="+cod,		
			success:function(result){
   		 		//$("#divformlivro").html(result);
				$("#divformlivro").html(result);
				
				scroller  = new jsScroller(document.getElementById("Scroller-1"), 780, 240);
				scrollbar = new jsScrollbar (document.getElementById("Scrollbar-Container"), scroller, false);
				
			}
		});
}

function urlExterno (url)
{
	window.open(url,'novajanela');
	}
function mudaBack(codigo,SLCampo,SLCodigo,img,lang)
{
	//alert(document.getElementById('mainChMenu').style.display)
	clearInterval(interval);//para parar o background de rodar
	
	//$("#headerimgs").hide();
	//document.body.background="imagens/" + img;
	
	
	document.getElementById('mainChMenu').style.display="none";
	//document.getElementById('conteudoSubmenuTop').style.display="none";
	//alert(document.getElementById('noticias').value);
	if (document.getElementById('menuSelec').value!="-1")
	{
		
		document.getElementById(document.getElementById('menuSelec').value).src="imagens/"+document.getElementById('menuSelec').value+"_"+lang+".png";
		
	}
	var data_string = "codigo=&lang="+ lang +"&" + SLCampo + "=" + SLCodigo;
	//xmlhttpPost('chMenuTituloRodape.php', data_string, 'muda_resultR','menuRodape');
	//xmlhttpPost('chMenuTituloRodape2.php', data_string, 'muda_resultR','menuRodape2');
	//xmlhttpPost('conteudoMenuLateral.php', data_string, 'muda_resultR','conteudoMenuLateral'+codigo);
	
	}
function chMenuNovo(SLCampo,SLCodigo,codigo,lang)
{
	$.ajax({
			type: "POST",
			url:"mostraResultMenu.php",
			cache: false,
			dataType: "html",
			data: "codigo=" + codigo + "&lang="+ lang +"&" + SLCampo + "=" + SLCodigo,
			
			success: function(result){
						
				$("#mainChMenu").html(result);	
				if (document.getElementById('Scrollbar-Container')!=null)
				{
					scroller  = new jsScroller(document.getElementById("Scroller-1"), 780, 300);
					scrollbar = new jsScrollbar (document.getElementById("Scrollbar-Container"), scroller, false);
					$("#Scroller-1").hide();
					$("#Scroller-1").animate({height:'toggle', opacity: 'toggle'}, "slow");
				}
				},

			error: function(){alert("Error on connection...");}
		});
}
function chMenu(SLCampo,SLCodigo,codigo,lang,id,img)
{
	//alert(id);
	if (img!="")
	{
		$("#headerimgs").hide();
		document.body.background="imagens/" + img;
		//alert("imagens/" + id + "back.jpg")
		}
	if (id.length>0)
	{
		$("#"+id).fadeOut(1000).fadeIn(1000).fadeOut(600).fadeIn(600)
    	.fadeOut(600).fadeIn(600);
		document.getElementById('menuSelec').value=id;
	}
	else
		document.getElementById('menuSelec').value='-1';
		
	var data_string = "codigo=" + codigo + "&id="+ id +"&lang="+ lang +"&" + SLCampo + "=" + SLCodigo;
	//alert(data_string);
	xmlhttpPost('chMenuTituloRodape.php', data_string, 'muda_resultR','menuRodape');
	xmlhttpPost('chMenuTituloRodape2.php', data_string, 'muda_resultR','menuRodape2');
	xmlhttpPost('chMenu.php', data_string, 'muda_resultR2','mainContainer');
	}
function pesquisar(SLCampo,SLCodigo,codigo,lang,id)
{
	if ((trim(document.getElementById('pesquisar').value)).length)
	{
		if (id.length>0)
		{
			$("#"+id).fadeOut(1000).fadeIn(1000).fadeOut(600).fadeIn(600)
			.fadeOut(600).fadeIn(600);
			document.getElementById('menuSelec').value=id;
		}
		else
			document.getElementById('menuSelec').value='-1';
			
		var data_string = "codigo=" + codigo + "&id="+ id +"&lang="+ lang +"&" + SLCampo + "=" + SLCodigo+"&palavra="+document.getElementById('pesquisar').value;
		//alert(data_string);
		xmlhttpPost('chMenuTituloRodape.php', data_string, 'muda_resultR','menuRodape');
		xmlhttpPost('chMenuTituloRodape2.php', data_string, 'muda_resultR','menuRodape2');
		xmlhttpPost('chMenu.php', data_string, 'muda_resultR2','mainContainer');
	}
	else
	{
		var msg;
		if (lang=="PT")
			msg="Deve indicar a(s) palavra(s) que pretende pesquisar!"
		else
			msg="You must!"
		alert(msg);
		document.getElementById('pesquisar').focus();
		}
		
}
function lerNoticia(SLCampo,SLCodigo,codigo,lang,id)
{
	//alert(id)
	var data_string = "codigo=" + codigo + "&id="+ id +"&lang="+ lang +"&" + SLCampo + "=" + SLCodigo;
	xmlhttpPost('lerNoticia.php', data_string, 'muda_resultR4','divConteudos');
}

function chConteudoSubmenu(SLCampo,SLCodigo,codigo,lang,tipo,classe)
{
	
	var data_string = "codigo=" + codigo + "&tipo="+ tipo +"&class=" + classe +"&lang="+ lang +"&" + SLCampo + "=" + SLCodigo;
	//alert(codigo.substring(0,3));
	xmlhttpPost('chConteudoSubmenu.php', data_string, 'muda_resultR3','conteudoSubmenu' + codigo.substring(0,3));
	xmlhttpPost('chSubmenuLeft.php', data_string, 'muda_resultR','subMenuLeft' + codigo.substring(0,3));
	}
function chConteudoSubGrupo(SLCampo,SLCodigo,codigo,lang,tipo,classe)
{
	
	var data_string = "codigo=" + codigo + "&tipo="+ tipo +"&class=" + classe +"&lang="+ lang +"&" + SLCampo + "=" + SLCodigo;
	//alert(codigo.substring(0,3));
	xmlhttpPost('chConteudoSubGrupo.php', data_string, 'muda_resultR2','divConteudos');
	xmlhttpPost('chLeftSubGrupo.php', data_string, 'muda_resultR','subMenuLeft' + codigo.substring(0,3));
	}
function chConteudoMenuTop(SLCampo,SLCodigo,codigo,lang)
{
	var data_string = "codigo=" + codigo + "&lang="+ lang +"&" + SLCampo + "=" + SLCodigo;
	var arrayCodigo=codigo.split(".");
	var divName=arrayCodigo[0]+arrayCodigo[1];
	//alert (arrayCodigo[0]+arrayCodigo[1]);
	xmlhttpPost('chConteudoMenuTop.php', data_string, 'muda_resultR','conteudoSubmenuTop' + arrayCodigo[0]);
	}
function livroVisitas(SLCampo,SLCodigo,opcao,lang)
{
	var data_string = "opcao="+ opcao +"&lang="+ lang +"&" + SLCampo + "=" + SLCodigo;
	if (opcao=="escrever")
	{
		var v_coment = document.getElementById("msg").value; 
		var v_nome   = document.getElementById("nome").value; 
		var v_codigo = document.getElementById("frm_code").value;
		var email=document.getElementById("email").value;
		var v_cidade=document.getElementById("cidade").value;
		var v_pais=document.getElementById("pais").value;
		var erro=0;
		if (trim(v_nome)=="")
		{
			if (lang=="PT")	alert ("Por favor, diga-nos o seu Nome!"); else	alert ("Please fill the Name Field.");
			document.getElementById("nome").focus();
			return;                         
		}
		if (v_nome.indexOf("<")!=-1 || v_nome.indexOf(">")!=-1 || v_nome.indexOf("\"")!=-1 || v_nome.indexOf("@")!=-1 || v_nome.indexOf("\'")!=-1)
		{
			if (lang=="PT")	alert ("ATENÇÃO! Caracteres não permitidos.");else	alert ("Words not allowed.");
			document.getElementById("nome").focus();
			return;
		}
		if (trim(document.getElementById("email").value)!="")
		{
			if (!(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/.test(document.getElementById("email").value)))
			{
				if (lang=="PT")	alert("Deve inserir um E-mail válido!"); else alert("Please type your Email so that you can recive a reply to your commentary.");
				document.getElementById("email").focus();
				return;
			}
		}
		else
		{
			if (email.indexOf("<")!=-1 || email.indexOf(">")!=-1 || email.indexOf("\"")!=-1 || email.indexOf("\'")!=-1)
			{
				if (lang=="PT")	alert ("ATENÇÃO! Caracteres não permitidos.");else	alert ("Words not allowed.");
				document.getElementById("email").focus();
				return;
			}
		}
		if (v_cidade.indexOf("<")!=-1 || v_cidade.indexOf(">")!=-1 || v_cidade.indexOf("\"")!=-1 || v_cidade.indexOf("\'")!=-1)
			{
				if (lang=="PT")	alert ("ATENÇÃO! Caracteres não permitidos.");else	alert ("Words not allowed.");
				document.getElementById("cidade").focus();
				return;
			}
		if (v_pais.indexOf("<")!=-1 || v_pais.indexOf(">")!=-1 || v_pais.indexOf("\"")!=-1 || v_pais.indexOf("\'")!=-1)
			{
				if (lang=="PT")	alert ("ATENÇÃO! Caracteres não permitidos.");else	alert ("Words not allowed.");
				document.getElementById("pais").focus();
				return;
			}
		if (trim(v_coment)=="")
		{
			if (lang=="PT")	alert ("Por favor escreva a sua Mensagem!"); else alert ("Please fill the Commentaries field.");
			document.getElementById("msg").focus();
			return; 
		}
		if (v_coment.indexOf("<")!=-1 || v_coment.indexOf(">")!=-1 || v_coment.indexOf("\'")!=-1 || v_nome.indexOf("@")!=-1)
			{
				if (lang=="PT")	alert ("ATENÇÃO! Caracteres não permitidos.");else	alert ("Words not allowed.");
				document.getElementById("msg").focus();
				return;
			}
		if (v_coment.length>1500)
		{
			if (lang=="PT")	alert ("Mensagem demasiado extensa. Por favor reduza-a."); else	alert ("The Commentary is too extensive. Please reduce it.");
			document.getElementById("msg").focus();
			return;                        
		}
		if (trim(v_codigo)=="")
		{
			if (lang=="PT")	alert ("Deve Digitar os caracteres que vê na imagem!");	else alert ("Please fill the Validation Code Field.");
			document.getElementById("frm_code").focus();
			return;                        
		}
		data_string = data_string + "&nome=" + v_nome + "&email="+ email +"&cidade="+ v_cidade + "&pais="+ v_pais +"&msg="+ v_coment +"&cod="+ v_codigo +"&opcao="+ opcao;
	}
	
	xmlhttpPost('verLivroVisitas.php', data_string, 'muda_resultR3','livroVisitas');
	xmlhttpPost('cabecalhoLivro.php', data_string, 'muda_resultR','cabecalhoLivro');
}
function chAno(valor,lang,opcao,SLCampo,SLCodigo)
{
	//alert(valor);
	var data_string = "ano="+ valor +"&opcao="+ opcao +"&lang="+ lang +"&" + SLCampo + "=" + SLCodigo;
	xmlhttpPost('chMes.php', data_string, 'muda_resultR','divMes');
	xmlhttpPost('verLivroVisitas.php', data_string, 'muda_resultR3','livroVisitas');
}
function chVerLivro(valor,lang,opcao,SLCampo,SLCodigo,ano)
{
	//alert(valor);
	var data_string = "ano="+ ano + "&mes="+ valor +"&opcao="+ opcao +"&lang="+ lang +"&" + SLCampo + "=" + SLCodigo;
	xmlhttpPost('verLivroVisitas.php', data_string, 'muda_resultR3','livroVisitas');
}

function muda_resultR2(value,div)
{
	
	document.getElementById(div).innerHTML = value;
	$(document).ready(
		function(){
			
			/*para validações do formulário*/
			if (value.search('id="nome"')>0)
			{
				//alert();
				jQuery(function(){
					jQuery("#nome").validate({
                    	expression: "if (VAL) return true; else return false;",
                    	message: "Por favor, diga-nos o seu Nome!"
                	});
					//alert(document.getElementById('email').value.length);
					
					jQuery("#email").validate({
						  
						expression: "if (VAL.match(/^[^\\W][a-zA-Z0-9\\_\\-\\.]+([a-zA-Z0-9\\_\\-\\.]+)*\\@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)*\\.[a-zA-Z]{2,4}$/) || VAL=='') return true; else return false;",
						message: "Deve inserir um E-mail válido!"
					});
					
					jQuery("#msg").validate({
                    	expression: "if (VAL) return true; else return false;",
                    	message: "Por favor escreva a sua Mensagem!"
                	});
				});
			}
			if (value.search('gallery')>0)
			{
				$('#gallery a').lightBox();
				}
			
			if (div!="divConteudos")
			{
				$(".haccordion").haccordion();
			$("#mainChMenu").hide();
				$("#mainChMenu").animate({height:'toggle', opacity: 'toggle'}, "slow");
			}
			if (value.search('Scrollbar-Container')>0)
			{
				scroller  = new jsScroller(document.getElementById("Scroller-1"), 650, 343);
				scrollbar = new jsScrollbar (document.getElementById("Scrollbar-Container"), scroller, false);
				$("#Scroller-1").hide();
				$("#Scroller-1").animate({height:'toggle', opacity: 'toggle'}, "slow");
			}
			
			var currentPosition = 0;
		  var slideWidth = 670;
		  var slides = $('.slide');
		  var numberOfSlides = slides.length;
						
		  // Remove scrollbar in JS
		  $('#slidesContainer').css('overflow', 'hidden');
		
		  // Wrap all .slides with #slideInner div
		  slides
			.wrapAll('<div id="slideInner"></div>')
			// Float left to display horizontally, readjust .slides width
			.css({
			  'float' : 'left',
			  'width' : slideWidth
			});
		
		  // Set #slideInner width equal to total width of all slides
		  $('#slideInner').css('width', slideWidth * numberOfSlides);
		
		  // Insert controls in the DOM
		  $('#slideshow')
			.prepend('<span class="control" id="leftControl"></span>')
			.append('<span class="control" id="rightControl"></span>');
		
		  // Hide left arrow control on first load
		  manageControls(currentPosition);
		
		  // Create event listeners for .controls clicks
		  $('.control')
			.bind('click', function(){
			// Determine new position
			currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
			
			// Hide / show controls
			manageControls(currentPosition);
			// Move slideInner using margin-left
			$('#slideInner').animate({
			  'marginLeft' : slideWidth*(-currentPosition)
			});
		  });
		
		  // manageControls: Hides and Shows controls depending on currentPosition
		  function manageControls(position){
			// Hide left arrow if position is first slide
			if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
			// Hide right arrow if position is last slide
			if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }
		  }	
		
		if (value.search('slideshow')>0)
			{
				$("#slideshow").hide();
				$("#slideshow").animate({height:'toggle', opacity: 'toggle'}, "slow");
			}
		}
		
	);
	
}
function muda_resultR3(value,div)
{
	//alert(div.substring((div.length-3)));
	var seccao=div.substring((div.length-3));
	document.getElementById(div).innerHTML = value;
	$(document).ready(
		function(){
			//alert();
			/*if (value.search('tabela009')>0)
			{
				alert(value);
				var wid=document.getElementById('tabela009').width;
				alert(wid.height);
				//document.getElementById('setapracima'+seccao).style.display='none';
				//document.getElementById('').style.width
			}*/
			if (value.search('Scrollbar-Container')>0)
			{
				//alert();
				scroller  = new jsScroller(document.getElementById("Scroller-1"), 595, 250);
				scrollbar = new jsScrollbar (document.getElementById("Scrollbar-Container"), scroller, false);
				//$("#Scroller-1").hide();
				//$("#Scroller-1").animate({height:'toggle', opacity: 'toggle'}, "slow");
			}
			
			if (value.search('gallery'+seccao)>0)
			{
				$('#gallery'+seccao+' a').lightBox();
				}
			
			if (value.search('subMenuSel008006')>0)
			{
				document.getElementById('subMenuSel008006').focus();
			}
			if (value.search('subMenuSel009006')>0)
			{
				document.getElementById('subMenuSel009006').focus();
			}
			
		}
	);
	
}
function muda_resultR4(value,div)
{
	document.getElementById(div).innerHTML = value;
	$(document).ready(
		function(){
								
			if (value.search('divConteudos')>0)
			{
				$("#divConteudos").hide();
				$("#divConteudos").animate({height:'toggle', opacity: 'toggle'}, "slow");
			}
		
			if (value.search('Scrollbar-Container')>0)
			{
				scroller  = new jsScroller(document.getElementById("Scroller-1"), 650, 250);
				scrollbar = new jsScrollbar (document.getElementById("Scrollbar-Container"), scroller, false);
				$("#Scroller-1").hide();
				$("#Scroller-1").animate({height:'toggle', opacity: 'toggle'}, "slow");
			}
		
		}
		
	);
	
}
function fazEfeito(opcao,over,cor)
{

	//alert(document.getElementById('op'+opcao).style.cursor);
	//alert(document.getElementById('op'+opcao).onmouseover);
	if (over==1)
	{
		$(this).animate({ paddingLeft: "1px" }, 350 );
		document.getElementById('op'+opcao).style.color="#"+cor;
		document.getElementById(opcao).style.color="#"+cor;
		$("#"+opcao).show().animate({ paddingLeft: "1px" },350);	
	}
	else
	{
		$(this).animate({ paddingLeft: "-1px" }, 350 );
		document.getElementById('op'+opcao).style.color="#ffffff";
		document.getElementById(opcao).style.color="#ffffff";
		$("#"+opcao).hide();
	}	
}

//Scroll
valorScr=3;
timerDown="" 
timerUp="" 
function scrollBaixo(id){
	clearTimeout(timerDown) 
	document.getElementById(id).scrollTop+=valorScr
	timerDown=setTimeout("scrollBaixo('"+id+"')",10)
}

function scrollCima(id){
	clearTimeout(timerUp)
	document.getElementById(id).scrollTop-=valorScr
	timerUp=setTimeout("scrollCima('"+id+"')",10)
} 
function pararScr(){
	clearTimeout(timerDown) 
	clearTimeout(timerUp)
}
//document.onmousemove=function(){pararScr()}

function teste(grupo){
	var t = grupo.split('.');
	var jQ = jQuery;
	var p = jQ(document.getElementById('009')).parent()[0];
	p.opened = document.getElementById('009');
}
