﻿// Twitter stream js:
jQuery.fn.reverse = Array.prototype.reverse;
String.prototype.linkify = function() {
    return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g, function(m) {
        return m.link(m);
    });
};

String.prototype.linkuser = function() {
    return this.replace(/[@]+[A-Za-z0-9-_]+/g, function(u) {
        var username = u.replace("@", "");
        return u.link("http://twitter.com/" + username);
    });
};

String.prototype.linktag = function() {
    return this.replace(/[#]+[A-Za-z0-9-_]+/, function(t) {
        var tag = t.replace("#", "%23")
        return t.link("http://search.twitter.com/search?q=" + tag);
    });
};

function fetch_tweets(elem, clear) {
    elem = jQuery(elem);
    if (clear) { 
        jQuery('.twDisplay', elem).empty();
        elem.removeData('tweet');
        rrp = tweetCount;
    }
    input = jQuery('.twSearch > input').val();

    input = elem.attr('title'); 
    if (!elem.data('tweet')) { elem.data('tweet', {lastId: 0, total: 12, timeout: null }); }

    var url = "http://search.twitter.com/search.json?q=" + input + "&rpp=" + rrp + "&since_id=" + elem.data('tweet').lastId + "&callback=?";

    jQuery.getJSON(url, function(json) {

        jQuery(json.results).reverse().each(function() {
            if (jQuery('#tw' + this.id, elem).length == 0) {
                elem.data('tweet').total++;

                var thedate = new Date(Date.parse(this.created_at));
                var thedatestr = thedate.getHours() + ':' + ((thedate.getMinutes() < 10) ? '0' + thedate.getMinutes() : thedate.getMinutes());

                var divstr = '<div id="tw' + this.id + '" class="tweet">' + 
							 '<div class="tweethead">' + 
							 '<img width="24" height="24" src="' + this.profile_image_url + '" />' + 
							 '<span class="twAuthor" ><a href="http://twitter.com/' + this.from_user + '" target="_blank" rel="nofollow">' + this.from_user + '</a></span>' +
							 '<span class="twDate">' + thedatestr + '</span>' +
							 '</div>' +
                             '<div class="tweetbody">' + this.text.linkify().linkuser().linktag() + '</div>' +
                             '</div>';

                elem.data('tweet').lastId = this.id;

                jQuery('.twDisplay', elem).prepend(divstr);
                jQuery('#tw' + this.id, elem).hide();
                jQuery('#tw' + this.id + ' img', elem).hide();
                jQuery('#tw' + this.id + ' img', elem).fadeIn(4000);
                jQuery('#tw' + this.id, elem).fadeIn('slow');
            }
        });
        
        jQuery('div.twDisplay>div:gt(' + (tweetCount - 1) + ')', elem).each(function() { 
            jQuery(this).remove(); 
        });
        
        input = escape(input);
        rrp = 1;
        elem.data('tweet').timeout = setTimeout(function() { fetch_tweets(elem, false) }, 5000);
    });

    return (false);
}

function submitSearch() {
    elem = jQuery(this).parents('.monitter');

    if (!clearOnSearch) {
        var divstr = '<div id="tw0" class="tweetSeparator">Previous Search: ' + elem.attr('title') + '</div>';
        jQuery('.twDisplay', elem).prepend(divstr);
        jQuery('div.tweet:gt(' + (tweetCount - 1) + ')', elem).each(function() { 
            jQuery(this).remove(); 
        });
    }

    if (elem.data('tweet')) { clearTimeout(elem.data('tweet').timeout); }
    elem.attr('title', jQuery('.twSearch > input', elem).val());

    fetch_tweets(elem, clearOnSearch);
    return false;
}

jQuery(document).ready(function() {
    tweetCount = rrp = dnn.getVar('TweetCount');
    clearOnSearch = (dnn.getVar('ClearOnSearch')=='True');
    var timeouts = {};
    
    jQuery('.monitter')
        .find('.twSearch > input')
        .addClass('searchbox')
        .end()
        .find('.twSearch > a')
        .click(submitSearch)
        .end()
        .each(function(e) {
            fetch_tweets(this, true);
        });
});
