/*
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 = 100; // 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>
		containerDiv="twittext6", // //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-5", //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;
		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;
			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();
			}   
		},
		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.length>0?"&lang="+lang:jlg=""; 
			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){
				$(".twittLI").each(
					function(o,elemLI){
						if(o>=numMSG) $(this).hide("slow");													  
					}
				);
			}	
		},
		conectaTwitter: function(e){
			// query the twitter api and create the tweets list
			var chcolor = getColorCode();

			var tabarray = Array();
			var tabarray2 = Array();
			for (i = 1; i <= 5; i++) {
				tabname = $("#hashtag_"+i).html();
				if (tabname != "") {
					tabarray.push(tabname);
				} else {
					tabarray.push('null');
				}
				chname = $("#substream_"+i).html();
				if (chname != "") {
					tabarray2.push(chname);
				} else {
					tabarray2.push('null');
				}
			}
			search_str = encodeURI(tabarray.join("+"));

//alert(param);

			$.ajax({
				url: aURL,
				type: 'GET',
				dataType: 'jsonp',
				timeout: 1000,
				error: function(){ $("#"+contDiv).html("fail#"); },
				success: function(json){
					if(e==1) $("#"+contDiv).html("");				
					$.each(json.results,function(i,item) {
						if(e==1 || (i<numMSG && item.id>ultID)){
							if(i==0){
								tultID = item.id;
								//$("<ul></ul>")
								$("<div></div>")
									.attr('id', 'twittList'+ultID)
									.attr('class','twittList')
									.prependTo("#"+contDiv);  
							}
							//if (item.text != "undefined" && $.Juitter.textFormat(item.text) != "") {
							if (item.text != "undefined") {
								var link =  "http://twitter.com/"+item.from_user+"/status/"+item.id;  
								var tweet = $.Juitter.filter(item.text);
								var tweet = $.Juitter.textFormat(item.text);
								var postdate = new Date(item.created_at);
								item.created_at = postdate;
								num = getChannelNum(search_str, tweet);
								color = chcolor[num];
								chname = tabarray2[num - 1];

								mHTML ="<div style='position:relative; width:450px; border:1px #e0e0e0 solid; margin-top:4px; margin-left:4px; margin-bottom:2px; padding:2px; background-color:"+color+"; clear:both;'>";
									mHTML+="<div style='position:absolute; border:1px #e0e0e0 solid; margin-left:2px; margin-bottom:2px; width:40px;'>";
										mHTML+="<a href='http://www.twitter.com/"+item.from_user+"'>";
										mHTML+="<img src='"+item.profile_image_url+"' class='juitterAvatar' width='40' height='40'>";
										mHTML+="</a>";
									mHTML+="</div>";
									mHTML+="<div style='position:relative; margin-left:48px; margin-top:4px; margin-bottom:4px; margin-right:4px; border:1px blue none;'>";
										mHTML+="<font style='font-size:7pt; background-color:white;'>"+chname+"</font><br>";
										mHTML+="<font style='font-size:8pt;'>"+tweet+"</font><br><font style='font-weight:bold; font-size:8pt;'>by "+item.from_user+"</font> <font style='font-size:8pt;'>"+item.created_at+"</font>";
									mHTML+="</div>";
								mHTML+="</div>";
								
								if (num != null) {
									//$("<li></li>") 
									$("<div></div>") 
										.html(mHTML)  
										.attr('id', 'twittLI'+msgNb)
										.attr('class', 'twittLI')
										.appendTo("#twittList"+ultID);

									$('#twittLI'+msgNb).hide();
									$('#twittLI'+msgNb).show("slow");
								}
								
								// remove old entries
								$.Juitter.delRegister();
								msgNb++;								
							}
						}
					});	
					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){

			texto = "<font style='font-size:8pt;'>"+texto+"</font>";
			//make links
			var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
			var exp2 = /[\@]+([A-Za-z0-9-_]+) live at/ig;
			var exp3 = /[\#]+([A-Za-z0-9-_]+) live at/ig;

//			if (texto.match(exp2) != null || texto.match(exp3) != null) {
				texto = texto.replace(exp,"<a href='$1' class='extLink' "+openLink+">$1</a>"); 
				texto = texto.replace(exp2,"<font style='color:orange'>@$1</font> live at"); 
				texto = texto.replace(exp3,"<font style='color:orange'>#$1</font> live at"); 
				// 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);					  
					});
				}
//			} else {
//				texto = "";
//			}
			return texto;

		},
		temporizador: function(){
			// live mode timer
			running=true;
			aTim = timer.split("-");
			if(aTim[0]=="live" && aTim[1].length>0){
				tempo = aTim[1]*1000;
				setTimeout("$.Juitter.update()",tempo);
			}
		}
	};	
})(jQuery);

function unixtimeToDate(ut, TZ) {
	var tD = new Date( ut * 1000 );
	tD.setTime( tD.getTime() + (60*60*1000 * TZ) );
	return tD;
}

function getChannelNum(hashtag, str) {
// (<font style='color:orange'>#HITORIKIKAKU</font> live at <a href='http://ustre.am/jeic' class='extLink' target='_blank'>http://ustre.am/jeic</a> )
	//param = param.replace("&ors=", "");
	//spl = param.split(",");
	spl = hashtag.split("+");
	ret = null;
	for (i = 0; i < 5; i++) {
		chn = spl[i];
		chn = chn.replace("%23", "#");
		chn = chn.replace("%40", "@");
		//if (str.match(chn) != null && str.match("ustre\.am") != null) {
		if (str.match(chn) != null) {
			ret = i+1;
		}
	}
	return ret;
}

