
var load_timeout_id;
var render_timeout_id;
var pause = false;
//var last_id;
function pause_feed(){
  pause = true;
  $("#article_tab a.pause").text('Start');
}

function start_feed(){
  $("#article_tab a.pause").text('Pause');
  pause = false;
  $("#fp_live_items_queue div.item").remove();
 var last_id = new Object(); 
		last_id.last_tweet_id = $(".item.message.twit:first").attr('tid') || 'null';
		last_id.last_treq_id = $(".item.message.treq:first").attr('tid') || 'null';
//  load_next_items();
//  render_next_item();
  next();
}

function update_items_events(){
    diagram();
  $("a.reply, a.retweet, a.shout-out").unbind('click');
  $("a.reply, a.retweet, a.shout-out").click(function(e){pause_feed(); return false;}).click(reply_on_click_fp);
  update_div_twit();
}

function next() {
    load_next_items();
    render_timeout_id = window.setTimeout(render_next_item, 2000);
}

$(document).ready(
    function(){
	$.get('/forex_items', function(data){
			$("#fp_live_items").html('<div class="new_item_cont" style="height: 100px;"></div>');
			$("#fp_live_items").append(data);
      update_items_events();
    	next_interval = window.setTimeout(next, 5000);

		 });

        diagram();
      
    $("#article_tab a.pause").click(
        function(e){
          e.preventDefault();
          var self = $(this);
          if(self.text() == 'Pause'){
            pause_feed();
          }
          else {
            start_feed();
          }
          return false;
        }
      );


      $("#fp_live_options_link a").click(function(e){
        var self = $(this);
        e.preventDefault();
        if(self.attr('show') == 'true'){
          self.attr('show', 'false');
          $("#fp_live_options").show();
        }
        else {
          $("#fp_live_options").hide();
          self.attr('show', 'true') 
        }
      });
      
      update_items_events();

      onhide_reply_box = function(){pause = false; start_feed();};

      $("#tab_article_today").click(function(){
		if ($(this).hasClass('active_tab') == false){		
		$(".active_tab").removeClass("active_tab");
		$(this).addClass("active_tab");
		$(".active_tab_cont").html('');
		$(".active_tab_cont").removeClass("active_tab_cont");
        $("#" + $(this).attr('id') + "_cont").addClass("active_tab_cont");

		$("#fp_live_options_link").css('display', 'block');
		$("#twitter-box").css("display", "block");
        $("#fp_live_items").html('<img src="/static/img/ajax-loader_1122.gif" alt="loading"/>');
		pause_feed();
		$.get('/forex_items', function(data){
			
//			$("#tab_article_today_cont").append(data);
            $("#fp_live_items").html('<div class="new_item_cont" style="height: 100px;"></div>');
            $("#fp_live_items").append(data);
        update_items_events();
        start_feed();
            });
		}	
	});
		  
	
      $("#tab_news").click(function(){
        if ($(this).hasClass('active_tab') == false){
		pause_feed();
		$(".active_tab").removeClass("active_tab");
		$(this).addClass("active_tab");
		$(".active_tab_cont #fp_live_items").html('');
		$(".active_tab_cont").removeClass("active_tab_cont");
        $("#" + $(this).attr('id') + "_cont").addClass("active_tab_cont");
		$("#fp_live_items_queue").html(' ');
		$("#fp_live_options_link").css('display', 'none');
		$("#twitter-box").css("display", "block");
//		$("#news_cont").css("display", "block");
    		$("#tab_news_cont").html('<img src="/static/img/ajax-loader_1122.gif" alt="loading"/>');
			
			$.get('/front_page_news?last_news_id=1', function(data){
            $("#tab_news_cont").html(data);
            update_items_events();
            start_feed();
            });
		}
	 });

});

function load_next_items(){
  if(pause){
    return;
  }
  if($("#fp_live_items_queue div.item").length > 5){
//    load_timeout_id = window.setTimeout(load_next_items, 4000);
    return;
  }
  if($("#tab_news").attr("class") == 'tab active_tab'){
   	var last_id = new Object();
	last_id.last_news_id = 0;
	last_id.last_news_id = $("#tab_news_cont .item.twit:first").attr('tid') || 'null';
    $.getJSON('/front_page_updates_news', last_id, add_new_items_to_queue);
    } 
    else {
    	var last_id = new Object();
		last_id.last_tweet_id = $(".item.message.twit:first").attr('tid') || 'null';
		last_id.last_treq_id = $(".item.message.treq:first").attr('tid') || 'null';
    $.getJSON('/forex_updates', last_id, add_new_items_to_queue);
    }
}


function add_new_items_to_queue(data){
    $("#fp_live_items_queue").html('');
    if($("#tab_news").attr("class") == 'tab active_tab'){
  	 $("#fp_live_items_queue").prepend(data.news); 
  } else {
	 $("#fp_live_items_queue").prepend(data.items); 
  }
}

function render_next_item() {
  if(pause){
    return;
  }
  if($("#tab_news").attr("class") == 'tab active_tab'){
  var fp_live_items = $("#tab_news_cont");
  } 
    else {
  var fp_live_items = $("#fp_live_items");
    }
//console.log($("#fp_live_items_queue > div:last").html());
//  console.log($("#fp_live_items_queue").length);
  var el = $("#fp_live_items_queue > div:last").clone();
//  console.log(fp_live_items);
  $("#fp_live_items_queue > div:last").remove();



  if ($("#fp_live_items_queue").html().length < 100){ 
  window.clearTimeout(next_interval);
  next_interval = window.setTimeout(next, 5000);
  } else { 
    render_timeout_id = window.setTimeout(render_next_item, 2000);
  }
  if(!el.length){

//    timeout_id = window.setTimeout(render_next_item, 2000);
    return;
  }

  var new_item_cont = $("div.new_item_cont:first", fp_live_items); 
  new_item_cont.prepend(el);
  var height = el.height() + 24;
  fp_live_items.css('top', -height);
  new_item_cont.css('height', height);
  new_item_cont.replaceWith(el);

  fp_live_items.prepend('<div class="new_item_cont"><!-- --></div>');
  new_item_cont = $("div.new_item_cont:first", fp_live_items); 
  new_item_cont.animate({height: height}, 1000,
    function(){
//      timeout_id = window.setTimeout(render_next_item, 2000);
		new_item_cont.css('height', 100);
		fp_live_items.css('top', -100);
      $('div.item:gt(40)', fp_live_items).remove();
      update_items_events();
    }
  );
  
}


