function tw_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("tw_debug"))
	{
		document.getElementById("tw_debug").innerHTML=document.getElementById("tw_debug").innerHTML+"<br />"+msg;
		document.getElementById("tw_debug").scrollTop=document.getElementById("tw_debug").scrollHeight;
	}
};
	
var tw_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 tw_cleanup=function()
{
	if(tw_widget_refresh_interval>0)
		{
			clearTimeout(tw_settings[tw_curidx]['tw_timer']);
			tw_settings[tw_curidx]['tw_timer']=setTimeout("tw_refresh_clbk("+tw_curidx+");",(tw_widget_refresh_interval*1000)+Math.floor(Math.random()*2000));
		}
		
		tw_widget_refresh_interval='';
		tw_divname='';
		tw_width='';
		tw_height='';
		tw_scroll='';
		tw_widget_background='';
		tw_widget_border='';
		tw_tweet_textcolor='';
		tw_tweet_linkcolor='';
		tw_tweet_background='';
		tw_tweet_border='';
		tw_tweet_margin='';
		tw_tweet_fontsize='';
		tw_tweet_lang='';
		tw_hide_img='';
		tw_num_tweets='';tw_search='';
		tw_pre_html='';
		tw_post_html='';
		tw_mid_html='';
		tw_center_widget='';
		tw_tweet_newbackground='';
		tw_lastsearch='';
		__d(tw_curidx+" RELEASED the lock...");
		tw_lock=-1;
};
	
var tw_searchfunc_pre1=function(arr)
{
	var x=1;
	if(x!=tw_lock)
	{
		__d(x+": i don't have the lock! aborting!!");
		return false;
	}tw_searchfunc(arr);
};

var tw_searchfunc_pre2=function(arr)
{
	var x=2;
	if(x!=tw_lock)
	{
		__d(x+": i don't have the lock! aborting!!");
		return false;
	}
	
	tw_searchfunc(arr);
};
	
var tw_searchfunc_pre3=function(arr)
{	
	var x=3;
	if(x!=tw_lock)
	{
		__d(x+": i don't have the lock! aborting!!");
		return false;
	}
	
	tw_searchfunc(arr);
};

var tw_searchfunc_pre4=function(arr)
{
	var x=4;
	if(x!=tw_lock)
	{
		__d(x+": i don't have the lock! aborting!!");
		return false;
	}
	
	tw_searchfunc(arr);
};

var tw_searchfunc_pre5=function(arr)
{
	var x=5;
	if(x!=tw_lock)
	{
		__d(x+": i don't have the lock! aborting!!");
		return false;
	}
	
	tw_searchfunc(arr);
};

var tw_searchfunc_pre6=function(arr)
{
	var x=6;
	if(x!=tw_lock)
	{
		__d(x+": i don't have the lock! aborting!!");
		return false;
	}
	
	tw_searchfunc(arr);
};

var tw_searchfunc_pre7=function(arr)
{
	var x=7;
	if(x!=tw_lock)
	{
		__d(x+": i don't have the lock! aborting!!");
		return false;}tw_searchfunc(arr);
};

var tw_searchfunc_pre8=function(arr)
{
	var x=8;
	if(x!=tw_lock)
	{
		__d(x+": i don't have the lock! aborting!!");
		return false;
	}
	
	tw_searchfunc(arr);
};

var tw_searchfunc_pre9=function(arr)
{
	var x=9;
	if(x!=tw_lock)
	{
		__d(x+": i don't have the lock! aborting!!");
		return false;
	}
	tw_searchfunc(arr);
};

var tw_searchfunc_pre10=function(arr)
{
	var x=10;
	if(x!=tw_lock)
	{
		__d(x+": i don't have the lock! aborting!!");
		return false;
	}
	
	tw_searchfunc(arr);
};

