var timer;

function getSearch(theQuery,limit)
{
	clearTimeout(timer);
	var results = '';
	var teller=0;
	$.get("twitter/getSearch.php", {query: theQuery,limit: limit},  function(xml){
		$('item',xml).each(function(i){
			var title = $(this).find("title").text();
			var published = $(this).find("pubDate").text();
			
			var link = '';
			$(this).find('link').each(function(){
				if($(this).attr('rel')=='image'){
					link = $(this).attr('href');
				}
			})
			newdate = published.replace('T',' ').replace('Z','');
			dagtijd=newdate.split(' ');
			datum=dagtijd[0].split('-');
			teller++;
			if(teller<2){
				results = results + '<div class="tweet">' + " <h3 class=\"tweetDatum\">"+published.replace('+0000','') + "</h3>" + Linkify(title,"_blank") + "</h3></div>";
			}
			//---oude versie hier in commentaar
			//results = results + '<div class="tweet"><img src="'+link+'" align="left"/>' + Linkify(title,"_blank") + " <i>Geplaatst: "+d.getDate()+'-'+d.getMonth()+'-'+d.getFullYear()+' '+d.getHours()+':'+d.getMinutes()+"</i></div>";
		});
		$("#twitter_container").html(results);
	});
	 
	timer = setTimeout('getSearch(\''+theQuery+'\',\''+limit+'\')', 30000);
}
function Linkify(inputText,target) {
    //URLs starting with http://, https://, or ftp://
    var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    var replacedText = inputText.replace(replacePattern1, '<a href="$1" target="'+target+'">$1</a>');

    //URLs starting with www. (without // before it, or it'd re-link the ones done above)
    var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
    var replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="'+target+'">$2</a>');

    //Change email addresses to mailto:: links
    var replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
    var replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

    return replacedText
}
