/* 	Who-Tweet Button v1.0
Blog : http://www.moretechtips.net
Project: http://code.google.com/p/who-tweet-button/
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.whoTweet=function(allOptions){var defaults={debug:0,url:"",url_querystring:0,n:10,who:1,button:"big",nick:"",inf_only:0,inf_tip:0,tweet_tip:0,stay_time:5000,enter_time:200,exit_time:200,animate:"opacity",topsy_author_url:0};allOptions=$.extend({},defaults,allOptions);return this.each(function(){var div=$(this);var count=0,span=null,vi=-1;var op=allOptions;var effectParams=new Object;var requestedWho=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}var imgs=[];for(var i=0;i<count;i++){imgs[i]=getImg(rs[i])}span.append(imgs.join(""));fadeIn()};var getImg=function(x){if(!x.author.influence_level){x.author.influence_level=0}var t=x.author.name+(op.inf_tip?" [influence: "+x.author.influence_level+"/10]":"")+(op.tweet_tip?": "+x.content.replace('"',"&quot;"):"");t=t.replace(/\"/g,"&quot;").replace(/'/g,"&#39;");if(!x.permalink_url){x.author.photo_url="http://cdn2.topsy.com/asset/master.16412.82c193e596/img/twitter_user.png"}return'<a style="display:none" href="'+(op.topsy_author_url?x.author.topsy_author_url:x.author.url)+'" title="'+t+'"><img src="'+x.author.photo_url+'" height="48" width="48" border="0"/></a>'};var fadeOut=function(){$("a",span).eq(vi).fadeOut(op.exit_time,fadeIn)};var fadeIn=function(){vi++;if(vi>=count){vi=0}$("a",span).eq(vi).animate(effectParams,op.enter_time,"linear",fadeStill)};var fadeStill=function(){span.animate({opacity:1},op.stay_time,"linear",fadeOut)};var requestWho=function(){$.ajax({url:"http://otter.topsy.com/trackbacks.js",data:{url:op.url,infonly:op.inf_only,perpage:op.n},success:requestedWho,dataType:"jsonp"})};var requestedURL=function(json){if(!json.response.topsy_trackback_url){if(op.debug){div.html('<b style="color:red">Error: '+(json.response.errors?json.response.errors.join(","):"unkown")+"</b>")}return}var res=json.response;var topsyURL=res.topsy_trackback_url;var backs=res.trackback_total;var topLevel=res.toplevel?res.toplevel.replace("top","").toUpperCase():"";if(op.button=="small"){div.html("").append('<div class="topsy-sm"><a href="'+topsyURL+'"	class="topsy-sm-total">'+backs+'</a><a href="http://button.topsy.com/retweet?url='+encodeURIComponent(op.url)+(op.nick?"&nick="+op.nick:"")+'" class="topsy-sm-retweet" target="_blank">retweet</a>'+(topLevel?'<a href="'+topsyURL+'" class="topsy-sm-badge">TOP <span class="topsy-sm-lvl">'+topLevel+"</span></a>":"")+"</div>");return}div.html("").append('<div class="topsy-big"><span class="topsy-big-count'+(op.who?"-wtb":"")+'"><a href="'+topsyURL+'" class="topsy-big-total-badge'+(op.who?"-wtb":"")+'"><span class="topsy-big-num">'+backs+"</span>"+(op.who?"":'<span class="topsy-big-unit">tweets</span>')+"</a>"+(op.who?'<span class="wtb"></span>':"")+"</span>"+(topLevel?'<a href="'+topsyURL+'" class="topsy-big-total-badge"><span class="topsy-big-badge">TOP <span class="topsy-big-lvl">'+topLevel+"</span></span></a>":"")+'<a href="http://button.topsy.com/retweet?url='+encodeURIComponent(op.url)+(op.nick?"&nick="+op.nick:"")+'" class="topsy-big-retweet" target="_blank">retweet</a></div>');if(op.who&&backs>0){span=$("span.wtb",div);requestWho()}};var requestURL=function(){$.ajax({url:"http://otter.topsy.com/urlinfo.js",data:{url:op.url},success:requestedURL,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.url){op.url=location.protocol+"//"+location.host+location.pathname+(op.url_querystring?location.search:"")}requestURL()};init()})}})(jQuery);jQuery(document).ready(function(){jQuery("div.who-tweet").whoTweet()});