var news_ticker; //array with all the news ticker data
var current_news_ticker = 0; // index of the current displayed news ticker
var current_index = 0; // index of the current sentence index, we can't display the full content sentence.
var MAX_SCROLL = 50; // number max of charater to display
var timer; // current timer
var pause_flag = 1; //active or deactive the pause

$(document).ready(function() {
	 	 
   $("#pause_btn").click(function() {
	   
	   //Stop the scrolling on the text
	   if(pause_flag){
		   clearTimeout(timer);
		   pause_flag = 0;
	   }
	   else{
		   //Restart the scrolling on the text
		   timer = setTimeout("display_content()", 100);		   
		   pause_flag = 1;		   
	   }
   });
   
   $("#previous_btn").click(function() {
	   //Select the next news ticker
	   if(current_news_ticker>0) current_news_ticker--;
	   init_content();
	   display_content();
    });
   
   $("#next_btn").click(function() {
	   //Select the previous news ticker
	   if(current_news_ticker < ($(news_ticker).size() - 1) ) current_news_ticker++;
	   else current_news_ticker = 0;
	   init_content();
	   display_content();
    });   
   
   
   //Get all the news ticker from database
   $.getJSON("news_ticker_ajax.php",
        function(data){
   		
   		 news_ticker = data;
   
   		 //Setup first content
   		 if($(data).size() > 0){
   		  $('#news_ticker_display').show();
   		  current_news_ticker = 0;
   		  init_content()
   		  display_content();
   		 }
   		 
        });   
 });
 


//Init the content and label
function init_content(){		  
   clearTimeout(timer);
   var label = news_ticker[current_news_ticker]['label'];

   //display the title smoothly
   $('#label_field').hide();
   $('#label_field').text(label);
   $('#label_field').fadeIn();
   
   //The number of max character to display change according to the size of the title
   if(label.length > 15)
	   MAX_SCROLL = 50;
   else
	   MAX_SCROLL = 60;
   
   
   text = news_ticker[current_news_ticker]['content'];
   
   //The size of the content to display change according to the size of the content
   //We can display the full content or just a part of it
   if(text.length > MAX_SCROLL)
	   current_index = MAX_SCROLL-1;
   else
	   current_index = text.length-1;
      
}

//Rotate the content sentence 
function display_content(){
	
   current_index++;

   
   //Calculate the start character
   if(current_index > MAX_SCROLL){
	 start = current_index - MAX_SCROLL;
   }
   else{
     start = 0;
   }
   
   var selected_content = news_ticker[current_news_ticker]['content'];
   var text = selected_content.substring(start, current_index);
      

   if(start == 0){//First loop, display the message with a fade in
	   $('#content_field').hide();
	   $('#content_field').text(text);
	   $('#content_field').fadeIn();
	   
   }
   else{//No fade
	   $('#content_field').text(text);	   
   }
	   
		
   if(current_index < selected_content.length  ){
	   if(start == 0){//First loop, display the message longer
		   timer = setTimeout("display_content()", 3000);
	   }
	   else{
		   timer = setTimeout("display_content()", 100);
	   }
   }
   else{//Last loop, display the message longer
	   timer = setTimeout("rotate()", 4000);
   }
                        
   
}

//Rotate the news ticker
function rotate(){
   if(current_news_ticker < ($(news_ticker).size() - 1) ) current_news_ticker++; //next news ticker
   else current_news_ticker = 0; // go back the first one
   init_content();
   display_content();
}

