$(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 playTweets(exec) {	
	milliseconds = .4 * 60 * 1000;
	
	$('.btn_play').html('<em class="silver">Atualizando</em>');
	$('.btn_stop').html('<a href="#nogo" onclick="stopTweets();" title="Pare a atualiza&ccedil;&atilde;o autom&aacute;tica para leitura!">Parar atualiza&ccedil;&otilde;es</a>');
	
	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);
				
				setTimeout(function() { $('#loading').hide(); }, 500);
			}
		});
	}
	
	exec ? f() : '';
	interval = setInterval(f,milliseconds);

	return false;
}

function stopTweets() {
	clearInterval(interval);
	
	$('.btn_play').html('<a href="#nogo" onclick="playTweets(true);" title="Ativar atualiza&ccedil;&atilde;o autom&aacute;tica!">Atualizar</a>');
	$('.btn_stop').html('<span class="silver" title="Atualiza&ccedil;&atilde;o autom&aacute;tica parada. Clique em Atualizar para ativar a atualiza&ccedil;&atilde;o autom&aacute;tica!">Atualiza&ccedil;&atilde;o autom&aacute;tica parada</span>');
	
	return false;
}

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

function popup(obj) {
	var url = obj.url != '' ? obj.url : 'blank';
	var width = typeof(obj.width) != 'undefined' ? obj.width : screen.width;
	var height = typeof(obj.height) != 'undefined' ? obj.height : screen.height;
	var scroll = typeof(obj.scroll) != 'undefined' ? obj.scroll : null;
	if(typeof(obj.resize) != 'undefined') {
		var resize = typeof(obj.resize) != 'undefined' ? obj.resize : null;
	} else {
		var resize = typeof(obj.resizable) != 'undefined' ? obj.resizable : null;
	}
	var left = typeof(obj.left) != 'undefined' ? obj.left : 0;
	var top = typeof(obj.top) != 'undefined' ? obj.top : 0;

	//toolbar=no, fullscreen=no, location=no, menubar=no
	return open(''+url+'', '', 'width='+width+', status=yes, height='+height+', left='+left+', top='+top+' scrollbars='+scroll+', resizable='+resize+'');
}

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

function contato(form) {
	$('.form button').attr('disabled', true);
	$('.form button').html('Aguarde...');
	
	$.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:280
				});
				
				$('.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);
			$('.form button').html('Enviar');
		}
	});
	
	return false;
}

function indique(form) {	
	$('.form button').attr('disabled', true);
	$('.form button').html('Aguarde...');
	
	$.ajax({
		url:'act.php?indicar', type:'post', data:$(form).serialize(),
		success:function(response) { //alert(response);			
			if(response == 'ok') {
				$.alert({
					html:'Indica&ccedil;&atilde;o enviada com sucesso. Agradecemos o interesse!', 
					width:450, timeout:8000
				});
					
				$(form)[0].reset();
			} else if(response == 'fields') {
				$.alert({
					type:'error', html:'Todos os campos s&atilde;o obrigat&oacute;rios.', width:300
				});
				
				$('.form input:first').focus();
			} else {
				$.alert({
					type:'error', html:'N&atilde;o foi poss&iacute;vel enviar. Tente novamente em instantes, por favor.', width:550
				});
			}
			
			$('.form button').attr('disabled', false);
			$('.form button').html('Enviar');
		}
	});
	
	return false;
}
