
function mysql_time(time_value){
	var d = new Date(time_value);
	var retValue = d.getFullYear()+"-";
	if(d.getMonth() < 10)
	{
		var mon = d.getMonth() + 1;
		retValue += "0" + mon;
	}
	else
	{
		retValue += d.getMonth() + 1;
	}
	retValue += "-";
	if(d.getDate() < 10)
	{
		retValue += "0" + d.getDate();
	}
	else
	{
		retValue += d.getDate();
	}
	retValue += " ";
	if(d.getHours() < 10)
	{
		retValue += "0" + d.getHours();
	}
	else
	{
		retValue += d.getHours();
	}
	retValue += ":";
	if(d.getMinutes() < 10)
	{
		retValue += "0" + d.getMinutes();
	}
	else
	{
		retValue += d.getMinutes();
	}
	retValue += ":";
	if(d.getSeconds() < 10)
	{
		retValue += "0" + d.getSeconds();
	}
	else
	{
		retValue += d.getSeconds();
	}
	
	return retValue;
}

function relative_time(time_value){
	if(time_value.indexOf("UTC") < 0) time_value = time_value.replace("+","UTC+");
	var parsed_date = Date.parse(time_value);
	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);

	if(delta < 60) {
		return '1分以内';
	} else if(delta < 120) {
		return '2分以内';
	} else if(delta < (45*60)) {
		return (parseInt(delta / 60)).toString() + '分前';
	} else if(delta < (90*60)) {
		return '1時間内';
	} else if(delta < (24*60*60)) {
		return (parseInt(delta / 3600)).toString() + '時間前';
	} else if(delta < (48*60*60)) {
		return '1日前';
	} else {
		return (parseInt(delta / 86400)).toString() + '日前';
	}
}
function delta_time(time_value){
	var d = new Date();
	d.setFullYear(parseInt(time_value.substring(0,4)));
	d.setMonth(parseInt(time_value.substring(5,7))-1);
	d.setDate(parseInt(time_value.substring(8,10)));
	d.setHours(parseInt(time_value.substring(11,13)));
	d.setMinutes(parseInt(time_value.substring(14,16)));
	d.setSeconds(parseInt(time_value.substring(17,19)));

	var parsed_date = d.getTime();
	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);

	return delta;
}

function check_new(check_value,base_value){
	var check = new Date();
	check.setFullYear(parseInt(check_value.substring(0,4)));
	check.setMonth(parseInt(check_value.substring(5,7))-1);
	check.setDate(parseInt(check_value.substring(8,10)));
	check.setHours(parseInt(check_value.substring(11,13)));
	check.setMinutes(parseInt(check_value.substring(14,16)));
	check.setSeconds(parseInt(check_value.substring(17,19)));

	var base = new Date();
	base.setFullYear(parseInt(base_value.substring(0,4)));
	base.setMonth(parseInt(base_value.substring(5,7))-1);
	base.setDate(parseInt(base_value.substring(8,10)));
	base.setHours(parseInt(base_value.substring(11,13)));
	base.setMinutes(parseInt(base_value.substring(14,16)));
	base.setSeconds(parseInt(base_value.substring(17,19)));

	if( base.getTime() < check.getTime() ){
		return true;
	}else{
		return false;
	}
}

function GetW() {
	var nIE = navigator.userAgent.indexOf("MSIE");
	var bIE6std = nIE >= 0 && navigator.userAgent.substr(nIE+5, 1) == "6" && document.compatMode && document.compatMode=="CSS1Compat";
	var nWidth;
	if (bIE6std) {/* IE6スタンダードモードの時 */
		nWidth = document.documentElement.clientWidth;
	} else if (nIE >= 0) {
		nWidth = document.body.clientWidth;
	} else {
		nWidth = window.innerWidth;
	}
	return nWidth;
}

function GetH() {
	var nIE = navigator.userAgent.indexOf("MSIE");
	var bIE6std = nIE >= 0 && navigator.userAgent.substr(nIE+5, 1) == "6" && document.compatMode && document.compatMode=="CSS1Compat";
	var nHeight;
	if (bIE6std) {/* IE6スタンダードモードの時 */
		nHeight = document.documentElement.clientHeight;
	} else if (nIE >= 0) {
		nHeight = document.body.clientHeight - 70;
	} else {
		nHeight = window.innerHeight;
	}
	return nHeight;
}

function SetWndSize(w,h) {
	
	$("#eegFrame").css("left",w+'px');
	$("#eegFrame").css("top",h+'px');
	$("#eegFrame").css("width",(GetW()-w)+'px');
	$("#eegFrame").css("height",(GetH()-h)+'px');

	$("#bar").css("width",GetW()+'px');

	$("#bar2").css("height",(GetH()-h)+'px');

}

var smart = 0;
var most_old_date = '';
var commentArray = new Array();
var page_comment_total = 0;
var reload_comment_lock = 0;
var lastSubMenu = 0;
var lastMenu = 0;
var moveLock = 0;
var lastKeyword = new Array();
var rankLoded = new Array();
var menuMax = 9;
var menuList = new Array();
menuList[0] = '';
menuList[1] = 'twitterに投稿';
menuList[2] = 'コメントを見る';
menuList[3] = '';
menuList[4] = '';
menuList[5] = '';
var bar_w = 0;
var bar_h = 70;
var listsCreate = 0;

