$(document).ready(function (){
	
	
	$('#mainHolder').fadeOut('fast', function () {
	$('#pageMainContent').fadeOut('fast');
	var noCacheKey2 = Math.random();
	if (window.location.hash == '' ){
		$.get("./modules/inicio/inicio.php", {nck: noCacheKey2}, function(data){
			$('#mainHolder').html(data);
			$('#mainHolder').fadeIn('slow');
			ajaxBusy = "0";
		});
	}else{
		var module = window.location.hash.slice(1).split('/');
		if(module[1] != undefined && module[1] != '' && module[1] != module[0]){
			if(module[2] != undefined && module[2] != '' && module[2] != module[1]){
				var modulePath2 = "./modules/"+module[0]+"/"+module[0]+".php";
				$.get(modulePath2+'?noCacheKey=2', function(data){
					$('#mainHolder').html(data);
					$('#mainHolder').fadeIn('slow');
					var noCacheKey3 = Math.random();
					var modulePath2 = "./modules/"+module[0]+"/"+module[2]+".php";
					subTitlehighlight();
					$.get(modulePath2, {nck: noCacheKey3}, function(data){
						$('#pageMainContent').html(data);
						$('#pageMainContent').fadeIn('slow');
						ajaxBusy = "0";
					});
				});
			}else{
				var modulePath = "./modules/"+module[0]+"/"+module[0]+".php";
				$.get(modulePath+'?noCacheKey=2', function(data){
					$('#mainHolder').html(data);
					$('#mainHolder').fadeIn('slow');
					var noCacheKey3 = Math.random();
					var modulePath2 = "./modules/"+module[0]+"/"+module[1]+".php";
					subTitlehighlight();
					$.get(modulePath2, {nck: noCacheKey3}, function(data){
						$('#pageMainContent').html(data);
						$('#pageMainContent').fadeIn('slow');
						ajaxBusy = "0";
					});
				});
			};
		}else{
			var noCacheKey3 = Math.random();
			var modulePath = "./modules/"+module[0]+"/"+module[0]+".php";
			$.get(modulePath, {nck: noCacheKey2}, function (data) {
				$('#mainHolder').html(data);
				$('#mainHolder').fadeIn('slow');
				//$('#mainHolder').fadeIn('fast');
				//currentMainPage = module;
				var noCacheKey = Math.random();
				
				switch (module[0]){
					case 'formacaoContinua':
						$.get("./modules/formacaoContinua/descricao.php", {nck: noCacheKey3}, function (data) {
							$('#pageMainContent').html(data);
							$('#pageMainContent').fadeIn('slow');
							ajaxBusy = "0";
						});
					break;
					case 'formacaoProfissional':
						$.get("./modules/formacaoProfissional/descricao.php", {nck: noCacheKey3}, function (data) {
							$('#pageMainContent').html(data);
							$('#pageMainContent').fadeIn('slow');
							ajaxBusy = "0";
						});
						break;
					case 'formacaoFormadores':
						$.get("./modules/formacaoFormadores/descricao.php", {nck: noCacheKey3}, function (data) {
							$('#pageMainContent').html(data);
							$('#pageMainContent').fadeIn('slow');
							ajaxBusy = "0";
						});
						break;
					case 'contactos':
						$.get("./modules/contactos/priorVelho.php", {nck: noCacheKey3}, function (data) {
							$('#pageMainContent').html(data);
							$('#pageMainContent').fadeIn('slow');
							ajaxBusy = "0";
						});
						break;
					case 'empresas':
						$.get("./modules/empresas/descricao.php", {nck: noCacheKey3}, function (data) {
							$('#pageMainContent').html(data);
							$('#pageMainContent').fadeIn('slow');
							ajaxBusy = "0";
						});
						break;
					case 'cno':
						$.get("./modules/cno/oQueE.php", {nck: noCacheKey3}, function (data) {
							$('#pageMainContent').html(data);
							$('#pageMainContent').fadeIn('slow');
							ajaxBusy = "0";
						});
						break;
					case 'noticias':
						$.get("./modules/noticias/todas.php", {nck: noCacheKey3}, function (data) {
							$('#pageMainContent').html(data);
							$('#pageMainContent').fadeIn('slow');
							ajaxBusy = "0";
						});
						break;
					case 'areaReservada':
						$.get("./modules/areaReservada/funcionamentoGeral.php", {nck: noCacheKey3}, function (data) {
							$('#pageMainContent').html(data);
							$('#pageMainContent').fadeIn('slow');
							ajaxBusy = "0";
						});
						break;
					case 'inicio':
						$('#mainHolder').fadeIn('slow');
						$('#pageMainContent').fadeIn('slow');
						ajaxBusy = "0";
						break;
					case 'catalogo':
						$.get("./modules/catalogo/solicitarCatalogo.php", {nck: noCacheKey3}, function (data) {
							$('#pageMainContent').html(data);
							$('#pageMainContent').fadeIn('slow');
							ajaxBusy = "0";
						});
						break;
					default:
						break;
				};
			});
		}
		
	}
	});
	
	
	if ("onhashchange" in window) {
		/*alert("The browser supports the hashchange event!");*/
		window.onhashchange = hashModuleUpdate;
	}else{
		
		$(window).bind('hashchange', function(e) {
			/*currentPage = window.location.hash.slice(1);
			//$.bbq.pushState( currentPage );
			
			var state = {},
			id = 0,
			url = currentPage;
			state[ id ] = url;
			$.bbq.pushState( state );
			
			
			hashModuleUpdate();*/
			
			
			/*window.location.reload();*/
		});
		
		/*previousPage = window.location.hash.slice(1);
		currentPage = window.location.hash.slice(1);
		setInterval("manualHashCheck();", 200);*/
	}
	
	$('#lightbox_futuralia').lightBox();
	
	
	

	$('.riNewsTitle').click(function () {
		ajaxBusy = "1";
		$("#mainHolder").fadeOut("slow");
		$("#pageMainContent").fadeOut("slow");
		var noCacheKey = Math.random();
		var temp = $(this).attr("id").split("_");
		window.location.hash = "noticias";
		var modulePath = "./modules/noticias/noticias.php";
		$.get(modulePath, {nck: noCacheKey}, function(data){
			$("#mainHolder").html(data);
			var modulePath2 = "./modules/noticias/todas.php";
			$.get(modulePath2, {nck: noCacheKey, selectedNews: temp[1]}, function(data){
				$("#pageMainContent").html(data);
				$("#mainHolder").fadeIn("slow");
				$('#pageMainContent').fadeIn('slow');
				
			});
		});
		ajaxBusy = "0";
	});
	
	
	$('.riSecondaryHeader').click(function () {
		ajaxBusy = "1";
		if(!$(this).attr("id") == ""){
			var noCacheKey2 = Math.random();
			var temp = $(this).attr("id").split("-");
			var module = temp[0].split("_");
			window.location.hash = module[0];
			var modulePath = "./modules/"+module[0]+"/"+module[0]+".php";
			$("#mainHolder").fadeOut("slow");
			$.get(modulePath, {nck: noCacheKey2}, function(data){
				$("#mainHolder").html(data);
				$("#mainHolder").fadeIn("slow");
			});
		}
		ajaxBusy = "0";
	});
	
	
	$(".mmBtn").click( function () {
		ajaxBusy = "1";
		$('#mainHolder').fadeOut('slow');
		$('#pageMainContent').fadeOut('slow');
		var module = $(this).attr('id').replace('mmBtn_', '');
		window.location.hash = module;
		var modulePath = './modules/'+module+'/'+module+'.php';
		var noCacheKey = Math.random();
		$.get(modulePath, {nck: noCacheKey}, function (data) {
			$('#mainHolder').html(data);
			//currentMainPage = module;
			var noCacheKey = Math.random();
			switch (module){
				case 'formacaoContinua':
					$.get("./modules/formacaoContinua/descricao.php", {nck: noCacheKey}, function (data) {
						$('#pageMainContent').html(data);
						$('#mainHolder').fadeIn('slow');
						$('#pageMainContent').fadeIn('slow');
						ajaxBusy = "0";
					});
				break;
				case 'formacaoProfissional':
					$.get("./modules/formacaoProfissional/descricao.php", {nck: noCacheKey}, function (data) {
						$('#pageMainContent').html(data);
						$('#mainHolder').fadeIn('slow');
						$('#pageMainContent').fadeIn('slow');
						ajaxBusy = "0";
					});
					break;
				case 'formacaoFormadores':
					$.get("./modules/formacaoFormadores/descricao.php", {nck: noCacheKey}, function (data) {
						$('#pageMainContent').html(data);
						$('#mainHolder').fadeIn('slow');
						$('#pageMainContent').fadeIn('slow');
						ajaxBusy = "0";
					});
					break;
				case 'contactos':
					$.get("./modules/contactos/priorVelho.php", {nck: noCacheKey}, function (data) {
						$('#pageMainContent').html(data);
						$('#mainHolder').fadeIn('slow');
						$('#pageMainContent').fadeIn('slow');
						ajaxBusy = "0";
					});
					break;
				case 'empresas':
					$.get("./modules/empresas/descricao.php", {nck: noCacheKey}, function (data) {
						$('#pageMainContent').html(data);
						$('#mainHolder').fadeIn('slow');
						$('#pageMainContent').fadeIn('slow');
						ajaxBusy = "0";
					});
					break;
				case 'cno':
					$.get("./modules/cno/oQueE.php", {nck: noCacheKey}, function (data) {
						$('#pageMainContent').html(data);
						$('#mainHolder').fadeIn('slow');
						$('#pageMainContent').fadeIn('slow');
						ajaxBusy = "0";
					});
					break;
				case 'inicio':
					$('#mainHolder').fadeIn('slow');
					ajaxBusy = "0";
					break;
				default:
					break;
			};
		});
	});
	
	$(".upperMenu").click( function () {
		ajaxBusy = true;
		$("#mainHolder").fadeOut("slow");
		$("#pageMainContent").fadeOut("slow");
		var module = $(this).attr('id').replace('umBtn_', '');
		window.location.hash = module;
		var modulePath = './modules/'+module+'/'+module+'.php';
		var noCacheKey = Math.random();
		$.get(modulePath, {nck: noCacheKey}, function (data) {
			$('#mainHolder').html(data);
			$('#mainHolder').fadeIn("fast");
			currentMainPage = module;
			var noCacheKey = Math.random();
			switch (module){
				case 'contactos':
					$.get("./modules/contactos/priorVelho.php", {nck: noCacheKey}, function (data) {
						$('#pageMainContent').html(data);
						$('#pageMainContent').fadeIn('fast');
					});
					break;
				default:
					break;
			};
		});
		ajaxBusy = false;
	});
	
	$(".nextActionsLink").click( function () {
		ajaxBusy = "1";
		$("#mainHolder").fadeOut("slow");
		$("#pageMainContent").fadeOut("slow");
		var noCacheKey2 = Math.random();
		var temp = $(this).attr("id").split("-");
		var module = temp[0].split("_");
		if(module[1] != undefined && module[1] != "" && module[1] != module[0]){
			window.location.hash = module[0]+"/"+module[1];
			var modulePath = "./modules/"+module[0]+"/"+module[0]+".php";
			$.get(modulePath, {nck: noCacheKey2}, function(data){
				$("#mainHolder").html(data);
				$("#mainHolder").fadeIn("slow");
				var modulePath2 = "./modules/"+module[0]+"/"+module[1]+".php";
				subTitlehighlight();
				$.get(modulePath2, {nck: noCacheKey2}, function(data){
					$("#pageMainContent").html(data);
					$("#pageMainContent").fadeIn("slow");
				});
			});
		}else{
			window.location.hash = module[0];
			var modulePath = "./modules/"+module[0]+"/"+module[0]+".php";
			$.get(modulePath, {nck: noCacheKey2}, function (data) {
				$("#mainHolder").html(data);
				$("#mainHolder").fadeIn("slow");
				//currentMainPage = module;
				var noCacheKey = Math.random();
				switch (module){
				case 'formacaoContinua':
					$.get("./modules/formacaoContinua/descricao.php", {nck: noCacheKey}, function (data) {
						$('#pageMainContent').html(data);
						$('#pageMainContent').fadeIn('slow');
					});
					break;
				case 'formacaoProfissional':
					$.get("./modules/formacaoProfissional/descricao.php", {nck: noCacheKey}, function (data) {
						$('#pageMainContent').html(data);
						$('#pageMainContent').fadeIn('slow');
					});
					break;
				case 'formacaoFormadores':
					$.get("./modules/formacaoFormadores/descricao.php", {nck: noCacheKey}, function (data) {
						$('#pageMainContent').html(data);
						$('#pageMainContent').fadeIn('slow');
					});
					break;
				case 'contactos':
					$.get("./modules/contactos/priorVelho.php", {nck: noCacheKey}, function (data) {
						$('#pageMainContent').html(data);
						$('#pageMainContent').fadeIn('slow');
					});
					break;
				case 'empresas':
					$.get("./modules/empresas/descricao.php", {nck: noCacheKey}, function (data) {
						$('#pageMainContent').html(data);
						$('#pageMainContent').fadeIn('slow');
					});
					break;
				case 'cno':
					$.get("./modules/cno/oQueE.php", {nck: noCacheKey}, function (data) {
						$('#pageMainContent').html(data);
						$('#pageMainContent').fadeIn('slow');
					});
					break;
				default:
					break;
			};
			});
		}
		ajaxBusy = "0";
	});
	
	$(".riLinkSpan").click( function () {
		ajaxBusy = "1";
		$("#mainHolder").fadeOut("slow");
		$("#pageMainContent").fadeOut("slow");
		var noCacheKey2 = Math.random();
		var temp = $(this).attr("id").split("-");
		var module = temp[0].split("_");
		if(module[1] != undefined && module[1] != "" && module[1] != module[0]){
			window.location.hash = module[0]+"/"+module[1];
			var modulePath = "./modules/"+module[0]+"/"+module[0]+".php";
			$.get(modulePath, {nck: noCacheKey2}, function(data){
				$("#mainHolder").html(data);
				$("#mainHolder").fadeIn("slow");
				var modulePath2 = "./modules/"+module[0]+"/"+module[1]+".php";
				$.get(modulePath2, {nck: noCacheKey2}, function(data){
					$("#pageMainContent").html(data);
					$("#pageMainContent").fadeIn("slow");
				});
			});
		}else{
			window.location.hash = module[0];
			var modulePath = "./modules/"+module[0]+"/"+module[0]+".php";
			$.get(modulePath, {nck: noCacheKey2}, function (data) {
				$("#mainHolder").html(data);
				$("#mainHolder").fadeIn("slow");
				//currentMainPage = module;
				var noCacheKey = Math.random();
				switch (module[0]){
					case "noticias":
						$.get("./modules/noticias/todas.php", {nck: noCacheKey}, function (data) {
							$("#pageMainContent").html(data);
							$("#pageMainContent").fadeIn("slow");
						});
						break;
					case "catalogo":
						$.get("./modules/catalogo/solicitarCatalogo.php", {nck: noCacheKey}, function (data) {
							$("#pageMainContent").html(data);
							$("#pageMainContent").fadeIn("slow");
						});
						break;
						
					default:
						break;
				};
			});
		}
		ajaxBusy = "0";
	});
	
	$("#restrictedAreaBtn").click( function () {
		ajaxBusy = "1";
		$( 'html, body' ).animate( { scrollTop: 0 }, 'slow' );
		$("#mainHolder").fadeOut("slow");
		window.location.hash = "areaReservada";
		var noCacheKey = Math.random();
		$.get("modules/areaReservada/areaReservada.php", {nck: noCacheKey}, function (data) {
			$("#mainHolder").html(data);
			$("#mainHolder").fadeIn("slow");
			if($('#pageMainContent').length){
				var noCacheKey2 = Math.random();
				$.get("./modules/areaReservada/funcionamentoGeral.php", {nck: noCacheKey2}, function (data) {
					$('#pageMainContent').html(data);
					$('#pageMainContent').fadeIn('slow');
					ajaxBusy = "0";
				});
			}else{ajaxBusy = "0";}
		});
		ajaxBusy = "0";
	});
	
	
	/*$(".smBtn").click( function () {
		
		if ($(this) == $(this).parent().children().eq(0)){
			alert(\'ahsdbfabds\');
		
		}
	
		$(\'#pageMainTitle\').css(\'color\', \'#C0C0C0\');
		$(\'#pageSubTitle\').html($(this).text());
		$(\'#pageMainContent\').fadeOut("slow");
		var modulePath = $(this).attr(\'id\').replace(\'smBtn_\', \'\').replace(\'_\',\'/\');
		//window.location.hash = modulePath;
		$.get(\'./modules/\'+modulePath+\'.php\', function(data){
			$(\'#pageMainContent\').html(data);
			$(\'#pageMainContent\').fadeIn(\'slow\');
			$(\'.smBtn\').css(\'font-weight\', \'normal\');
			$(\'this\').css(\'font-weight\', \'bold\');
		});
	});*/
	
	$("#headerSlideShow > img:eq(0)").fadeIn("slow");
	var intervaloActualizacao = setInterval("actualizarCabecalho()",10000);
	
	customAlert('Formação financiada<br/>Novos Cursos de Formação:<br/><span class="pageLinkSpan" id="formacaoContinua_fmcLisboa-popupLinkSpan" style="background-color: #F2F5F6; font-size:12px;">Prior Velho</span> e <span class="pageLinkSpan" id="formacaoContinua_fmcAlentejo-popupLinkSpan" style="background-color: #F2F5F6; font-size:12px;">Grândola</span> <br/> <a target="_blank" href="http://finiform.forinsia.com/">Inscreva-se</a> já!');
});