﻿$(document).ready(function () {
    tweet();
    setInterval('tweet();', 15000);
});

var tweetsList = new Array();
var currentTweet = 0;
function showTweet() {
    // Build the html string for the current tweet
    var tweetje = tweetsList[currentTweet];
    var tweet_html = '<div class="TweetText">';
    tweet_html += '<a href="http://twitter.com/';
    tweet_html += tweetje.from_user;
    tweet_html += '" class="TweetName">@';
    tweet_html += tweetje.from_user;
    tweet_html += '</a> <p>';
    tweet_html += Linkify(tweetje.text);
    tweet_html += '</p></div>';

    // Append html string to tweet_container div
    if ($('#TweetContainer').html() != tweet_html) {
        //$("#test").fadeToggle(800, function () {
        $("#TweetContainer").fadeToggle(800, function () {
            $('#TweetContainer').html(tweet_html);
            $("#TweetContainer").fadeToggle(800);
        });
    }

    currentTweet++;
    if (currentTweet >= tweetsList.length) {
        currentTweet = 0;
    }
}

function tweet() {
    // Declare variables to hold twitter API url and user name
    var twitter_api_url = 'http://search.twitter.com/search.json';
    var twitter_user = '%40sbdelft%20OR%20%23schaatsbaandelft%20OR%20%23sbdelft%20OR%20from:sbdelft';
    //var twitter_user = '%40cbcnews%20OR%20%23party%20OR%20%23fun'; // TESTING => @cbcnews #party #fun

    // Enable caching
    $.ajaxSetup({ cache: false });

    // Send JSON request
    // The returned JSON object will have a property called "results" where we find
    // a list of the tweets matching our request query
    $.getJSON(twitter_api_url + '?callback=?&rpp=10&q=' + twitter_user,
    function (data) {
        var firstNew = -1;
        var additional = new Array();

        $.each(data.results, function (i, tweetje) {
            // Uncomment line below to show tweet data in Fire Bug console
            // Very helpful to find out what is available in the tweet objects
            //console.log(tweetje);

            if (tweetje.text !== undefined) {
                var tweetId = tweetje.id;
                var add = true;
                for (var i = 0; i < tweetsList.length; i++) {
                    if (tweetsList[i].id !== undefined && tweetsList[i].id == tweetje.id) {
                        add = false;
                    }
                }

                if (add) {
                    additional.push(tweetje);
                }
            }

            // Before we continue we check that we got data
            //if (tweetje.text !== undefined) {
            //// Calculate how many hours ago was the tweet posted
            //var date_tweet = new Date(tweetje.created_at);
            //var date_now = new Date();
            //var date_diff = date_now - date_tweet;
            //var hours = Math.round(date_diff / (1000 * 60 * 60));

            //// Build the html string for the current tweet
            //var tweet_html = '<div class="TweetText">';
            //tweet_html += '<a href="http://twitter.com/';
            //tweet_html += tweetje.from_user;
            //tweet_html += '" class="TweetName">@';
            //tweet_html += tweetje.from_user;
            //tweet_html += '</a> <p>';
            //tweet_html += tweetje.text;
            //tweet_html += '</p></div>';


            //// Append html string to tweet_container div
            //if ($('#TweetContainer').html() != tweet_html) {
            //    $("#test").fadeToggle(800);
            //    $('#TweetContainer').html(tweet_html);
            //    $("#test").fadeToggle(800);
            //}
            //}
        });

        if (additional.length > 0) {
            additional.concat(tweetsList);
            tweetsList = additional;
            currentTweet = 0;
            showTweet();
            setInterval('showTweet();', 10000);
        }
    }
  );
}