function ToggleSub(id)
{
	if( moveLock == 1 )
	{
		return;
	}
	moveLock = 1;
	if(
			(
				(id == 2)
			) && 
			(timer == '') && 
			(id != lastMenu)
	)
	{
		timer = setTimeout(onReloadComment, 1000);
	}
	else if( 
		(
			(id == 2)
		) && 
		(timer != '') && 
		(id == lastMenu) 
	)
	{
		clearTimeout(timer);
		timer = '';
	}

	if( (id == 1) && (id == lastMenu) )
	{
		var path = $("#optionButton1").attr('src');
		path = path.replace('_x','');
		$("#optionButton1").attr('src',path);
	}
	else if( (id == 1) && (id != lastMenu) )
	{
		var path = $("#optionButton1").attr('src');
		path = path.replace('.png','_x.png');
		$("#optionButton1").attr('src',path);
	}
	else if( (id != 1) && (1 == lastMenu) )
	{
		var path = $("#optionButton1").attr('src');
		path = path.replace('_x','');
		$("#optionButton1").attr('src',path);
	}
	


	if( (id == 3) && (id != lastMenu) )
	{
		if( member == 1 )
		{
			if( listsCreate == 0 )
			{
				LoadLists();
			}
			LoadMyData();
		}
	}
	if( (id == 4) && (id != lastMenu) )
	{
		if( $.cookie('lastSubMenu') > 0 )
		{
			ToggleSubSub($.cookie('lastSubMenu'));
		}
		else
		{
			ToggleSubSub(1);
		}
	}
	if( (id == 6) && (id != lastMenu) )
	{
		Search('resultsDiv6','sbt_active');
	}

	if( id == lastMenu )
	{
		$("#eegFrame").animate({
			left:0+'px',
			top:71+'px',
			width: GetW()+'px',
			height: (GetH()-70)+'px'
		}, 500 );
		$("#bar").animate({
			left:0+'px',
			top:0+'px',
			width: GetW()+'px',
			height: 70+'px'
		}, 500 );
		if( lastMenu > 1 )
		{
			var endMenu = lastMenu;
			$("#bar2").animate({
				left:-340+'px',
				top:71+'px',
				width: 340+'px',
				height: (GetH()-70)+'px'
			}, 500 ,'swing',function(){
				$("#optionWnd"+endMenu).css("display","none");
				moveLock = 0;
			});
		}
		else
		{
			$("#bar2").animate({
				left:-340+'px',
				top:71+'px',
				width: 340+'px',
				height: (GetH()-70)+'px'
			}, 500,'swing',function(){
				moveLock = 0;
			});
		}
		bar_w = 0;
		bar_h = 70;
		lastMenu = 0;
	}
	else
	{
		if( 1 < id )
		{
			if( 1 == lastMenu )
			{
				var nHeight = document.getElementById('optionWnd1').clientHeight;
				$("#eegFrame").animate({
					left:0+'px',
					top:71+'px',
					width: GetW()+'px',
					height: (GetH()-70)+'px'
				}, 500,'swing',function(){
						$("#eegFrame").animate({
							left:341+'px',
							top:71+'px',
							width: (GetW()-340)+'px',
							height: (GetH()-70)+'px'
							}, 500);
				});
			}
			else
			{
				$("#eegFrame").animate({
					left:341+'px',
					top:71+'px',
					width: (GetW()-340)+'px',
					height: (GetH()-70)+'px'
				}, 500 );
			}
			$("#bar").animate({
				left:0+'px',
				top:0+'px',
				width: GetW()+'px',
				height: 70+'px'
			}, 500 );
			if( lastMenu > 1 )
			{
				var startMenu = id;
				var endMenu = lastMenu;
				$("#bar2").animate({
					left:-340+'px',
					top:71+'px',
					width: 340+'px',
					height: (GetH()-70)+'px'
				}, 500 ,'swing',function(){
					$("#optionWnd"+endMenu).css("display","none");
					$("#optionWnd"+startMenu).css("display","block");
					$("#bar2").animate({
						left:0+'px',
						top:71+'px',
						width: 340+'px',
						height: (GetH()-70)+'px'
					}, 500,'swing',function(){
						moveLock = 0;
					});
				});
			}
			else
			{
				$("#optionWnd"+id).css("display","block");
				if( lastMenu == 0 )
				{
					$("#bar2").animate({
						left:0+'px',
						top:71+'px',
						width: 340+'px',
						height: (GetH()-70)+'px'
					}, 500,'swing',function(){
						moveLock = 0;
					});
				}
				else
				{
					var nHeight = document.getElementById('optionWnd1').clientHeight;
					$("#bar2").animate({
						left:-340+'px',
						top:71+'px',
						width: 340+'px',
						height: (GetH()-70)+'px'
					}, 500 ,'swing',function(){
						$("#bar2").animate({
							left:0+'px',
							top:71+'px',
							width: 340+'px',
							height: (GetH()-70)+'px'
							}, 500 ,'swing',function(){
								moveLock = 0;
						});
					});
				}
			}
			bar_w = 340;
			bar_h = 70;
		}
		else
		{
			var nHeight = document.getElementById('optionWnd1').clientHeight;
			if( lastMenu > 1 )
			{
				$("#eegFrame").animate({
					left:341+'px',
					top:(71+nHeight)+'px',
					width: (GetW()-340)+'px',
					height: (GetH()-70-nHeight)+'px'
				}, 500,'swing',function(){
					$("#eegFrame").animate({
						left:0+'px',
						top:(71+nHeight)+'px',
						width: GetW()+'px',
						height: (GetH()-70-nHeight)+'px'
					}, 500);
				});
			}
			else
			{
				$("#eegFrame").animate({
					left:0+'px',
					top:(71+nHeight)+'px',
					width: GetW()+'px',
					height: (GetH()-70-nHeight)+'px'
				}, 500);
			}
			$("#bar").animate({
				left:0+'px',
				top:0+'px',
				width: GetW()+'px',
				height: (70+nHeight)+'px'
			}, 500 );
			if( lastMenu > 1 )
			{
				var endMenu = lastMenu;
				$("#bar2").animate({
					left:0+'px',
					top:(71+nHeight)+'px',
					width: 340+'px',
					height: (GetH()-70-nHeight)+'px'
					}, 500 ,'swing',function(){
					$("#bar2").animate({
						left:-340+'px',
						top:(71+nHeight)+'px',
						width: 340+'px',
						height: (GetH()-70-nHeight)+'px'
						}, 500 ,'swing',function(){
						$("#optionWnd"+endMenu).css("display","none");
						moveLock = 0;
					});
				});
			}
			else
			{
				$("#bar2").animate({
					left:-340+'px',
					top:(71+nHeight)+'px',
					width: 340+'px',
					height: (GetH()-70-nHeight)+'px'
				}, 500,'swing',function(){
						moveLock = 0;
					});
			}
			bar_w = 0;
			bar_h = nHeight+70;
		}
		lastMenu = id;
	}
	$(".topMenu li").each(function(){
		var path = $(this).css("background-image");
		path = path.replace('_x','');
		$(this).css("background-image",path);
	});
	if( lastMenu > 1 ) {
		var path = $("#topmenu"+lastMenu).css("background-image");
		path = path.replace('.gif','_x.gif');
		$("#topmenu"+lastMenu).css("background-image",path);
	}

	$.cookie("lastMenu", 0, { path:'/',expires: -365 });
	if( 1 != lastMenu )
	{
		$.cookie("lastMenu", lastMenu, { path:'/',expires: 365,domain:'.eeg.jp' });
	}
}

