var selectedType; //latest, voted, viewed 
$(document).ready(function() {
		selectedType = 'latest';

                $('.btn_login').click(function(){
                    $('.popup_login').toggle();
                    $('#text_area').toggle();
                    $('#avvia_ricerca').toggle();
                });

		$('#homepage_cells .pagination img').live('click', function() {
		
			   page = $(this).attr('id').split('_')[1];
			   wellcomeCells(selectedType, page);
		});
		
		$('#ultimi').click(function(){
			selectedType = 'latest';
			setButtonsNotSelected();
			$(this).addClass("div_left_selected");
			//MM_swapImgRestore();
			//MM_swapImage('ultimi','','/images/new_lajout/pls_ultimi_rollover.jpg',1);
    		
			wellcomeCells(selectedType, 1);
		});
		$('#piu_letti').click(function(){
			selectedType = 'voted';
			setButtonsNotSelected();
			$(this).addClass("div_mid_selected");
			//MM_swapImgRestore();
    		//MM_swapImage('piu_letti','','/images/new_lajout/pls_piu_letti_rollover.jpg',1);
			wellcomeCells(selectedType, 1);
		});	
		$('#piu_votati').click(function(){
			selectedType = 'viewed';
			setButtonsNotSelected();
			$(this).addClass("div_right_selected");
			//MM_swapImgRestore();
			//MM_swapImage('piu_votati','','/images/new_lajout/pls_piu_commentati_rollover.jpg',1);
			wellcomeCells(selectedType, 1);
		});		
		//$('#ultimi').addClass('div_left_selected');
		wellcomeCells('latest', 1);
		getLatestNews();
		$(document).everyTime(30000,function(){
			getLatestNews();
	  	});

 	

});

function scrollerEnds()
{
}

function wellcomeCells(type, page)
{
	//aniHeight = ($('#cells_layout').height())/2;
	$('#cells_layout ').css({'visibility':'hidden'});
	//$('.page_animation ').css({'bottom':aniHeight});
	$('#page_animation').css({'display':'inline-block'});
	
			$.ajax({
		    url: '/',
		    type: 'POST',
		    data: "type="+type+"&page="+page,
		    success: function(response){
				$("#homepage_cells").html(response);
				$('#page_animation').css({'display':'none'});
		    },
			error:function(response){
			}  
	      });
}
function newsCarousel ()
{
	$(".newsticker-jcarousellite").jCarouselLite({
			vertical: true,
			hoverPause:true,
			visible: 5,
			auto:2000,
			speed:1000,
			afterEnd:function() {
				//alert("After animation ends:" + a);
			}
		});
}
function getLatestNews()
{
	
			$.ajax({
		    url: '/',
		    type: 'POST',
		    data: "news=news",
		    success: function(response){
				$("#newsticker-layout .newsticker-jcarousellite").html(response);
				
				newsCarousel();
				//$("#newsticker-layout").css({'display':'block'});
				//$('#page_animation').css({'display':'none'});
		    },
			error:function(response){
			}  
	      });
}

function setButtonsNotSelected()
{
	$('#ultimi').removeClass("div_left_selected");
	$('#piu_letti').removeClass("div_mid_selected");
	$('#piu_votati').removeClass("div_right_selected");
	$('#ultimi').addClass("div_left");
	$('#piu_letti').addClass("div_mid");
	$('#piu_votati').addClass("div_right");
}

