$(function() {
	$('#search input').focus(function() {
		var val = $(this).val();
		
		if (val.indexOf(' sua busca') != -1) {
			 $(this).val("").removeClass("noselected");
		} else {
			$(this).select();
		}
	});
	
	$('#search input').blur(function() {
		if ($.trim($(this).val()) == "") {
		    $(this).val("Fa\u00e7a sua busca").addClass("noselected");
		}
	});

	//$('#search input').focus();
	$('a[rel=facebox]').facebox();
	
	if(location.href.indexOf('p=') == -1) {
		setInterval(function() { location.reload(); }, 600000);	
	}
})

var query = interval = '';

function getptweets(exec) {	
	milliseconds = .4 * 60 * 1000;
	
	var f = function() {		
		$('#loading').show();
		
		var busca = $('#search input[name=q]').val();
		
		query = busca.indexOf(' sua busca') == -1 ? busca : '';	
		
		$.ajax({
			url:'ptweets.php', type:'post', data:'q='+query, cache:false,
			success:function(response) { //alert(response);
				$('#ptweets .tweets').html(response);
				$('#loading').hide();
				$('a[rel=facebox]').facebox();
			}
		});
	}
	
	exec ? f() : '';
	interval = setInterval(f,milliseconds);

	return false;
}

function rankings() {
	$.ajax({
		url:'hotsearches.php', type:'post', 
		success:function(response) { //alert(response);
			$('.hotsearches span').html(response);
		}
	});
	
	$.ajax({
		url:'ranking.php', type:'post', 
		success:function(response) { //alert(response);
			$('.ranking span').html(response);
		}
	});
}

function setHotSearch(search) {
	$('#search input').removeClass("noselected").val( search );
	getptweets(true);
	$('#search input').focus();
}

function rtPromotweets(form) {
	$('.form button').attr('disabled', true);
	
	$.ajax({
		url:'act.php?rtPromotweets', type:'post', data:$(form).serialize(),
		success:function(response) {
			if(response.indexOf('created_at') != -1) {
				$.facebox.close();
				
				$.alert({ width:350, html:'PromoTweets foi indicado com sucesso. Obrigado!' });
				$(form)[0].reset();
			} else {
				$.alert({
					type:'error', width:550,
					html:'N&atilde;o foi poss&iacute;vel indicar o PromoTweets. Tente novamente em instantes, por favor.'
				});
			}
			
			$('.form button').attr('disabled', false);
		}
	});
	
	return false;
}

function rtTweet(form) {
	$('.form button').attr('disabled', true);
	
	$.ajax({
		url:'act.php?rtTweet', type:'post', data:$(form).serialize(),
		success:function(response) { //alert(response);
			if(response.indexOf('created_at') != -1) {
				$.facebox.close();
				$.alert({ width:250, html:'Tweet retuitado com sucesso!' });
			} else {
				$.alert({
					type:'error', width:550,
					html:'N&atilde;o foi poss&iacute;vel retuitar. Tente novamente em instantes, por favor.'
				});
			}
			
			$('.form button').attr('disabled', false);
		}
	});
	
	return false;
}

function fptweets(exec) {
	clearInterval(interval);	
	getptweets(exec);
	
	return false;
}

function contato(form) {	
	$('.form button').attr('disabled', true);
	
	$.ajax({
		url:'act.php?contato', type:'post', data:$(form).serialize(),
		success:function(response) {
			if(response == 'ok') {
				$.alert({
					html:'Contato enviado com sucesso! Entraremos em contato o mais breve poss&iacute;vel.', 
					width:550, timeout:8000
				});
					
				$(form)[0].reset();
			} else if(response == 'fields') {
				$.alert({
					type:'error', html:'Todos os campos s&atilde;o obrigat&oacute;rios.', width:250
				});
				
				$('.form input:first').focus();
			} else {
				$.alert({
					type:'error', html:'N&atilde;o foi poss&iacute;vel enviar. Tente novamente em instantes, por favor.', width:250
				});
			}
			
			$('.form button').attr('disabled', false);
		}
	});
	
	return false;
}