function ToggleSubSub(id)
{
	if( id != lastSubMenu )
	{
		var startMenu = id;
		var endMenu = lastSubMenu;
		if( lastSubMenu != 0 )
		{
			$("#optionSubWnd"+endMenu).css("display","none");
		}

		if( id == 1 )
		{
			GetRankData('1','rankDiv1');
			$("#optionSubWnd"+startMenu).fadeIn("slow");
		}
		else if( id == 2 )
		{
			GetRankData('5','rankDiv5');
			$("#optionSubWnd"+startMenu).fadeIn("slow");
		}
		else if( id == 4 )
		{
			GetRankData('11','rankDiv11');
			$("#optionSubWnd"+startMenu).fadeIn("slow");
		}
		else if( id == 5 )
		{
			GetRankData('12','rankDiv12');
			$("#optionSubWnd"+startMenu).fadeIn("slow");
		}
		else if( id == 3 )
		{
			LoadTrendWords('tbt_area')
			$("#optionSubWnd"+startMenu).fadeIn("slow");
		}
		lastSubMenu = id;
	}
	$(".subMenu li").each(function(){
		var path = $(this).css("background-image");
		path = path.replace('_x','');
		$(this).css("background-image",path);
	});
	if( lastSubMenu != 0 )
	{
		var path = $("#submenu"+lastSubMenu).css("background-image");
		path = path.replace('.gif','_x.gif');
		$("#submenu"+lastSubMenu).css("background-image",path);

		$.cookie("lastSubMenu", lastSubMenu, { path:'/',expires: 365,domain:'.eeg.jp' });
	}
}

var twtxtChanged = 0;

function onRetweet()
{
	var tmpTxt = $("#twtxt2").val();
	if( tmpTxt == '' )
	{
		alert('つぶやきが空です');
		return;
	}
	tmpTxt = tmpTxt.substr(0, 70);
	tmpTxt += ' (eegコメント on http://eeg.jp/'+urlid+' '+title_short+')';

	$(".rblock2").prepend('<img id="ajax-loader" src="/img/ajax-loader.gif" />');

	var dataString  = "mode=tweet";
			dataString += "&urlindex="+urlindex;
			dataString += "&url_hash="+hash;
			dataString += "&user_twid="+user_twid;
			dataString += "&user_hash="+user_hash;
			dataString += "&reply_twstatusid="+reply_twstatusid;
			if( last_insert_date != '' )
			{
				dataString += "&last_insert_date="+last_insert_date;
			}
			dataString += "&comment="+tmpTxt;

	$.ajax({
		type: "POST",
		url: "/json.php",
		data: dataString,
		dataType: "json",
		success: function(result){
			if( result.result_num != 0 )
			{
				if( result.total_num != 0 )
				{
					page_comment_total = result.total_num;
				}
				else
				{
					page_comment_total += result.result_num;
				}
				$("#topmenu2").fadeOut("slow",function(){
					$("#topmenu2").html('eegコメント<br />('+page_comment_total+')件▼');
					$("#topmenu2").fadeIn("slow");
				});
				$.each(result.tweets, function(i,results){
					if( last_insert_date == '' || true == check_new(mysql_time(results.insert_date),last_insert_date) ){

					var comment = decodeURIComponent(results.comment);
					if( i == result.result_num-1 )
					{
						last_insert_date = mysql_time(results.insert_date);
							var tmpStr = '<a href="http://pf.eeg.jp/'+results.twid+'">';
									tmpStr += '<img src="'+results.twimg+'" class="tw_face" />';
									tmpStr += '</a><a href="javascript:;" onclick="ToggleSub(2)">';
									tmpStr += '<span class="size_s">'+comment+'</span></a>';
						$(".comebox").fadeOut("slow",function(){
							$(".comebox").empty();
							$(".comebox").append(tmpStr);
							$(".comebox").fadeIn("slow");
						});
					}
							comment2 = 'RT @'+results.twid+' http://eeg.jp/'+urlid+' '+comment.replace(/http:\/\/eeg.jp\/([0-9a-zA-Z]+)/g,'');
							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>');
					var contents = '<div class="item '+results.twstatusid+'">'
						+'<span>'
						+'<a href="http://pf.eeg.jp/'+results.twid+'">'
						+'<img src="'+results.twimg+'" height="48" />'
						+'</a>'
						+'<a href="http://pf.eeg.jp/'+results.twid+'">'
						+results.twname
						+'</a> '
						+'"'+comment+'"'
						+'<br />'
						+relative_time(results.insert_date)
						+(
							( member == 1 )?
							(
								'<a href="javascript:;">'
								+'<img src="/img/reply.png" style="float:right;margin:0px;padding:0px;" onclick="Reply(\''+results.twid+' \');" /></a>'
								+'<a href="javascript:;">'
								+'<img src="/img/rt.png" style="float:right;margin:0px;padding:0px;" onclick="Retweet(\''+encodeURIComponent(comment2)+'\',\''+results.twstatusid+'\',\'0\');" /></a>'
								+'<a href="javascript:;">'
								+'<img src="/img/favorite.png" style="float:right;margin:0px;padding:0px;" onclick="Favorite(\''+results.twstatusid+'\');" /></a>'
							):
							(
								'<a href="/login.html?directLogin=1">'
								+'<img src="/img/reply.png" style="float:right;margin:0px;padding:0px;" /></a>'
								+'<a href="/login.html?directLogin=1">'
								+'<img src="/img/rt.png" style="float:right;margin:0px;padding:0px;" /></a>'
								+'<a href="/login.html?directLogin=1">'
								+'<img src="/img/favorite.png" style="float:right;margin:0px;padding:0px;" /></a>'
							)
						)
						+'</span>'
						+'</div>';
					commentArray.push(contents);
					if( -1 !== decodeURIComponent(results.comment).indexOf('eeg.jp') )
					{
						$("#fromAJAX2").prepend(contents);
					}
					
					}
				});
				$("#twtxt2").val('')
				$("#twtxt2").trigger("change")
			}
			else
			{
				alert('投稿に失敗しました');
			}
		},
		complete: function(){
			$("#ajax-loader").remove();
		}
	});
}

