$(document).ready(function(){
	var HOME = "http://"+top.location.host.toString();
	FormatarLinksExternos();
	LoginLinkToggle();
	LightboxBind();
	GaleriasHoverFade();
	CollapsablePosts();
	TopSlideshow();
	PostGallery();
	GaleriaWeb();
	BackgroundResizer();
	Searchform();
	GoogleAnalytics();
	function GoogleAnalytics(){
		var pageTracker = _gat._getTracker("UA-1349393-27");
		pageTracker._trackPageview();
		}
	function BackgroundResizer(){
		bgCheck();
		function bgCheck(){
			if($(window).width()>=1440){
				$('html').addClass('big');
				}
			else{
				$('html').removeClass('big');
				}
			}
		$(window).resize(bgCheck);
		}
	function FormatarLinksExternos(){
		$(".post a,.site a").not("a[href^='"+HOME+"'],a[iref^='"+HOME+"']").each(
			function(){
				$(this).attr("rel","external").attr("target","blank_");
				}
			);
		}
	function Searchform(){
		if($('#s').length){
			$('#s').addClass('begin').attr('value','Palavra-chave...').click(
				function(){
					if($(this).attr('value')=='Palavra-chave...'){
						$(this).removeClass('begin').attr('value','');
						}
					}
				)
			.blur(
				function(){
					if($(this).attr('value')==''){
						$(this).addClass('begin').attr('value','Palavra-chave...');
						}
					}
				);
			}
		}
	function LoginLinkToggle(){
		$('#login-form input[type="text"]').attr('value','utilizador');
		$('#login-form input[type="password"]').attr('value','password');
		$('#login-form input[type="text"],#login-form input[type="password"]').addClass('begin').click(
			function(){
				$('#login-form input[type="text"],#login-form input[type="password"]').removeClass('begin').attr('value','');
				}
			)
		.blur(
			function(){
				if($('#login-form input[type="text"]').attr('value')==''&&$('#login-form input[type="password"]').attr('value')==''){
					$('#login-form input[type="text"],#login-form input[type="password"]').addClass('begin');
					$('#login-form input[type="text"]').attr('value','utilizador');
					$('#login-form input[type="password"]').attr('value','password');
					
					}
				}
			);
		$("#login-link").attr('href','#entrar').click(
			function(){
				$("#login-form").slideToggle();
				var targetOffset = $("#login-form").offset().top-20;
				$('html').animate({scrollTop: targetOffset}, 250);
				}
			);
		}
	function GaleriasHoverFade(){
		$("a img").hover(
			function(){
				$(this).fadeTo(250,0.85);
				},
			function(){
				$(this).fadeTo(100,1);
				}
			);
		}
	function LightboxBind(){
		$("img.size-medium,img.size-large,img.size-thumbnail").parents('a').not("a[rel='external']").each(
			function(){
				if($(this).attr('href')!="#abrir"){
					href=$(this).attr('href');
					$(this).attr('href','#abrir').attr('iref',href);
					$(this).children('img.size-medium,img.size-large,img.size-thumbnail').bind('click',
						function(){
								LightboxOpen($(this).parents('a').attr('iref'));
								//return false;
							}
						);
					}
				}
			);
		}
	function CollapsablePosts(){
		function cola(par){
			$('.post-content').slideUp(250).parents('.post').css({opacity: "0.80",cursor:"pointer"});
			$('.activo').removeClass('activo');
			par.css({opacity: "1",cursor:"auto"}).children('.post-content').slideDown(250,
				function(){
					par.addClass('activo');
					var targetOffset = par.offset().top-20;
					if($.browser.safari){ bodyelem = $("body") } else{ bodyelem = $("html,body") }
					bodyelem.animate({scrollTop: targetOffset}, 250);
					}
				);
			}
		if($(".post-content").length>1){
			$(".post-content:gt(0)").hide().parents('.post').css({opacity: "0.80",cursor:"pointer"});
			$(".post:eq(0)").addClass('activo');
			$(".post").click(
				function(){
					if(!$(this).hasClass('activo')){
						cola($(this));
						}	
					}
				);
			$('.post a').click(
				function(){
					if(!$(this).parents('.post').hasClass('activo')){
						cola($(this).parents('.post'));
						return false;
						}
					}
				);
			}
		}
	function TopSlideshow(){
		if(!$("#top-slideshow").length==0){
			setInterval(topSlide, 2500);
			$("#top-slideshow div").click(
				function(){
					window.location = $(this).find("a").attr('href');
					}
				);
			}
		}
	function topSlide() {
		var active = $('#top-slideshow div.active');
		if ( active.length == 0 ) active = $('#top-slideshow div:last');
		var next =  active.next().length ? active.next()
			: $('#top-slideshow div:first');
		active.addClass('last-active').animate({opacity: 0}, 1000);
		next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, 
			function() {
				active.removeClass('active last-active');
				}
			);
		}
// WEB
	function GaleriaWeb(){
		HideIni();
		function HideIni(){
			$("#galeria-web dl").slice(3).hide().addClass('hid');
			}

		$('#galeria-web li').hover(
			function(){
				if($(this).children().hasClass('hid')==true){
					$("#galeria-web dl").not('hid').hide().addClass('hid');
					i=$('#galeria-web li').index(this);
					ci=(i-(i%3));
					$("#galeria-web dl").slice(ci,(ci+3)).removeClass('hid').fadeIn();
					}
				}
			);
		}
// LIGHTBOX
	function LightboxOpen(iref,type){
		function LightboxZoom(el){
			if(el.hasClass('big')){
				el.removeClass('big').css({maxHeight: ($(window).height()-30)});
				}
			else{
				el.addClass('big').css({maxHeight: 9999});						
				}
			}
		if(!$("#lightbox").lenght>0){
			$('body').append('<div id="lightbox"><em>a carregar a imagem...</em></div>');
			}
		if(type=="gallery"){
			currentimg='<img class="lightbox-image" src="'+iref+'"/>';
			$("#lightbox").html(currentimg).children('img').css({maxHeight: ($(window).height()-30),maxWidth: ($(window).width()-30)});
			$('#lightbox img.lightbox-image').click(
				function(){
					LightboxZoom($(this));
					return false;
					}
				);
			}
		else{
			$.get(iref,
				function(data){
					data = $("<div>").html(data);
					currentimg=data.find("img.attachment-full");
					$("#lightbox").html(currentimg).children('img').addClass('lightbox-image').css({maxHeight: ($(window).height()-30),maxWidth: ($(window).width()-30)});
					$('#lightbox img.lightbox-image').click(
						function(){
							LightboxZoom($(this));
							return false;
							}
						);
					}
				);
			}
		$('#lightbox').click(
			function(){
				$(this).remove();
				if(type=="gallery"){inPostSliding=true;}
				}
			);
		if(type=="gallery"){
			$('#lightbox').hover(
				function(){inPostSliding=false;},
				function(){inPostSliding=true;}
				);
			}
		}
// POST GALLERY
	var inPostSliding;
	function PostGallery(){
		inPostSliding=true;
		if(!$(".post-gallery").length==0){
			$(".post-gallery").css({height: '400px',position: 'relative'});
			$(".post-gallery div.post-gallery-image").css({opacity:'0',position:'absolute',top:'0',left:'0'});
			$(".post-gallery div.post-gallery-image.active").css({opacity:'1'});
			$(".post-gallery .legenda").after('<ul class="nav"><li id="post-gallery-prev"><a><</a></li><li id="post-gallery-next"><a>></a></li></ul>');
			inSlide=setInterval( inSlide, 2500 );
			$(".post-gallery div.post-gallery-image").click(
				function(){
					LightboxOpen($(this).find("div.legenda a").attr('iref'),"gallery");
					}
				);
			$(".post-gallery").hover(
				function(){inPostSliding=false;$('.post-gallery .nav').fadeIn(250);},
				function(){inPostSliding=true;$('.post-gallery .nav').fadeOut(250);}
				);
			$(".post-gallery .legenda a").click(
				function(){
					LightboxOpenGalleryImage($(this).attr('iref'));
					return false;
					}
				);
			$(".post-gallery #post-gallery-prev").click(
				function(){
					var active = $('.post-gallery div.active');
					var next =  active.prev().length ? active.prev() : $('.post-gallery div.post-gallery-image:last');
					active.addClass('last-active').animate({opacity: 0}, 100);
					next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0},100, 
						function() {
							active.removeClass('active last-active');
							}
						);
					return false;
					}
				);
			$(".post-gallery #post-gallery-next").click(
				function(){
					var active = $('.post-gallery div.active');
					var next =  active.next().length ? active.next() : $('.post-gallery div.post-gallery-image:first');
					active.addClass('last-active').animate({opacity: 0}, 100);
					next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 100, 
						function() {
							active.removeClass('active last-active');
							}
						);
					return false;
					}
				);
			}
		function inSlide() {
			if(inPostSliding){
				var active = $('.post-gallery div.active');
				if ( active.length == 0 ) active = $('#top-slideshow div:last');
				var next =  active.next().length ? active.next()
					: $('.post-gallery div:first');
				active.addClass('last-active').animate({opacity: 0}, 1000);
				next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, 
					function() {
						active.removeClass('active last-active');
						}
					);
				}
			}
		}
	});
