var currentPage = 1;

$(document).ready(function (){
	
	$("#first_tab").tabs({ cookie: { expires: 1 } });
	
	$("#second_tab").tabs({ cookie: { expires: 1 } });
		
	$("#begin").datepicker();
	
	$("#end").datepicker();
	
	$('a[name=tweet]').click(function (event){
		event.preventDefault();
		var tipo = 'see_tweets';		
		$('#send_button_tweets input').attr('name',tipo);
		paginate(currentPage, tipo);
	});
	
	$('a[name=notification-tweet]').click(function (event){
		event.preventDefault();
		var tipo = 'see_price_tweets';
		$('#send_button_tweets input').attr('name',tipo);
		paginate(currentPage, tipo);
	});
	
	if( $('#second_tab .ui-state-active a').attr('name') == 'tweet' ){
		$('#send_button_tweets input').attr('name','see_tweets');
	}
	else{
		$('#send_button_tweets input').attr('name','see_price_tweets');
	}
	
	var type = $('#send_button_tweets input').attr('name');
	
	$(".msgs").click (function (){$(this).fadeOut(1000)});

	paginate(currentPage, type);

	$(".error_message_blue img").click (function (){$(this).parent().slideUp(1000)});
	$(".error_message_red img").click (function (){$(this).parent().slideUp(1000)});

	$("#remove a").click (function () { 
		var test = confirm('Voce tem certeza que deseja remover essa palavra? \n Com isso, todos os dados armazenados serao perdidos.');		
		if (test)
			window.location = "http://www.mestreseo.com.br/twitter-monitor/remove_word.php?word="+$("#keyword").val();
	});		
});

function paginate ( page, type ){

	var begin = $("#processed_begin").val();
	var end = $("#processed_end").val();
	var keyword = $("#keyword").val();	

	if( $(".paginator_box").text() != ""){
		$(".loader_ajax").show();
		$(".paginator_box").css("opacity", "0.3");		
	}
	
	$.get("change_page.php", { "view_page" : currentPage, "begin" : begin, "end" : end, "keyword" : keyword, "type" : type} , function(data) {
		$(".loader_ajax").hide();
		$(".paginator_box").css("opacity", "1");	
			
		$(".paginator_box").html(data).find(".prev_button").click(  function (event){
			if( currentPage > 1){		
				event.preventDefault();	
				currentPage --;
				paginate( currentPage );
			}				
		}).end().find(".next_button").click( function (event){
			event.preventDefault();	
			currentPage ++;
			paginate( currentPage );
		}).end().find(".paginas a").click ( function (event){
			event.preventDefault();
			currentPage = $(this).text();						
			paginate( currentPage );
		}) ;
	});		
}