function getMoreComment(){
	$.getJSON("/json.php", { mode: "get_comment", url_hash:hash,most_old_date:most_old_date },function(result){
		if( result != null && result.result_num != 0 ){
			$.each(result.tweets, function(i,results){
				var comment = decodeURIComponent(results.comment);
						comment2 = 'RT @'+results.twid+' http://eeg.jp/'+urlid+' '+comment.replace(/http:\/\/eeg.jp\/([0-9a-zA-Z]+)/g,'');
						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>');

				var contents = '<div class="item '+results.twstatusid+'">'
					+'<span>'
					+'<a href="http://pf.eeg.jp/'+results.twid+'">'
					+'<img src="'+results.twimg+'" height="48" />'
					+'</a>'
					+'<a href="http://pf.eeg.jp/'+results.twid+'">'
					+results.twname
					+'</a> '
					+'"'+comment+'"'
					+'<br />'
					+relative_time(results.insert_date)
					+(
						( member == 1 )?
						(
							'<a href="javascript:;">'
							+'<img src="/img/reply.png" style="float:right;margin:0px;padding:0px;" onclick="Reply(\''+results.twid+' \');" /></a>'
							+'<a href="javascript:;">'
							+'<img src="/img/rt.png" style="float:right;margin:0px;padding:0px;" onclick="Retweet(\''+encodeURIComponent(comment2)+'\',\''+results.twstatusid+'\',\'0\');" /></a>'
							+'<a href="javascript:;">'
							+'<img src="/img/favorite.png" style="float:right;margin:0px;padding:0px;" onclick="Favorite(\''+results.twstatusid+'\');" /></a>'
						):
						(
							'<a href="/login.html?directLogin=1">'
							+'<img src="/img/reply.png" style="float:right;margin:0px;padding:0px;" /></a>'
							+'<a href="/login.html?directLogin=1">'
							+'<img src="/img/rt.png" style="float:right;margin:0px;padding:0px;" /></a>'
							+'<a href="/login.html?directLogin=1">'
							+'<img src="/img/favorite.png" style="float:right;margin:0px;padding:0px;" /></a>'
						)
					)
					+'</span>'
					+'</div>';
				$(contents).appendTo("#fromAJAX");
				if( i == result.result_num-1 ){
					most_old_date = mysql_time(results.insert_date);
					$('<div>',{id:("more"+most_old_date),className:'more'}).appendTo("#fromAJAX").click(function(){
						if( !$(this).hasClass("clicked") ){
							$(this).addClass("clicked");
							getMoreComment();
							$(this).fadeOut();
						}
					});
				}
			});
		}
	});
}

function onReloadComment()
{
	if( timer != '' )
	{
		clearTimeout(timer);
		timer = '';
	}
	
	if( reload_comment_lock == 1 )
	{
		return;
	}
	reload_comment_lock = 1;
	if( lastMenu == 1 )
	{
		updateEEG();
	}

	$("#fromAJAX").each(function(){
		if( lastMenu == 2 )
		{
			$(".rblock2").prepend('<img id="ajax-loader" src="/img/ajax-loader.gif" />');
		}
		else if( lastMenu == 1 )
		{
			$("#sideCome").prepend('<img id="ajax-loader" src="/img/ajax-loader.gif" />');
		}
		$.ajax({
			complete: function(){
				$("#ajax-loader").remove();
			}
		});
		$.getJSON("/json.php", { mode: "get_comment", url_hash:hash,last_insert_date:last_insert_date },function(result){
			reload_comment_lock = 0;
			if( result != null && result.result_num != 0 )
			{
				if( result.total_num != 0 )
				{
					page_comment_total = result.total_num;
				}
				else
				{
					page_comment_total += result.result_num;
				}
				$("#topmenu2").fadeOut("slow",function(){
					$("#topmenu2").html('eegコメント<br />('+page_comment_total+')件▼');
					$("#topmenu2").fadeIn("slow");
				});

				$.each(result.tweets, function(i,results){

					if( last_insert_date == '' || true == check_new(mysql_time(results.insert_date),last_insert_date) ){

					if( i == 0 && most_old_date == '' ){
						most_old_date = mysql_time(results.insert_date);
						$('<div>',{id:("more"+most_old_date),className:'more'}).appendTo("#fromAJAX").click(function(){
							if( !$(this).hasClass("clicked") ){
								$(this).addClass("clicked");
								getMoreComment();
								$(this).fadeOut();
							}
						});
					}
					var comment = decodeURIComponent(results.comment);
					if( i == result.result_num-1 )
					{
						last_insert_date = mysql_time(results.insert_date);
						var tmpStr = '<a href="http://pf.eeg.jp/'+results.twid+'">';
								tmpStr += '<img src="'+results.twimg+'" class="tw_face" />';
								tmpStr += '</a><a href="javascript:;" onclick="ToggleSub(2)">';
								tmpStr += '<span class="size_s">'+comment+'</span></a>';
						$(".comebox").fadeOut("slow",function(){
							$(".comebox").empty();
							$(".comebox").append(tmpStr);
							$(".comebox").fadeIn("slow");
						});
					}
							comment2 = 'RT @'+results.twid+' http://eeg.jp/'+urlid+' '+comment.replace(/http:\/\/eeg.jp\/([0-9a-zA-Z]+)/g,'');
							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>');

					var contents = '<div class="item '+results.twstatusid+'">'
						+'<span>'
						+'<a href="http://pf.eeg.jp/'+results.twid+'">'
						+'<img src="'+results.twimg+'" height="48" />'
						+'</a>'
/*
						+(
							( member == 1 )?
							(
								'<a href="javascript:;">'
								+'<img src="/img/follow.png" style="position:absolute;left:4px;top:54px;" onclick="Follow(\''+results.twid+'\');" alt="'+results.twname+'をフォローする" />'
								+'</a>'
							):
							(
								'<a href="/login.html?directLogin=1">'
								+'<img src="/img/follow.png" style="position:absolute;left:4px;top:54px;" alt="'+results.twname+'をフォローする" />'
								+'</a>'
							)
						)
*/
						+'<a href="http://pf.eeg.jp/'+results.twid+'">'
						+results.twname
						+'</a> '
						+'"'+comment+'"'
						+'<br />'
						+relative_time(results.insert_date)
						+(
							( member == 1 )?
							(
								'<a href="javascript:;">'
								+'<img src="/img/reply.png" style="float:right;margin:0px;padding:0px;" onclick="Reply(\''+results.twid+' \');" /></a>'
								+'<a href="javascript:;">'
								+'<img src="/img/rt.png" style="float:right;margin:0px;padding:0px;" onclick="Retweet(\''+encodeURIComponent(comment2)+'\',\''+results.twstatusid+'\',\'0\');" /></a>'
								+'<a href="javascript:;">'
								+'<img src="/img/favorite.png" style="float:right;margin:0px;padding:0px;" onclick="Favorite(\''+results.twstatusid+'\');" /></a>'
							):
							(
								'<a href="/login.html?directLogin=1">'
								+'<img src="/img/reply.png" style="float:right;margin:0px;padding:0px;" /></a>'
								+'<a href="/login.html?directLogin=1">'
								+'<img src="/img/rt.png" style="float:right;margin:0px;padding:0px;" /></a>'
								+'<a href="/login.html?directLogin=1">'
								+'<img src="/img/favorite.png" style="float:right;margin:0px;padding:0px;" /></a>'
							)
						)
						+'</span>'
						+'</div>';
					commentArray.push(contents);
					if( -1 !== decodeURIComponent(results.comment).indexOf('eeg.jp') )
					{
						$("#fromAJAX2").prepend(contents);
					}

					}
				});
			}
			if( last_insert_date != '' && lastMenu == 2 )
			{
				var delta = delta_time(last_insert_date);
				if( delta < 600 )
				{
					timer = setTimeout(onReloadComment, 10000);
				}
				else
				{
					timer = setTimeout(onReloadComment, 30000);
				}
			}
		});
	});
}

