var TB_version="2.0.3",TB_ws,TB_config,TB_minTweetID,TB_maxTweetID,TB_tweetsShown=0,TB_rateLimitData,TB_mode="widget";function TB_init(){TB_ws=TB_pluginPath+"/ws.php";TB_showLoader();if(jQuery("#tweetblender").hasClass("archive")){TB_mode="archive"}jQuery.ajax({url:TB_ws+"?action=get_config",dataType:"json",success:function(json){if(typeof(json.general_source_ids)=="undefined"){TB_showMessage("Twitter sources to blend are not defined",true);jQuery("#refreshlink").remove()}else{TB_config=json;TB_initInfoBox();TB_blend()}},error:function(XMLHttpRequest,textStatus,errorThrown){try{TB_config=eval("("+XMLHttpRequest.responseText+")");if(typeof(TB_config)!="undefined"){if(typeof(TB_config.general_source_ids)=="undefined"){TB_showMessage("Twitter sources to blend are not defined",true);jQuery("#refreshlink").remove()}else{TB_initInfoBox();TB_blend()}}else{TB_showMessage("Cannot retrieve Tweet Blender configuration options",true);jQuery("#refreshlink").remove()}}catch(e){TB_showMessage("Cannot retrieve Tweet Blender configuration options",true);jQuery("#refreshlink").remove()}}})}function TB_initInfoBox(){TB_config.sourcesHTML="";TB_config.sourcesCount=0;jQuery.each(TB_config.general_source_ids.split(","),function(a,b){TB_config.sourcesHTML+='<a target="_blank" href="';if(b.charAt(0)=="#"){TB_config.sourcesHTML+="http://search.twitter.com/search?q="+escape(b)}else{TB_config.sourcesHTML+="http://twitter.com/"+b}TB_config.sourcesHTML+='">'+b+"</a> ";TB_config.sourcesCount++});jQuery("#twitterlogo").click(function(){TB_showMessage("Powered by Tweet Blender plugin v"+TB_version+" blending "+TB_config.sourcesHTML,false)})}function TB_blend(){if(typeof(TB_config)!="undefined"){TB_config.sourcesDone=0;jQuery.each(TB_config.general_source_ids.split(","),function(a,b){TB_getTweets(b)})}}function TB_checkComplete(){if(TB_config.sourcesDone==TB_config.sourcesCount){if(jQuery("#tweetlist").children("li").size()==0){TB_showMessage("No tweets found for "+TB_config.sourcesHTML,true)}}}function TB_getTweets(a){jQuery.ajax({url:"http://twitter.com/account/rate_limit_status.json",dataType:"jsonp",success:function(b){if(b.remaining_hits==0){TB_getCachedTweets(a)}else{TB_getFreshTweets(a)}},error:function(){TB_getCachedTweets(a)}})}function TB_getCachedTweets(a){jQuery.ajax({url:TB_pluginPath+"/cache/"+escape(a)+".json",dataType:"json",success:TB_addTweets,error:function(){TB_showMessage("Can't get tweets for "+a+". You've reached your maximum Twitter connection limit and cache is not available.",false)}})}function TB_getFreshTweets(a){if(a.charAt(0)=="#"){jQuery.ajax({dataType:"jsonp",url:"http://search.twitter.com/search.json",data:({q:a,rpp:TB_config.archive_tweets_num,page:1}),success:function(b){TB_cacheData(a,b);TB_addTweets(b)},error:function(){TB_getCachedTweets(a)}})}else{jQuery.ajax({url:"http://twitter.com/statuses/user_timeline.json",dataType:"jsonp",data:({screen_name:a,count:TB_config.archive_tweets_num}),success:function(b){TB_cacheData(a,b);TB_addTweets(b)},error:function(){TB_getCachedTweets(a)}})}}function TB_cacheData(b,a){jQuery.ajax({url:TB_ws+"?action=cache_data",type:"POST",dataType:"json",data:({source:b,data:jQuery.toJSON(a)})})}function TB_addTweets(a){TB_showTweetList();var b=a;if(typeof(a.results)!="undefined"){b=a.results}jQuery.each(b,function(d,e){if(jQuery("#"+e.id).length>0){return true}else{if(TB_tweetsShown==0){TB_minTweetID=e.id;TB_maxTweetID=e.id;jQuery("#tweetlist").append(TB_makeHTML(e));TB_tweetsShown++}else{if(TB_minTweetID>0&&e.id<TB_minTweetID){if(TB_tweetsShown==TB_config[TB_mode+"_tweets_num"]){return false}else{jQuery("#tweetlist").append(TB_makeHTML(e));TB_tweetsShown++;TB_minTweetID=e.id}}else{if(TB_maxTweetID>0&&e.id>TB_maxTweetID){TB_enforceLimit();jQuery("#tweetlist").prepend(TB_makeHTML(e));TB_tweetsShown++;TB_maxTweetID=e.id}else{TB_enforceLimit();var c=TB_maxTweetID;jQuery("#tweetlist li").each(function(f,g){if(e.id<c&&e.id>g.id){jQuery("#"+c).after(TB_makeHTML(e));TB_tweetsShown++;return false}c=g.id})}}}}});if(b.length>0&&TB_mode!="archive"){jQuery("#archivelink").show()}TB_config.sourcesDone++;TB_checkComplete()}function TB_enforceLimit(){if(TB_tweetsShown==TB_config[TB_mode+"_tweets_num"]){var a=jQuery("#"+TB_minTweetID),b=a.prev("li");a.remove();TB_tweetsShown--;TB_minTweetID=parseInt(b.attr("id"))}}function TB_makeHTML(c){var b="",a=new Date();if(typeof(c.user)=="undefined"&&c.from_user){c.user={screen_name:c.from_user}}b+='<li id="'+c.id+'">';if(TB_config[TB_mode+"_show_photos"]){if(!c.user.profile_image_url&&c.profile_image_url){c.user.profile_image_url=c.profile_image_url}b+='<a class="tb_photo" href="http://twitter.com/'+c.user.screen_name+'" target="_blank">';b+='<img src="'+c.user.profile_image_url+'" alt="'+c.user.screen_name+'"/>';b+="</a>"}b+='<span class="tb_author"><a href="http://twitter.com/'+c.user.screen_name+'" target="_blank">'+c.user.screen_name+"</a>: </span> ";if(TB_config.general_link_urls){c.text=c.text.replace(/(https?:\/\/\S+)/gi,'<a href="$1" target="_blank">$1</a>')}if(TB_config.general_link_screen_names){c.text=c.text.replace(/\@([^\s\.\:]+)/gi,'<a href="http://twitter.com/$1" target="_blank">@$1</a>')}if(TB_config.general_link_hash_tags){c.text=c.text.replace(/\#([^\s\,]+)/gi,'<a href="http://search.twitter.com/search?q=%23$1" target="_blank">#$1</a>')}b+='<span class="tb_msg">'+c.text+"</span><br/>";b+=' <span class="tb_tweetinfo">';b+='<a href="http://twitter.com/'+c.user.screen_name+"/statuses/"+c.id+'">';a.setTime(Date.parse(c.created_at));if(TB_config.general_timestamp_format){b+=jQuery.PHPDate(TB_config.general_timestamp_format,a)}else{b+=jQuery.timeago(a)}b+="</a>";if(TB_config[TB_mode+"_show_source"]&&c.source){b+=" from "+jQuery("<textarea/>").html(c.source).val()}b+="</span>";b+="</li>\n";return b}function TB_showLoader(){jQuery("#refreshlink img").attr("src",TB_pluginPath+"/img/ajax-refresh.gif");jQuery("#refreshlink").addClass("loading")}function TB_hideLoader(){jQuery("#tb_loading").hide();jQuery("#refreshlink img").attr("src",TB_pluginPath+"/img/ajax-refresh-icon.gif");jQuery("#refreshlink").removeClass("loading")}function TB_showTweetList(){TB_hideLoader();jQuery("#tweetlist").show()}function TB_showMessage(b,a){TB_hideLoader();jQuery("#tweetlist").before('<div class="tb_msg">'+b+"</div>");jQuery("div.tb_msg").slideDown();if(!a){setTimeout("TB_hideMessages()",15000)}}function TB_hideMessages(){jQuery("div.tb_msg").slideUp()}jQuery(document).ready(function(){TB_init()});