window.onload = function() {
	document.getElementById('feedshow').onclick = function(){
		if (document.getElementById('show-hide').style.display == 'none') {
			document.getElementById('show-hide').style.display = 'inline';
			document.getElementById('feedshow').innerHTML = 'Menos opções &laquo;';
		} else {
			document.getElementById('show-hide').style.display = 'none';
			document.getElementById('feedshow').innerHTML = 'Mais opções &raquo;';
		}
		return false;
	};
	
	// SHARE
	$('.share a').click(function(e){
		if (!($("#share").length > 0)) {
			$('body').append('<div style="background:#fff;position:absolute;z-index:4;" id="share" class="boxfloat">'
			+ '<div class="header">Compartilhe<div class="close">X</div></div>'
			+ '<div class="content">'
			+ '<div class="left">'
			+ '<a href="" class="delicious">Delicious</a>'
			+ '<a href="" class="gbookmarks">Google Bookmarks</a>'
			+ '<a href="" class="facebook">Facebook</a>'
			+ '</div>'
			+ '<div class="right">'
			+ '<a href="" class="linkedin">LinkedIn</a>'
			+ '<a href="" class="myspace">MySpace</a>'
			+ '<a href="" class="friendfeed">FriendFeed</a>'
			+ '</div><div class="both"></div>'
			+ '</div>'
			+ '</div>');
			
			$('#share a').attr('target', '_blank');
			
			$('#share .close').click(function(){
				$('#share').remove();
			});
		}
		
		url = $('#post-' + $(this).attr('href').replace('#compartilhe-', '')).find('h2').children('a').attr('href');
		title = $('#post-' + $(this).attr('href').replace('#compartilhe-', '')).find('h2').children('a').html();
		
		// Checa se não estamos em uma singlepage...
		if (url == null) {
			url = $('#post-' + $(this).attr('href').replace('#compartilhe-', '')).find('h1').children('a').attr('href');
			title = $('#post-' + $(this).attr('href').replace('#compartilhe-', '')).find('h1').children('a').html();
		}
		
		$('.delicious').attr('href', 'http://del.icio.us/post?url=' + url + '&title=' + title);
		$('.gbookmarks').attr('href', 'http://www.google.com/bookmarks/mark?op=edit&bkmk=' + url + '&title=' + title);
		$('.facebook').attr('href', 'http://www.facebook.com/share.php?u=' + url + '&t=' + title);
		$('.linkedin').attr('href', 'http://www.linkedin.com/shareArticle?mini=true&url=' + url + '&title=' + title + '&summary=&source=');
		$('.myspace').attr('href', 'http://www.myspace.com/Modules/PostTo/Pages/?l=3&u=' + url + '&t=' + title + '&c=');
		$('.friendfeed').attr('href', 'http://friendfeed.com/share?url=' + url + '&title=' + title);
		
		$('#share').css('left', e.pageX);
		$('#share').css('top', e.pageY);
		return false;
	});
	
	$('.mail a').click(function(e){
		
		if (!($("#sendmail").length > 0)) {
			$('body').append('<div style="background:#fff;position:absolute;z-index:4;" id="sendmail" class="boxfloat">'
			+ '<div class="header">Enviar este post por e-mail<div class="close">X</div></div>'
			+ '<div class="content">'
			+ '<div class="warning"></div>'
			+ '<form action="" method="post" id="enviaemail">'
			+ '<p><label for="enome">Seu nome: </label><input type="text" name="enome" id="enome" /></p>'
			+ '<p><label for="eemail">Seu e-mail: </label><input type="text" name="eemail" id="eemail" /></p>'
			+ '<p><label for="enamigo">Nome do amigo: </label><input type="text" name="enamigo" id="enamigo" /></p>'
			+ '<p><label for="eamigomail">E-mail do amigo: </label><input type="text" name="eamigomail" id="eamigomail" /></p>'
			+ '<p><label for="emensagem">Mensagem: </label><textarea name="emensagem" id="emensagem"></textarea></p>'
			+ '<input type="hidden" name="postID" id="postID" value="" /></input>'
			+ '<p><input type="submit" class="enviar" value="Enviar" ></p>'
			+ '<div class="both"></div>'
			+ '</form>'
			+ '</div>'
			+ '</div>');
			
			$('#sendmail .close').click(function(){
				$('#sendmail').remove();
			});
			
			$('#enviaemail').submit(function(){
				postID = $('#postID').val();
				enome = $('#enome').val();
				eemail = $('#eemail').val();
				enamigo = $('#enamigo').val();
				eamigomail = $('#eamigomail').val();
				emensagem = $('#emensagem').val();
				
				if (enome == '') {
					$('.warning').html('Preencha o seu nome.').show(); return false;
				} else if (eemail == '' || !checkMail(eemail)) {
					$('.warning').html('Preencha o seu e-mail.').show(); return false;
				} else if (enamigo == '') {
					$('.warning').html('Preencha o nome do amigo.').show(); return false;
				} else if (eamigomail == '' || !checkMail(eamigomail)) {
					$('.warning').html('Preencha o e-mail do amigo.').show(); return false;
				}
				
				$('.warning').html('Enviando e-mail, aguarde...').show();
				
				$.post(".",
					{postID: postID, enome: enome, eemail: eemail, enamigo: enamigo, eamigomail: eamigomail,
					emensagem: emensagem, enviarPost: true},
					function(data) {
						if (data == 'true') {
							$('.warning').html('E-mail enviado com sucesso! :)').show();
							$('#enamigo').val('');
							$('#eamigomail').val('');
							$('#emensagem').val('');
						} else {
							$('.warning').html('Houve um problema no envio da mensagem, tente mais tarde.').show();
						}
					});
				return false;
			});
		}
		
		$('.warning').hide();
		
		$('#postID').val($(this).attr('href').replace('#envie-mail-', ''));
		
		$('#sendmail').css('left', e.pageX);
		$('#sendmail').css('top', e.pageY);
		return false;
	});
	
}

function checkMail(mail) {
	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	return er.test(mail);
}