﻿
var tweetcount = 25;
var tweetZ = 500;

// animation settings
var anim_Pause = 6000; // how long between tweet reveals
var anim_Duration = 8000; // how long do tweets stay moving onscreen

var shiftLeft = 40; //maximum horizontal distance for tweets to move
var shiftTop = 20; //maximum vertical distance for tweets to move

// position and size of the tweetspace
var canvasLeft = 450;
var canvasTop = 20;
var canvasWidth = 400;
var canvasHeight = 200;



//right side option
//        canvasLeft = 980;
//        canvasTop = 20;
//        canvasWidth = 300;
//        canvasHeight = 800;
//        anim_Pause = 3000;


//top option
canvasLeft = 400;
canvasTop = 20;
canvasWidth = 500;
canvasHeight = 210;
anim_Pause = 5800;
anim_Duration = 6000;





// how large are the tweet boxes
var tweetHeight = 150;
var tweetWidth = 225;

var i = 0;



function tweetSpam(searchterm) {

    var twitter_api_url = 'http://search.twitter.com/search.json';

    // Enable caching
    $.ajaxSetup({ cache: true });

    $.getJSON(
            twitter_api_url + '?callback=?&rpp=' + tweetcount + '&q=' + searchterm,

        function (data) {


            $.each(data.results, function (i, tweet) {

                // Before we continue we check that we got data
                if (tweet.text !== undefined) {

                    // Calculate how many hours ago was the tweet posted
                    var date_tweet = new Date(tweet.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 tweetBG = "O";
                    if (i % 2 == 0) { tweetBG = "G" };

                    var tweet_html = '<div id="tweet_' + i + '" class="floatTweet_' + tweetBG + '" style="top:' + tweet * 60 + ';" onClick="toFront(' + i + ')">'

                    tweet_html += '<a onClick="fadeTweet(' + i + ')" style="float:right;font-weight:bold;cursor:pointer;"><img src="/images/clear.gif" style="width:8px;height:8px;" /><\/a>'


                    //tweet_html += '<b>Tweet #' + i + '<\/b><br \/>'
                    tweet_html += '<a href="http://www.twitter.com/' + tweet.from_user + '" target="_blank">'

                    tweet_html += '<img src="' + tweet.profile_image_url + '" class="twitImg" \/><\/a>'

                    tweet_html += '<b class="twit">@' + tweet.from_user + '<\/b><br clear="all" \/>';
                    tweet_html += '<div class="tweettext">'
                    tweet_html += tweet.text.replace(/(http\:\/\/[\w\.\-\?\!\&\=\/]+[\w\&\=\/])/g, '<a href="$1" target="_blank">$1</a>'); //clicky clicky little links
                    tweet_html += '<\/div>'
                    //tweet_html += '<a href="http://www.twitter.com/' + tweet.from_user + '/status/' + tweet.id + '" target="_blank">';
                    //tweet_html += '<div class="tweet_hours">' + hours + ' hours ago<\/div><\/a>';
                    tweet_html += '</div>';

                    // Append html string to tweet_container div
                    $('#twitterZone').append(tweet_html);
                }
                i++;

            });

            breeze();
        }
    );



}


function breeze() {

    floater(0);
}




function floater(tweetnum) {

    var positionLeft = Math.floor(Math.random() * (canvasWidth - tweetWidth + 1)) + canvasLeft;
    var positionTop = Math.floor(Math.random() * (canvasHeight - tweetHeight + 1)) + canvasTop;

    if (Math.random() > .5) { shiftLeft = shiftLeft * -1 }
    if (Math.random() > .5) { shiftTop = shiftTop * -1 }
    var positionLeftDest = positionLeft + Math.floor(Math.random() * shiftLeft);
    var positionTopDest = positionTop + Math.floor(Math.random() * shiftTop);


    var t = $('#tweet_' + tweetnum);
    t.css('left', positionLeft + 'px');
    t.css('top', positionTop + 'px');

    tweetZ++;
    t.css('zIndex', tweetZ);

    t.fadeIn('slow');
    t.animate({ left: positionLeftDest }, { queue: false, duration: anim_Duration, easing: 'linear' });
    t.animate({ top: positionTopDest }, { queue: false, duration: anim_Duration, easing: 'linear', complete: function () { fadeTweet(tweetnum) } });

    //setTimeout('fadeTweet(' + tweetnum + ')', anim_Duration - 500);

    var newtweetnum = tweetnum + 1;

    if (newtweetnum < tweetcount) {
        setTimeout('floater(' + newtweetnum + ')', anim_Pause);
    }
    else {
        setTimeout('floater(0)', anim_Pause);
    }
}


function fadeTweet(tweetnum) {
    //alert('fade tweet #' + tweetnum);
    setTimeout('hideTweet(' + tweetnum + ')', 100);
}

function toFront(tweetnum) {
    tweetZ++;
    $('#tweet_' + tweetnum).css("zIndex", tweetZ + 1000);
    $('#tweet_' + tweetnum).stop();
    $('#tweet_' + tweetnum).draggable();
}

function hideTweet(tweetnum) {
    $('#tweet_' + tweetnum).fadeOut();
}