var tw_searchfunc=function(arr)
{
	clearTimeout(tw_lock_timeout_timer);
	var ret=tw_pre_html;
	var i=0;
	var bg;
	var wasnew=0;
	var now=new Date();
	var now_ms=now.getTime();
	
	if((!arr||!arr.results[0])&&tw_settings[tw_curidx]['tw_lastid']==0)
	{
		document.getElementById(tw_divname).innerHTML='0 Results to display.  Enter new search terms.';tw_cleanup();
		return false;
	}
	
	if((!arr||!arr.results[0])&&(tw_lastsearch==tw_search))
	{
		__d(tw_curidx+" --no new tweets--");tw_cleanup();
		return false;
	}
	
	ret=ret+'<div style="color:'+tw_tweet_textcolor+';padding:0px 5px 0px 5px;'+tw_results_style_misc+'">';

	var num_newtweets;
	
	if(!arr||!arr.results[0])
	{
		num_newtweets=0;
	}
	else
	{
		num_newtweets=arr.results.length;}var newptr=(tw_settings[tw_curidx]['tw_ringptr']-num_newtweets+100)%100;
		tw_settings[tw_curidx]['tw_ringptr']=newptr;
		__d(tw_curidx+" num_newtweets "+num_newtweets);
		__d(tw_curidx+" newptr "+newptr);
		
		for(i=0;i<num_newtweets;i++)
		{
			tw_settings[tw_curidx]['tw_tweets'][(newptr+i)%100]=new Array(6);
			tw_settings[tw_curidx]['tw_tweets'][(newptr+i)%100]['user']=arr.results[i].from_user;
			tw_settings[tw_curidx]['tw_tweets'][(newptr+i)%100]['tweet']=arr.results[i].text;
			tw_settings[tw_curidx]['tw_tweets'][(newptr+i)%100]['tstamp']=new Date(arr.results[i].created_at);
			tw_settings[tw_curidx]['tw_tweets'][(newptr+i)%100]['tstamp']=tw_settings[tw_curidx]['tw_tweets'][(newptr+i)%100]['tstamp'].toLocaleString();
			tw_settings[tw_curidx]['tw_tweets'][(newptr+i)%100]['img']=arr.results[i].profile_image_url;
			tw_settings[tw_curidx]['tw_tweets'][(newptr+i)%100]['id']=arr.results[i].id;
			tw_settings[tw_curidx]['tw_tweets'][(newptr+i)%100]['search']=tw_search;
		}
		
		for(i=0;i<tw_num_tweets;i++)
		{
			if(!tw_settings[tw_curidx]['tw_tweets'][(newptr+i)%100]||tw_settings[tw_curidx]['tw_tweets'][(newptr+i)%100]['search']!=tw_search)break;
			var user=tw_settings[tw_curidx]['tw_tweets'][(newptr+i)%100]['user'];
			var tweet=tw_settings[tw_curidx]['tw_tweets'][(newptr+i)%100]['tweet'];
			var tstamp=tw_settings[tw_curidx]['tw_tweets'][(newptr+i)%100]['tstamp'];
			var img=tw_settings[tw_curidx]['tw_tweets'][(newptr+i)%100]['img'];
			var tid=tw_settings[tw_curidx]['tw_tweets'][(newptr+i)%100]['id'];
			
			if(tid>tw_settings[tw_curidx]['tw_lastid']&&tw_settings[tw_curidx]['tw_lastid']!=0)
			{
				bg=tw_tweet_newbackground;wasnew=1;
			}
			else{bg=tw_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="'+tw_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="'+tw_tweet_linkcolor+'text-decoration:none;" target="_new" href=http://twitter.com/$1>$1</a>');
			tweet=tweet.replace(/&/g,'&');
			ret=ret+'<div align="left" style="background:'+bg+';font-size:'+tw_tweet_fontsize+';border:'+tw_tweet_border+';padding:2px;margin:'+tw_tweet_margin+';'+tw_tweet_style_misc;
			
			if(tid>tw_settings[tw_curidx]['tw_lastid']&&tw_settings[tw_curidx]['tw_lastid']!=0&&typeof(jQuery)=='function'&&typeof($)=='function')
			{
				ret=ret+'display:none;';
			}
			
			ret=ret+'" ';if(tid>tw_settings[tw_curidx]['tw_lastid']&&tw_settings[tw_curidx]['tw_lastid']!=0)
			
			{
				ret=ret+' class="tw_new_tweet" ';
			}
			
			ret=ret+'>';
			
			if(!tw_hide_img)
			{
				ret=ret+'<img src='+img+' height='+tw_img_size+' width='+tw_img_size+' align=left style="padding:2px;">';
			}
			
			ret=ret+'<b><a style="'+tw_tweet_linkcolor+'text-decoration:none;" target="_new" href=http://twitter.com/'+user+'>'+user+'</a></b>: '+tweet+'<br /><span class="tw_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>';
			
			if(i<tw_num_tweets-1)ret=ret+tw_mid_html;
		
		}
		
		ret=ret+tw_post_html;ret=ret+'<center><small style="background:#fff; color:#000; border: 0px solid #000; padding: 1px; font-size: 10px; margin:2px;">Thanks for visiting!</small></center>';ret=ret+'</div>';
		
		tw_settings[tw_curidx]['tw_lastid']=(arr.results[0]?arr.results[0].id:tw_settings[tw_curidx]['tw_lastid']);
		tw_settings[tw_curidx]['tw_lastsearch']=(wasnew?'':tw_search);
		document.getElementById(tw_divname).innerHTML=ret;
		
		if(typeof(jQuery)=='function'&&typeof($)=='function')
		{
			$('#'+tw_divname+' .tw_new_tweet').fadeIn("slow");
		}
		
		tw_cleanup();
};

tw_refresh_clbk=function(i)
{
	if(tw_lock!=-1)
	{
		if(tw_lock==i)
		{
			__d(i+": trying to double lock!! aborting...");
			return false;
		}
		
		__d(i+": "+tw_lock+" already has lock, backing off...");
		setTimeout("tw_refresh_clbk("+i+");",1000+Math.floor(Math.random()*1000));
		return false;
	}
	
	__d(i+" GRABBED lock...");
	tw_lock=i;
	tw_lock_timeout_timer=setTimeout("tw_lock_timeout("+i+");",6500);
	
	if(tw_lock!=i)
	{
		setTimeout("tw_refresh_clbk("+i+");",1000+Math.floor(Math.random()*1000));
		return false;
	}
	
	tw_divname=tw_settings[i]['tw_divname'];
	tw_curidx=tw_settings[i]['tw_curidx'];
	tw_width=tw_settings[i]['tw_width'];
	tw_height=tw_settings[i]['tw_height'];
	tw_scroll=tw_settings[i]['tw_scroll'];
	tw_widget_background=tw_settings[i]['tw_widget_background'];
	tw_widget_border=tw_settings[i]['tw_widget_border'];
	tw_widget_refresh_interval=tw_settings[i]['tw_widget_refresh_interval'];
	tw_tweet_textcolor=tw_settings[i]['tw_tweet_textcolor'];
	tw_tweet_linkcolor=tw_settings[i]['tw_tweet_linkcolor'];
	tw_tweet_background=tw_settings[i]['tw_tweet_background'];
	tw_tweet_newbackground=tw_settings[i]['tw_tweet_newbackground'];
	tw_tweet_border=tw_settings[i]['tw_tweet_border'];
	tw_tweet_margin=tw_settings[i]['tw_tweet_margin'];
	tw_tweet_fontsize=tw_settings[i]['tw_tweet_fontsize'];
	tw_tweet_lang=tw_settings[i]['tw_tweet_lang'];
	tw_hide_img=tw_settings[i]['tw_hide_img'];
	tw_big_img=tw_settings[i]['tw_big_img'];
	tw_img_size=tw_settings[i]['tw_img_size'];
	tw_num_tweets=tw_settings[i]['tw_num_tweets'];
	tw_search=tw_settings[i]['tw_search'];
	tw_lastsearch=tw_settings[i]['tw_lastsearch'];
	tw_pre_html=tw_settings[i]['tw_pre_html'];
	tw_post_html=tw_settings[i]['tw_post_html'];
	tw_mid_html=tw_settings[i]['tw_mid_html'];
	tw_center_widget=tw_settings[i]['tw_center_widget'];
	tw_widget_style_misc=tw_settings[i]['tw_widget_style_misc'];
	tw_results_style_misc=tw_settings[i]['tw_results_style_misc'];
	tw_tweet_style_misc=tw_settings[i]['tw_tweet_style_misc'];
	tw_lastid=tw_settings[i]['tw_lastid'];
	
	if(tw_lock!=i)
	{
		__d(i+" failed 2nd lock check..."+tw_lock+" has it...");
		setTimeout("tw_refresh_clbk("+i+");",1000+Math.floor(Math.random()*1000));
		return false;
	}
	
	__d(i+" lastid is "+tw_settings[i].tw_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=tw_searchfunc_pre'+tw_curidx+'&'+tw_tweet_lang+'q='+tw_urlencode(tw_search)+'&rpp='+tw_num_tweets+'&since_id='+tw_settings[i].tw_lastid+'&rand='+Math.floor(Math.random()*10000000);
	h.appendChild(sc);
};

function tw_lock_timeout(i)
{
	__d("!!! forcing UNLOCK for "+i+"");
	tw_cleanup();
}

var tw_idx;
var tw_curidx;
var tw_lock;
var tw_lock_timeout_timer;
var tw_settings;
var tw_divname;
var tw_width;
var tw_height;
var tw_scroll;
var tw_widget_background;
var tw_widget_border;
var tw_widget_refresh_interval;
var tw_tweet_textcolor;
var tw_tweet_linkcolor;
var tw_tweet_background;
var tw_tweet_newbackground;
var tw_tweet_border;
var tw_tweet_margin;
var tw_tweet_fontsize;
var tw_tweet_lang;
var tw_hide_img;
var tw_big_img;
var tw_img_size;
var tw_num_tweets;
var tw_search;
var tw_lastsearch;
var tw_pre_html;
var tw_post_html;
var tw_mid_html;
var tw_center_widget;
var tw_widget_style_misc;
var tw_results_style_misc;
var tw_tweet_style_misc;
var tw_trk;
var _gat;

if(!tw_settings)tw_settings=new Array(0);

if(!tw_idx)tw_idx=0;

if(!tw_lock)tw_lock=-1;

if(!tw_divname)tw_divname='tw_widget';

if(!tw_width)tw_width='300px';

if(!tw_height)tw_height='400px';
if(!tw_scroll||tw_scroll=='yes')tw_scroll='overflow:auto;';else{tw_scroll='';}if(!tw_widget_background)tw_widget_background='#fff';
if(!tw_widget_border)tw_widget_border='1px solid #aaa';
if(!tw_widget_refresh_interval)tw_widget_refresh_interval=10;
if(tw_widget_refresh_interval<8&&tw_widget_refresh_interval!=0)tw_widget_refresh_interval=8;
if(!tw_tweet_textcolor)tw_tweet_textcolor='#000';
if(!tw_tweet_linkcolor)tw_tweet_linkcolor='color:#00f;';else{tw_tweet_linkcolor='color:'+tw_tweet_linkcolor+';';}if(!tw_tweet_background)tw_tweet_background='#f8f8f8';
if(!tw_tweet_newbackground)tw_tweet_newbackground=tw_tweet_background;
if(!tw_tweet_border)tw_tweet_border='1px solid #aaa';
if(!tw_tweet_margin)tw_tweet_margin='1px';
if(!tw_tweet_fontsize)tw_tweet_fontsize='14px';
if(!tw_search)tw_search='twitter';else{tw_search=tw_search;}if(!tw_num_tweets)tw_num_tweets=20;
if(!tw_pre_html)tw_pre_html='<center><b>found on <a href="http://twitter.com">Twitter</a></b></center>';
if(!tw_post_html)tw_post_html='';
if(!tw_mid_html)tw_mid_html='';
if(tw_center_widget=='yes')tw_center_widget='margin: 0 auto;';else{tw_center_widget='';}if(!tw_tweet_lang)tw_tweet_lang='';else{tw_tweet_lang='lang='+tw_tweet_lang+'&';}if(!tw_widget_style_misc)tw_widget_style_misc='';
if(!tw_results_style_misc)tw_results_style_misc='';
if(!tw_tweet_style_misc)tw_tweet_style_misc='';

if(tw_big_img=='yes')tw_img_size=48;
	else
	{
		tw_img_size=24;
	}
	if(tw_hide_img=='yes')tw_hide_img=1;
	else
	{
		tw_hide_img=0;
		tw_tweet_style_misc=tw_tweet_style_misc+';min-height:'+(tw_img_size+4)+'px;';
	}
	tw_idx++;
	tw_curidx=tw_idx;
	tw_settings[tw_idx]=new Array(40);
	tw_settings[tw_idx]['tw_divname']=tw_divname;
	tw_settings[tw_idx]['tw_curidx']=tw_curidx;
	tw_settings[tw_idx]['tw_width']=tw_width;
	tw_settings[tw_idx]['tw_height']=tw_height;
	tw_settings[tw_idx]['tw_scroll']=tw_scroll;
	tw_settings[tw_idx]['tw_widget_background']=tw_widget_background;
	tw_settings[tw_idx]['tw_widget_border']=tw_widget_border;
	tw_settings[tw_idx]['tw_widget_refresh_interval']=tw_widget_refresh_interval;
	tw_settings[tw_idx]['tw_tweet_textcolor']=tw_tweet_textcolor;
	tw_settings[tw_idx]['tw_tweet_linkcolor']=tw_tweet_linkcolor;
	tw_settings[tw_idx]['tw_tweet_background']=tw_tweet_background;
	tw_settings[tw_idx]['tw_tweet_newbackground']=tw_tweet_newbackground;
	tw_settings[tw_idx]['tw_tweet_border']=tw_tweet_border;
	tw_settings[tw_idx]['tw_tweet_margin']=tw_tweet_margin;
	tw_settings[tw_idx]['tw_tweet_fontsize']=tw_tweet_fontsize;
	tw_settings[tw_idx]['tw_tweet_lang']=tw_tweet_lang;
	tw_settings[tw_idx]['tw_hide_img']=tw_hide_img;
	tw_settings[tw_idx]['tw_big_img']=tw_big_img;
	tw_settings[tw_idx]['tw_img_size']=tw_img_size;
	tw_settings[tw_idx]['tw_num_tweets']=tw_num_tweets;
	tw_settings[tw_idx]['tw_search']=tw_search;
	tw_settings[tw_idx]['tw_lastsearch']=tw_search;
	tw_settings[tw_idx]['tw_pre_html']=tw_pre_html;
	tw_settings[tw_idx]['tw_post_html']=tw_post_html;
	tw_settings[tw_idx]['tw_mid_html']=tw_mid_html;
	tw_settings[tw_idx]['tw_center_widget']=tw_center_widget;
	tw_settings[tw_idx]['tw_widget_style_misc']=tw_widget_style_misc;
	tw_settings[tw_idx]['tw_results_style_misc']=tw_results_style_misc;
	tw_settings[tw_idx]['tw_tweet_style_misc']=tw_tweet_style_misc;
	tw_settings[tw_idx]['lastid']=0;tw_settings[tw_idx]['tw_timer']='';
	tw_settings[tw_idx]['tw_tweets']=new Array(100);
	tw_settings[tw_idx]['tw_ringptr']=0;document.write('<div style="width:'+tw_width+';height:'+tw_height+';'+tw_center_widget+';padding:0px;margin:0px;'+'">');document.write('<div id='+tw_divname+' style="'+tw_scroll+''+tw_center_widget+'width:auto;height:'+tw_height+';background:'+tw_widget_background+';'+'border:'+tw_widget_border+';'+tw_widget_style_misc+'">');document.write('</div>');document.write('</div>');

	if(tw_search!='-')
	{
		document.write('<script src="http://search.twitter.com/search.json?callback=tw_searchfunc&'+tw_tweet_lang+'q='+tw_urlencode(tw_search)+'&rpp='+tw_num_tweets+'&since_id=0" type="text/javascript"></script>');
	}
	else{}

