function tweet_urlencode(str) {
    str = escape(str);
    str = str.replace(/\+/g, '%2B');
    str = str.replace(/%20/g, '+');
    str = str.replace(/\*/g, '%2A');
    str = str.replace(/\//g, '%2F');
    str = str.replace(/@/g, '%40');
    str = str.replace(/#/g, '%23');
    return str;
}
var __d = function (msg) {
    if (document.getElementById("tweet_debug")) {
        document.getElementById("tweet_debug").innerHTML = document.getElementById("tweet_debug").innerHTML + "<br>" + msg;
        document.getElementById("tweet_debug").scrollTop = document.getElementById("tweet_debug").scrollHeight;
    }
};
var tweet_getdatediff = function (now_ms, gmt) {
    var dt_ms = Date.parse(gmt);
    var delta = now_ms - dt_ms;
    delta = Math.floor(delta / 1000);
    if (delta > 86400) {
        return '' + Math.floor(delta / 86400) + ' days ago';
    } else if (delta > 3600) {
        return '' + Math.floor(delta / 3600) + ' hours ' + Math.floor(delta / 60) + ' minutes ago';
    } else if (delta > 60) {
        return '' + Math.floor(delta / 60) + ' minutes ' + (delta % 60) + ' seconds ago';
    } else {
        return '' + (delta % 60) + ' seconds ago';
    }
};
var tweet_cleanup = function () {
    if (tweet_widget_refresh_interval > 0) {
        clearTimeout(tweet_settings[tweet_curidx]['tweet_timer']);
        tweet_settings[tweet_curidx]['tweet_timer'] = setTimeout("tweet_refresh_clbk(" + tweet_curidx + ");", (tweet_widget_refresh_interval * 1000) + Math.floor(Math.random() * 2000));
    }
    tweet_widget_refresh_interval = '';
    tweet_divname = '';
    tweet_width = '';
    tweet_height = '';
    tweet_scroll = '';
    tweet_widget_background = '';
    tweet_widget_border = '';
    tweet_tweet_textcolor = '';
    tweet_tweet_linkcolor = '';
    tweet_tweet_background = '';
    tweet_tweet_border = '';
    tweet_tweet_margin = '';
    tweet_tweet_fontsize = '';
    tweet_tweet_lang = '';
    tweet_hide_img = '';
    tweet_num_tweets = '';
    tweet_search = '';
    tweet_pre_html = '';
    tweet_post_html = '';
    tweet_mid_html = '';
    tweet_center_widget = '';
    tweet_tweet_newbackground = '';
    tweet_lastsearch = '';
    __d(tweet_curidx + " RELEASED the lock...");
    tweet_lock = -1;
};
var tweet_searchfunc_pre1 = function (arr) {
    var x = 1;
    if (x != tweet_lock) {
        __d(x + ": i don't have the lock! aborting!!");
        return false;
    }
    tweet_searchfunc(arr);
};
var tweet_searchfunc_pre2 = function (arr) {
    var x = 2;
    if (x != tweet_lock) {
        __d(x + ": i don't have the lock! aborting!!");
        return false;
    }
    tweet_searchfunc(arr);
};
var tweet_searchfunc_pre3 = function (arr) {
    var x = 3;
    if (x != tweet_lock) {
        __d(x + ": i don't have the lock! aborting!!");
        return false;
    }
    tweet_searchfunc(arr);
};
var tweet_searchfunc_pre4 = function (arr) {
    var x = 4;
    if (x != tweet_lock) {
        __d(x + ": i don't have the lock! aborting!!");
        return false;
    }
    tweet_searchfunc(arr);
};
var tweet_searchfunc_pre5 = function (arr) {
    var x = 5;
    if (x != tweet_lock) {
        __d(x + ": i don't have the lock! aborting!!");
        return false;
    }
    tweet_searchfunc(arr);
};
var tweet_searchfunc_pre6 = function (arr) {
    var x = 6;
    if (x != tweet_lock) {
        __d(x + ": i don't have the lock! aborting!!");
        return false;
    }
    tweet_searchfunc(arr);
};
var tweet_searchfunc_pre7 = function (arr) {
    var x = 7;
    if (x != tweet_lock) {
        __d(x + ": i don't have the lock! aborting!!");
        return false;
    }
    tweet_searchfunc(arr);
};
var tweet_searchfunc_pre8 = function (arr) {
    var x = 8;
    if (x != tweet_lock) {
        __d(x + ": i don't have the lock! aborting!!");
        return false;
    }
    tweet_searchfunc(arr);
};
var tweet_searchfunc_pre9 = function (arr) {
    var x = 9;
    if (x != tweet_lock) {
        __d(x + ": i don't have the lock! aborting!!");
        return false;
    }
    tweet_searchfunc(arr);
};
var tweet_searchfunc_pre10 = function (arr) {
    var x = 10;
    if (x != tweet_lock) {
        __d(x + ": i don't have the lock! aborting!!");
        return false;
    }
    tweet_searchfunc(arr);
};
var tweet_searchfunc = function (arr) {
    clearTimeout(tweet_lock_timeout_timer);
    var ret = tweet_pre_html;
    var i = 0;
    var bg;
    var wasnew = 0;
    var now = new Date();
    var now_ms = now.getTime();
    if ((!arr || !arr.results[0]) && tweet_settings[tweet_curidx]['tweet_lastid'] == 0) {
        document.getElementById(tweet_divname).innerHTML = '0 Results to display.  Enter new search terms.';
        tweet_cleanup();
        return false;
    }
    if ((!arr || !arr.results[0]) && (tweet_lastsearch == tweet_search)) {
        __d(tweet_curidx + " --no new tweets--");
        tweet_cleanup();
        return false;
    }
    ret = ret + '<div style="color:' + tweet_tweet_textcolor + ';padding:0px 5px 0px 5px;' + tweet_results_style_misc + '">';
    var num_newtweets;
    if (!arr || !arr.results[0]) {
        num_newtweets = 0;
    } else {
        num_newtweets = arr.results.length;
    }
    var newptr = (tweet_settings[tweet_curidx]['tweet_ringptr'] - num_newtweets + 100) % 100;
    tweet_settings[tweet_curidx]['tweet_ringptr'] = newptr;
    __d(tweet_curidx + " num_newtweets " + num_newtweets);
    __d(tweet_curidx + " newptr " + newptr);
    for (i = 0; i < num_newtweets; i++) {
        tweet_settings[tweet_curidx]['tweet_tweets'][(newptr + i) % 100] = new Array(6);
        tweet_settings[tweet_curidx]['tweet_tweets'][(newptr + i) % 100]['user'] = arr.results[i].from_user;
        tweet_settings[tweet_curidx]['tweet_tweets'][(newptr + i) % 100]['tweet'] = arr.results[i].text;
        tweet_settings[tweet_curidx]['tweet_tweets'][(newptr + i) % 100]['tstamp'] = new Date(arr.results[i].created_at);
        tweet_settings[tweet_curidx]['tweet_tweets'][(newptr + i) % 100]['tstamp'] = tweet_settings[tweet_curidx]['tweet_tweets'][(newptr + i) % 100]['tstamp'].toLocaleString();
        tweet_settings[tweet_curidx]['tweet_tweets'][(newptr + i) % 100]['img'] = arr.results[i].profile_image_url;
        tweet_settings[tweet_curidx]['tweet_tweets'][(newptr + i) % 100]['id'] = arr.results[i].id;
        tweet_settings[tweet_curidx]['tweet_tweets'][(newptr + i) % 100]['search'] = tweet_search;
    }
    for (i = 0; i < tweet_num_tweets; i++) {
        if (!tweet_settings[tweet_curidx]['tweet_tweets'][(newptr + i) % 100] || tweet_settings[tweet_curidx]['tweet_tweets'][(newptr + i) % 100]['search'] != tweet_search) break;
        var user = tweet_settings[tweet_curidx]['tweet_tweets'][(newptr + i) % 100]['user'];
        var tweet = tweet_settings[tweet_curidx]['tweet_tweets'][(newptr + i) % 100]['tweet'];
        var tstamp = tweet_settings[tweet_curidx]['tweet_tweets'][(newptr + i) % 100]['tstamp'];
        var img = tweet_settings[tweet_curidx]['tweet_tweets'][(newptr + i) % 100]['img'];
        var tid = tweet_settings[tweet_curidx]['tweet_tweets'][(newptr + i) % 100]['id'];
        if (tid > tweet_settings[tweet_curidx]['tweet_lastid'] && tweet_settings[tweet_curidx]['tweet_lastid'] != 0) {
            bg = tweet_tweet_newbackground;
            wasnew = 1;
        } else {
            bg = tweet_tweet_background
        }
        var re = new RegExp("(([a-zA-Z]+:\/\/)([a-zA-Z][a-zA-Z0-9_\.-]*[a-zA-Z]{2,6})([a-zA-Z0-9~\#\/\._\?\&%-=]*[a-zA-Z0-9~\#\/_\?\&%-=]))", "g");
        tweet = tweet.replace(re, '<a style="' + tweet_tweet_linkcolor + 'text-decoration:none;" target="_new" href=$1 >$1</a>');
        re = new RegExp("@([a-zA-Z0-9_]+)", "g");
        tweet = tweet.replace(re, '@<a style="' + tweet_tweet_linkcolor + 'text-decoration:none;" target="_new" href=http://twitter.com/$1>$1</a>');
        tweet = tweet.replace(/&/g, '&');
        ret = ret + '<div class="tweet-row"';
        if (tid > tweet_settings[tweet_curidx]['tweet_lastid'] && tweet_settings[tweet_curidx]['tweet_lastid'] != 0 && typeof(jQuery) == 'function' && typeof($) == 'function') {
            ret = ret + 'display:none;';
        }
        ret = ret + '" ';
        if (tid > tweet_settings[tweet_curidx]['tweet_lastid'] && tweet_settings[tweet_curidx]['tweet_lastid'] != 0) {
            ret = ret + ' class="tweet_new_tweet" ';
        }
        ret = ret + '>';
        if (!tweet_hide_img) {
            ret = ret + '<img class="twitter-avatar" src=' + img + ' height=' + tweet_img_size + ' width=' + tweet_img_size + ' align=left style="padding:2px;"><div class="tweet-body>"';
        }
        ret = ret + '<b><a class="tweet-user" target="_new" href=http://twitter.com/' + user + '>' + user + '</a></b>: ' + tweet + '<br><span class="tweet_tstamp" style="font-size:10px;">' + tstamp;
        ret = ret + '</span>';
        re = new RegExp("(http://twitpic.com/)([0-9a-zA-Z]*)");
        var m = re.exec(tweet);
        if (!m || m.length == 0 || m[2] == "photos" || m[2] == "photo" || m[2] == "tag") {} else {
            ret = ret + '<center><a href="http://twitpic.com/' + m[2] + '" target="_blank"><img src="http://twitpic.com/show/thumb/' + m[2] + '" border=0 height=150 width=150></a></center>';
        }
        ret = ret + '</div></div>';
        if (i < tweet_num_tweets - 1) ret = ret + tweet_mid_html;
    }
    ret = ret + tweet_post_html;
    ret = ret + '</div>';
    tweet_settings[tweet_curidx]['tweet_lastid'] = (arr.results[0] ? arr.results[0].id: tweet_settings[tweet_curidx]['tweet_lastid']);
    tweet_settings[tweet_curidx]['tweet_lastsearch'] = (wasnew ? '': tweet_search);
    document.getElementById(tweet_divname).innerHTML = ret;
    if (typeof(jQuery) == 'function' && typeof($) == 'function') {
        $('#' + tweet_divname + ' .tweet_new_tweet').fadeIn("slow");
    }
    tweet_cleanup();
};
tweet_refresh_clbk = function (i) {
    if (tweet_lock != -1) {
        if (tweet_lock == i) {
            __d(i + ": trying to double lock!! aborting...");
            return false;
        }
        __d(i + ": " + tweet_lock + " already has lock, backing off...");
        setTimeout("tweet_refresh_clbk(" + i + ");", 1000 + Math.floor(Math.random() * 1000));
        return false;
    }
    __d(i + " GRABBED lock...");
    tweet_lock = i;
    tweet_lock_timeout_timer = setTimeout("tweet_lock_timeout(" + i + ");", 6500);
    if (tweet_lock != i) {
        setTimeout("tweet_refresh_clbk(" + i + ");", 1000 + Math.floor(Math.random() * 1000));
        return false;
    }
    tweet_divname = tweet_settings[i]['tweet_divname'];
    tweet_curidx = tweet_settings[i]['tweet_curidx'];
    tweet_width = tweet_settings[i]['tweet_width'];
    tweet_height = tweet_settings[i]['tweet_height'];
    tweet_scroll = tweet_settings[i]['tweet_scroll'];
    tweet_widget_background = tweet_settings[i]['tweet_widget_background'];
    tweet_widget_border = tweet_settings[i]['tweet_widget_border'];
    tweet_widget_refresh_interval = tweet_settings[i]['tweet_widget_refresh_interval'];
    tweet_tweet_textcolor = tweet_settings[i]['tweet_tweet_textcolor'];
    tweet_tweet_linkcolor = tweet_settings[i]['tweet_tweet_linkcolor'];
    tweet_tweet_background = tweet_settings[i]['tweet_tweet_background'];
    tweet_tweet_newbackground = tweet_settings[i]['tweet_tweet_newbackground'];
    tweet_tweet_border = tweet_settings[i]['tweet_tweet_border'];
    tweet_tweet_margin = tweet_settings[i]['tweet_tweet_margin'];
    tweet_tweet_fontsize = tweet_settings[i]['tweet_tweet_fontsize'];
    tweet_tweet_lang = tweet_settings[i]['tweet_tweet_lang'];
    tweet_hide_img = tweet_settings[i]['tweet_hide_img'];
    tweet_big_img = tweet_settings[i]['tweet_big_img'];
    tweet_img_size = tweet_settings[i]['tweet_img_size'];
    tweet_num_tweets = tweet_settings[i]['tweet_num_tweets'];
    tweet_search = tweet_settings[i]['tweet_search'];
    tweet_lastsearch = tweet_settings[i]['tweet_lastsearch'];
    tweet_pre_html = tweet_settings[i]['tweet_pre_html'];
    tweet_post_html = tweet_settings[i]['tweet_post_html'];
    tweet_mid_html = tweet_settings[i]['tweet_mid_html'];
    tweet_center_widget = tweet_settings[i]['tweet_center_widget'];
    tweet_widget_style_misc = tweet_settings[i]['tweet_widget_style_misc'];
    tweet_results_style_misc = tweet_settings[i]['tweet_results_style_misc'];
    tweet_tweet_style_misc = tweet_settings[i]['tweet_tweet_style_misc'];
    tweet_lastid = tweet_settings[i]['tweet_lastid'];
    if (tweet_lock != i) {
        __d(i + " failed 2nd lock check..." + tweet_lock + " has it...");
        setTimeout("tweet_refresh_clbk(" + i + ");", 1000 + Math.floor(Math.random() * 1000));
        return false;
    }
    __d(i + " lastid is " + tweet_settings[i].tweet_lastid);
    var sc = document.createElement('script');
    var h = document.getElementsByTagName('head')[0];
    sc.language = 'javascript';
    sc.type = 'text/javascript';
    sc.src = 'http://search.twitter.com/search.json?callback=tweet_searchfunc_pre' + tweet_curidx + '&' + tweet_tweet_lang + 'q=' + tweet_urlencode(tweet_search) + '&rpp=' + tweet_num_tweets + '&since_id=' + tweet_settings[i].tweet_lastid + '&rand=' + Math.floor(Math.random() * 10000000);
    h.appendChild(sc);
};
function tweet_lock_timeout(i) {
    __d("!!! forcing UNLOCK for " + i + "");
    tweet_cleanup();
}
var tweet_idx;
var tweet_curidx;
var tweet_lock;
var tweet_lock_timeout_timer;
var tweet_settings;
var tweet_divname;
var tweet_width;
var tweet_height;
var tweet_scroll;
var tweet_widget_background;
var tweet_widget_border;
var tweet_widget_refresh_interval;
var tweet_tweet_textcolor;
var tweet_tweet_linkcolor;
var tweet_tweet_background;
var tweet_tweet_newbackground;
var tweet_tweet_border;
var tweet_tweet_margin;
var tweet_tweet_fontsize;
var tweet_tweet_lang;
var tweet_hide_img;
var tweet_big_img;
var tweet_img_size;
var tweet_num_tweets;
var tweet_search;
var tweet_lastsearch;
var tweet_pre_html;
var tweet_post_html;
var tweet_mid_html;
var tweet_center_widget;
var tweet_widget_style_misc;
var tweet_results_style_misc;
var tweet_tweet_style_misc;
var tweet_trk;
var _gat;
if (!tweet_settings) tweet_settings = new Array(0);
if (!tweet_idx) tweet_idx = 0;
if (!tweet_lock) tweet_lock = -1;
if (!tweet_divname) tweet_divname = 'tweet_widget';
if (!tweet_width) tweet_width = '300px';
if (!tweet_height) tweet_height = '400px';
if (!tweet_scroll || tweet_scroll == 'yes') tweet_scroll = 'overflow:auto;';
else {
    tweet_scroll = '';
}
if (!tweet_widget_background) tweet_widget_background = '#fff';
if (!tweet_widget_border) tweet_widget_border = '1px solid #aaa';
if (!tweet_widget_refresh_interval) tweet_widget_refresh_interval = 10;
if (tweet_widget_refresh_interval < 8 && tweet_widget_refresh_interval != 0) tweet_widget_refresh_interval = 8;
if (!tweet_tweet_textcolor) tweet_tweet_textcolor = '#000';
if (!tweet_tweet_linkcolor) tweet_tweet_linkcolor = 'color:#00f;';
else {
    tweet_tweet_linkcolor = 'color:' + tweet_tweet_linkcolor + ';';
}
if (!tweet_tweet_background) tweet_tweet_background = '#ffffff';
if (!tweet_tweet_newbackground) tweet_tweet_newbackground = tweet_tweet_background;
if (!tweet_tweet_border) tweet_tweet_border = 'none';
if (!tweet_tweet_margin) tweet_tweet_margin = '1px';
if (!tweet_tweet_fontsize) tweet_tweet_fontsize = '.9em';
if (!tweet_search) tweet_search = 'twitter';
else {
    tweet_search = tweet_search;
}
if (!tweet_num_tweets) tweet_num_tweets = 20;
if (!tweet_pre_html) tweet_pre_html = '';
if (!tweet_post_html) tweet_post_html = '';
if (!tweet_mid_html) tweet_mid_html = '';
if (tweet_center_widget == 'yes') tweet_center_widget = 'margin: 0 auto;';
else {
    tweet_center_widget = '';
}
if (!tweet_tweet_lang) tweet_tweet_lang = '';
else {
    tweet_tweet_lang = 'lang=' + tweet_tweet_lang + '&';
}
if (!tweet_widget_style_misc) tweet_widget_style_misc = '';
if (!tweet_results_style_misc) tweet_results_style_misc = '';
if (!tweet_tweet_style_misc) tweet_tweet_style_misc = '';
if (tweet_big_img == 'yes') tweet_img_size = 48;
else {
    tweet_img_size = 24;
}
if (tweet_hide_img == 'yes') tweet_hide_img = 1;
else {
    tweet_hide_img = 0;
    tweet_tweet_style_misc = tweet_tweet_style_misc + ';min-height:' + (tweet_img_size + 4) + 'px;';
}
tweet_idx++;
tweet_curidx = tweet_idx;
tweet_settings[tweet_idx] = new Array(40);
tweet_settings[tweet_idx]['tweet_divname'] = tweet_divname;
tweet_settings[tweet_idx]['tweet_curidx'] = tweet_curidx;
tweet_settings[tweet_idx]['tweet_width'] = tweet_width;
tweet_settings[tweet_idx]['tweet_height'] = tweet_height;
tweet_settings[tweet_idx]['tweet_scroll'] = tweet_scroll;
tweet_settings[tweet_idx]['tweet_widget_background'] = tweet_widget_background;
tweet_settings[tweet_idx]['tweet_widget_border'] = tweet_widget_border;
tweet_settings[tweet_idx]['tweet_widget_refresh_interval'] = tweet_widget_refresh_interval;
tweet_settings[tweet_idx]['tweet_tweet_textcolor'] = tweet_tweet_textcolor;
tweet_settings[tweet_idx]['tweet_tweet_linkcolor'] = tweet_tweet_linkcolor;
tweet_settings[tweet_idx]['tweet_tweet_background'] = tweet_tweet_background;
tweet_settings[tweet_idx]['tweet_tweet_newbackground'] = tweet_tweet_newbackground;
tweet_settings[tweet_idx]['tweet_tweet_border'] = tweet_tweet_border;
tweet_settings[tweet_idx]['tweet_tweet_margin'] = tweet_tweet_margin;
tweet_settings[tweet_idx]['tweet_tweet_fontsize'] = tweet_tweet_fontsize;
tweet_settings[tweet_idx]['tweet_tweet_lang'] = tweet_tweet_lang;
tweet_settings[tweet_idx]['tweet_hide_img'] = tweet_hide_img;
tweet_settings[tweet_idx]['tweet_big_img'] = tweet_big_img;
tweet_settings[tweet_idx]['tweet_img_size'] = tweet_img_size;
tweet_settings[tweet_idx]['tweet_num_tweets'] = tweet_num_tweets;
tweet_settings[tweet_idx]['tweet_search'] = tweet_search;
tweet_settings[tweet_idx]['tweet_lastsearch'] = tweet_search;
tweet_settings[tweet_idx]['tweet_pre_html'] = tweet_pre_html;
tweet_settings[tweet_idx]['tweet_post_html'] = tweet_post_html;
tweet_settings[tweet_idx]['tweet_mid_html'] = tweet_mid_html;
tweet_settings[tweet_idx]['tweet_center_widget'] = tweet_center_widget;
tweet_settings[tweet_idx]['tweet_widget_style_misc'] = tweet_widget_style_misc;
tweet_settings[tweet_idx]['tweet_results_style_misc'] = tweet_results_style_misc;
tweet_settings[tweet_idx]['tweet_tweet_style_misc'] = tweet_tweet_style_misc;
tweet_settings[tweet_idx]['lastid'] = 0;
tweet_settings[tweet_idx]['tweet_timer'] = '';
tweet_settings[tweet_idx]['tweet_tweets'] = new Array(100);
tweet_settings[tweet_idx]['tweet_ringptr'] = 0;
document.write('<div style="width:' + tweet_width + ';height:' + tweet_height + ';' + tweet_center_widget + ';padding:0px;margin:0px;' + '">');
document.write('<div id=' + tweet_divname + ' style="' + tweet_scroll + '' + tweet_center_widget + 'width:auto;height:' + tweet_height + ';background:' + tweet_widget_background + ';' + 'border:' + tweet_widget_border + ';' + tweet_widget_style_misc + '">');
document.write('</div>');
document.write('</div>');
if (tweet_search != '-') {
    document.write('<script src="http://search.twitter.com/search.json?callback=tweet_searchfunc&' + tweet_tweet_lang + 'q=' + tweet_urlencode(tweet_search) + '&rpp=' + tweet_num_tweets + '&since_id=0" type="text/javascript"></script>');
} else {}