var tweets = {
  timestamp: 0,
  interval: null,
  tweets: Array(),
  
  init: function(){
    tweets.getTweets();
    
    tweets.interval = setInterval(function(){
      tweets.setTweet();
    }, 1000);
  },
  
  getTweets: function(){
    var date = new Date();
    $.getJSON('getLatestTweets.php?timestamp=' + tweets.timestamp + '&rand=' + date.getTime(), function(json){
      var results = json.results;
        
      for(i=0; i < results.length; i++){
        if(results[i].timestamp > tweets.timestamp) tweets.timestamp = results[i].timestamp;
      
        tweets.tweets[tweets.tweets.length] = results[i].content;
      }
      
      setTimeout(function(){
        tweets.getTweets();
      }, 30000);
      
    });
  },
  
  setTweet: function(){
    if(tweets.tweets.length > 0){
      var tweet = tweets.tweets.shift();
      
      $(tweet).prependTo('#tweets ul').hide().slideDown('fast').find('a').attr('target', '_blank');
      
      $('#tweets li').filter(function(index){
        return index > 20
      }).remove();
    }
  }
}

var photos = {
  timestamp: 0,
  interval: null,
  photos: Array(),
  
  init: function(){
    photos.getPhotos();
    
    setTimeout(function(){
      photos.interval = setInterval(function(){
        photos.setPhoto();
      }, 1000);
    }, 500);
  },
  
  getPhotos: function(){
    var date = new Date();
    $.getJSON('getLatestPhotos.php?timestamp=' + photos.timestamp + '&rand=' + date.getTime(), function(json){
      var results = json.results;
        
      for(i=0; i < results.length; i++){
        if(results[i].timestamp > photos.timestamp) photos.timestamp = results[i].timestamp;
      
        photos.photos[photos.photos.length] = results[i].content;
      }
      
      setTimeout(function(){
        photos.getPhotos();
      }, 60000);
      
    });
  },
  
  setPhoto: function(){
    if(photos.photos.length > 0){
      var photo = photos.photos.shift();
      
      $(photo).prependTo('#photos ul').hide().slideDown('fast').find('a').attr('target', '_blank');
      
      $('#photos li').filter(function(index){
        return index > 20
      }).remove();
    }
  }
}

var videos = {
  timestamp: 0,
  interval: null,
  videos: Array(),
  
  init: function(){
    videos.getVideos();
    
    setTimeout(function(){
      videos.interval = setInterval(function(){
        videos.setVideo();
      }, 1000);
    }, 500);
  },
  
  getVideos: function(){
    $.getJSON('getLatestVideos.php?timestamp=' + videos.timestamp, function(json){
      var results = json.results;
        
      for(i=0; i < results.length; i++){
        if(results[i].timestamp > videos.timestamp) videos.timestamp = results[i].timestamp;
      
        videos.videos[videos.videos.length] = results[i].content;
      }
      
      setTimeout(function(){
        videos.getVideos();
      }, 20000);
      
    });
  },
  
  setVideo: function(){
    if(videos.videos.length > 0){
      var video = videos.videos.shift();
      
      $(video).prependTo('#videos ul').hide().slideDown('fast').find('a').attr('target', '_blank');
      
      $('#videos li').filter(function(index){
        return index > 20
      }).remove();
    }
  }
}

$(document).ready(function(){
  if($('#tweets').length > 0) tweets.init();
  if($('#photos').length > 0) photos.init();
});


