function searchform () {
	$("li.pesquisaTrigger").show();
	$(".pesquisaImg, .pesquisaField").hide();
	$(".pesquisaTrigger a").click(function () {
		$("li.pesquisaTrigger").fadeOut(function () {
			$("li.pesquisaImg, li.pesquisaField").fadeIn();
		});
		return false;
	});
}
function noEmptyPages() {
	var lastchild = $('.page:last-child');
	var found = lastchild.find('div.slide');
	if (found.length == 0) { lastchild.remove(); }
}
function noEmptySlides() {
	var lastchild = $('#cv_list-carousel').find('div.slide:last');
	var sons = lastchild.children();
	if (sons.length == 0) { lastchild.remove(); }
}
function changeTitle() {
	if ($('h3.vermelho').length) {
		var titulodapagina = $('h3.vermelho').text();
		$('title').text("GOP // " +titulodapagina);
	} else if ($('h3.vermelho').length==false && $('.barra').length) {
		$('title').text("GOP // " + $('.barra').attr('alt'));
	} else {
		$('title').text("Gabinete de Organização e Projectos");
	}
}
function carousels() {
	if ($("#homeimagens-carousel").length) {
		$("#homeimagens-carousel").carouFredSel({
			"items": { "visible": 1 },
			"circular": true,
			"infinite": false,
			"auto": true,
			"pagination": "#pager-homeimagens"
		});
	}
	if ($("#gop-carousel").length) {
		$("#gop-carousel").carouFredSel({
			"circular": false,
			"items": { "visible": 2 },
			"infinite": false,
			"auto": false,
			"prev": { "button": "#prev-gop" },
			"next": { "button": "#next-gop" },
			"pagination": "#pager-gop"
		});
	}
	if ($("#gopimagens-carousel").length) {
		$("#gopimagens-carousel").carouFredSel({
			"items": { "visible": 1 },
			"circular": true,
			"infinite": false,
			"auto": { "play": true, "pauseOnHover": true, "pauseDuration": 5000, "duration": 1000 },
			"pagination": "#pager-gopimagens"
		});
	}
	if ($("#categorias-carousel").length) {
		noEmptyPages();
		$("#categorias-carousel").carouFredSel({
			"items": { "visible": 1 },
			"circular": false,
			"infinite": false,
			"auto": false,
			"prev": { "button": "#prev-categorias" },
			"next": { "button": "#next-categorias" },
			"pagination": "#pager-categorias",
			"cookie": true
		});
	}
	if ($("#projectos-carousel").length) {
		noEmptyPages();
		$("#projectos-carousel").carouFredSel({
			"items": { "visible": 1 },
			"circular": false,
			"infinite": false,
			"auto": false,
			"prev": { "button": "#prev-projectos" },
			"next": { "button": "#next-projectos" },
			"pagination": "#pager-projectos",
			"cookie": true
		});
	}
	if ($("#proimagens-carousel").length) {
		$("#proimagens-carousel").carouFredSel({
			"items": { "visible": 1 },
			"circular": true,
			"infinite": false,
			"auto": { "play": true, "pauseOnHover": true, "pauseDuration": 5000, "duration": 1000 },
			"pagination": "#pager-proimagens"
		});
	}
	if ($("#prodetalhe-carousel").length) {
		$("#prodetalhe-carousel").carouFredSel({
			"items": { "visible": 2 },
			"circular": false,
			"infinite": false,
			"auto": false,
			"prev": { "button": "#prev-prodetalhe" },
			"next": { "button": "#next-prodetalhe" },
			"pagination": "#pager-prodetalhe"
		});
	}
	if ($("#cv_list-carousel").length) {
		noEmptySlides();
		$("#cv_list-carousel").carouFredSel({
			"items": { "visible": 3 },
			"circular": false,
			"infinite": false,
			"auto": false,
			"prev": { "button": "#prev-cv_list" },
			"next": { "button": "#next-cv_list" },
			"pagination": "#pager-cv_list",
			"cookie":true
		});
	}
	if ($("#cv_imagens-carousel").length) {
  		$("#cv_imagens-carousel").carouFredSel({
   			"items": { "visible": 1 },
   			"circular": true,
   			"infinite": false,
   			"auto": { "play": true, "pauseOnHover": true, "pauseDuration": 5000, "duration": 1000 },
   			"pagination" : "#pager-cv_imagens"
  		});
 	}
	if ($("#noticias-carousel").length) {
		noEmptyPages();
		$("#noticias-carousel").carouFredSel({
			"items": { "visible": 1 },
			"circular": false,
			"infinite": false,
			"auto": false,
			"prev": { "button": "#prev-noticias" },
			"next": { "button": "#next-noticias" },
			"pagination": "#pager-noticias",
			"cookie":"noticias-cookie"
		});
	}
	if ($("#notimagens-carousel").length) {
		$("#notimagens-carousel").carouFredSel({
			"items": { "visible": 1 },
			"circular": true,
			"infinite": false,
			"auto": { "play": true, "pauseOnHover": true, "pauseDuration": 5000, "duration": 1000 },
			"pagination"	: "#pager-notimagens"
		});
	}
	if ($("#notdetalhe-carousel").length) {
		$("#notdetalhe-carousel").carouFredSel({
			"items": { "visible": 2 },
			"circular": false,
			"infinite": false,
			"auto": false,
			"prev": { "button": "#prev-notdetalhe" },
			"next": { "button": "#next-notdetalhe" },
			"pagination"	: "#pager-notdetalhe"
		});
	}
	if ($("#papers-carousel").length) {
		noEmptyPages();
		$("#papers-carousel").carouFredSel({
			"items": { "visible": 1 },
			"circular": false,
			"infinite": false,
			"auto": false,
			"prev": { "button": "#prev-papers" },
			"next": { "button": "#next-papers" },
			"pagination": "#pager-papers",
			"cookie":true
		});
	}
	if ($("#papimagens-carousel").length) {
		$("#papimagens-carousel").carouFredSel({
			"items": { "visible": 1 },
			"circular": true,
			"infinite": false,
			"auto": { "play": true, "pauseOnHover": true, "pauseDuration": 5000, "duration": 1000 },
			"pagination" : "#pager-papimagens"
		});
	}
	if ($("#papdetalhe-carousel").length) {
		$("#papdetalhe-carousel").carouFredSel({
			"items": { "visible": 2 },
			"circular": false,
			"infinite": false,
			"auto": false,
			"prev": { "button": "#prev-papdetalhe" },
			"next": { "button": "#next-papdetalhe" },
			"pagination"	: "#pager-papdetalhe"
		});
	}
	if ($("#pesquisa-carousel").length) {
		$("#pesquisa-carousel").carouFredSel({
			"height": 240,
			"items": { "visible": 7, "width": 700, "height": 34 },
			"circular": false,
			"infinite": false,
			"auto": false,
			"pagination": "#pager-pesqui"
		});
	}
	if ($("#contacto-carousel").length) {
		$("#contacto-carousel").carouFredSel({
			"circular": false,
			"items": { "visible": 1 },
			"infinite": true,
			"auto": { "play": true, "duration": "5000"},
			"pagination": "#pager-contacto"
		});
	}
	$('.galeria').click(function(){
		$('.fancybox-thumb:first').click();
		return false;
	});
	$(".fancybox-thumb").fancybox({
		'prevEffect': 'none',
		'nextEffect': 'none',
		'helpers': {
			'title': {
				'type': 'outside'
			},
			'overlay'	: {
				'opacity' : 0.7,
				'css' : {	'background-color' : '#000'	}
			},
			'thumbs': { 'width': 50, 'height': 50 }
		},
		'onComplete': function () {
			$('#homeimagens-carousel, #proimagens-carousel, #notimagens-carousel,  #papimagens-carousel').trigger('pause');
		},
		'onCleanup': function () {
			$('#homeimagens-carousel, #proimagens-carousel, #notimagens-carousel,  #papimagens-carousel').trigger('play');
		}
	});
}
var lasturl="";
function checkURL () {
	hash = window.location.hash;
	if (hash != lasturl) {
		var categoriaGop = hash.match(/\#\!\/gop\/categoria=\d+&ordem=\d+/);
		var categoriaProjecto = hash.match(/\#\!\/projectos\/catProj=\d+&ordem=\d+/);
		var cvProjecto = hash.match(/\#\!\/projectos-cv\/catProj=\d+&ordem=\d+/);
		var projectoDetalhe = hash.match(/\#\!\/projecto-detalhe\/projecto=\d+&catProj=\d+/);
		var noticiaDetalhe = hash.match(/\#\!\/noticia-detalhe\/noticia=\d+&ordem=\d+/);
		var paperDetalhe = hash.match(/\#\!\/paper-detalhe\/paper=\d+&ordem=\d+/);
		if (hash == "#!/inicio/") {
			lasturl = hash;
			$("#container").fadeOut("slow", function() {
				$(this).remove();
				$("#wrapper").load("index.php #container", function() {
					$("#container").hide().fadeIn("slow");
					init();
				});
			});
		} else if (hash=="#!/gop-categorias/") {
			lasturl = hash;
			$("#container").fadeOut("slow", function() {
				$(this).remove();
				$("#wrapper").load("gop-categorias.php #container", function() {
					$("#container").hide().fadeIn("slow");
					init();
				});
			});
		} else if (hash=="#!/projectos-categorias/") {
			lasturl = hash;
			$("#container").fadeOut("slow", function() {
				$(this).remove();
				$("#wrapper").load("projectos-categorias.php #container", function() {
					$("#container").hide().fadeIn("slow");
					init();
				});
			});
		} else if (categoriaGop) {
			lasturl = hash;
			$("#container").fadeOut("slow", function() {
				$(this).remove();
				$("#wrapper").load("gop.php?"+hash.match(/categoria=\d+&ordem=\d+/)+" #container", function() {
					$("#container").hide().fadeIn("slow");
					init();
				});
			});
		} else if (categoriaProjecto) {
			lasturl = hash;
			$("#container").fadeOut("slow", function() {
				$(this).remove();
				$("#wrapper").load("projectos.php?"+hash.match(/catProj=[0-9]+&ordem=[0-9]+/)+" #container", function() {
					$("#container").hide().fadeIn("slow");
					init();
				});
			});
		} else if (cvProjecto) {
			lasturl = hash;
			$("#container").fadeOut("slow", function() {
				$(this).remove();
				$("#wrapper").load("cv.php?"+hash.match(/catProj=[0-9]+&ordem=[0-9]+/)+" #container", function() {
					$("#container").hide().fadeIn("slow");
					init();
				});
			});
		} else if (projectoDetalhe) {
			lasturl = hash;
			$("#container").fadeOut("slow", function() {
				$(this).remove();
				$("#wrapper").load("projecto-detalhe.php?"+hash.match(/projecto=\d+&catProj=\d+/)+" #container", function() {
					$("#container").hide().fadeIn("slow");
					init();
				});
			});
		} else if (noticiaDetalhe) {
			lasturl = hash;
			$("#container").fadeOut("slow", function() {
				$(this).remove();
				$("#wrapper").load("noticia-detalhe.php?"+hash.match(/noticia=\d+&ordem=\d+/)+" #container", function() {
					$("#container").hide().fadeIn("slow");
					init();
				});
			});
		} else if (paperDetalhe) {
			lasturl = hash;
			$("#container").fadeOut("slow", function() {
				$(this).remove();
				$("#wrapper").load("paper-detalhe.php?"+hash.match(/paper=\d+&ordem=\d+/)+" #container", function() {
					$("#container").hide().fadeIn("slow");
					init();
				});
			});
		} else if (hash=="#!/noticias/") {
			lasturl = hash;
			$("#container").fadeOut("slow", function() {
				$(this).remove();
				$("#wrapper").load("noticias.php #container", function() {
					$("#container").hide().fadeIn("slow");
					init();
				});
			});
		} else if (hash=="#!/papers/") {
			lasturl = hash;
			$("#container").fadeOut("slow", function() {
				$(this).remove();
				$("#wrapper").load("papers.php #container", function() {
					$("#container").hide().fadeIn("slow");
					init();
				});
			});
		} else if (hash=="#!/contacto/") {
			lasturl = hash;
			$("#container").fadeOut("slow", function() {
				$(this).remove();
				$("#wrapper").load("contacto.php #container", function() {
					$("#container").hide().fadeIn("slow");
					init();
				});
			});
		} else if (hash=="") {
			lasturl = hash;
			$("#container").fadeOut("slow", function() {
				$(this).remove();
				$("#wrapper").load("index.php #container", function() {
					$("#container").hide().fadeIn("slow");
					init();
				});
			});
		}
	}
}
var hashed_url;
function triggers () {
	$.ajaxSetup({
		cache: false
	});
	function changeURL (url) {
		var template = url.match(/^[a-zA-Z-]+.php/);
		if (url.match(/\?[a-zA-Z]+=[0-9]+&[a-zA-Z]+=[0-9]+/)) {
			var request = url.match(/[a-zA-Z]+=[0-9]+&[a-zA-Z]+=[0-9]+/);
		}
		if (template=="index.php") {
			var nome = "inicio";
			var campos = "/";
		} else if (template=="gop-categorias.php") {
			var nome = "gop-categorias";
			var campos = "/";
		} else if (template=="gop.php") {
			var nome = "gop";
			var campos = "/"+request+"/";
		} else if (template=="projectos-categorias.php") {
			var nome = "projectos-categorias";
			var campos = "/";
		} else if (template=="projectos.php") {
			var nome = "projectos";
			var campos = "/"+request+"/";
		} else if (template=="cv.php") {
			var nome = "projectos-cv";
			var campos = "/"+request+"/";
		} else if (template=="projecto-detalhe.php") {
			var nome = "projecto-detalhe";
			var campos = "/"+request+"/";
		} else if (template=="noticias.php") {
			var nome = "noticias";
			var campos = "/";
		} else if (template=="noticia-detalhe.php") {
			var nome = "noticia-detalhe";
			var campos = "/"+request+"/";
		} else if (template=="papers.php") {
			var nome = "papers";
			var campos = "/";
		} else if (template=="paper-detalhe.php") {
			var nome = "paper-detalhe";
			var campos = "/"+request+"/";
		} else if (template=="contacto.php") {
			var nome = "contacto";
			var campos = "/";
		} else {
			var nome = "inicio";
			var campos = "/";
		}
		hashed_url = "#!/"+ nome + campos;
		return hashed_url;
	}
	$('.down-trigger').one('click', function () {
		var href = $(this).attr('href');
		hashed_url = changeURL(href);
		var toLoad = href+' #container';
		$('#container').attr('id','to-go');
		$('#wrapper').append('<div id="newcontainer"/>');
		var newcontainer = $('#newcontainer');
		newcontainer.load(toLoad, function () {
			newcontainer.append($('#container').children()).css({'position': 'absolute', 'top': '696px'});
			$('#to-go, #newcontainer').animate({top:'-=691'},600, function () {
				$('#to-go').remove();
				carousels();
				changeTitle();
			});
			$('#container').remove();
			newcontainer.attr('id','container');
			searchform();
			triggers();
			window.location.hash = hashed_url;
			lasturl = window.location.hash;
		});
		return false;
	});
	$('.back').one('click', function () {
		var href = $(this).attr('href');
		hashed_url = changeURL(href);
		
		var value = "";
		if (href=="index.php") {
			document.cookie = 'caroufredsel_cookie_categorias-equipa-carousel'+'='+value+'; path=/';
			document.cookie = 'caroufredsel_cookie_equipa-carousel'+'='+value+'; path=/';
			document.cookie = 'caroufredsel_cookie_papers-carousel'+'='+value+'; path=/';
			document.cookie = 'caroufredsel_cookie_categorias-carousel'+'='+value+'; path=/';
			document.cookie = 'caroufredsel_cookie_projectos-carousel'+'='+value+'; path=/';
			document.cookie = 'noticias-cookie'+'='+value+'; path=/';
		}
		
		var toLoad = href+' #container';
		$('#container').attr('id','to-go');
		$('#wrapper').append('<div id="newcontainer"/>');
		var newcontainer = $('#newcontainer');
		newcontainer.load(toLoad, function () {
			newcontainer.append($('#container').children()).css({'position': 'absolute', 'top': '-696px'});
			$('#to-go, #newcontainer').animate({top:'+=701'},600, function () {
				$('#to-go').remove();
				carousels();
				changeTitle();
			});
			$('#container').remove();
			newcontainer.attr('id','container');
			searchform();
			triggers();
			window.location.hash = hashed_url;
			lasturl = window.location.hash;
		});
		return false;
	});
	$('.right-trigger').one('click', function () {
		var href = $(this).attr('href');
		hashed_url = changeURL(href);
		var toLoad = href+' #container';
		$('#container').attr('id','to-go');
		$('#wrapper').append('<div id="newcontainer"/>');
		var newcontainer = $('#newcontainer');
		newcontainer.load(toLoad, function () {
			newcontainer.append($('#container').children()).css({'position': 'absolute', 'left': '942px'});
			$('#to-go, #newcontainer').animate({left:'-=937'},600, function () {
				$('#to-go').remove();
				carousels();
				changeTitle();
			});
			$('#container').remove();
			newcontainer.attr('id','container');
			searchform();
			triggers();
			window.location.hash = hashed_url;
			lasturl = window.location.hash;
		});
		return false;
	});
	$('.left-trigger').one('click', function () {
		var href = $(this).attr('href');
		hashed_url = changeURL(href);
		var toLoad = href+' #container';
		$('#container').attr('id','to-go');
		$('#wrapper').append('<div id="newcontainer"/>');
		var newcontainer = $('#newcontainer');
		newcontainer.load(toLoad, function () {
			newcontainer.append($('#container').children()).css({'position': 'absolute', 'left': '-942px'});
			$('#to-go, #newcontainer').animate({left:'+=947'},600, function () {
				$('#to-go').remove();
				carousels();
				changeTitle();
			});
			$('#container').remove();
			newcontainer.attr('id','container');
			searchform();
			triggers();
			window.location.hash = hashed_url;
			lasturl = window.location.hash;
		});
		return false;
	});
}
function init() {
	setInterval("checkURL(); changeTitle();",250);
	searchform();
	carousels();
	triggers();
}
$(function () {
	init();
});
