var rotateTweets; // function to rotate tweets
$(document).ready(function () {

    var totalTweets = 20;
    var searchTerm = '#population OR #worldpopulation OR "world population" OR OR "world population day"'; // the search term to use to query twitter
    $.ajax({
        url: 'http://search.twitter.com/search.json',
        data: {
            q: searchTerm,
            lang: 'en',
            rpp: totalTweets
        },
        dataType: 'jsonp',
        success: function (jsonp) {

            processTwitterJSON(jsonp)
        }
    });


    var contDiv = ''; // the container to append the twitter to
    var twitterContent = new Array(); // array to store queried twitter messages
    var openLink = "target='_blank'"; // to insert into hyperlinks


    function processTwitterJSON(json) {
        $.each(json.results, function (i, item) {
            if (item.text != "undefined") {
                var link = "http://twitter.com/" + item.from_user + "/status/" + item.id;

                var tweet = filter(item.text);

                twitterContent.push("<a href='http://www.twitter.com/" + item.from_user + "' target='_blank'>@" + item.from_user + ":</a> " + formatFeed(tweet) /*+ " <a href='" + link + "' "+openLink+">read more</a>"*/ );
            }
        });

        totalTweets = twitterContent.length;
        rotateTweets();
    }

    var counter = 0;
    rotateTweets = function () {
        if (counter == totalTweets) {
            counter = 0;
        }
        $('#twitterContainer').slideUp('slow', function () {
            $(this).html(twitterContent[counter]);
            $(this).slideDown('slow');
        });
        setTimeout("rotateTweets()", 5000);
        counter++; // increment counter
    }

    function formatFeed(feed) {
        //make links
        var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
        feed = feed.replace(exp, "<a href='$1' class='extLink' " + openLink + ">$1</a>");
        var exp = /[\@]+([A-Za-z0-9-_]+)/ig;
        feed = feed.replace(exp, "<a href='http://twitter.com/$1' class='profileLink'>@$1</a>");
        // make it bold
        //tempParam = param.replace(/&ors=/,""); 
        tempParam = searchTerm.replace(/&ors=/, "");
        arrParam = tempParam.split("+");
        $.each(arrParam, function (i, item) {
            regExp = eval('/' + item + '/gi');
            newString = new String(' <b>' + item + '</b> ');
            feed = feed.replace(regExp, newString);
        });

        return feed;
    }

    var filterWords = "sex->*BAD word*,porn->*BAD word*,fuck->*BAD word*,shit->*BAD word*";

    function filter(s) {
        if (filterWords) {
            searchWords = filterWords.split(",");
            if (searchWords.length > 0) {
                cleanHTML = s;
                $.each(searchWords, function (i, item) {
                    sW = item.split("->").length > 0 ? item.split("->")[0] : item;
                    rW = item.split("->").length > 0 ? item.split("->")[1] : "";
                    regExp = eval('/' + sW + '/gi');
                    cleanHTML = cleanHTML.replace(regExp, rW);
                });
            } else cleanHTML = s;
            return cleanHTML;
        } else return s;
    }
});
