
tweetEffect=function(){document.getElementById('f').onsubmit=function(){sm.disabled=true;tweetEffect.fetch(document.getElementById('user').value);return false;};var sm=document.getElementById('sm');var butst=sm.value;var state=false;function get(o){sm.value=butst;sm.disabled=false;if(o.error){document.getElementById('results').innerHTML='<div id="error"><h2>Invalid user name</h2><p>Sorry we didn\'t find any user with that name.</p></div>';}else{var updates=[];var all=o.length-1;var results=[];var data={user:o[0].user.screen_name,all:all+1,start:o[0].user.followers_count,end:o[all].user.followers_count,losses:0,ilosses:0,igains:0,gains:0,gainstotal:0,lossestotal:0};for(var i=0;i<all;i++){var current=o[i].user.followers_count;var previous=o[i+1].user.followers_count;var date=o[i].created_at.replace(/ \+.*$/,'');var period=difference(o[i].created_at,o[i+1].created_at);var change=current-previous;var cur={t:o[i+1].text,f:current,c:change,date:date,cl:'none'};if(change<0){if(period<5&&change<-1){cur.cl='iloss';data.ilosses++;}else{cur.cl='loss';data.losses++;}
data.lossestotal+=change;}
if(change>0){if(period<5&&change>1){cur.cl='iwin';data.igains++;}else{cur.cl='win';data.gains++;}
data.gainstotal+=change;}
var disp=change+50;if(disp<0){disp=0;}
if(disp>100){disp=100;}
results.push(disp);updates.push(cur);}
adduser(o);if(data.ilosses>0||data.igains>0){table(updates,1,results);}
else if(data.losses>0||data.gains>0){table(updates,0,results);}else{document.getElementById('results').innerHTML='';}
shortresults(data,o);}}
function shortresults(o,u){var d=(o.start-o.end);var text='<p'+(d<0?' class="loss"':'');text+='>In the last <strong>'+o.all+'</strong> updates';if(o.losses!==0){text+=' you lost followers <strong>'+o.losses+'</strong> times';}
if(o.gains!==0){text+=' and got new followers <strong>'+o.gains+'</strong> times';}
if(o.losses===0&&o.gains===0){text+=' you had no changes in your follower numbers. Loyal readers, you have';}
text+='. ';
var str='';
if(d!==0){
text+='Overall you ';
if(o.lossestotal!==0){
  text+='lost  <strong>'+(-o.lossestotal)+'</strong>';
}
if(o.gainstotal!==0){
  text+=' and gained <strong>' + o.gainstotal + '</strong>';
}
text+=' which means that in total you ';if(d===0){str=' have still the same number of followers.';}else{str+=d<0?'lost '+'<strong>'+-d+'</strong>':'gained '+'<strong>'+d+'</strong>';str+=' followers.';}
text+=str+'</p>';
}
document.getElementById('shortresults').innerHTML=text;var viral='';
if(d!==0){viral+= '<p><a href="http://twitter.com/home?status=Lately I lost followers '+o.losses+' times and gained '+o.gains+' times. I '+str.replace(/<\/?strong>/g,'')+' http://tweeteffect.com">Twitter this</a>';}viral+=' <a href="get-badge.php?user='+o.user+'">Get this badge</a></p>';document.getElementById('viral').innerHTML=viral;}
function filtertable(cl,o){if(!state){state=document.getElementById('buttons').getElementsByTagName('input')[0];}
state.className='';o.className='cur';document.getElementById('tweetdata').className=cl;state=o;}
function table(o,s,chart){var info='';info+='<h2>Your Frequency of follower changes</h2><img src="http://chart.apis.google.com/chart?cht=ls&chd=t:'+chart.join(',')+'&chs=738x100'+'" style="margin-bottom:10px;margin-top:-10px" alt="chart of your follower activity stats">';info+='<h2>And the details</h2><p>Following is the data we got from Twitter about you. Tweets that affected your follower numbers are colour coded. <span class="win">Green tweets</span> mean you gained followers and <span class="loss">Red Tweets</span> mean you lost followers (or check the arrow icons).</p>';if(s===1){info+='<p><span class="iloss">Bold</span> and <span class="iwin">coloured</span> tweets are important changes, which means they happened in less than five minutes and resulted in a change of at least two followers.</p><p>You can filter the type of tweets you want to see by pressing the following buttons</p>';}
var buttons='';if(s==1){var buttons='<div id="buttons">';buttons+='<input type="button" class="cur" onclick="tweetEffect.filter(\'changes\',this)" value="changes">';buttons+='<input type="button" onclick="tweetEffect.filter(\'important\',this)" value="important changes">';buttons+='<input type="button" onclick="tweetEffect.filter(\'all\',this)" value="all"></div>';}
var table='<table id="tweetdata" class="';table+=(s===1)?'changes':'all';table+='">';var all=o.length-1;table+='<thead><tr><th>Trend</th><th>Date</th><th>Update</th><th>Followers</th><th>Change</th></tr></thead>';for(var i=0;i<all;i++){table+='<tr class="'+o[i].cl+'">';var addon='-';if(o[i].cl==='win'||o[i].cl==='iwin'){addon='⇧';}
if(o[i].cl==='loss'||o[i].cl==='iloss'){addon='⇩';}
table+='<td class="trend">'+addon+'</td><td>'+o[i].date+'</td>';table+='<td>'+o[i].t+'</td><td>'+o[i].f+'</td><td>'+o[i].c+'</td>';table+='</tr>';}
table+='</table>';document.getElementById('results').innerHTML=info+buttons+table;}
function adduser(data){var ui=document.getElementById('userinfo');var prof='<h2>And we\'re done, here is your results summary:</h2><p>';prof+='<a href="http://twitter.com/'+data[0].user.screen_name+'"><img src="'+data[0].user.profile_image_url+'"alt="'+data[0].user.name+'"></a>';prof+='You are '+data[0].user.name;prof+=' (<a href="http://twitter.com/'+data[0].user.screen_name+'">'+data[0].user.screen_name+'</a>)';prof+=' and you have '+data[0].user.followers_count+' followers.';prof+="</p>";if(data[0].user.protected){prof+='<strong>Your updates are protected only you and your followers can see the data below!</strong>'}ui.innerHTML=prof;}
function fetch(user){sm.value='loading...';document.getElementById('results').innerHTML='<img src="ajax-loader.gif" alt="Loading">';document.getElementById('shortresults').innerHTML='';document.getElementById('userinfo').innerHTML='';document.getElementById('viral').innerHTML='';var url='http://twitter.com/statuses/user_timeline/'+user+'.json?count=200&callback=tweetEffect.get&suppress_response_codes';var s=document.createElement('script');s.type='text/javascript';s.src=url;document.getElementsByTagName('head')[0].appendChild(s);};function difference(d1,d2){var nd=new Date(d1);var nd2=new Date(d2);var delta=nd.getTime()-nd2.getTime();return delta/60000;}
return{fetch:fetch,get:get,filter:filtertable};}();
