/*
* twead - JavaScript Twitter widget.
*
* Copyright (c) 2009 skwas (Martijn Bodeman), http://skwas.com/
* 
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Date: 2009-08-02 22:31:00 +0200 (Sun, 02 Aug 2009)
* Revision: 107
*/
(function(){var d=this,e,b=d.twead,f,a=false,c=d.twead=function(g){return new c.fn.init(g)};c.fn=c.prototype={element:null,init:function(g){if(typeof g==="string"){this.element=f(g)}else{if(typeof g==="object"){this.element=f(g.id)}}if(!this.element){throw new Error("Element not found.")}if(this.element.className.indexOf("twead")===-1){this.element.className+=" twead"}this.settings={searchTerm:"#twead",itemCount:10,refreshInterval:10000};var h=this;this.max_id=0;this.cache=new Array();this.callbackFunc="twead_"+this.element.id+"_callback";if(d[this.callbackFunc]){throw new Error("Twead already registered for element "+this.element.id)}d[this.callbackFunc]=function(m){var n=typeof jQuery==="function";h.cache.reverse();var l=m.results;l.reverse();for(var k=0;k<l.length;k++){l[k].isNew=n&&(h.max_id!=0);h.cache.push(l[k])}h.cache.reverse();while(h.cache.length>h.settings.itemCount){h.cache.pop()}h.element.innerHTML=twead_SearchResultCallback(h.cache,h);if(n){$(h.element).find("li:hidden").fadeIn("medium",function(){$(this).css("zoom","")})}h.max_id=m.max_id};if(!a&&this.globalSettings.googleTrackerCode){a=true;var i=false;function j(){i=true;var m=typeof _gat==="object";var q=typeof urchinTracker==="function";if(!m&&!q){var o=(("https:"==document.location.protocol)?"https://ssl.":"http://www.");c.utils.loadScript(o+"google-analytics.com/ga.js",function(){m=typeof _gat==="object";if(m){try{var s=_gat._getTracker(h.globalSettings.googleTrackerCode);s._trackPageview()}catch(r){}}});return}if(m){try{var l=_gat._getTracker(h.globalSettings.googleTrackerCode);l._trackPageview()}catch(k){}}else{if(q){var p=_uacct;_uacct=h.globalSettings.googleTrackerCode;try{urchinTracker()}catch(n){}_uacct=p}}}if(document.addEventListener){document.addEventListener("DOMContentLoaded",function(){document.removeEventListener("DOMContentLoaded",arguments.callee,false);j()},false)}else{if(document.attachEvent){document.attachEvent("onreadystatechange",function(){if(document.readyState==="complete"){document.detachEvent("onreadystatechange",arguments.callee);j()}});if(document.documentElement.doScroll&&d==d.top){(function(){if(i){return}try{document.documentElement.doScroll("left")}catch(k){setTimeout(arguments.callee,0);return}j()})()}}}}return this},isRunning:function(){return this.timerID},stop:function(){if(this.timerID){clearTimeout(this.timerID);this.timerID=null}},refresh:function(i,h){var j=i?i:this;j.stop();if(h){if(h<1000){h=1000}j.timerID=setTimeout(function(){j.refresh(j)},h);return}var g="http://search.twitter.com/search.json?callback="+this.callbackFunc+"&q="+escape(this.settings.searchTerm)+"&rpp="+this.settings.itemCount+"&rand="+Math.floor(Math.random()*10000000);if(j.max_id!==0){g+="&since_id="+j.max_id}c.utils.loadScript(g,function(){j.refresh(j,j.settings.refreshInterval)},true)},globalSettings:{googleTrackerCode:null}};c.fn.init.prototype=c.fn;c.utils=d.twead.utils={loadScript:function(i,l,k){var j=document.getElementsByTagName("head")[0];var h=document.createElement("script");h.src=i;var g=false;h.onload=h.onreadystatechange=function(){if(!g&&(!this.readyState||this.readyState=="loaded"||this.readyState=="complete")){g=true;h.onload=h.onreadystatechange=null;if(k){j.removeChild(h)}l()}};j.appendChild(h)},htmlDecode:function(g){var h=document.createElement("div");h.innerHTML=g;return h.textContent?h.textContent:h.innerText},htmlEncode:function(g){var h=document.createElement("div");if(h.textContent){h.textContent=g}else{h.innerText=g}return h.innerHTML},toRelativeTime:function(h){var g=Date.parse(h);var i=(arguments.length>1)?arguments[1]:new Date();var j=parseInt((i.getTime()-g)/1000);if(j<60){return c.localization.lessThanAMinuteAgo}else{if(j<120){return c.localization.aboutAMinuteAgo}else{if(j<3600){return this.format(c.localization.xMinutesAgo,parseInt(j/60))}else{if(j<7200){return c.localization.aboutAnHourAgo}else{if(j<86400){return this.format(c.localization.aboutXHoursAgo,parseInt(j/3600))}else{if(j<172800){return c.localization.oneDayAgo}else{return this.format(c.localization.xDaysAgo,parseInt(j/86400))}}}}}}},format:function(h){var g=0;var j=arguments.length-1;while(h.indexOf("{"+g+"}")>=0){if(g<j){h=h.replace("{"+g+"}",arguments[g+1])}else{break}g++}return h}};c.localization=d.twead.localization={postedThrough:"from",reply:"Reply",viewTweet:"View Tweet",lessThanAMinuteAgo:"less than a minute ago",aboutAMinuteAgo:"about a minute ago",xMinutesAgo:"{0} minutes ago",aboutAnHourAgo:"about an hour ago",aboutXHoursAgo:"about {0} hours ago",oneDayAgo:"1 day ago",xDaysAgo:"{0} days ago"};if(!f){f=function(g){if(document.all){return document.all[g]}return document.getElementById(g)}}})();function twead_SearchResultCallback(f,e){var a=[];for(var d=0;d<f.length;d++){var c=f[d];var b=c.text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g,function(g){return'<a href="'+g+'" target="_blank">'+g+"</a>"}).replace(/\B@([_a-z0-9]+)/ig,function(g){return'<a href="http://twitter.com/'+g.substring(1)+'" target="_blank">'+g+"</a>"}).replace(/\B#([_a-z0-9]+)/ig,function(g){return'<a href="http://search.twitter.com/search?q='+escape(g)+'" target="_blank">'+g+"</a>"});a.push("<li"+(c.isNew?' style="display: none;" class="twead_new_tweed"':"")+'><a href="http://twitter.com/'+c.from_user+'" target="_blank"><img class="twead_profile_image" src="'+c.profile_image_url+'"></a><div><a href="http://twitter.com/'+c.from_user+'" target="_blank">'+c.from_user+"</a>: "+b+'</div><div class="twead_tweed_details">'+twead.utils.toRelativeTime(c.created_at)+" "+twead.localization.postedThrough+' <span class="twead_source">'+twead.utils.htmlDecode(c.source)+'</span> <a href="http://twitter.com/?status=@'+escape(c.from_user)+"%20&amp;in_reply_to_status_id="+c.id+"&amp;in_reply_to="+escape(c.from_user)+'" target="_blank">'+twead.localization.reply+'</a> <a href="http://twitter.com/'+c.from_user+"/statuses/"+c.id+'" target="_blank">'+twead.localization.viewTweet+"</a></div></li>");c.isNew=false}return a.join("")};
