
function truncateTicker(){
 jQuery('.list-wrapper ul li').each(function(index) {
	  
	var obj = $(this);
	var max_height = 70;
	
	var titleobj = $('.title',obj);
	var copyobj = $('.copy',obj);
	
	var title_height = titleobj.height();
	
	if(title_height + copyobj.height() > max_height){ 
		
		
		var arr_words = String(copyobj.html()).split(" ");
		var new_content = "";
		
		for (var i = 0 ; i < 500 ; i ++ ){
			
			var	old_content = new_content;
			if (new_content.length > 0){
				new_content+=" ";
			}
			
			new_content+=arr_words[i];
			
			copyobj.html(new_content + " ..." );
			
			if( title_height + copyobj.height() > max_height){ 						  	
				copyobj.html(old_content + " ...");//old_content + "...");
				break;							
			}
		}
		
	}
 });	
}

jQuery(document).ready(function() {

  var numItems = jQuery('.ticker ul li div').length;
  var ticker_pos = 0;
  var old_ticker_pos = 0;
  var max_ticker_pos = numItems - 2;
  var animating = false;
  var stopAnimating = false;
  var timer;
 
  jQuery('.ticker').hover(
	  function()
	  {				
		  stopAnimation();
	  },
	  function()
	  {
		  doAnimation();
	  }
  );
	  
  jQuery('.ticker-button').click(
	  function(e)
	  {
		  e.preventDefault();
		  stopAnimating = true;
		  clearTimeout(timer);
		  animate(this.id);
	  }
  );

  jQuery('.list-wrapper ul li').hover(			
	  function()
	  {
		  if(jQuery(this).attr('class')!='no-hover')
		  {
			  jQuery(this).addClass('hover');
		  }
	  },
	  function()
	  {
		  if(jQuery(this).attr('class')!='no-hover')
		  {	
			  jQuery(this).removeClass('hover');
		  }
	  }			
  );

  var animate = function(id)
  {			
	  if(animating==false)
	  {
		  animating = true;
		  old_ticker_pos = ticker_pos;
		  
		  if(id=='ticker-button-left')
		  {
			  
			  ticker_pos--;
			  
			  if (ticker_pos < 0){
				ticker_pos = max_ticker_pos;  
			  }
			  
		  }
		  else
		  {
			  
			  ticker_pos++;
			 
			  if (ticker_pos > max_ticker_pos){
				ticker_pos = 0;
				
			  }
			  
		  }
		  if(ticker_pos == 0) {
		    jQuery('.list-wrapper ul').animate({'left' : "+="+String(302*(old_ticker_pos - ticker_pos))  }, 0);
		    ticker_pos = 0;
		    animating = false;
		  }
		  else {
		    jQuery('.list-wrapper ul').animate({'left' : "+="+String(302*(old_ticker_pos - ticker_pos))  }, 550, 'easeOutCubic', onAnimateDone);
		  }
		  

		  if(stopAnimating==false)
		  {
			  doAnimation();
		  }
	  }

  }
  
   

  function doAnimation()
  {
	  stopAnimating = false;
	  clearTimeout(timer);
	  timer = setTimeout(function() { animate("ticker-button-right"); }, 4000);			
  }

  function stopAnimation()
  {
	  stopAnimating = true;
	  clearTimeout(timer);
  }

  doAnimation();
  
  function onAnimateDone()
  {
	  animating = false;
	  
  }
  
  setTimeout("truncateTicker()",1000);
  
});	
