client/js/tweetanim.js
author veltr
Tue, 21 Feb 2012 12:09:36 +0100
changeset 14 1cc92b8c3a38
parent 8 d41e19b3b043
child 11 ef4287a73c4a
permissions -rwxr-xr-x
changed visual aspect of remove button

var tweetQueue = [];
var isAnimating = false;
var countTweets = 0;
var isPlayingTweets = false; // auto start
var SPEED_TWEETS = 10;
var MAX_VOTES = 25;
var DUMP_TWEETS = 10;
var toDel = [];

var current_tweet = null;
var current_past_tweet = 0;

var ts = [];

var b = 0;

var defaultTweet = {
  created_at: "Sun, 19 Feb 2012 19:12:15 +0000",
  date_value: 1329678735000,
  from_user: "chateaudeau",
  from_user_id: 164810536,
  from_user_id_str: "164810536",
  from_user_name: "Olivia Duch�teau",
  geo: null,
  id: 171311150900854800,
  id_str: "171311150900854784",
  iso_language_code: "fr",
  profile_image_url: "http://a2.twimg.com/profile_images/1481618117/clip_image00212_normal.jpg",
  profile_image_url_https: "https://si0.twimg.com/profile_images/1481618117/clip_image00212_normal.jpg",
  source: "<a href="http://phnxapp.com" rel="nofollow">phnx</a>",
  text: "RT @LeProf_Higgins: Le carton rouge � Sarkozy, on a eu l'id�e avant MLP mais le mien sera rouge, puis rose, mais pas brun !",
  to_user: null,
  to_user_id: null,
  to_user_id_str: null,
  to_user_name: null,
};

var TweetAnim = {

  init: function() {
  
    var html = getDefaultTweet();
    $('#tweet-container').fadeOut(0).html(html).fadeIn(2000, function() {
      $(this).fadeOut(500,  function() {
      isPlayingTweets=true;
      $(this).html("").fadeIn();
      });
    });


    
   },
  queueTweet: function(t) {
    console.log("Queued " + t.text);
    tweetQueue.push({t: t, anim: true}); 
    $('#nbr-mesure-new').html(tweetQueue.length);
    /*
    if(tweetQueue.length>0)
      document.getElementById('btn-tweet-next').style.visibility="visible";
      */
  },
  
  shiftQueue: function() {
    if(tweetQueue.length==0) {
        $('#tweet-container').html(getDefaultTweet());
    }  
    if((tweetQueue.length>0) && (!isAnimating)) {
      countTweets++;
      isAnimating = true;
      var q = tweetQueue.shift();
      this.showTweet(q.t, q.anim);

    }

  },
  
  skipQueue: function() {

    if((tweetQueue.length>0) && (current_tweet!=null)) {
      countTweets++;
      isAnimating = true;
      $('#tweet_anim_'+ current_tweet.id_str).dequeue();
      TweetAnim.animate(current_tweet, true);
      var q = tweetQueue.shift();
      this.showTweet(q.t, true);
    }
  },
  
	showTweet: function(t, anim) {

    var tweetId = "tweet_anim_" + t.id_str;
    var imgId = "img_" + t.id_str;
    
    if(anim)
      current_tweet = t;
    
    var htmlStr = "<div id='"+tweetId+"'><div class='tweet-bulle-avatar'>";
    htmlStr += "  <img class='tweet-img-avatar' src='"+t.profile_image_url+"' id='"+imgId+"'/>";
    htmlStr += "</div>";    
    htmlStr += "<div class='tweet-preview-container'>";
    htmlStr += "  <span class='tweet-screen-name'><a target='_blank' href='http://twitter.com/intent/user?screen_name="+t.from_user+"'>"+t.from_user+"</a></span>";    
    htmlStr += "  <span class='tweet-full-name'>"+t.from_user_name+"</span>";
    htmlStr += "  <div class='tweet-text'>"+t.text+"</div>";
    htmlStr += "  <div class='tweet-time'><a target='_blank' href='http://twitter.com/#!/" + t.from_user + "/status/" +t.id_str+"'>"+t.created_at+"</div>";
    htmlStr += "</div>";

    $('#tweet-container').html(htmlStr);
    $('#nbr-mesure-show').html(countTweets);
    $('#nbr-mesure-new').html(tweetQueue.length);
    /*
    if(tweetQueue.length>0)
      document.getElementById('btn-tweet-next').style.visibility="visible";
    else 
       document.getElementById('btn-tweet-next').style.visibility="hidden";
*/
        
    if(anim) {
      TweetAnim.animate(t, true);
      
      $('#'+tweetId).hover(function() {
          $(this).stop(true, true).fadeIn();
        }, function() {
          TweetAnim.animate(t, true);
      });
    }
	},
	
	animate: function(t, doSshift) {
      var tweetId = "tweet_anim_" + t.id_str;

    t.cat = t.from_user_id%4;
    createBallTweetForce(t);

    
    // Pour exp�rimenter le balayage de tout le podium
    //setInterval(function() {defaultTweet.cat = b%4; createBallTweetForce(defaultTweet); b++;}, 500);



 isAnimating = false;
  if(doSshift)
  TweetAnim.shiftQueue();
          
/*             
        toDel.push(t);
        current_past_tweet = toDel.length;
        ts[t.candidats[0]]++;
        checkSilos();
  */      
	}
}


function getDefaultTweet() {

  var htmlStr = "<div class='tweet-bulle-avatar'>";
  htmlStr += "  <img class='tweet-img-avatar' src='img/avatar-icon.png' />";
  htmlStr += "  </div>";
  htmlStr += "  <div class='tweet-preview-container'>";
  htmlStr += "  <span class='tweet-screen-name'><a target='_blank' href='http://twitter.com/intent/user?screen_name=bubbleT2012'>Bubble T</a></span>";
  htmlStr += "  <span class='tweet-full-name'>tea or tweet ?</span>";
  htmlStr += "  <div class='tweet-text'>";
  htmlStr += "  <a href='http://www.twitter.com' target='blank'>";
  htmlStr += "  Pendant que les prochains tweets se chargent.</br>";
  htmlStr += "  Exprimez, vous aussi, vos opinions sur vos candidats favoris !</a>";
  htmlStr += "  </div>";
  htmlStr += "  <div class='tweet-time'>maintenant</div>";
  
  return htmlStr;
      
}

function checkSilos() {

  for(var j=0; j<ts.length; j++) {
    if(ts[j]>MAX_VOTES) {
      j=ts.length+1;
      emptySilos();
    }
  }
}


function emptySilos() {

    var toDel2 = [];
    var m =0;

    for(var k=0; k<DUMP_TWEETS; k++) {
      world.DestroyBody(b2bod[toDel[k].id_str]);
      $('#'+toDel[k].id_str).delay(10).hide("slow").queue(function() {
        $(this).remove(); 
      });
    }
    
    for(var h= 0; h < candidats.length; h++) {
     ts[h] = 0;
    }

    for(var l=DUMP_TWEETS; l<toDel.length; l++) {
      toDel2[l-DUMP_TWEETS] = toDel[l];
      ts[toDel2[l-DUMP_TWEETS].candidats[0]]++;  
    }

    toDel = toDel2;
    current_past_tweet=toDel.length;
}

TweetAnim.init();