function commentArrayMng()
{
	if( commentArray.length != 0 && lastMenu == 2 )
	{
		var target = $(commentArray.shift()).hide().appendTo('body');
		var nHeight = target.outerHeight(true);
		target.detach();
		$("#fromAJAX").animate({
			paddingTop:"+=" + nHeight + "px"
		},500,"swing",function(){
			$("#fromAJAX").css("paddingTop","0px");
			target.prependTo("#fromAJAX").hide().fadeIn("slow");
		});
	}
	setTimeout(commentArrayMng, 1000);
}


function updateEEG()
{
	var reqUrl = 'http://search.twitter.com/search.json?q=http%3A%2F%2Feeg%2Ejp%2F'+urlid+'&rpp=100&callback=?';
	if( since_id != '' )
	{
		reqUrl = 'http://search.twitter.com/search.json?q=http%3A%2F%2Feeg%2Ejp%2F'+urlid+'&since_id='+since_id+'&rpp=100&callback=?';
	}
	$.getJSON(reqUrl, function(json) {
		var dataString = "finaldata="+code+"&urlindex="+urlindex+"&urlhash="+hash;
		var counter = 0;
		if( json.results != undefined )
		{
			since_id = json.results.max_id;
			$.each(json.results, function(i,results){
				if( ( results.from_user != 'eegjp')  && (results.text.indexOf(urlid,0) != -1 ) && (results.source.indexOf('eeg',0) == -1)  )
				{
					dataString += "&twid[]="+results.from_user;
					dataString += "&twimg[]="+results.profile_image_url;
					dataString += "&comment[]="+results.text;
					dataString += "&insert_date[]="+results.created_at;
					dataString += "&twstatusid[]="+results.id_str;
					counter++;
				}
			});
			if( counter != 0 )
			{
				$.ajax({
							type: "POST",
							url: "json.php",
							data: dataString,
							success: function(msg){
							}
				});
			}
		}
	});
}
function LoadLists()
{
	$("#lists").empty();
	$("#lists").prepend('<img id="ajax-loader" src="/img/ajax-loader.gif" />');
	$.ajax({
		complete: function(){
			$("#ajax-loader").remove();
		}
	});
	var reqUrl = 'json.php?mode=lists';
	$.getJSON(reqUrl, function(json) {

		var tmpStr = "<select id='twlists'></select>";
		var target = $(tmpStr);

		if( ($.cookie('lastList') == '') || (($.cookie('lastList') == '0')) )
		{
			tmpStr = "<option value='"+0+"' selected />";
		}
		else
		{
			tmpStr = "<option value='"+0+"' />";
		}
		var target2 = $(tmpStr);
		target2.append(document.createTextNode('タイムライン'));
		target.append(target2);

		if( $.cookie('lastList') == '1' )
		{
			tmpStr = "<option value='"+1+"' selected />";
		}
		else
		{
			tmpStr = "<option value='"+1+"' />";
		}
		target2 = $(tmpStr);
		target2.append(document.createTextNode('@自分宛て'));
		target.append(target2);

		$.each(json.lists, function(i,item){
			if( $.cookie('lastList') == item.slug )
			{
				tmpStr = "<option value='"+item.slug+"' selected />";
			}
			else
			{
				tmpStr = "<option value='"+item.slug+"' />";
			}
			var target2 = $(tmpStr);
			target2.append(document.createTextNode(item.slug));
			target.append(target2);
		});
		$("#lists").append(target);
		listsCreate = 1;
	  $("#lists>select").change(onListSelectChange);
	});
}
function onListSelectChange()
{
	$.cookie("lastList", 0, { path:'/',expires: -365 });
	$.cookie("lastList",$(this).val(), { path:'/',expires: 365,domain:'.eeg.jp' });
	LoadMyData($(this).val());
}

function LoadMyData()
{
	var listNum = $.cookie('lastList');
	
	if( arguments.length == 1 )
	{
		listNum = arguments[0];
	}

	if( (listNum == '') || (listNum == undefined) || ((listNum == '0')) )
	{
		LoadTimeline();
	}
	else if( (listNum == '1') )
	{
		LoadMentions();
	}
	else
	{
		LoadMyLists(listNum);
	}
}
function LoadMyLists(targetList)
{
	$("#timeline").prepend('<img id="ajax-loader" src="/img/ajax-loader.gif" />');
	var reqUrl = 'json.php?mode=list&target='+targetList;
	$.getJSON(reqUrl, function(json) {
		$("#timeline").empty();
		$.each(json, function(i,item){
			var comment = String(item.text);
					comment = comment.split('　').join(' ');
					comment = comment.replace(/(http:\/\/[^ ]+)/g,'<a href="http://eeg.jp/$1">$1</a>');
					comment = comment.replace('http://eeg.jp/http://eeg.jp/','http://eeg.jp/');
					comment = comment.replace(/@([0-9a-z_A-Z]+)/g,'@<a href="http://pf.eeg.jp/$1">$1</a>');

			var screen_name = String(item.user["screen_name"]);

			var tmpS = String(item.text);
					tmpS = tmpS.split("'").join("’");
					tmpS = tmpS.split("(").join("（");
					tmpS = tmpS.split(")").join("）");
					tmpS = tmpS.split('"').join("”");
			var tmpSrc = encodeURIComponent('RT @'+screen_name+' '+tmpS);

			var itemSrc = '';
			itemSrc += '<div class="item">';
			itemSrc += '<span>';
			itemSrc += '<a href="http://pf.eeg.jp/'+screen_name+'">';
			itemSrc += '<img src="'+item.user.profile_image_url+'" height="48" />';
			itemSrc += '</a>';
			itemSrc += '<a href="http://pf.eeg.jp/'+screen_name+'">';
			itemSrc += item.user.name;
			itemSrc += '</a> ';
			itemSrc += '"'+comment+'"';
			itemSrc += '<br />';
			itemSrc += relative_time(item.created_at);
			itemSrc += '<a href="javascript:;">';
			itemSrc += '<img src="/img/reply.png" style="float:right;margin:0px;padding:0px;" onclick="Retweet(\'@'+screen_name+' \',\''+item.id+'\',\'1\');" />';
			itemSrc += '</a>';
			itemSrc += '<a href="javascript:;">';
			itemSrc += '<img src="/img/rt.png" style="float:right;margin:0px;padding:0px;" onclick="Retweet(\''+tmpSrc+'\',\''+item.id+'\',\'1\');" />';
			itemSrc += '</a>';
			if( item.favorited == false )
			{
				itemSrc += '<a href="javascript:;">';
				itemSrc += '<img src="/img/favorite.png" style="float:right;margin:0px;padding:0px;" onclick="Favorite(\''+item.id+'\');" />';
				itemSrc += '</a>';
			}
			itemSrc += '</span>';
			itemSrc += '</div>';
			$("#timeline").append(itemSrc);
		});
	});
}

