jQuery.fn.reverse = Array.prototype.reverse;
String.prototype.linkify = function() {
	return this.replace(
			/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g, function(
					m) {
				return m.link(m);
			});
};
String.prototype.linkuser = function() {
	return this.replace(/[@]+[A-Za-z0-9-_]+/g, function(u) {
		var username = u.replace("@", "")
		return u.link("http://twitter.com/" + username);
	});
};
String.prototype.linktag = function() {
	return this.replace(/[#]+[A-Za-z0-9-_]+/, function(t) {
		var tag = t.replace("#", "%23")
		return t.link("http://search.twitter.com/search?q=" + tag);
	});
};

function mo() 
{
	me = this;
	monitter = {};
	max = 1;
	startelem = null;
	
	this.start = function(elem,starttwitts,refreshtime)
	{
		monitter['last_id'] = 0;
		monitter['limit'] = 1500;
		elem = $(elem);
		me.fetch_tweets(elem,starttwitts,refreshtime);
		
	}
	
	this.fetch_tweets = function(elem,startmax,timer) {
		
		if (max==1)
		{
			max = startmax;
		}
		
		input = elem.attr('title');
		lang = elem.attr('lang');
		
		if (input != monitter['text']) {
			monitter['last_id'] = 0;
			monitter['text'] = input;
			monitter['count'] = 12;
		}

		if (monitter['count'] > 11 ) {
			
			monitter['count'] = 0;
			elem.prepend('<div class="tweet"><img src="http://monitter.com/widget/favicon.gif" align="absmiddle" />real time twitter by: <a href="http://monitter.com" target="_blank">monitter.com</a></div>');
		}
		
		if (monitter['last_id'] <= 0)
		{
			url = "http://search.twitter.com/search.json?q=" + input + "&lang="	+ lang + "&rpp="+ max +"&callback=?";
			//url = "http://search.twitter.com/search.json?q=" + input + "&callback=?";
		}
		else
		{
			url = "http://search.twitter.com/search.json?q=" + input + "&lang="	+ lang + "&since_id="+ monitter['last_id'] +"&rpp=1&page=1&callback=?";
		}
		
		$.getJSON(
						url,
						function(json) {
							$('div.tweet:gt(' + monitter['limit'] + ')',elem).each(function() {	$(this).fadeOut('slow')	});
							$(json.results)
									.reverse()
									.each(
											function() {
												if ($('#tw' + this.id, elem).length == 0)
												{
													monitter['count']++;
													var thedate = new Date(Date.parse(this.created_at));
													var thedatestr = thedate.getDay() + '.' + thedate.getMonth() + '.' +thedate.getYear()  +' ' + thedate.getHours()	+ ':' + thedate.getMinutes() + ' MEZ';
													//var thedatestr = thedate.toLocaleString();
													var divstr = '<div id="tw'+ this.id
															+ '" class="tweet"><img width="48" height="48" src="'
															+ this.profile_image_url
															+ '" ><p class="text"><span class="eck"></span>'
															+ this.text.linkify().linkuser().linktag()
															+ '<br />&nbsp;<b><a href="http://twitter.com/'
															+ this.from_user
															+ '" target="_blank">'
															+ this.from_user
															+ '</a></b> &nbsp;-&nbsp;<b>'
															+ thedatestr  
															+ '</b></p></div>';
													monitter['last_id'] = this.id;
													if (json.error)
													{
														monitter['last_id'] = 0 ;
													}
													
													elem.prepend(divstr);
													
													$('#tw' + this.id, elem).hide();
													$('#tw' + this.id + ' img',	elem).hide();
													$('#tw' + this.id + ' img',	elem).fadeIn(4000);
													$('#tw' + this.id, elem).fadeIn('slow');
													
												}
											});
											
								input = escape(input);
								//max = 7;
						});
						setTimeout(function() {
							me.fetch_tweets(elem,max,timer);
						}, timer);

		return (false);
	}
	
	this.pagestart = function(elem,starttwitts)
	{
		elem = $(elem);
		startelem = $(elem);
		monitter['page'] = 1;
		me.fetch_page_tweets(elem,starttwitts);
	}
	
	this.fetch_page_tweets = function(elem,startmax) {
		
		input = elem.attr('title');
		lang = elem.attr('lang');
		var ItemFound = false;
		
		url = "http://search.twitter.com/search.json?q=" + input + "&lang="	+ lang + "&rpp=50&page=" + monitter['page'] +"&callback=?";
		
		$.getJSON(
						url,
						function(json) {
							$('.ot').remove();
							
							$(json.results)
									.reverse()
									.each(
											function() {
													var thedate = new Date(Date.parse(this.created_at));
													//var thedatestr = thedate.getDay() + '.' + thedate.getMonth() + '.' +thedate.getYear()  +' ' + thedate.getHours()	+ ':' + thedate.getMinutes();
													var thedatestr = thedate.toUTCString();

													var divstr = '<div id="tw'+ this.id
															+ '" class="tweet ot"><img width="48" height="48" src="'
															+ this.profile_image_url
															+ '" ><p class="text"><span class="eck"></span>'
															+ this.text.linkify().linkuser().linktag()
															+ '<br />&nbsp;<b><a href="http://twitter.com/'
															+ this.from_user
															+ '" target="_blank">'
															+ this.from_user
															+ '</a></b> &nbsp;-&nbsp;<b>'
															+ thedatestr
															+ '</b></p></div>';
													elem.prepend(divstr);

													$('#tw' + this.id, elem).hide();
													$('#tw' + this.id + ' img',
															elem).hide();
													$('#tw' + this.id + ' img',
															elem).fadeIn(2000);
													$('#tw' + this.id, elem)
															.fadeIn('fast');
													ItemFound = true;
											});
								
								$('.mbd').hide();
								if (ItemFound)
								{
									elem.append('<div class="mbd"><input type="button" class="mb" onclick="me.newer();" value="Newer" /> Page '+ monitter['page'] +' <input type="button" class="mb" onclick="me.older();" value="Older" /></div>');
								}
								else
								{
									elem.append('<div class="mbd"><input type="button" class="mb" onclick="me.newer();" value="Newer" />');
								}
								
								$(".onh2").hide();
								$('<h2 class="onh2">Old tweets</h2>').insertBefore('.ot:first');
						});
		return (false);
	}
	
	
	this.older = function()
	{
		if (monitter['page'] > 1500)
		{
			return false;
		}
		
		monitter['page'] ++;
		me.fetch_page_tweets(startelem,50,0);
	}

	this.newer = function()
	{
		if (monitter['page'] <= 1)
		{
			return false;
		}
		monitter['page'] --;
		me.fetch_page_tweets(startelem,50,0);
		return true;
	}
}
