(function($) {
$(document).ready(function(){
	var conf = {},

		numMSG = 20; // 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;
		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
			$.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>")
									.attr('id', 'ibm-twitter-list')
									.attr('class','ibm-link-list')
									.prependTo("#"+contDiv);  
							}
							if (item.text != "undefined") {
								var link =  "http://twitter.com/"+item.from_user;  
								
								var tweet = $.Juitter.filter(item.text);
								/*pattern = /(\+0000)/;
                date = item.created_at.replace(pattern,'');*/
                var tim = new Date(item.created_at);
                date = tim.toString().substring(0, 24); 
								//data =  relative_time(date);
								if(fromID=="image") mHTML="<a class='ibm-chat-link' 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="<a class='ibm-chat-link' href='http://www.twitter.com/"+item.from_user+"'>@"+item.from_user+":</a> "+ "<span class='ibm-tweet-body'>"+ $.Juitter.textFormat(tweet)+"</span> <p class='ibm-item-note'>"+ date +" - <a href='" + link + "' "+openLink+">"+readMore+"</a></p>";
								
								$("<li></li>") 
									.html(mHTML)  
									.attr('id', 'twittLI'+msgNb)
									.attr('class', 'twittLI pd')
									.attr('style', 'padding: 5px 5px 5px 21px;')
									.appendTo("#ibm-twitter-list");

								$('#twittLI'+msgNb).hide();
								$('#twittLI'+msgNb).show("slow");
								
								// remove old entries
								$.Juitter.delRegister();
								msgNb++;								
							}
						}
					});	
					ultID=tultID;
						$("#ibm-twitter-list li:first").addClass("ibm-first");
						$('.twittLI a').css('display','inline');
						$('.twittLI a').css('padding','0 0 0 0');
						$('#ibm-content-sidebar ul.ibm-link-list').css('font-size','0.7em');
						$('#ibm-content-sidebar ul.ibm-link-list a').css('font-size','1em');
						$('#ibm-content-sidebar .ibm-container p.ibm-item-note').css('font-size','0.9em');
						$('.twittLI p.ibm-item-note').css('font-size','0.9em');
						$('.ibm-chat-link').css('padding','3px 5px 3px 21px');   
						$('.ibm-chat-link').css('margin-left','-21px');
						$('#ibm-twitter-list li').css('padding-left','3px');
						$('.custom').css('display','inline');
						$('.custom').css('margin','4px 4px 0px 4px');
						
				}
			});
		},	
		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='extLink' "+openLink+">$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.start({searchType:\"searchWord\",searchObject:\"$1\"});return false;' class='hashLink'>#$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(''+item+'');
					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;
				setTimeout("$.Juitter.update()",tempo);
			}
		}
	};
});
})(jQuery);

//12/8/2010 psampaio@br.ibm.com DOES NOT WORK, NEED 2 FIX

/*        
function relative_time(J) {
  var tim = new Date(J);
  date = tim.toString().substring(0, 24); 
  var G = {
    minute_less: "less than a minute ago",
    minute: "about a minute ago",
    minutes_before: "",
    minutes_after: " minutes ago",
    hour: "about an hour ago",
    hours_before: "about ",
    hours_after: " hours ago",
    day: "1 day ago",
    days_before: "",
    days_after: " days ago"
  }

  var K = date.split(" ");
  J = K[1] + " " + K[2] + ", " + K[5] + " " + K[3];
  var F = Date.parse(date);
  var I = (arguments.length > 1) ? arguments[1] : new Date();
  var H = parseInt((I.getTime() - F) / 1000);
  H = H + (I.getTimezoneOffset() * 60);
  
  if (H < 60) {
    return G.minute_less;
  }
  else {
    if (H < 120) {
      return G.minute;
    }
    else {
      if (H < (60 * 60)) {
        return G.minutes_before + (parseInt(H / 60)).toString() + G.minutes_after;
      }
      else {
        if (H < (120 * 60)) {
          return G.hour;
        }
        else {
          if (H < (24 * 60 * 60)) {
            
            return G.hours_before + (parseInt(H / 3600)).toString() + G.hours_after;
          }
          else {
            if (H < (48 * 60 * 60)) {
              return G.day;
            }
            else {
              return G.days_before + (parseInt(H / 86400)).toString() + G.days_after;
            }
          }
        }
      }
    }
  }
}
*/
