function twitTracker(query,refreshWrapDomId,feedListDomId,maxItemsInFeedList,refreshTime)
{mylogger.log('FUNCTION: twitTracker');this.countdownId='';this.countdownObj=null;this.timeoutId=null;this.ajaxRequest='';this.running=false;this.cache=new Array();this.query=query;this.refreshWrapDomId=refreshWrapDomId;this.feedListDomId=feedListDomId;this.maxItemsInFeedList=maxItemsInFeedList;this.refreshTime=refreshTime;this.pauseLabel="Pause";this.pauseTitle="Click to pause automatic updates";this.resumeLabel="Resume";this.resumeTitle="Click to resume automatic updates";this.filterReplyLinks=false;this.init=function(){var that=this;this.running=true;this.toggling=false;this.fetching=false;this.showCountdown();$(this.refreshWrapDomId+" a.toggleupdates").html(this.pauseLabel).attr("title",this.pauseTitle);$(this.refreshWrapDomId+" a.toggleupdates").click(function(){that.toggleUpdates();return false;});}}
twitTracker.prototype.toggleUpdates=function()
{mylogger.log('FUNCTION: toggleUpdates');var that=this;if(that.toggling==true)return;that.toggling==true;if(that.running==true){that.pause();}else{that.resume();}
that.running=!that.running;}
twitTracker.prototype.pause=function()
{mylogger.log('FUNCTION: pause');var that=this;this.hideLoader();this.hideNewTweetCount(0);this.hideNoTweets(0);if(this.countdownObj)
{clearTimeout(this.countdownObj.countdownId);}
this.countdownObj=null;this.hideCountdown();$(that.refreshWrapDomId+" a.toggleupdates").animate({opacity:0},500,function()
{$(that.refreshWrapDomId+" a.toggleupdates").html(that.resumeLabel).attr("title",that.resumeTitle);$(that.refreshWrapDomId+" a.toggleupdates").animate({opacity:1},500);that.toggling==false;});}
twitTracker.prototype.resume=function()
{mylogger.log('FUNCTION: resume');var that=this;$(this.refreshWrapDomId+" a.toggleupdates").animate({opacity:0},500,function()
{$(that.refreshWrapDomId+" a.toggleupdates").html(that.pauseLabel).attr("title",that.pauseTitle);$(that.refreshWrapDomId+" a.toggleupdates").animate({opacity:1},500,function()
{that.toggling==false;that.prepTweetSearch();});});}
twitTracker.prototype.prepTweetSearch=function()
{mylogger.log('FUNCTION: prepTweetSearch');var that=this;if(this.running==false)return;var latestTweetId=$(this.feedListDomId).find('li:first').attr('id').substring(6);var url="http://search.twitter.com/search.json?q="+this.query+"&since_id="+latestTweetId+"&rpp=15&callback=?";this.showLoader(function(data){that.doTweetSearch(data,url);});}
twitTracker.prototype.doTweetSearch=function(data,url)
{mylogger.log('FUNCTION: doTweetSearch');var that=this;var ajaxRequest=$.ajax({url:url,type:'GET',dataType:'json',timeout:7000,data:data,success:function(data,textStatus){mylogger.log('ajax success: testStatus = '+textStatus);clearTimeout(that.timeoutId);ajaxRequest=null;that.parseResults(data.results)},error:function(XMLHttpRequest,textStatus,errorThrown){mylogger.log('ajax error: testStatus = '+XMLHttpRequest.status+' - '+textStatus+' - '+errorThrown);clearTimeout(that.timeoutId);ajaxRequest=null;that.showCommunicationBreakdown();},complete:function(XMLHttpRequest,textStatus){mylogger.log('ajax complete: testStatus = '+textStatus);}});that.timeoutId=setTimeout(function(){if(ajaxRequest){mylogger.log('ABORT THE AJAX CALL');ajaxRequest.abort();}
that.running=false;that.showCommunicationBreakdown();},7000);}
twitTracker.prototype.parseResults=function(data)
{mylogger.log('FUNCTION: parseResults');var that=this;if(data)
{var i=-1,result,strTweetOutput='',strTweetOutput2='',arrTweetOutput=[];while((result=data[++i]))
{var tweet_id=result.id;var tweet_li_id='tweet-'+result.id;var tweet_published=result.created_at;var tweet_published_relative=relative_time(result.created_at);var tweet_author_name=result.from_user;var tweet_author_uri='http://twitter.com/'+tweet_author_name;var tweet_uri='http://twitter.com/'+tweet_author_name+'/statuses/'+tweet_id;var tweet_avatar=result['profile_image_url'];var tweet_title=result.text;var tweet_title_encoded=encodeUrl(tweet_title);tweet_published_gmt=tweet_published.replace("+0000","GMT");var timezone_offset=-6*1000*60*60;var tweet_published_formatted=new Date().formatDate('d M Y, g:i a',Date.parse(tweet_published_gmt)+timezone_offset);tweet_title=tweet_title.replace("Live at http://ustre.am/2j0r","");tweet_title=tweet_title.replace("Live at http://ustre.am/3lth","");tweet_title=tweet_title.replace("Live at http://ustre.am/3gGI","");tweet_title=tweet_title.replace("Live at http://ustre.am/3iGy","");tweet_title=tweet_title.replace("Live at http://ustre.am/3mtb","");tweet_title=tweet_title.replace("(PhishTube Broadcast live > http://ustre.am/2j0r)","");tweet_title=tweet_title.replace("(Bonnaboobs live > http://ustre.am/3lth)","");tweet_title=tweet_title.replace("(Joephus0311 live > http://ustre.am/3gGI)","");var imageRegEx=/(http:\/\/(www.|)+(twitpic.com|img.ly|yfrog.com)\/){1}[a-zA-Z0-9]{1,}/g;var tweet_twitpic_id='';var tweet_twitpic_img='';var tweet_twitpic_url='';if(tweet_title.search(imageRegEx)!==-1)
{imgFind=tweet_title.match(imageRegEx);if(imgFind[0].indexOf("http://twitpic.com/")>-1)
{tweet_twitpic_id=imgFind[0].substr(19);tweet_twitpic_img='http://twitpic.com/show/thumb/'+tweet_twitpic_id+'.jpg';tweet_twitpic_url='http://twitpic.com/'+tweet_twitpic_id;}
if(imgFind[0].indexOf("http://www.twitpic.com/")>-1)
{tweet_twitpic_id=imgFind[0].substr(23);tweet_twitpic_img='http://twitpic.com/show/mini/'+tweet_twitpic_id+'.jpg';tweet_twitpic_url='http://twitpic.com/'+tweet_twitpic_id;}}
strTweetOutput+='<li id="'+tweet_li_id+'" class="new" style="display:none;background-color:#FFEFE5;">';strTweetOutput+='<div class="tweet-wrap clearfix">';strTweetOutput+='<div class="tweet-avatar-wrap">';strTweetOutput+='<a href="'+tweet_author_uri+'" title="Click to see the Twitter profile for '+tweet_author_name+'"><img src="'+tweet_avatar+'" alt="" class="small-image-wrap" width="48" height="48" /></a>';strTweetOutput+='</div>';strTweetOutput+='<div class="tweet-content-wrap">';strTweetOutput+='<div class="tweet-content clearfix">';if(tweet_twitpic_img!='')
{strTweetOutput+='<a href="'+tweet_twitpic_url+'" title="Click to see larger image on TwitPic."><img src="'+tweet_twitpic_img+'" alt="" class="small-image-wrap" width="150" height="150" /></a>';}
strTweetOutput+='<h4>'+FormatIt(tweet_title)+'</h4>';strTweetOutput+='</div>';strTweetOutput+='<div class="tweet-meta clearfix">';strTweetOutput+='<div class="date-wrap short note">By <a href="'+tweet_author_uri+'" title="Go to profile for '+tweet_author_name+'" class="normal">@'+tweet_author_name+'</a> on <a href="'+tweet_uri+'" title="Click to see this Tweet on Twitter" rel="nofollow">'+tweet_published_formatted+'</a> CT <span class="newflag">NEW</span></div>';strTweetOutput+='<div class="tweet-tools-wrap">';strTweetOutput+='<a href="http://twitter.com/home?status=%40'+tweet_author_name+'&in_reply_to_status_id='+tweet_id+'&in_reply_to='+tweet_author_name+'" title="Click to reply to this Tweet on Twitter" rel="nofollow tweetreply"><img src="/images/twitter-reply.gif" width="11" height="12" alt="reply" class="reply" /></a>';strTweetOutput+=' <a href="http://twitter.com/home?status=RT+%40'+tweet_author_name+':+'+tweet_title_encoded+'" title="Click to retweet this Tweet on Twitter" rel="nofollow tweetretweet"><img src="/images/twitter-retweet.gif" width="14" height="9" alt="RT" /></a>';strTweetOutput+='</div>';strTweetOutput+='</div>';strTweetOutput+='</div>';strTweetOutput+='</div>';strTweetOutput+='</li>';arrTweetOutput.push(strTweetOutput);strTweetOutput='';}
if(that.running==false)return;strTweetOutput=arrTweetOutput.join('');if(that.running==false)return;if(strTweetOutput!=='')
{$(that.feedListDomId).prepend(strTweetOutput)
$("a",$(that.feedListDomId)).filter(function(){if($(this).attr('rel')=='samewindow')return false;return this.hostname&&this.hostname!==location.hostname;}).attr('target','_blank');if(that.filterReplyLinks==true){twitPosterFilterReplyLinks();}
that.showNewTweets();$(that.feedListDomId+" li:gt("+that.maxItemsInFeedList+")").remove();}else{that.showNoTweets();}}
return this;};twitTracker.prototype.showCountdown=function()
{mylogger.log('FUNCTION: showCountdown');var that=this;if(that.running==false)return;$(this.refreshWrapDomId).append('<span class="tweetcountdown">Next refresh in <span class="counter">...</span></span>');this.countdownObj=$(this.refreshWrapDomId+' span.tweetcountdown span.counter').countdown({seconds:this.refreshTime,loopTime:750},function(){that.hideCountdown(function(){that.prepTweetSearch();})},function(){});$(this.refreshWrapDomId+' span.tweetcountdown').fadeIn(1500);return this;};twitTracker.prototype.hideCountdown=function(callback)
{mylogger.log('FUNCTION: hideCountdown');$(this.refreshWrapDomId+' span.tweetcountdown').fadeOut(750,function()
{$(this).remove();if(callback)
{callback();}});return this;};twitTracker.prototype.showLoader=function(callback)
{mylogger.log('FUNCTION: showLoader');var that=this;if(that.running==false)return;$(this.refreshWrapDomId).append('<span class="loading">Fetching new tweets</span>');$(this.refreshWrapDomId+' span.loading').fadeIn(1500,function()
{if(callback)
{callback();}});return this;};twitTracker.prototype.hideLoader=function(callback)
{mylogger.log('FUNCTION: hideLoader');$(this.refreshWrapDomId+' span.loading').fadeOut(500,function()
{$(this).remove();if(callback)
{callback();}});return this;};twitTracker.prototype.showCommunicationBreakdown=function()
{mylogger.log('FUNCTION: showCommunicationBreakdown');var that=this;this.hideLoader(function()
{mylogger.log('hideLoaderCallback');$(that.refreshWrapDomId).append('<span class="notweets">Network timeout, trying again</span>');$(that.refreshWrapDomId+' span.notweets').fadeIn(500);that.hideCommunicationBreakdown(2);});return this;}
twitTracker.prototype.hideCommunicationBreakdown=function(waittime)
{mylogger.log('FUNCTION: hideCommunicationBreakdown');var that=this;$(that.refreshWrapDomId+' span.notweets').wait(waittime).then.fadeOut(500,function()
{$(this).remove();that.running=true;that.showCountdown();});return this;}
twitTracker.prototype.showNoTweets=function()
{mylogger.log('FUNCTION: showNoTweets');var that=this;if(that.running==false)return;this.hideLoader(function()
{$(that.refreshWrapDomId).append('<span class="notweets">No new tweets found</span>');$(that.refreshWrapDomId+' span.notweets').fadeIn(500);that.hideNoTweets(2);});return this;}
twitTracker.prototype.hideNoTweets=function(waittime)
{mylogger.log('FUNCTION: hideNoTweets');var that=this;$(that.refreshWrapDomId+' span.notweets').wait(waittime).then.fadeOut(500,function()
{$(this).remove();if(that.running==false)return;that.showCountdown();});return this;}
twitTracker.prototype.showNewTweets=function()
{mylogger.log('FUNCTION: showNewTweets');var that=this;if(that.running==false)return;var $newTweets=$(that.feedListDomId).find('li:hidden');var tweetCount=$newTweets.length;that.hideLoader(function()
{var toggletime;toggletime=Math.log(tweetCount)*750;toggletime=Math.round(toggletime);toggletime=Math.max(toggletime,1000);that.showNewTweetCount(tweetCount,toggletime);$newTweets.hoverIntent({sensitivity:2,interval:25,over:hideNewFlag,timeout:0,out:unbindNewFlag});$newTweets.slideFadeToggle(toggletime,'easeOutQuad',function()
{$(that.feedListDomId+" li.new").highlightFade({start:'#FFEFE5',end:'#FFFFFF',speed:2500}).removeClass("new");});});return this;};twitTracker.prototype.showNewTweetCount=function(tweetCount,toggletime)
{mylogger.log('FUNCTION: showNewTweetCount');var that=this;if(that.running==false)return;waittime=Math.ceil(toggletime/1000)+1;var newTweetLabel;tweetCount==1?newTweetLabel=tweetCount+'  new tweet found.':newTweetLabel=tweetCount+'  new tweets found';$(that.refreshWrapDomId).append('<span class="tweetcount" style="display:none;">'+newTweetLabel+'</span>');$(that.refreshWrapDomId+' span.tweetcount').fadeIn(500,function()
{waittime=Math.ceil(toggletime/1000)+1;that.hideNewTweetCount(waittime);});tweetCount='';return this;}
twitTracker.prototype.hideNewTweetCount=function(toggletime)
{mylogger.log('FUNCTION: hideNewTweetCount');var that=this;$(that.refreshWrapDomId+' span.tweetcount').wait(toggletime).then.fadeOut(750,function()
{$(this).remove();if(that.running==false)return;that.showCountdown();});return this;}
function hideNewFlag(){$(this).find(".newflag").doHideNewFlag('')}
function unbindNewFlag(){$(this).unbind('mouseover').unbind('mouseout')}
jQuery.fn.doHideNewFlag=function(callback)
{mylogger.log('FUNCTION: doHideNewFlag');this.fadeOut(250,function()
{$(this).remove();if(callback)
{callback();}});return this;};function FormatIt(text_results)
{return text_results.parseURL().parseUsername().parseHashtag();}
String.prototype.parseURL=function(){return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g,function(url){return url.link(url);});};String.prototype.parseUsername=function(){return this.replace(/[@]+[A-Za-z0-9-_]+/g,function(u){var username=u.replace("@","")
return u.link("http://twitter.com/"+username);});};String.prototype.parseHashtag=function(){return this.replace(/[#]+[A-Za-z0-9-_]+/g,function(t){var tag=t.replace("#","%23")
return t.link("http://search.twitter.com/search?q="+tag);});};function encodeUrl(str){var ret=str;ret=ret.toString();ret=encodeURIComponent(ret);ret=ret.replace(/%20/g,'+');return ret;}
function relative_time(time_value){var values=time_value.split(" ");time_value=values[2]+" "+values[1]+", "+values[3]+" "+values[4];var parsed_date=Date.parse(time_value);var relative_to=(arguments.length>1)?arguments[1]:new Date();var delta=parseInt((relative_to.getTime()-parsed_date)/1000);delta=delta+(relative_to.getTimezoneOffset()*60);var r='';if(delta<60){r='a minute ago';}else if(delta<120){r='couple of minutes ago';}else if(delta<(45*60)){r=(parseInt(delta/60)).toString()+' minutes ago';}else if(delta<(90*60)){r='an hour ago';}else if(delta<(24*60*60)){r=''+(parseInt(delta/3600)).toString()+' hours ago';}else if(delta<(48*60*60)){r='1 day ago';}else{r=(parseInt(delta/86400)).toString()+' days ago';}
return r;}
jQuery.fn.countdown=function(options,completeCallback,loopCallback){mylogger.log('FUNCTION: countdown');if(!options)options='()';if(jQuery(this).length==0)return false;this.countdownId='';this.loopTime=1000;if(options.loopTime!='')
{this.loopTime=options.loopTime;}
var that=this;if(options.seconds<0||options.seconds=='undefined')
{if(completeCallback){completeCallback();}
this.countdownId='';return null;}
this.countdownId=window.setTimeout(function(){jQuery(that).html(String(options.seconds));--options.seconds;if(loopCallback)
{loopCallback();}
jQuery(that).countdown(options,completeCallback,loopCallback);},this.loopTime);return this;}