RELOAD_TIME_OUT = null;
RELOAD_TIME_INTERVAL = 2000;



$(function(){

  RELOAD_TIME_OUT = setTimeout(loadNewTweets, RELOAD_TIME_INTERVAL);
  
  var pane = $("div#liveMessagesPaneContainer");
  pane.scrollTo( 0 );
  
  /*
  $("div.pagination a.scroll-up").click(function(){
    
    pane.stop().scrollTo( '-=100px', 500 );
    return false;
    
  })
  $("div.pagination a.scroll-down").click(function(){
    
    pane.stop().scrollTo( '+=100px', 500 );
    return false;
    
  })
  */
  initUp();
  initDown();
  checkIfAtMax();
  
  
  if (window.location.href.match("#")) {
			var urlArray = window.location.href.split("#");
			var sharpValue = urlArray[1];
			//window.location.replace(urlArray[0]+"#live-feed");
			$("div#liveMessagesPaneContainer").stop().scrollTo( $("#"+sharpValue), 500, {onAfter:checkIfAtMax, easing:"swing"} );
//			window.location.replace(urlArray[0]+"#"+urlArray[1]);	
		}
		
});


function initUp() {
  $(".pagination a.scroll-up").bind("click",scrollUp );
}
function resetUp() {
  $(".pagination a.scroll-up").unbind("click",scrollUp );
}

function initDown() {
  $(".pagination a.scroll-down").bind("click",scrollDown );
}
function resetDown() {
  $(".pagination a.scroll-down").unbind("click",scrollDown );
}

function scrollUp() {
  var h = $("div#liveMessagesPaneContainer").height();
  $("div#liveMessagesPaneContainer").stop().scrollTo( '-='+h+'px', 500, {onAfter:checkIfAtMax, easing:"swing"} );
  return false;
}
function scrollDown() {
  var h = $("div#liveMessagesPaneContainer").height();
  $("div#liveMessagesPaneContainer").stop().scrollTo( '+='+h+'px', 500, {onAfter:checkIfAtMax, easing:"swing"} );
  return false;
}

function checkIfAtMax() {
  var pane   = $("div#liveMessagesPaneContainer")
  var inside = $("div#liveMessagesPaneContainer div.live-messages-container");
  var top = inside.position().top; 
  
  if(top==0)
    $(".pagination a.scroll-up").css("opacity", "0.3");
  else
    $(".pagination a.scroll-up").css("opacity", "1");
  
  var diff = ( top + (inside.height() - pane.height()) );
  
  if(diff<=0)
    $(".pagination a.scroll-down").css("opacity", "0.3");
  else
    $(".pagination a.scroll-down").css("opacity", "1");
    
}


function reloadMessages( data )
{
  clearTimeout(RELOAD_TIME_OUT);
  RELOAD_TIME_OUT = setTimeout(loadNewTweets, 1000);
}

function loadNewTweets()
{
  var es = $(".live-message");
  var recent=0;
  if(es.length > 0)
  {
    recent = $(es[0]).attr("id").split("mes-").join("");
  }

  $.post("/lm", {id: recent, event:PAGEID}, newMesssLoaded, "json");
}

function newMesssLoaded( data )
{
  
  if(data.length)
  {
    var pane = $("div#liveMessagesPaneContainer");
    pane.scrollTo( 0 );
    checkIfAtMax();    
    
    var cnt=0;
    for(var a=data.length-1; a>=0; a--)
    {
      t = data[a];
      var tw = buildEntry(t);
    	var newelem = $(tw)
    	$(".live-messages-container").prepend(newelem.hide());
    	
    	var time = cnt*650;
    	var fnc = Delegate.create(newelem, deployNewTweet);
    	if(cnt>0)
    	 setTimeout(fnc, time);
    	else
    	 fnc();
    	cnt++;
    }
  }
  
  RELOAD_TIME_OUT = setTimeout(loadNewTweets, RELOAD_TIME_INTERVAL + (data.length*650));
}
function deployNewTweet()
{
  $(this).slideDown(500);
}