var reg7=/MSIE 7.0/g,ie7=reg7.test(navigator.userAgent),reg6=/MSIE 6.0/g,ie6=reg6.test(navigator.userAgent),docMode=document.documentMode?document.documentMode.toString():"",classAttr="class";if(docMode==="7"||docMode==="5"){classAttr="className"}else if(docMode===""){classAttr=(ie7||ie6)?"className":"class"};(function($){var config={date:'date',display:"list",errorHtml:"Twitter feed currently unavailable",hashTags:"",loadingHtml:"Loading twitter feed...",nextLinkText:"&raquo;",prevLinkText:"&laquo;",screenNames:"",shoutOuts:"",tweetCount:10},singleFeed=false,rex={shout:/@[\w-]*/,hash:/#[\w-]*/,link:/(http:\/\/).*/},TweetSet=function(tweets,elm){var $this=this;this.curTweet=0;this.tweetCount=tweets.length;this.nextBtn=document.createElement("a");this.prevBtn=document.createElement("a");this.dateSpan=document.createElement("span");this.authorSpan=document.createElement("span");this.dateAuth=document.createElement("span");this.tweet=document.createElement("span");this.nextBtn.innerHTML=config.nextLinkText;this.prevBtn.innerHTML=config.prevLinkText;this.nextBtn.setAttribute(classAttr,"twitterNext");this.prevBtn.setAttribute(classAttr,"twitterPrev");this.dateSpan.setAttribute(classAttr,"twitterDate");this.authorSpan.setAttribute(classAttr,"twitterAuth");this.dateAuth.setAttribute(classAttr,"dateAuth");this.tweet.setAttribute(classAttr,"twitterText");this.tweets=tweets;this.showTweet=function(i){if(i<0||i>$this.tweetCount-1){return false};var username=$this.tweets[i].from_user||$this.tweets[i].user.screen_name;$this.tweet.innerHTML=parseTweet($this.tweets[i].text);$this.dateSpan.innerHTML=doDate($this.tweets[i].created_at)+" ";$this.authorSpan.innerHTML=parseTweet("by @"+username);this.curTweet=i};this.nextBtn.onclick=function(){$this.showTweet($this.curTweet+1)};this.prevBtn.onclick=function(){$this.showTweet($this.curTweet-1)};this.showTweet(0);$(this.dateAuth).append(this.dateSpan,this.authorSpan);$(elm).append(this.nextBtn,this.prevBtn,this.tweet,this.dateAuth)},TweetList=function(tweets,elm){var alt=false;this.tweets=tweets;this.Tweet=function(tweet){var username=tweet.from_user||tweet.user.screen_name;this.wrap=document.createElement("p");this.date=document.createElement("span");this.author=document.createElement("span");this.text=document.createElement("span");this.date.setAttribute(classAttr,"twitterDate");this.author.setAttribute(classAttr,"twitterAuth");this.text.setAttribute(classAttr,"twitterText");this.text.innerHTML=parseTweet(tweet.text);this.date.innerHTML=doDate(tweet.created_at)+" ";this.author.innerHTML=parseTweet("by @"+username);$(this.wrap).append(this.text,this.date,this.author)};for(var x in this.tweets){var tmpTwt=new this.Tweet(this.tweets[x]);tmpTwt.wrap.setAttribute(classAttr,alt?"even":"odd");elm.appendChild(tmpTwt.wrap);alt=!alt}},parseTweet=function(str){var strArr=str.split(" ");for(var x in strArr){var word=strArr[x];if(rex.shout.test(word)){var name=word.match(rex.shout)[0];name="<a target='_blank' href='http://www.twitter.com/"+name.substring(1,name.length)+"'>"+name+"</a>";strArr[x]=word.replace(rex.shout,name)};if(rex.hash.test(word)){var hash=word.match(rex.hash)[0];hash="<a target='_blank' href='http://search.twitter.com/search?q="+encodeURIComponent(hash)+"'>"+hash+"</a>";strArr[x]=word.replace(rex.hash,hash)};if(rex.link.test(word)){var link=word.match(rex.link)[0];link="<a target='_blank' href='"+link+"'>"+link+"</a>";strArr[x]=word.replace(rex.link,link)}};return strArr.join(" ")},doDate=function(rawDate){var $return=false;if(config.date==='date'){return rawDate.split(" +")[0]};if(config.date==='difference'){if(singleFeed){theArray=rawDate.split(" ");theArray[0]+=",";theArray.splice(3,0,theArray.pop());theArray.splice(2,0,theArray.splice(1,1));rawDate=theArray.join(" ")};var today=new Date(),tDate=new Date(rawDate),oneSec=1000,oneMin=oneSec*60,oneHour=oneMin*60,oneDay=oneHour*24,oneWeek=oneDay*7,oneMonth=oneWeek*4,msDiff=today.getTime()-tDate.getTime();if(msDiff>=oneMonth)$return=Math.round(msDiff/oneMonth)+" month";else if(msDiff>=oneWeek)$return=Math.round(msDiff/oneWeek)+" week";else if(msDiff>=oneDay)$return=Math.round(msDiff/oneDay)+" day";else if(msDiff>=oneHour)$return=Math.round(msDiff/oneHour)+" hour";else if(msDiff>=oneMin)$return=Math.round(msDiff/oneMin)+" minute";else if(msDiff>=oneSec)$return=Math.round(msDiff/oneSec)+" second";if(parseInt($return)>1)$return=$return+"s";if($return)return($return+" ago");return"less than 1 second ago"}},ajaxURI="",setAjaxUri=function(){var rex=/^(@|#)/i,scArr=config.screenNames.split(","),hashArr=config.hashTags.split(","),shoutArr=config.shoutOuts.split(","),searchTerms=scArr.concat(hashArr).concat(shoutArr);if(config.hashTags===""&&config.shoutOuts===""&&config.screenNames.split(",").length===1){singleFeed=true;ajaxURI="http://api.twitter.com/1/statuses/user_timeline.json?count=";ajaxURI+=config.tweetCount+"&include_rts=true&screen_name="+config.screenNames+"&callback=?"}else{for(var x in searchTerms){var word=searchTerms[x];if(word!=""){searchTerms[x]=encodeURIComponent(rex.test(word)?word:"from:"+word);ajaxURI+=searchTerms[x]+"+OR+"}};ajaxURI="http://search.twitter.com/search.json?q="+ajaxURI.substring(0,ajaxURI.length-4)+"&rpp="+config.tweetCount+"&callback=?"}};$.fn.twitter=function(o){var $this=this,x;$this.addClass("ajaxLoading").html("");if(typeof o==='object'){config=$.extend(config,o)}else{config.screenNames=o};setAjaxUri();x=$.ajax({type:'GET',url:ajaxURI,dataType:'json',success:function(o){var data=o;$this.removeClass("ajaxLoading");return $this.each(function(){var feedData=data.results||data;tweets=config.display==="list"?new TweetList(feedData,this):new TweetSet(feedData,this);$(this).addClass("twitter"+config.display)})},error:function(e){$this.removeClass("ajaxLoading").html(config.errorHtml);window.ajaxError=e}})}})(window.jQuery);
