(function($) {
	var conf = {},
	
		aURL="";msgNb=1;
		var mode,param,time,lang,contDiv,loadMSG,gifName,numMSG,fromID,ultID;
		var running=false;

		apifMultipleUSER = "https://search.twitter.com/search.json?from%3A";
		apifUSER = "https://search.twitter.com/search.json?q=from%3A";
		apitMultipleUSER = "https://search.twitter.com/search.json?to%3A";
		apitUSER = "https://search.twitter.com/search.json?q=to%3A";
		apiSEARCH = "https://search.twitter.com/search.json?q=";
	$.tweetWall = {
		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;
			fromID=opt.nameUser?opt.nameUser:nameUser;
		},
		start: function(opt) {		
			ultID=0;
			if($("#"+contDiv)){	
				this.registerVar(opt);
				this.loading();
				aURL = this.createURL();
				this.conectaTwitter(1);		
				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(){
			if(msgNb>=numMSG){
				$(".twittLI").each(
					function(o,elemLI){
						if(o>=numMSG) $(this).hide("slow");													  
					}
				);
			}	
		},
		conectaTwitter: function(e){
			$.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', 'twittList'+ultID)
									.attr('class','twittList')
									.prependTo("#"+contDiv);  
							}
							if (item.text != "undefined") {
								var link  = "https://twitter.com/"+item.from_user+"/status/"+item.id;  
								var tweet = item.text;
								var day   = Date.parse(item.created_at).toString('d');
								var month = Date.parse(item.created_at).toString('MMM');
								var year  = Date.parse(item.created_at).toString('yyyy');
								var date  = Date.parse(item.created_at).toString('d/M/yyyy à HH:mm');

								if(fromID=="image") mHTML="<a href='https://twitter.com/"+item.from_user+"' class='author'><img src='"+item.profile_image_url+"' alt='"+item.from_user+"' width='48px' height='48px' /></a><span class='tweet'>"+$.tweetWall.textFormat(tweet)+" <br/><a href='" + link + "' class='JRM time'>"+item.created_at+"</a></span>";
								else mHTML="<a href='" + link + "' class='time' title='" + date + "'><span class='day'>" + day + "</span> <span class='month'>" + month + "</span> <span class='year'>" + year + "</span></a><span class='tweet'>" + $.tweetWall.textFormat(tweet) + "</span>";

								$("<li></li>") 
									.html(mHTML)  
									.attr('id', 'twittLI'+msgNb)
									.attr('class', 'twittLI')
									.appendTo("#twittList"+ultID);

								$('#twittLI'+msgNb).hide();
								$('#twittLI'+msgNb).slideDown("slow");

								$.tweetWall.delRegister();
								msgNb++;								
							}
						}
					});	
					ultID=tultID;
				}
			});
		},	
		textFormat: function(texto){
			var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
			texto = texto.replace(exp,"<a href='$1'>$1</a>"); 
			var exp = /[\@]+([A-Za-z0-9-_]+)/ig;
			texto = texto.replace(exp,"<a href='https://twitter.com/$1' class='profileLink'>@$1</a>"); 
			return texto;
		},
		temporizador: function(){
			running=true;
			aTim = timer.split("-");
			if(aTim[0]=="live" && aTim[1].length>0){
				tempo = aTim[1]*1000;
				setTimeout("$.tweetWall.update()",tempo);
			}
		}
	};	
})(jQuery);

/* Powered by http://juitter.com - Juitter is distributed under the MIT License */
