var images_directory = template_directory + '/imagens/';
var label_exibir = 'Mostrar';
var label_ocultar = 'Esconder';

$(function() {
	$(document).ready(function() {
		if ( $(document).find("#s") ) {
			$("#s").focus(function() {
				if ( $("#s").val() == "Pesquisar no blxg" ) {
					$("#s").val('');
				}
			});
			$("#s").blur(function() {
				if ( $("#s").val() == "" ) {
					$("#s").val('Pesquisar no blxg');
				}
			});
			if ( $("#s").val() == "" ) {
				$("#s").val('Pesquisar no blxg');
			}
		}
	});
	
	// Mostra os links com a classe 'external' em uma nova janela
	$('a.external').click(function(e) {
		window.open( $(this).attr('href') );
		e.preventDefault();
	});
	
	//Carrega primeira imagem
	$('.cabecalho-img').attr('src', images_directory + 'cabecalho/' + imagens[0]);

	var cookie = ($.cookie('cabecalho') === 'false') ? false : true;
	
	if (cookie) {
		cabecalhoIntervalo(true);
	} 
	
	$('#cabecalho-box').toggleClass('mostra', cookie);
	$('#cabecalho-box').toggle(cookie);
	$('#cabecalho-lnk').html(cookie ? label_ocultar : label_exibir);
	$('#cabecalho-lnk').addClass(cookie ? 'cabecalho-controle-cima' : 'cabecalho-controle-baixo');
	
	$('#cabecalho-lnk').click(function(e) {
		$('#cabecalho-box').slideToggle('slow', function() {
				cookie = $(this).hasClass('mostra');
				$.cookie('cabecalho', !cookie);
				
				cabecalhoIntervalo(!cookie);
				$('#cabecalho-box').toggleClass('mostra', !cookie);
				$('#cabecalho-lnk').html(!cookie ? label_ocultar : label_exibir);
				
				$('#cabecalho-lnk').toggleClass('cabecalho-controle-cima');
				$('#cabecalho-lnk').toggleClass('cabecalho-controle-baixo');
			});
		e.preventDefault();
	});
	
	$('a.compartilhar').click(function(e) {
	
		// Puxa o link e o título do post pela tag <h2>
		post_permalink = $(this).parent().parent().parent().children('h2').children('a').attr('href');
		post_titulo = $(this).parent().parent().parent().children('h2').text();
		
		share_delicious_link = 'http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url='+encodeURIComponent(post_permalink)+'&amp;title='+encodeURIComponent(post_titulo);
		share_delicious = '<a href="'+share_delicious_link+'" title="Delicious"><img src="' + images_directory + 'social/delicious.png" alt="delicious.com" /></a>';
		
		share_digg_link = 'http://digg.com/submit?url='+encodeURIComponent(post_permalink)+'&amp;title='+encodeURIComponent(post_titulo);
		share_digg = ' <a href="'+share_digg_link+'" title="Digg"><img src="' + images_directory + 'social/digg.png" alt="digg.com" /></a>'
		
		share_reddit_link = 'http://reddit.com/submit?url='+encodeURIComponent(post_permalink)+'&amp;title='+encodeURIComponent(post_titulo);
		share_reddit = ' <a href="'+share_reddit_link+'" title="Reddit"><img src="' + images_directory + 'social/reddit.png" alt="digg.com" /></a>'

		share_technorati_link = 'http://technorati.com/cosmos/search.html?url='+encodeURIComponent(post_permalink);
		share_technorati = ' <a href="'+share_technorati_link+'" title="Technorati"><img src="' + images_directory + 'social/technorati.png" alt="technorati.com" /></a>'

		share_stumbleupon_link = 'http://www.stumbleupon.com/submit?url='+encodeURIComponent(post_permalink)+'&amp;title='+encodeURIComponent(post_titulo);
		share_stumbleupon = ' <a href="'+share_stumbleupon_link+'" title="StumbleUpon"><img src="' + images_directory + 'social/stumbleupon.png" alt="digg.com" /></a>'
				
		compartilhamento = '<div class="sharethis">'+share_delicious+share_technorati+share_digg+share_stumbleupon+share_reddit+'</div>';
		
		$(this).replaceWith(compartilhamento);

		// Varre os links recem criados para que abram em uma nova janela.
		$('div.sharethis').children('a').click(function(f) {
			window.open( $(this).attr('href'), $(this).attr('title'), 'toolbar=no,width=550,height=550' );
			f.preventDefault();
		});
		
		e.preventDefault();				
		
	});
	
}
);

var contador = 1;
function cabecalhoTroca() {
	var imagem = imagens[contador];
	
	$('.cabecalho-img').ImageSwitch({
			Type:"ScrollIn",
			NewImage: images_directory + 'cabecalho/' + imagens[contador],
			Direction:"DownTop",
			EffectOriginal: true,
			StartOpacity:1
			});
			
	contador = (++contador >= imagens.length) ? 0 : contador;
}

var cabecalho_timer;

function cabecalhoIntervalo(inicializa) {
	if (inicializa) {
		cabecalho_timer = setInterval("cabecalhoTroca()", 10000);
	} else {
		clearInterval(cabecalho_timer);
	}
}
