window.onload = function() {
	
	
	/*$('.c').click(function(){
		displayLogin();
	});*/
	
	$('.share-orkut').click(function(){
		id = $(this).attr('id').replace('share-', '');
		$.post(".", {shared: 'orkut', post_id: id}, function(data) {});
	});
	
	$('#author').blur(function(){
		author = $('#author').val();
		if (author[0] == '@') $('#url').val($('#author').val().replace(/(^|\s)@(\w+)/g, "$1http://twitter.com/$2"));
	});
	
	$('#commentform').submit(function(){
		if ($('#url').val() == 'http://Site' || $('#url').val() == 'Site') { $('#url').val(''); }
		if ($('#author').val() == 'Nome ou @') { $('#author').val(''); }
		return true;
	});
	
	// 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;
	});
	
	$('.gallery-icon a').click(function(){
		console.log(_gaq.push(['_trackEvent', 'Galeria', 'click', document.title]));
	});
	
}

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);
}

var ref = document.referrer;
var sevisitor;
var SE = new Array('web.info.com', 'search.', 'del.icio.us/search', 'soso.com', '.yahoo.','/url','/search','.google.','.ask.','.baidu.','.bing.','.aol.','.answers.');
for ( var source in SE){
var match = ref.indexOf(SE[source]);
if (match !=-1) {
sevisitor=true;
}
}

function adsenseTop(){
if (sevisitor){
var large = '<div style="margin:10px 0;"><script type="text/javascript"><!-- \n\
google_ad_client = "pub-5896325915799888"; \n\
/* 336x280, criado 05/02/09 */ \n\
google_ad_slot = "5269200846"; \n\
google_ad_width = 336; \n\
google_ad_height = 280; \n\
//--> \n\
</script> \n\
<script type="text/javascript" \n\
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> \n\
</script></div> \n\
';
document.write(large);
}
}