function LoadTimeline()
{
	$("#timeline").prepend('<img id="ajax-loader" src="/img/ajax-loader.gif" />');
	var reqUrl = 'json.php?mode=statuses/home_timeline';
	$.getJSON(reqUrl, function(json) {
		$("#timeline").empty();
		$.each(json, function(i,item){
			var comment = String(item.text);
					comment = comment.replace('　',' ');
					comment = comment.replace(/(http:\/\/[^ ]+)/g,'<a href="http://eeg.jp/$1">$1</a>');
					comment = comment.replace('http://eeg.jp/http://eeg.jp/','http://eeg.jp/');
					comment = comment.replace(/@([0-9a-z_A-Z]+)/g,'@<a href="http://pf.eeg.jp/$1">$1</a>');

			var screen_name = String(item.user["screen_name"]);

			var tmpS = String(item.text);
					tmpS = tmpS.split("'").join("’");
					tmpS = tmpS.split("(").join("（");
					tmpS = tmpS.split(")").join("）");
					tmpS = tmpS.split('"').join("”");
			var tmpSrc = encodeURIComponent('RT @'+screen_name+' '+tmpS);

			var itemSrc = '';
			itemSrc += '<div class="item">';
			itemSrc += '<span>';
			itemSrc += '<a href="http://pf.eeg.jp/'+screen_name+'">';
			itemSrc += '<img src="'+item.user.profile_image_url+'" height="48" />';
			itemSrc += '</a>';
			itemSrc += '<a href="http://pf.eeg.jp/'+screen_name+'">';
			itemSrc += item.user.name;
			itemSrc += '</a> ';
			itemSrc += '"'+comment+'"';
			itemSrc += '<br />';
			itemSrc += relative_time(item.created_at);
			itemSrc += '<a href="javascript:;">';
			itemSrc += '<img src="/img/reply.png" style="float:right;margin:0px;padding:0px;" onclick="Retweet(\'@'+screen_name+' \',\''+item.id+'\',\'1\');" />';
			itemSrc += '</a>';
			itemSrc += '<a href="javascript:;">';
			itemSrc += '<img src="/img/rt.png" style="float:right;margin:0px;padding:0px;" onclick="Retweet(\''+tmpSrc+'\',\''+item.id+'\',\'1\');" />';
			itemSrc += '</a>';
			if( item.favorited == false )
			{
				itemSrc += '<a href="javascript:;">';
				itemSrc += '<img src="/img/favorite.png" style="float:right;margin:0px;padding:0px;" onclick="Favorite(\''+item.id+'\');" />';
				itemSrc += '</a>';
			}
			itemSrc += '</span>';
			itemSrc += '</div>';
			$("#timeline").append(itemSrc);
		});
	});
}
function LoadMentions()
{
	$("#timeline").prepend('<img id="ajax-loader" src="/img/ajax-loader.gif" />');

	var reqUrl = 'json.php?mode=statuses/mentions';
	$.getJSON(reqUrl, function(json) {
		$("#timeline").empty();
		$.each(json, function(i,item){
			var comment = String(item.text);
					comment = comment.split("　").join(" ");
					comment = comment.replace(/(http:\/\/[^ ]+)/g,'<a href="http://eeg.jp/$1">$1</a>');
					comment = comment.replace('http://eeg.jp/http://eeg.jp/','http://eeg.jp/');
					comment = comment.replace(/@([0-9a-z_A-Z]+)/g,'@<a href="http://pf.eeg.jp/$1">$1</a>');

			var screen_name = String(item.user["screen_name"]);
			var tmpS = String(item.text);
					tmpS = tmpS.split("'").join("’");
					tmpS = tmpS.split("(").join("（");
					tmpS = tmpS.split(")").join("）");
					tmpS = tmpS.split('"').join("”");
			var tmpSrc = encodeURIComponent('RT @'+screen_name+' '+tmpS);

			var itemSrc = '';
			itemSrc += '<div class="item">';
			itemSrc += '<span>';
			itemSrc += '<a href="http://pf.eeg.jp/'+screen_name+'">';
			itemSrc += '<img src="'+item.user.profile_image_url+'" height="48" />';
			itemSrc += '</a>';
			itemSrc += '<a href="http://pf.eeg.jp/'+screen_name+'">';
			itemSrc += item.user.name;
			itemSrc += '</a> ';
			itemSrc += '"'+comment+'"';
			itemSrc += '<br />';
			itemSrc += relative_time(item.created_at);
			itemSrc += '<a href="javascript:;">';
			itemSrc += '<img src="/img/reply.png" style="float:right;margin:0px;padding:0px;" onclick="Retweet(\'@'+screen_name+' \',\''+item.id+'\',\'1\');" />';
			itemSrc += '</a>';
			itemSrc += '<a href="javascript:;">';
			itemSrc += '<img src="/img/rt.png" style="float:right;margin:0px;padding:0px;" onclick="Retweet(\''+tmpSrc+'\',\''+item.id+'\',\'1\');" />';
			itemSrc += '</a>';
			if( item.favorited == false )
			{
				itemSrc += '<a href="javascript:;">';
				itemSrc += '<img src="/img/favorite.png" style="float:right;margin:0px;padding:0px;" onclick="Favorite(\''+item.id+'\');" />';
				itemSrc += '</a>';
			}
			itemSrc += '</span>';
			itemSrc += '</div>';
			
			$("#timeline").append(itemSrc);
		});
	});
}
function Favorite( statusid )
{
	if( member == 1 )
	{
		var reqUrl = 'json.php?mode=favorites/create&twstatusid='+statusid;
			$.getJSON(reqUrl, function(json) {
			alert('お気に入りに追加しました');
		});
	}
}

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 Retweet( comment,twstatusid,norelation )
{
	document.getElementById('twtxt').innerHTML = decodeURIComponent(comment);
	document.getElementById('twstatusid').value = twstatusid;
	document.getElementById('norelation').value = norelation;
	if( document.getElementById('optionWnd2') != undefined )
	{
		document.getElementById('optionWnd2').scrollTop = 0;
	}
	if( document.getElementById('optionWnd3') != undefined )
	{
		document.getElementById('optionWnd3').scrollTop = 0;
	}
	if( lastMenu != 1 )
	{
		ToggleSub(1);
	}
}

