$(function(){ 

	// Check if tweets block is on this page
	if ($('.twitter-panel').length)
	{
	
		// Load up a list of tweets from the V Festival tweet cache.
		
		$.get('/social/tweets', function(data) {
			
			// Turn the data into a jQuery object and hold onto this so we can 
			// delete elements each iteration.		
			
			var dataObject = $(data);
			
			// Remove the first items as we are already showing these in page.
			
			dataObject.find('.v-tweets div:first, .artists-tweets div:first, .your-tweets div:first').remove();
			  		
			// Repeat periodically.  		
  			
  			$(this).everyTime('7s', function() {
  				
				// Move to next Artist Tweet.
			
				dataObject.find('.artists-tweets div:first').each(function(){ 
  				
  					var tweet = $(this).html();
  				
  					$('.artists-tweet').fadeOut(function() {
  					
  						$(this).html(tweet).fadeIn(function() {
  						
  							// Do once after a delay.
  						
  							$(this).everyTime('1s', function() {
								  				
  								// Move to next Your Tweet.
		
								dataObject.find('.your-tweets div:first').each(function(){ 
								
									tweet = $(this).html();
								
		  							$('.your-tweet').fadeOut(function(){
		  								$(this).html(tweet).fadeIn();
		  							});
		  							  							
		  						}).remove();  	  						
  							
  							}, 1);
  											
  						});
  						
  					});
  				
  				}).remove();
				
				// Make sure external links open in a new window.
			
				$("a[href^='http']:not(a[href^='http://localhost'], a[href^='http://www.vfestival.com'], a[href^='http://staging.vfestival.com'])").attr('target','_blank');
  				 		
  			});
  			
		});	
	
	}
		
});

function extractTweet(node) {
  return {
    image: $(node).find('a.profile-image img').attr('src'),
    user: $(node).find('a.user-name').text(),
    href: $(node).find('a.user-name').attr('href'),
    text: $(node).find('.tweet-text').html(),
    date: $(node).find('.date').text()
  };
}

function setTweet(node, tweet) {
  $(node).fadeOut(500, function () {
    $(node).find('.profile-image img').attr('src',tweet.image);
    $(node).find('.user-name').attr('href', tweet.href).html(tweet.user);
    $(node).find('.tweet-text').html(tweet.text + " <span class='date'></span>");
    $(node).find('.date').html(tweet.date);
    $(node).fadeIn(500);
  });
}

$(function(){ 
	if ($('.twitter-panel-long').length) {
	  
	  var vTweets = [],
	      artistTweets = [],
	      yourTweets = [],
	      currentTweet = 3,
	      vTweetIndex = 3,
	      artistTweetIndex = 3,
	      yourTweetIndex = 6;
	      
		$.get('social/tweets', function(data) {
			$(data).find('.v-tweets .tweet').each(function (i, n){
			  vTweets.push(extractTweet(n));
			});
						
			$(data).find('.artists-tweets .tweet').each(function (i, n){
			  artistTweets.push(extractTweet(n));
			});
			
			$(data).find('.your-tweets .tweet').each(function (i, n){
			  yourTweets.push(extractTweet(n));
			});
	
  	});
	
	  setInterval(function () {
	    $('.twitter-panel-long .tweet:eq(' + currentTweet + ')').each(function (i, n) {
	      /*if ($(n).hasClass('v-tweet')) {
	        setTweet(n, vTweets[vTweetIndex]);
	        vTweetIndex = vTweetIndex == vTweets.length - 1 ? 0 : vTweetIndex + 1;
	      } */
	      
	      if ($(n).hasClass('artist-tweet')) {
	        setTweet(n, artistTweets[artistTweetIndex]);
	        artistTweetIndex = artistTweetIndex == artistTweets.length - 1 ? 0 : artistTweetIndex + 1;
	      } else if ($(n).hasClass('your-tweet')){
	        setTweet(n, yourTweets[yourTweetIndex]);
	        yourTweetIndex = yourTweetIndex == yourTweets.length - 1 ? 0 : yourTweetIndex + 1;
	      }
	    });
	    
	    currentTweet = currentTweet == 11 ? 3 : currentTweet + 1;
	  }, 5000);
	  
	}
});
