(function($){$.fn.extend({jTweetFeed:function(options,points,legenda){var defaults={keyword:null,username:null,autoUpdate:true,refreshRate:10,showPhotos:false,populateAmount:'auto',animatePopulation:false,maxUpdateAmount:30,animationSpeed:400,url:'/site/social/twitter?'};var options=$.extend(defaults,options);var feedContainer=null;var urlParts=window.location.pathname.split('/');if(urlParts[1]=='gb'){var langCode='gb'}else{var langCode='nl'}function translate(str){if(langCode=='nl'){var replace=new Array("by","about","hours","hour","minute","tmp","seconds","second","ago","right now");var by=new Array("door","ongeveer","uur","uur","minuut","minuten","seconden","seconde","geleden","nu net");str=str.replace('minutes','tmp');for(var i=0;i<replace.length;i++){str=str.replace(replace[i],by[i])}}return str}return this.each(function(){feedContainer=this;populateFeed(feedContainer);if(options.autoUpdate){setTimeout(function(){setInterval(getNewTweets,(options.refreshRate*1000))},10000)}});function getNewTweets(){if($(feedContainer).find('.socialBlock:first').length){var lastid=$(feedContainer).find('.socialBlock:first').attr('id').substring(9);if(lastid){getTweets(options.url+"&rpp="+options.maxUpdateAmount+"&since_id="+lastid,'update')}}}function populateFeed(){if(options.populateAmount!='auto'){getTweets(options.url+"&rpp="+options.populateAmount,'populate')}else{getTweets(options.url+"&rpp=50",'populate')}}function getTweets(url,type){$.ajax({url:url,dataType:"json",success:function(tweets){if(tweets.results.length>0){var tweets=tweets.results.reverse();var tweetArray=[];for(i in tweets){var tweetBlock=$('<dl id="tweet_id_'+tweets[i].id_str+'" class="socialBlock" style="display: none;"><dt><span></span></dt><dd><span class="photo"></span><span class="author"></span><span class="text"></span><span class="date"></span></dd></dl>');$(tweetBlock).find('.author').html('<a target="_blank" href="http://www.twitter.com/'+tweets[i].from_user+'" target="_blank">'+translate('by')+' '+tweets[i].from_user+'</a>');$(tweetBlock).find('.text').html(replaceURLWithHTMLLinks(tweets[i].text));$(tweetBlock).find('.date').html('<a target="_blank" href="http://twitter.com/'+tweets[i].from_user+'/statuses/'+tweets[i].id_str+'" target="_blank">'+translate(prettyDate(tweets[i].created_at))+'</a>');if(options.showPhotos){$(tweetBlock).find('.photo').html($('<img>').attr('src',tweets[i].profile_image_url))}tweetArray[tweetArray.length]=tweetBlock}addTweetsToFeed(tweetArray,type)}}})}function addTweetsToFeed(tweets,type){var startside="Left";var otherside="Right";if($(feedContainer).find('.socialBlock:first').length>0){var firstBlockClasses=$(feedContainer).find('.socialBlock:first').attr('class').split(' ');if(firstBlockClasses[1]=='socialBlockLeft'){startside="Right";otherside="Left"}}$(tweets[0]).addClass('socialBlock'+startside);for(i in tweets){if($(feedContainer).find('.socialBlock').length>=2){var firstBlock=$(feedContainer).find('.socialBlock:eq(0)');var secondBlock=$(feedContainer).find('.socialBlock:eq(1)');var firstClasses=$(firstBlock).attr('class').split(' ');for(a in firstClasses){if(firstClasses[a].indexOf('socialRowIndent')>-1){var firstIndent=parseInt(firstClasses[a].substring(15))}}var secondClasses=$(secondBlock).attr('class').split(' ');for(a in secondClasses){if(secondClasses[a].indexOf('socialRowIndent')>-1){var secondIndent=parseInt(secondClasses[a].substring(15))}}if(firstIndent==secondIndent){var newIndent=secondIndent+1}else if(firstIndent!=secondIndent){var newIndent=firstIndent}if(newIndent>8){newIndent=1}var indentClass='socialRowIndent'+newIndent}else{var indentClass='socialRowIndent1'}var side=(i%2)?otherside:startside;$(tweets[i]).addClass('socialBlock'+side).addClass(indentClass);$(feedContainer).prepend(tweets[i]);var theHeight=Math.ceil(tweets[i].height());var theHalf=Math.ceil(theHeight/2);$(tweets[i]).css('top','-'+theHalf+'px').css('margin-bottom','-'+theHalf+'px')}if(type!='populate'||options.animatePopulation==true){var i=0;showNextTweet()}else{$(feedContainer).find('.socialBlock').show();$(feedContainer).css('background-image','none')}function showNextTweet(){if(options.populateAmount=='auto'&&type=='populate'){var lastTweet=$(feedContainer).find('.socialBlock:last');var yPosBottom=$(lastTweet).offset().top+$(lastTweet).height();if(yPosBottom<$('body').height()){var withinViewport=true}else{var withinViewport=false}}if(i<tweets.length){if(options.populateAmount=='auto'&&type=='populate'){if(withinViewport==true){$(tweets[i]).slideDown(options.animationSpeed,showNextTweet)}else{$(tweets[i]).slideDown(options.animationSpeed);$(feedContainer).css('background-image','none')}}else{$(tweets[i]).slideDown(options.animationSpeed,showNextTweet)}++i}}}function replaceURLWithHTMLLinks(text){var exp=/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i;return text.replace(exp,"<a target='_blank' href='$1'>$1</a>")}function prettyDate(a){var b=new Date();var c=new Date(a);if(navigator.userAgent.match(/MSIE\s([^;]*)/)){c=Date.parse(a.replace(/( \+)/,' UTC$1'))}var d=b-c;var e=1000,minute=e*60,hour=minute*60,day=hour*24,week=day*7;if(isNaN(d)||d<0){return""}if(d<e*7){return"right now"}if(d<minute){return Math.floor(d/e)+" seconds ago"}if(d<minute*2){return"about 1 minute ago"}if(d<hour){return Math.floor(d/minute)+" minutes ago"}if(d<hour*2){return"about 1 hour ago"}if(d<day){return Math.floor(d/hour)+" hours ago"}if(d>day&&d<day*2){return"yesterday"}if(d<day*365){return Math.floor(d/day)+" days ago"}else{return"over a year ago"}}}})})(jQuery);
