var htmlEscape = (function(){
  var map = {"<":"&lt;", ">":"&gt;", "&":"&amp;", "'":"&#39;", "\"":"&quot;", " ":"&nbsp;"};
  var replaceStr = function(s){ return map[s]; };
  return function(str) { return str.replace(/<|>|&|'|"|\s/g, replaceStr); };
})();

function Follow( screen_name )
{
	if( member == 1 )
	{
		var reqUrl = 'json.php?mode=friendships/create&screen_name='+screen_name;
			$.getJSON(reqUrl, function(json) {
			alert(screen_name+'をフォローしました');
		});
	}
}

$(function(){
	if( specialq != '' )
	{
		var reqUrl = 'http://search.twitter.com/search.json?q='+specialq+'&rpp=20&callback=?';
		$.getJSON(reqUrl, function(json) {
			$.each(json.results, function(i,results){
				var comment = results.text;
				if( results.from_user != 'eegjp' )
				{
					comment = comment.replace(/http:\/\/eeg.jp\/([0-9a-zA-Z]+)/g,'<a href="http://eeg.jp/$1">http://eeg.jp/$1</a>');
					comment = comment.replace(/@([0-9a-z_A-Z]+)/g,'@<a href="http://pf.eeg.jp/$1">$1</a>');
					$("#tweetWnd").append(
					'<div class="item">'
					+'<a href="http://pf.eeg.jp/'+results.from_user+'">'
					+'<img src="'+results.profile_image_url+'" height="48" />'
					+'</a>'
					+'<span class="tweet">'
					+'<a href="http://pf.eeg.jp/'+results.from_user+'">'
					+results.from_user
					+'</a> '
					+'"'+comment+'"'
					+'<br />'
					+relative_time(results.created_at)
					+'</span>'
					+'</div>'
					);
				}
	    });
		});
	}
	$(".follow").click(function(){
		var twid = $(this).attr("id");
		if( member == 1 )
		{
			var reqUrl = 'json.php?mode=friendships/create&screen_name='+twid;
			$.getJSON(reqUrl, function(json) {
				alert(twid+'をフォローしました');
				$(".follow").attr("src","/img/followed.png");
				$(".follow").attr("alt",twid+"をフォロー中");
			});
		}
	});
	$("#link_bt").click(function(){
		if( $("#link_url").val() != 'http://' )
		{
			var tag = '<a href="http://eeg.jp/'+$("#link_url").val()+'" target="_blank">';
					tag += '<img src="http://eeg.jp/img/bookmark.png" alt="twitterで便利な短縮ＵＲＬ:eegで見る" />';
					tag += '</a>';
			$("#link_box").html(htmlEscape(tag));
		}
		else
		{
			alert('urlを入力してください');
		}
	});
	$("#pf_go").click(function(){
		location.href = 'http://pf.eeg.jp/'+$("#pf_twid").val();
	});
});


