/*
JUITTER 1.0.0 - 22/07/2009 - http://juitter.com
BY RODRIGO FANTE - http://rodrigofante.com

** jQuery 1.2.* or higher required

Juitter is distributed under the MIT License
Read more about the MIT License --> http://www.opensource.org/licenses/mit-license.php

This script is just a beta test version, download and use it at your own risk.
The Juitter developer shall have no responsability for data loss or damage of any kind by using this script.
*/
(function($) {
	var conf = {},
		// JUITTER DEFAULT CONFIGURATION ========================
		// YOU CAN CHANGE THE DYNAMIC VARS ON CALLING THE start method, see the system.js for more information about it.

		numMSG = 30; // set the number of messages to be show
		containerDiv="juitterContainer", // //Set a place holder DIV which will receive the list of tweets example <div id="juitterContainer"></div>
		loadMSG="Loading messages...", // Loading message, if you want to show an image, fill it with "image/gif" and go to the next variable to set which image you want to use on 
		imgName="loader.gif", // Loading image, to enable it, go to the loadMSG var above and change it to "image/gif"
		readMore="Read it on Twitter", // read more message to be show after the tweet content
		nameUser="image" // insert "image" to show avatar of "text" to show the name of the user that sent the tweet 
		live:"live-20", //optional, disabled by default, the number after "live-" indicates the time in seconds to wait before request the Twitter API for updates, I do not recommend to use less than 60 seconds.
		// end of configuration
	
		// some global vars
		aURL="";msgNb=1;
		var mode,param,time,lang,contDiv,loadMSG,gifName,numMSG,readMore,fromID,ultID,filterWords,curTimer;
		var running=false;
		// Twitter API Urls
		apifMultipleUSER = "http://search.twitter.com/search.json?from%3A";
		apifUSER = "http://search.twitter.com/search.json?q=from%3A";
		apitMultipleUSER = "http://search.twitter.com/search.json?to%3A";
		apitUSER = "http://search.twitter.com/search.json?q=to%3A";
		apiSEARCH = "http://search.twitter.com/search.json?q=";
	$.Juitter = {
		registerVar: function(opt){
			mode=opt.searchType;
			param=opt.searchObject;
			timer=opt.live;
			lang=opt.lang?opt.lang:"";
			contDiv=opt.placeHolder?opt.placeHolder:containerDiv;
			loadMSG=opt.loadMSG?opt.loadMSG:loadMSG;
			gifName=opt.imgName?opt.imgName:imgName;
			numMSG=opt.total?opt.total:numMSG;
			readMore=opt.readMore?opt.readMore:readMore;
			fromID=opt.nameUser?opt.nameUser:nameUser;
			filterWords=opt.filter;
			openLink=opt.openExternalLinks?"target='_blank'":"";
		},
		start: function(opt) {		
			ultID=0;
			msgNb=0;
			if($("#"+contDiv)){	
				this.registerVar(opt);
				// show the load message
				this.loading();
				// create the URL  to be request at the Twitter API
				aURL = this.createURL();
				// query the twitter API and create the tweets list
				this.conectaTwitter(1);		
				// if live mode is enabled, schedule the next twitter API query
				if(timer!=undefined&&!running) this.temporizador();
			}   
		},
		stop: function() {
			if (curTimer!=undefined)
				clearTimeout(curTimer);
			running=false;
		},
		update: function(){
			this.conectaTwitter(2);		
			if(timer!=undefined) this.temporizador();
		},
		loading: function(){
			if(loadMSG=="image/gif"){
				$("<img></img>")
					.attr('src', gifName)
					.appendTo("#"+contDiv); 
			} else $("#"+contDiv).html(loadMSG);
		},
		createURL: function(){
			var url = "";
			jlg="&lang=en"; 
			var seachMult = param.search(/,/);
			if(seachMult>0) param = "&ors="+param.replace(/,/g,"+");
			if(mode=="fromUser" && seachMult<=0) url=apifUSER+param;
			else if(mode=="fromUser" && seachMult>=0) url=apifMultipleUSER+param;
			else if(mode=="toUser" && seachMult<=0) url=apitUSER+param;
			else if(mode=="toUser" && seachMult>=0) url=apitMultipleUSER+param;
			else if(mode=="searchWord") url=apiSEARCH+param+jlg;
			url += "&rpp="+numMSG;		
			return url;
		},
		delRegister: function(){
			// remove the oldest entry on the tweets list
			if(msgNb>=numMSG){
				$(".twittTD").each(
					function(o,elemLI){
						if(o>=numMSG)  {
							// Altered this to just remove the element to prevent memory overflow and gradual speed degradation
							$(this).hide(1, function() { $(this).remove(); } );													  
						}
					}
				);
			}	
		}, 
		conectaTwitter: function(e){
			// query the twitter api and create the tweets list
			$.ajax({
				cache: false,
				async: true,
				url: aURL,
				type: 'GET',
				dataType: 'jsonp',
				timeout: 1000,
				error: function(){ $("#"+contDiv).html("fail#"); },
				success: function(json){
					if(e==1) 
						$("#"+contDiv).html("");				
					
					//var htmlContent = "";
					
					$.each(json.results,function(i,item) {
						if(e==1 || (i<numMSG && item.id>ultID)){
							if(i==0){
								tultID = item.id;
								//htmlContent += "<ul id='twittList"+ultID + "' class='twittList'>";
								$("<table></table>")
									.attr('id', 'twittList'+ultID)
									.attr('width','100%')
									.prependTo("#"+contDiv);  
								
							}
							if (item.text != "undefined") {
								var link =  "http://twitter.com/"+item.from_user+"/status/"+item.id;  
								
								var tweet = $.Juitter.filter(item.text);
								
								
								/*
								Post Template
								<table width="100%" border="0" cellspacing="5" cellpadding="0">
									<tr>
										<td width="8%" rowspan="2" valign="top">
											<table width="100%" border="0" cellspacing="3" cellpadding="0">
												<tr>
													<td><img src="images/timeline/user.png" width="48" height="48" class="blueborrder" /></td>
												</tr>
												<tr>
													<td bgcolor="#66B8D6" class="whitelink11"><div align="center">twitter</div></td>
												</tr>
											</table>
										</td>
										<td width="92%" colspan="2" class="greytextreal12">
											<p class="bluelink12">ELSUPERBOB<br /></p>
											<p>Duis sit amet elit leo, in vehicula ipsum. Sed posuere leo dui, et placerat orci. Donec cursus semper fringilla<br /><br />
												<span class="greytext12">Thu, 18 Feb 2010 23:23:36 +0000</span><br />
											</p>
										</td>
									</tr>
									<tr>
										<td class="greytextreal12"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('readon','','images/timeline/readon2.png',1)"><img src="images/timeline/readon.png" name="readon" width="100" height="15" border="0" id="readon" /></a></td>
										<td class="greytextreal12"><div align="right"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('retweet','','images/timeline/retweet2.png',1)"><img src="images/timeline/retweet1.png" name="retweet" width="72" height="15" border="0" id="retweet" /></a><img src="images/spacer.gif" width="1" height="2" /><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('plus','','images/timeline/plus2.png',1)"><img src="images/timeline/plus1.png" name="plus" width="15" height="15" border="0" id="plus" /></a><img src="images/spacer.gif" width="1" height="2" /><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('minus','','images/timeline/minus11.png',1)"><img src="images/timeline/minus1.png" name="minus" width="15" height="15" border="0" id="minus" /></a></div></td>
									</tr>
								</table>
								*/
								
								if(fromID=="image")  {
									mHTML = "<table width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td height=\"5\" colspan=\"3\"><img src=\"images/spacer.gif\" width=\"2\" height=\"5\" /></td></tr><tr><td width=\"6\" height=\"6\"><img src=\"images/wrtopleft.png\" width=\"6\" height=\"6\" /></td><td height=\"6\" bgcolor=\"#FFFFFF\"><img src=\"images/spacer.gif\" width=\"2\" height=\"6\" /></td><td width=\"6\" height=\"6\"><img src=\"images/wrtopright.png\" width=\"6\" height=\"6\" /></td></tr><tr><td bgcolor=\"#FFFFFF\">&nbsp;</td><td bgcolor=\"#FFFFFF\"><table width=\"100%\" border=\"0\" cellspacing=\"5\" cellpadding=\"0\"><tr><td width=\"8%\" rowspan=\"2\" valign=\"top\"><table width=\"100%\" border=\"0\" cellspacing=\"3\" cellpadding=\"0\"><tr><td><img src=\"" +item.profile_image_url+"\" width=\"48\" height=\"48\" class=\"blueborrder\" /></td></tr><tr><td bgcolor=\"#66B8D6\" class=\"whitelink11\"><div align=\"center\">twitter</div></td></tr></table></td><td width=\"92%\" colspan=\"2\" class=\"greytextreal12\"><p class=\"bluelink12\">" + item.from_user + "<br /></p><p>" + $.Juitter.textFormat(tweet) + "<br /><br /><span class=\"greytext12\">" + item.created_at + "</span><br /></p></td></tr><tr><td class=\"greytextreal12\"><a href=\"http://www.twitter.com/"+item.from_user+"\"><img src=\"images/timeline/readon.png\" name=\"readon\" width=\"100\" height=\"15\" border=\"0\" id=\"readon\" onmouseover=\"this.src='images/timeline/readon2.png'\" onmouseout=\"this.src='images/timeline/readon.png'\" /></a></td><td class=\"greytextreal12\"><!--<div align=\"right\"><a href=\"#\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('retweet','','images/timeline/retweet2.png',1)\"><img src=\"images/timeline/retweet1.png\" name=\"retweet\" width=\"72\" height=\"15\" border=\"0\" id=\"retweet\" /></a><img src=\"images/spacer.gif\" width=\"1\" height=\"2\" /><a href=\"#\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('plus','','images/timeline/plus2.png',1)\"><img src=\"images/timeline/plus1.png\" name=\"plus\" width=\"15\" height=\"15\" border=\"0\" id=\"plus\" /></a><img src=\"images/spacer.gif\" width=\"1\" height=\"2\" /><a href=\"#\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('minus','','images/timeline/minus11.png',1)\"><img src=\"images/timeline/minus1.png\" name=\"minus\" width=\"15\" height=\"15\" border=\"0\" id=\"minus\" /></a></div>--></td></tr></table></td><td bgcolor=\"#FFFFFF\">&nbsp;</td></tr><tr><td height=\"6\"><img src=\"images/wrbotleft.png\" width=\"6\" height=\"6\" /></td><td height=\"6\" bgcolor=\"#FFFFFF\"><img src=\"images/spacer.gif\" width=\"2\" height=\"6\" /></td><td height=\"6\"><img src=\"images/wrbotright.png\" width=\"6\" height=\"6\" /></td></tr><tr><td height=\"3\" colspan=\"5\" style=\"border-bottom:1px #ccc solid;\"><img src=\"images/spacer.gif\" width=\"2\" height=\"5\" /></td></tr></table>";
								//mHTML="(IMG)<a href='http://www.twitter.com/"+item.from_user+"'><img src='"+item.profile_image_url+"' alt='"+item.from_user+"' class='juitterAvatar' /></a> "+$.Juitter.textFormat(tweet)+" -| <span class='time'>"+item.created_at+"</span> |- <a href='" + link + "' class='JRM' "+openLink+">"+readMore+"</a>";
							} else { mHTML="(NO)<a href='http://www.twitter.com/"+item.from_user+"'>@"+item.from_user+":</a> "+$.Juitter.textFormat(tweet)+" -| <span class='time'>"+item.created_at+"</span> |-  <a href='" + link + "' "+openLink+">"+readMore+"</a>"; }
								
								//htmlContent += "<li class='twittLI'>" + mHTML + "</li>";
								$("<tr></tr>")
									.attr('id','twttTR' + msgNb)
									.prependTo('#twittList'+ultID);
								
								$("<td></td>") 
									.html(mHTML)  
									.attr('id', 'twittTD'+msgNb)
									.attr('class','twittTD')
									.appendTo("#twttTR"+msgNb);

								$('#twittTD'+msgNb).hide();
								$('#twittTD'+msgNb).show("fast");
								
								
								// remove old entries
								$.Juitter.delRegister();
								msgNb++;								
							}
						}
					});	
					//htmlContent += "</ul>";
					//$("#"+contDiv).html(htmlContent);

					ultID=tultID;
				}
			});
		},	
		filter: function(s){
			if(filterWords){
				searchWords = filterWords.split(",");				
				if(searchWords.length>0){
					cleanHTML=s;
					$.each(searchWords,function(i,item){	
						sW = item.split("->").length>0 ? item.split("->")[0] : item;
						rW = item.split("->").length>0 ? item.split("->")[1] : "";					
						regExp=eval('/'+sW+'/gi');					
						cleanHTML = cleanHTML.replace(regExp, rW);							
					});
				} else cleanHTML = s;			
				return cleanHTML;
			} else return s;
		},
		textFormat: function(texto){
			//make links
			var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
			texto = texto.replace(exp,"<a href='$1' class='bluelink12' "+openLink+">$1</a>"); 
			var exp = /[\@]+([A-Za-z0-9-_]+)/ig;
			texto = texto.replace(exp,"<a href='http://twitter.com/$1' class='bluelink12'>@$1</a>"); 
			var exp = /[\#]+([A-Za-z0-9-_]+)/ig;
			texto = texto.replace(exp,"<a href='http://juitter.com/#$1' onclick='$.Juitter.start({searchType:\"searchWord\",searchObject:\"$1\"});return false;' class='bluelink12'>#$1</a>"); 
			// make it bold
			if(mode=="searchWord"){
				tempParam = param.replace(/&ors=/,"");
				arrParam = tempParam.split("+");
				$.each(arrParam,function(i,item){					
					regExp=eval('/'+item+'/gi');
					newString = new String(' <b>'+item+'</b> ');
					texto = texto.replace(regExp, newString);					  
				});				
			}
			return texto;
		},
		temporizador: function(){
			// live mode timer
			running=true;
			aTim = timer.split("-");
			if(aTim[0]=="live" && aTim[1].length>0){
				tempo = aTim[1]*1000;
				curTimer = setTimeout("$.Juitter.update()",tempo);
			}
		}
	};	
})(jQuery);