var ajaxLoad;
$(document).ready(function() {
	actualizaRodape();
	$("#rodape").stop(true,true).fadeIn();
	
	$(".auto .jCarouselLite").jCarouselLite({
		visible: 1,
		auto: 5000,
    	speed: 1000,
    	vertical: true
	});
});
$(window).resize(function(){ actualizaRodape(); });
$.ajaxSetup ({ cache: false });

function actualizaRodape(){
	if($(window).height() > $('body').height()){
		$("#rodape").stop(true,true).animate({'bottom':'0'},300);
	} else {
		var dif = $('body').height() - $(window).height();
		$("#rodape").stop(true,true).animate({'bottom':-$("#rodape").height()-dif},300);
	}
	/*
	if($(window).height() > ($("#rodape").offset().top+$("#rodape").height())){
		$("#rodape").stop(true,true).css('position', 'absolute');
	} else {
		$("#rodape").stop(true,true).css('position', 'inherit');
	}*/
}
function actualizaJS(){
	$("a[rel^='prettyPhoto']").prettyPhoto({show_title:false, deeplinking: false, social_tools:false, hideflash:true});
	
	actualizaRodape();
	setMapa();
	toggleLoading(false);
}
// GERAL
function toggleMenu(lista){
	
	ficheiro = 'apresentacao';
	if(lista[0] == 'equipa' || lista[0] == 'especialidades' || lista[0] == 'equipamentos-tecnologicos' || lista[0] == 'casos-clinicos' || lista[0] == 'formacao' || lista[0] == 'instalacoes' || lista[0] == 'contactos') ficheiro = lista[0];
	
	$("#conteudo").stop(true,true).fadeOut(500,function(){
		if(ajaxLoad) ajaxLoad.abort();
		toggleLoading(true);
		var vars = {id:lista[1]}; 
		ajaxLoad = $.ajax({ type: "POST", url: "pg/"+ficheiro+".php", data: vars, success: function(msg){ 
			$("#conteudo").stop(true,true).html(msg).fadeIn(500,function(){actualizaJS();});
		},	error: function(xhr, ajaxOptions, thrownError){ debug('missing file!'); } });
	})
	
	$("#menu li a").each(function(){ if($(this).attr('rel') == ficheiro) $(this).addClass('inactivo'); else $(this).removeClass('inactivo'); });
}

//
function setMapa(){
	if($("#mapa").exists()){
	
	var latlng = new google.maps.LatLng( 41.177904,-8.660575 );
	var myOptions = {
		zoom: 15,
		center: latlng,
		streetViewControl: false,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControlOptions: {
			style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
		}
	};
	mapa = new google.maps.Map(document.getElementById("mapa"), myOptions);
	
	var imagem = new google.maps.MarkerImage('imagens/marcador.png', new google.maps.Size(20, 20), new google.maps.Point(0,0), new google.maps.Point(10, 10));
	var marcador = new google.maps.Marker({
		position: latlng,
		icon: imagem,
		draggable: false,
		map: mapa
	});
	}
}


//
function sendContacto(){
	$("#contacto .alerta").stop(true,true).fadeOut(300, function(){
		$("#contacto .alerta").stop(true,true).html('');
		var erro = false;
		if($("#contacto-nome").val().length < 2 || $("#contacto-nome").val() == $("#contacto-nome").attr('title')){erro=true;$("#contacto-nome").css('border-color','#fff');} else $("#contacto-nome").css('border-color','transparent');
		if(!emailValido($("#contacto-email").val())){erro=true;$("#contacto-email").css('border-color','#fff');} else $("#contacto-email").css('border-color','transparent');
		if($("#contacto-assunto").val().length < 2 || $("#contacto-assunto").val() == $("#contacto-assunto").attr('title')){erro=true;$("#contacto-assunto").css('border-color','#fff');} else $("#contacto-assunto").css('border-color','transparent');
		if($("#contacto-mensagem").val().length < 2 || $("#contacto-mensagem").val() == $("#contacto-mensagem").attr('title')){erro=true;$("#contacto-mensagem").css('border-color','#fff');} else $("#contacto-mensagem").css('border-color','transparent');
		
		if(erro == false){
			toggleLoading(true);
			var vars = {f:'sendContacto', nome:$("#contacto-nome").val(), email:$("#contacto-email").val(), assunto:$("#contacto-assunto").val(), mensagem:$("#contacto-mensagem").val()}; 
			$.ajax({ type: "POST", url: "php/ajax.php", data: vars, success: function(msg){ 
				if(strIsNull(msg)){
					$("#contacto .alerta").stop(true,true).html('Message sent!').fadeIn();;
					$("#contacto-nome").val($("#contacto-nome").attr('title'));
					$("#contacto-email").val($("#contacto-email").attr('title'));
					$("#contacto-assunto").val($("#contacto-assunto").attr('title'));
					$("#contacto-mensagem").val($("#contacto-mensagem").attr('title'));
					toggleLoading(false);
				} else
					$("#contacto .alerta").stop(true,true).html(msg).fadeIn();
			},	error: function(xhr, ajaxOptions, thrownError){ debug('missing file!'); } });
		} else
			$("#contacto .alerta").stop(true,true).html('error').fadeIn();
	});
}

function actualizaURL(){ var url = SWFAddress.getValue(); if(url.substr(0,1) == '/') url = url.substr(1,url.length); if($.isFunction(toggleMenu)){ if(strIsNull(url) || url == '/') toggleMenu([]); else toggleMenu(url.split("/")); } }
function mudaLingua(lg){ if(lg != $("#linguas .inactivo").attr('rel')){ var vars = {f:'mudaLingua', lingua:lg}; ajaxLoad = $.ajax({ type: "POST", url: "php/ajax.php", data: vars, success: function(msg){ if(strIsNull(msg)) window.location.reload(); else debug(msg); },	error: function(xhr, ajaxOptions, thrownError){ debug('missing file!'); } }); } }
function goTop(){$('html, body').animate({scrollTop:0}, 'slow');}
/* ÚTIL */
function inputON(obj,valor){ if($(obj).val() == valor) $(obj).val(""); }
function inputOFF(obj,valor){ if(strIsNull($(obj).val())) $(obj).val(valor); }
function debug(txt){ toggleLoading(false); alert(txt);}
function strIsNull(str){ if(str == null || str == "" || str == undefined) return true; else return false; }
function emailValido(email){ var er = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/; return er.test(email); }
function toggleLoading(on){if(ajaxLoad) ajaxLoad.abort();if(on==true)$("#loading").stop(true,true).delay(0.5).slideDown();else $("#loading").stop(true,true).slideUp();}
function setFocusBlur(){$(".focus-blur").each(function(){$(this).focus(function(){if($(this).val() == $(this).attr('title')) $(this).val('');}).blur(function(){if($(this).val() == '')$(this).val($(this).attr('title'));});});}
jQuery.fn.exists = function(){return this.length>0;}
//function fileExists(urlFile){ $.ajax({ url:urlFile, type:'HEAD',  error: function(){  return 'false';  }, success: function(){ return 'true'; } }); }
