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://fx-on.com/twitter/profile.php?s="+username);
											}
	);
};

String.prototype.linktag=function(){
	return this.replace(/[#]+[A-Za-z0-9-_]+/g,function(t){
												var tag=t.replace("#","")
												return t.link("http://fx-on.com/twitter/list.php?s="+tag);
											}
	);
};

String.prototype.dateformat=function(){
	year = Number(this.substring(0, 4));
	month = Number(this.substring(5, 7));
	day = Number(this.substring(8, 10));
	hour = this.substring(11, 13);
	minute = this.substring(14, 16);
	second = this.substring(17, 19);
	return month + '/' + day + ' ' + hour + ':' + minute + ':' + second;
};

String.prototype.getformat=function(){
	year = this.substring(0, 4);
	month = this.substring(5, 7);
	day = this.substring(8, 10);
	hour = this.substring(11, 13);
	minute = this.substring(14, 16);
	second = this.substring(17, 19);
	return year + month + day + hour + minute + second;
};

var db_id = 0;
var ar_cnt = 0;
var ar = [];
function fetch_tweets(elem)
{
	elem=jQuery(elem);
	var input = '1';
	if(input!=window.tweet_area['text-'+input]){
		window.tweet_area['last_id'+input]=0;
		window.tweet_area['text-'+input]=input;
		window.tweet_area['count-'+input]=5;
	}

	var url="http://fx-on.com/twitter/json.php?i=" + db_id;
	if(document.URL.indexOf('localhost',0) > -1)
	{
		url="http://localhost/fx/twitter/json.php?i=" + db_id;
	}
	jQuery.getJSON(url,function(json){
		jQuery(json).reverse().each(function(){
											if(jQuery('#tw'+this.id,elem).length==0){
												db_id = this.created_at.getformat();
												window.tweet_area['count-'+input]++;
												var divstr = '';
												divstr += '<div id="tw'+this.id+'" class="tweet">';
												divstr += '<div class="lc">';
												divstr += '<img src="'+this.profile_image_url+'" onerror="this.src=\'http://fx-on.com/img/community/no_photo.png\'" />';
												divstr += '</div>';
												divstr += '<div class="tc"></div>';
												divstr += '<div class="rc">';
												divstr += '<div class="user"><a href="http://fx-on.com/twitter/profile.php?s='+this.from_user+'">'+this.from_user+'</a></div>';
												divstr += '<div class="text">'+this.text.linkify().linkuser().linktag()+'</div>';
												divstr += '<div class="time_tw">'+this.created_at.dateformat()+'</div>';
												divstr += '</div>';
												divstr += '</div>';
												window.tweet_area['last_id'+input]=this.id;
												elem.prepend(divstr);

												jQuery('#tw'+this.id,elem).hide();
												if(ar_cnt > 9)
												{
													jQuery('#tw'+this.id+' .tc',elem).hide();
													jQuery('#tw'+this.id+' .text',elem).hide();
													jQuery('#tw'+this.id,elem).slideDown(1000);
													jQuery('#tw'+this.id+' .text',elem).fadeIn(3000);
													jQuery('#tw'+this.id+' .tc',elem).fadeIn(6000);
												}
												else
												{
													jQuery('#tw'+this.id+' .tc',elem).hide();
													jQuery('#tw'+this.id+' img',elem).hide();
													jQuery('#tw'+this.id+' img',elem).fadeIn(3000);
													jQuery('#tw'+this.id+' .tc',elem).fadeIn(6000);
													jQuery('#tw'+this.id,elem).fadeIn('slow');
												}

												ar[ar_cnt] = this.id;
												ar_cnt++;

												if(ar_cnt > 10)
												{
													for(i = 0; i < ar_cnt - 10; i++)
													{
														jQuery('#tw' + ar[i]).hide();
													}
												}
											}
		});

		input=escape(input);
		rrp=1;
		setTimeout(function(){
		fetch_tweets(elem)
		},
		7000);
	});
	return(false);
}

jQuery(document).ready(function(){
	window.tweet_area={};
	jQuery('#tweet_area').each(function(e){
		rrp=6;
		fetch_tweets(this);
	});
});

