/* 	Popular-On-Twitter Widget v1.0
	Blog : http://www.moretechtips.net
	Project: http://code.google.com/p/popular-on-twitter/
	Copyright 2009 [Mike @ moretechtips.net]
	Licensed under the Apache License, Version 2.0 
	(the "License"); you may not use this file except in compliance with the License. 
	You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
*/
(function($){$.fn.popularOnTwitter=function(allOptions){var defaults={debug:0,window:"",site:"",from:"",keywords:"",n:10,show_n:5,show_tweet:1,tweets_text:"_d_ tweets",score_text:"_d_ score",tweeter_text:"by _d_",stay_time:5000,enter_time:200,exit_time:200,animate:"opacity",q:""};allOptions=$.extend({},defaults,allOptions);return this.each(function(){var div=$(this);var count=0,ul=null,vp=-1,startI=-1,endI=-1;var op=allOptions;var effectParams=new Object;var requested=function(json){if(!json.response.list){if(op.debug){div.html('<b style="color:red">Error: '+(json.response.errors?json.response.errors.join(","):"unkown")+"</b>")}return}var rs=json.response.list;count=rs.length;if(count==0){return}ul=$('<ul class="pot"></ul>').appendTo(div.html(""));for(var i=0;i<count;i++){addLI(rs[i])}if(op.show_n>0){fadeIn()}};var addLI=function(x){var uname=x.trackback_permalink.replace(/http:\/\/twitter\.com\/([^\/]+)\/status\/\d+/,"$1");var li=$("<li"+(op.show_n>0?' style="display:none"':"")+'><span class="pot-url"><a href="'+x.url+'">'+x.title+"</a></span>"+(op.show_tweet?'<span class="pot-tweet">'+x.content+"</span>":"")+'<span class="pot-meta">'+(op.tweeter_text?' <span class="pot-tweeter">'+op.tweeter_text.replace("_d_",'<a href="'+x.trackback_permalink+'">'+uname+"</a>")+"</span>":"")+(op.tweets_text?' <span class="pot-tweets"><a href="'+x.topsy_trackback_url+'">'+op.tweets_text.replace("_d_",x.trackback_total)+"</a></span>":"")+(op.score_text?' <span class="pot-score">'+op.score_text.replace("_d_",Math.round(x.score*100)/100)+"</span>":"")+"</span></li>").appendTo(ul);if(op.show_tweet){var tweet=$("span.pot-tweet",li);tweet.html(linkify(tweet.html()))}};var fadeOut=function(){$("li",ul).eq(startI).fadeOut(op.exit_time,fadeIn);if(op.show_n>1){$("li",ul).slice(startI+1,endI).fadeOut(op.exit_time)}};var fadeIn=function(){vp++;if(vp*op.show_n>=count){vp=0}startI=vp*op.show_n;endI=(vp+1)*op.show_n;$("li",ul).eq(startI).animate(effectParams,op.enter_time,"linear",fadeStill);if(op.show_n>1){$("li",ul).slice(startI+1,endI).animate(effectParams,op.enter_time,"linear")}};var fadeStill=function(){ul.animate({opacity:1},op.stay_time,"linear",fadeOut)};var linkify=function(d){return d.replace(/\B\@([A-Z0-9_]{1,15})/gi,'@<a class="pot-at" href="http://twitter.com/$1">$1</a>').replace(/\B\#([A-Z0-9_]+)/gi,'<a class="pot-hashtag" href="http://search.twitter.com/search?q=%23$1">#$1</a>')};var request=function(){var data={q:op.q,window:op.window,perpage:op.n};$.ajax({url:"http://otter.topsy.com/search.js",data:data,success:requested,dataType:"jsonp"})};var init=function(){if(div.attr("options")){try{op=eval("("+div.attr("options")+")")}catch(e){div.html('<b style="color:red">'+e+"</b>");return}op=$.extend({},defaults,op)}effectParams[op.animate]="show";if(!op.q){if(op.site){op.q="site:"+op.site}if(op.from){op.q=(op.q?op.q+" ":"")+"from:"+op.from}if(op.keywords){op.q=(op.q?op.q+" ":"")+op.keywords}}request()};init()})}})(jQuery);jQuery(document).ready(function(){jQuery("div.popular-on-twitter").popularOnTwitter()});