function Reply( twid )
{
	if( lastMenu == 1 )
	{
		document.getElementById('twtxt').innerHTML = '@'+twid+' ';
		document.getElementById('twstatusid').value = '';
		document.getElementById('norelation').value = '1';
	}
	else
	{
		document.getElementById('twtxt2').innerHTML = '@'+twid+' ';
		if( document.getElementById('optionWnd2') != undefined )
		{
			document.getElementById('optionWnd2').scrollTop = 0;
		}
		if( document.getElementById('optionWnd3') != undefined )
		{
			document.getElementById('optionWnd3').scrollTop = 0;
		}
	}
}

function Search(block_name,word_name) {
	var search_words = new Array();
	$("."+word_name).each(function(){
		search_words.push($(this).val());
	});
	return SearchWords(search_words.join(' '),block_name);
}

function SearchWords(keyword,block_name)
{
	if( keyword == '' )
	{
		return 0;
	}
	if( keyword == lastKeyword[block_name] )
	{
		return 0;
	}
	lastKeyword[block_name] = keyword;

	var config = {
		siteURL		: '',
		searchSite	: false,
		type		: 'web',
		append		: false,
		perPage		: 8,			// A maximum of 8 is allowed by Google
		page		: 0				// The start page
	}

	config.type = "news";
	googleSearch();
	return 1;

	function googleSearch(settings){
		settings = $.extend({},config,settings);
		settings.term = settings.term || keyword;

		var apiURL = 'http://ajax.googleapis.com/ajax/services/search/'+settings.type+'?v=1.0&callback=?';
		var resultsDiv = $('#'+block_name);
		resultsDiv.prepend('<img id="ajax-loader" src="/img/ajax-loader.gif" />');

		$.getJSON(apiURL,{q:settings.term,rsz:settings.perPage,start:settings.page*settings.perPage},function(r){
			$("#ajax-loader").remove();
			var results = r.responseData.results;
			$("#more"+block_name).remove();
			
			if(results.length){
				var pageContainer = $('<div>',{className:'pageContainer'});

					if( !settings.append )
					{
						pageContainer.append('<div class="item_title">'+keyword+'の記事</div>');
					}
				
				for(var i=0;i<results.length;i++){
					if( (results[i].unescapedUrl.substr(0,top_url.length) != top_url) &&
							(-1 === results[i].unescapedUrl.indexOf('http://twitter.com')) &&
							(-1 === results[i].unescapedUrl.indexOf('http://youtube.com')) &&
							(-1 === results[i].unescapedUrl.indexOf('http://ustream.tv'))
					)
					{
						pageContainer.append(new result(results[i]) + '');
					}
				}
				
				if(!settings.append){
					resultsDiv.empty();
				}

				pageContainer.append('<div class="clear"></div>')
							 .hide().appendTo(resultsDiv)
							 .fadeIn('slow');

				var cursor = r.responseData.cursor;

				if( +cursor.estimatedResultCount > (settings.page+1)*settings.perPage){
					$('<div>',{id:("more"+block_name),className:'more'}).appendTo(resultsDiv).click(function(){
						if( !$(this).hasClass("clicked") ){
							$(this).addClass("clicked");
							googleSearch({append:true,page:settings.page+1});
							$(this).fadeOut();
						}
					});
				}
				else if( settings.type == 'news' )
				{
					config.type = "web";
					googleSearch({append:true});
				}
			}
			else {
				if( settings.type == 'news' && settings.page == 0 )
				{
					resultsDiv.empty();
					config.type = "web";
					googleSearch();
				}
				else if( settings.type == 'news' )
				{
					config.type = "web";
					googleSearch({append:true});
				}
				else
				{
					resultsDiv.empty();
					$('<p>',{className:'notFound',html:'情報が見つかりませんでした'}).hide().appendTo(resultsDiv).fadeIn();
				}
			}
		});
	}
	
	function result(r){
		var arr = [];
		
		switch(r.GsearchResultClass){

			case 'GwebSearch':
				arr = [
					'<div class="item">',
					'<a href="http://eeg.jp/',r.unescapedUrl,'"><img src="http://capture.heartrails.com/64x64?',r.unescapedUrl,'" style="float:left;" /></a>',
					'<span class="rank_body"><a href="http://eeg.jp/',r.unescapedUrl,'">',r.title,'</a></span>',
					'</div>'
				];
			break;
			case 'GimageSearch':
				arr = [
					'<div class="imageResult">',
					'<a target="_blank" href="',r.unescapedUrl,'" title="',r.titleNoFormatting,'" class="pic" style="width:',r.tbWidth,'px;height:',r.tbHeight,'px;">',
					'<img src="',r.tbUrl,'" width="',r.tbWidth,'" height="',r.tbHeight,'" /></a>',
					'<div class="clear"></div>','<a href="',r.originalContextUrl,'" target="_blank">',r.visibleUrl,'</a>',
					'</div>'
				];
			break;
			case 'GvideoSearch':
				arr = [
					'<div class="imageResult">',
					'<a target="_blank" href="',r.url,'" title="',r.titleNoFormatting,'" class="pic" style="width:150px;height:auto;">',
					'<img src="',r.tbUrl,'" width="100%" /></a>',
					'<div class="clear"></div>','<a href="',r.originalContextUrl,'" target="_blank">',r.publisher,'</a>',
					'</div>'
				];
			break;
			case 'GnewsSearch':
				arr = [
					'<div class="item">',
					'<a href="http://eeg.jp/',r.unescapedUrl,'"><img src="http://capture.heartrails.com/64x64?',r.unescapedUrl,'" style="float:left;" /></a>',
					'<span class="rank_body"><a href="http://eeg.jp/',r.unescapedUrl,'">',r.title,'</a></span>',
					'</div>'
				];
			break;
		}
		
		this.toString = function(){
			return arr.join('');
		}
	}

}

