// (c)2002 Douglas Crockford

(function($){var conf={},username="akhia",word="akhia"
contDiv="tweetContainer",loadMSG="Loading messages...";gifName="../images/loader.gif";numMSG=4;readMore="more...";fromID="text";apifUSER="http://search.twitter.com/search.json?q=from%3Aakhia";apitUSER="http://search.twitter.com/search.json?q=to%3A";apiSEARCH="http://search.twitter.com/search.json?q=";aURL="";ultID=0;msgNb=1;mode="",param="",time=""
$.Juitter={registerVar:function(localMode,localParam,localTimer){mode=localMode;param=localParam;timer=localTimer;},start:function(localMode,localParam,localTimer){if($("#"+contDiv)){this.registerVar(localMode,localParam,localTimer);this.loading();aURL=this.createURL(mode,param);this.conectaTwitter(1);if(timer!=undefined)this.temporizador();}},update:function(){this.conectaTwitter(2);if(timer!=undefined)this.temporizador();},changeRequest:function(localMode,localParam,localTimer,id){$(".jLinks").removeClass("on");$("#"+id).addClass("on");this.loading();this.registerVar(localMode,localParam,localTimer);aURL=this.createURL(mode,param);this.conectaTwitter(1);},loading:function(){if(loadMSG=="image/gif"){$("<img></img>").attr('src',gifName).appendTo("#"+contDiv);}else{$("#"+contDiv).html(loadMSG);}},createURL:function(mode,param){var url="";if(mode=="fromUser"){if(param!=undefined)username=param;url=apifUSER;}else if(mode=="toUser"){if(param!=undefined)username=param;url=apitUSER+user;}else if(mode=="searchWord"){if(param!=undefined)word=param;url=apiSEARCH+word;}
url+="&rpp="+numMSG;return url;},delRegister:function(){if(msgNb>=numMSG){$(".twittLI").each(function(o,elemLI){if(o>=numMSG){$(this).hide("slow");}});}},conectaTwitter:function(opt){$.ajax({url:aURL,type:'GET',dataType:'jsonp',timeout:1000,error:function(){$("#"+contDiv).html("fail#");},success:function(json){if(opt==1){$("#"+contDiv).html("");$("<ul></ul>").attr('id','twittList').appendTo("#"+contDiv);$.each(json.results,function(i,item){if(i==0)ultID=item.id;if(i<numMSG){if(item.text!="undefined"){var link="http://twitter.com/"+item.from_user;if(fromID=="image"){$("<li></li>").html($.Juitter.textFormat(item.text)+" <a href='"+link+"' target='_blank'>"+readMore+"</a><hr />").attr('id','twittLI'+msgNb).attr('class','twittLI').appendTo("#twittList");}else{$("<li></li>").html("<a href='http://www.twitter.com/"+item.from_user+"'  target='_blank'>@"+item.from_user+":</a> "+$.Juitter.textFormat(item.text)+" <a href='"+link+"'  target='_blank'>"+readMore+"</a>").attr('id','twittLI'+msgNb).attr('class','twittLI').appendTo("#twittList");}
msgNb++;}}});}else{$.each(json.results,function(i,item){if(i<numMSG&&item.id>ultID){if(i==0)ultID=item.id;if(item.text!="undefined"){var link="http://twitter.com/"+item.from_user+"/status/"+item.id;if(fromID=="image"){$("<li></li>").html("<a href='http://www.twitter.com/"+item.from_user+"'  target='_blank'><img src='"+item.profile_image_url+"' alt='"+item.from_user+"' class='juitterAvatar' /></a> "+$.Juitter.textFormat(item.text)+" <a href='"+link+"'  target='_blank'>"+readMore+"</a>").attr('id','twittLI'+msgNb).attr('class','twittLI').prependTo("#twittList");$('#twittLI'+msgNb).hide();$('#twittLI'+msgNb).show("slow");}else{$("<li></li>").html("<a href='http://www.twitter.com/"+item.from_user+"'  target='_blank'>@"+item.from_user+":</a> "+$.Juitter.textFormat(item.text)+" <a href='"+link+"'  target='_blank'>"+readMore+"</a>").attr('id','twittLI'+msgNb).attr('class','twittLI').prependTo("#twittList");$('#twittLI'+msgNb).hide();$('#twittLI'+msgNb).show("slow");}
$.Juitter.delRegister();msgNb++;}}});}}});},textFormat:function(texto){var exp=/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;texto=texto.replace(exp,"<a href='$1' class='extLink' target='_blank'>$1</a>");var exp=/[\@]+([A-Za-z0-9-_]+)/ig;texto=texto.replace(exp,"<a href='http://twitter.com/$1' class='profileLink'>@$1</a>");var exp=/[\#]+([A-Za-z0-9-_]+)/ig;texto=texto.replace(exp,"<a href='http://juitter.com/#$1' onclick='$.Juitter.changeRequest(\"searchWord\",\"$1\");return false;' class='hashLink'>#$1</a>");if(mode=="searchWord"){regExp=eval('/ '+param+'{1}/ig');newString=new String(' <b>'+param+'</b> ');texto=texto.replace(regExp,newString);}
return texto;},temporizador:function(){aTim=timer.split("-");if(aTim[0]=="live"&&aTim[1].length>0){tempo=aTim[1]*180000;setTimeout("$.Juitter.update()",tempo);}}};})(jQuery);