function LoadTrendWords(block_name)
{
	if( rankLoded[block_name] == 1 )
	{
		return 0;
	}
	rankLoded[block_name] = 1;
	var resultsDiv = $('.'+block_name);
	resultsDiv.prepend('<img id="ajax-loader" src="/img/ajax-loader.gif" />');
	$.getJSON("json.php",{mode:"trendword"},function(r){
		$("#ajax-loader").remove();
		var results = r.responseData.results;
		if(results.length){
			for(var i=0;i<results.length;i++){
				if( i == 0 )
				{
					resultsDiv.append('<input type="button" value="'+results[i].keyword+'" class="tbt tbt_active" />');
				}
				else
				{
					resultsDiv.append('<input type="button" value="'+results[i].keyword+'" class="tbt" />');
				}
			}
			$(".tbt").click(function(){
				$(".tbt_active").toggleClass("tbt_active");
				$(this).addClass('tbt_active');
				Search('resultsDiv4','tbt_active');
			});
			Search('resultsDiv4','tbt_active');
		}
	});
}

function GetRankData(rank_mode,block_name)
{
	if( rankLoded[block_name] == 1 )
	{
		return 0;
	}
	rankLoded[block_name] = 1;

	var config = {
		type		: 1,
		append		: false,
		perPage		: 10,
		page		: 0
	}
	config.type = rank_mode;
	getEEGRank();
	return 1;

	function getEEGRank(settings){
		settings = $.extend({},config,settings);

		var resultsDiv = $('#'+block_name);
		resultsDiv.prepend('<img id="ajax-loader" src="/img/ajax-loader.gif" />');
		
		$.getJSON("json.php",{mode:"ranking",rankmode:settings.type,rsz:settings.perPage,start:settings.page},function(r){
			$("#ajax-loader").remove();
			var results = r.responseData.results;
			$("#more"+block_name).remove();
			
			if(results.length){
				var pageContainer = $('<div>',{className:'pageContainer'});
				
				for(var i=0;i<results.length;i++){
					pageContainer.append(new result(results[i]) + '');
				}
				
				if(!settings.append){
					resultsDiv.empty();
				}

				pageContainer.append('<div class="clear"></div>')
							 .hide().appendTo(resultsDiv)
							 .fadeIn('slow');

				var cursor = r.responseData.cursor;

				if( +cursor.estimatedResultCount > (settings.page+1)*settings.perPage){
					$('<div>',{id:("more"+block_name),className:'more'}).appendTo(resultsDiv).click(function(){
						if( !$(this).hasClass("clicked") ){
							$(this).addClass("clicked");
							getEEGRank({append:true,page:settings.page+1});
							$(this).fadeOut();
						}
					});
				}
			}
		});
	}

	function result(r){
		var arr = [];
				arr = [
					'<div class="item">',
					'<div class="pt">',
					'<p class="rank">',
					r.rank,
					'位　',
					'<img src="',
					r.rankImg,
					'" />',
					r.tweetNum,
					'コメント　(前回：',
					((r.prevRank==0)?'圏外':(r.prevRank+'位')),
					')</p></div>',
					'<a href="',
					r.unescapedUrl,
					'">',
					'<img src="http://capture.heartrails.com/64x64?',
					r.nativeUrl,
					'" alt="',
					r.title,
					'" style="float:left" />',
					'</a>',
					'<span class="rank_body">',
					'<a href="',
					r.unescapedUrl,
					'">',
					r.title,
					'</a></span></div>'
				];
		this.toString = function(){
			return arr.join('');
		}
	}
}

function FamousSelectChange(cat_id)
{
	if( cat_id == undefined || cat_id == null || cat_id < 1 )
	{
		cat_id = 100;
	}
	$("#famous").prepend('<img id="ajax-loader" src="/img/ajax-loader.gif" />');
	$.getJSON("/json.php", { mode: "famous_category", category_id:cat_id },function(r){
		$("#ajax-loader").remove();
		var data = r.data;
		for( var i = 0; i < data.length; i++ )
		{
			var tmpStr = "<select name='"+data[i].name+"' id='"+data[i].name+"'></select>";
			var target = $(tmpStr);
			for( var k =0; k < data[i].count; k++ )
			{
				if( data[i].selected == data[i].options[k].value )
				{
					tmpStr = "<option value='"+data[i].options[k].value+"' selected />";
				}
				else
				{
					tmpStr = "<option value='"+data[i].options[k].value+"' />";
				}
				var target2 = $(tmpStr);
				target2.append(document.createTextNode(data[i].options[k].name));
				target.append(target2);
				$("#famous").append(target);
			}
		}
	  $("#famous>select").change(onFamousSelectChange);
		$.cookie("lastFamousCategory", r.cat_id, { path:'/',expires: 365,domain:'.eeg.jp' });
		$("#famous_list").prepend('<img id="ajax-loader" src="/img/ajax-loader.gif" />');
		$.getJSON("/json.php", { mode: "famous", cat_id:r.cat_id },function(dat){
			$("#ajax-loader").remove();
			var items = dat.responseData.results;
			$("#famous_list").empty();
			var tmpStr = '';
			for( var i = 0; i < items.length; i++ )
			{
				tmpStr = '<div class="item">';
				tmpStr += '<a href="http://pf.eeg.jp/'+items[i].twid+'">';
				tmpStr += '<img src="'+items[i].twimg+'" width="48" />';
				tmpStr += '</a>';
				tmpStr += '<span>';
				tmpStr += '<a href="http://pf.eeg.jp/'+items[i].twid+'">';
				tmpStr += items[i].twname;
				tmpStr += '</a> ';
				tmpStr += '('+items[i].tw_followers_count+'F.)<br />';
				tmpStr += items[i].twdescription;
				tmpStr += '</span>';
				tmpStr += '</div>';
				$("#famous_list").append(tmpStr);
			}
		});
	});
}

function onFamousSelectChange()
{
	$("#famous>select").remove();
	FamousSelectChange($(this).val());
}

function scrollToEnd(){
	var scrolltop=$(this).attr('scrollTop');
	var scrollheight=$(this).attr('scrollHeight');
	var windowheight=$(this).attr('clientHeight');
	var scrolloffset=0;
	if(scrolltop==(scrollheight-(windowheight+scrolloffset))){
		var id = $(this).attr('id');
		$("#"+id+" .more").click();
	}
}



