var left = "(";
var right = ")";
var linkfree = "<BR> <div style=\"size:25px;color:#9999FF;font-style:italic;\">&nbsp;&nbsp;<img src=\"image/star.gif\" width=\"10px\" height=\"10px\" border=\"0\"> 夜景スタイルはリンクフリーです。</div>";
var hash = "<a target=\"_blank\" style=\"color:#006699;\" href=\"http://twitter.com/?status=RT %23yakeistyle :\">#yakeistyle</a>";
function callback(aray) {
	arr = aray.results;
	var s = arr.length;
	if(s<10){
		document.forms[0].twitterPageNext.disabled=true;
		document.forms[0].twitterPageNext.style.cursor='auto';
	}
	var v = '';
	var presentDate = (arguments.length > 1) ? arguments[1] : new Date();
	var style_class1 = " class=\"twtr-tweet\" style=\"border-bottom:1px dotted #333333;overflow:hidden;zoom:1;font-family:'ヒラギノ角ゴ Pro W3','Hiragino Kaku Gothic Pro','Times New Roman','Verdana,Arial','sans-serif';\"><div class=\"twtr-tweet-wrap\"><div class=\"twtr-tweet-text\">";
	var style_class2 = "<span style=\"line-height:165%;color:#9e8f62;font-size:11px;font-family:Times New Roman;\">";
	var style_class3 = "</div></div></div>";
	for (var i=0; i<s; ++i) {
		var obj = arr[i];
		relativeDates = obj.created_at.split(" ");
		var relativeDate = new Date(relativeDates[2]+" "+relativeDates[1]+", "+relativeDates[3]+" "+relativeDates[4]);
		relativeDate.setTime(relativeDate.getTime()-(relativeDate.getTimezoneOffset()*60*1000));//JP
		var date = (relativeDate.getMonth() + 1) + '月' + relativeDate.getDate() + '日 ' + relativeDate.getHours() + '時' + relativeDate.getMinutes() + '分 ';
		v += '<div id=\"tweet-id-'+(s-i)+'\"' + style_class1;
		if(obj.from_user=="yakeistyle"){
			obj.from_user="MD";
			end = "<img src=\"./image/ico_pushpin.gif\" width=\"11\" height=\"11\" border=\"0\"><br>";
			v += '<p>' + replaceImage(obj.text) + style_class2 + date + relative_time(relativeDate,presentDate) + '&nbsp;&nbsp;&nbsp;<span><a target=\"_blank\" style=\"color:#9e8f62;\" href=\"http://twitter.com/yakeistyle\">By MD</a>&nbsp;&nbsp;<a class=\"type2\" href=\"javascript:onclick=deleteTwitter(\''+ obj.id +'\',\'' + date +'\')\">×</a></span></p>';
		}
		else if(obj.from_user=="kakakucamera"){
			//obj.from_user="価格コム";
			obj.text=obj.text.replace("デジタル一眼レフカメラ (SONY)","</span><BR>");
			obj.text=obj.text.replace("フラッシュ (SONY)","</span><BR>");
			obj.text=obj.text.replace("レンズフィルター (SONY)","</span><BR>");
			obj.text=obj.text.replace("レンズ (SONY)","</span><BR>");
			obj.text=obj.text.replace("レンズ (コシナ)","</span><BR>");
			obj.text=obj.text.replace(":","");
			end = "<img src=\"./image/ico_pushpin3.gif\" width=\"11\" height=\"11\" border=\"0\"><br>";
			v += '<p>' + replaceImage("<span style=\"color:#9966FF;\">◎ "+obj.text) + style_class2 + date + relative_time(relativeDate,presentDate) + '&nbsp;&nbsp;&nbsp;<span><a class=\"type2\" href=\"javascript:onclick=deleteTwitter(\''+ obj.id +'\',\'' + date +'\')\">×</a></p>';
		}
		else{
			end = "<img src=\"./image/ico_pushpin2.gif\" width=\"11\" height=\"11\" border=\"0\"><br>";
			v += '<p>' + replaceImage(obj.text) + style_class2 + date + relative_time(relativeDate,presentDate) + '&nbsp;&nbsp;&nbsp;<span><a target=\"_blank\" style=\"color:#9966FF;\" href=\"http://twitter.com/' + obj.from_user + '\">By ' + obj.from_user +'さん</a>&nbsp;&nbsp;<a class=\"type2\" href=\"javascript:onclick=deleteTwitter(\''+ obj.id +'\',\'' + date +'\')\">×</a></p>';
		}
		v += style_class3;
	}
	document.getElementById('result').innerHTML = v+linkfree;
}
//インライン画像化するなど
function replaceImage(s) {
	if(checkPhotoMovie(s)[0] != null){
		text = replaceURLWithHTMLLinks(img[3]).replace("#yakeistyle",hash);
		tag = "<a target=\"_blank\" class=\"type3\" href=\"" + img[0] + img[2] + "\"><span class=\"top_twitter_shadow\"><img src=\"" + img[1] + img[2] + img[4] + "\" width=\"50px\" height=\"39px\"></span></a>";
		s = "<table class=\"twtr-tweet-text\" border=\"0\"style=\"margin:-3px -2px;\"><tr><td style=\"font-size:12px;color:#f0f0f0;margin-left:0px;\">" + text + end + "</td><td style=\"width:51px;text-align:right;vertical-align:bottom;\">" + tag + "</td></tr></table>";
	}
	else {
		s = replaceURLWithHTMLLinks(s).replace("#yakeistyle",hash) + end;
	}
	return s;
}
function checkPhotoMovie(s){
	img = new Array(5);
	//TwitPic
	if(s.indexOf("http://twitpic.com/")>-1){
		img[0] = "http://twitpic.com/";//変換前
		img[1] = "http://twitpic.com/show/thumb/";//変換後
		img[2] = s.substring(s.lastIndexOf(img[0])+(img[0].length),s.lastIndexOf(img[0])+(img[0].length)+6);//画像ID
		img[3] = s.substring(0,s.lastIndexOf(img[0]));//本文
		img[4] = "";
		return img;
	}
	//TwitPic
	else if(s.indexOf("http://twitpic.com/p")>-1){
		img[0] = "http://twitpic.com/p";//変換前
		img[1] = "http://twitpic.com/show/thumb/";//変換後
		img[2] = s.substring(s.lastIndexOf(img[0])+(img[0].length));//画像ID
		img[3] = s.substring(0,s.lastIndexOf(img[0]));//本文
		img[4] = "";
		return img;
	}
	//youtube
	else if(s.indexOf("http://www.youtube.com/watch?v=")>-1){
		img[0] = "http://www.youtube.com/watch?v=";//変換前
		img[1] = "http://i.ytimg.com/vi/";//変換後
		p = s.lastIndexOf(img[0])+(img[0].length);
		img[2] = s.substring(p,p+11);//画像ID
		img[3] = s.substring(0,s.lastIndexOf(img[0]));//本文
		img[4] = "/default.jpg";
		return img;
	}
	//ニコニコ動画
	else if(s.indexOf("http://www.nicovideo.jp/watch/sm")>-1){
		img[0] = "http://www.nicovideo.jp/watch/sm";//変換前
		img[1] = "http://tn-skr.smilevideo.jp/smile?i=";//変換後
		p = s.lastIndexOf(img[0])+(img[0].length);
		img[2] = s.substring(p);//画像ID
		img[3] = s.substring(0,s.lastIndexOf(img[0]));//本文
		img[4] = "";
		return img;
	}
	//携帯百景
	else if(s.indexOf("http://movapic.com/pic/")>-1){
		img[0] = "http://movapic.com/pic/";//変換前
		img[1] = "http://image.movapic.com/pic/s_";//変換後
		p = s.lastIndexOf(img[0])+(img[0].length);
		img[2] = s.substring(p,p+27);//画像ID
		img[3] = s.substring(0,s.lastIndexOf(img[0]));//本文
		img[4] = ".jpeg";
		return img;
	}
	//youtu.be
	else if(s.indexOf("http://youtu.be/")>-1){
		img[0] = "http://youtu.be/";//変換前
		img[1] = "http://i.ytimg.com/vi/";//変換後
		p = s.lastIndexOf(img[0])+(img[0].length);
		img[2] = s.substring(p,p+11);//画像ID
		img[3] = s.substring(0,s.lastIndexOf(img[0]));//本文
		img[4] = "/default.jpg";
		return img;
	}
	//twitvideo
	else if(s.indexOf("http://twitvideo.jp/")>-1){
		img[0] = "http://twitvideo.jp/";//変換前
		img[1] = "http://twitvideo.jp/img/thumb/";//変換後
		p = s.lastIndexOf(img[0])+(img[0].length);
		img[2] = s.substring(p,p+7);//画像ID
		img[3] = s.substring(0,s.lastIndexOf(img[0]));//本文
		img[4] = "/default.jpg";
		return img;
	}
	//yfrog
	else if(s.indexOf("http://yfrog.com/")>-1){
		img[0] = "http://yfrog.com/";//変換前
		img[1] = "http://yfrog.com/";//変換後
		p = s.lastIndexOf(img[0])+(img[0].length);
		img[2] = s.substring(p,p.length);//画像ID
		img[3] = s.substring(0,s.lastIndexOf(img[0]));//本文
		img[4] = ".th.jpg";
		return img;
	}
	//ニコニコ動画
	else if(s.indexOf("http://nico.ms/sm")>-1){
		img[0] = "http://nico.ms/sm";//変換前
		img[1] = "http://tn-skr.smilevideo.jp/smile?i=";//変換後
		img[2] = s.substring(s.lastIndexOf(img[0])+(img[0].length));//画像ID
		img[3] = s.substring(0,s.lastIndexOf(img[0]));//本文
		img[4] = "";
		return img;
	}
	//img.ly
	else if(s.indexOf("http://img.ly/")>-1){
		img[0] = "http://img.ly/";//変換前
		img[1] = "http://img.ly/show/thumb/";//変換後
		img[2] = s.substring(s.lastIndexOf(img[0])+(img[0].length));//画像ID
		img[3] = s.substring(0,s.lastIndexOf(img[0]));//本文
		img[4] = "";
		return img;
	}
	return img;
}
//httpをリンク化するなど
function replaceURLWithHTMLLinks(s) {
	var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i;
	return s.replace(exp,"<BR><a target=\"_blank\" class=\"type3\" href=\"$1\">$1</a>&nbsp;");
}
//日付フォーマットなど
function relative_time(relativeDate,presentDate) {
	var d = parseInt((presentDate.getTime() - relativeDate) / 1000);
	if(d < 10) {
		return left + 'たった今' + right;
	}
	else if(d < 60) {
		return left + '数十秒前' + right;
	}
	else if(d < 120) {
		return left + '1分前' + right;
	}
	else if(d < (45*60)) {
		return left + (parseInt(d / 60)).toString() + '分前' + right;
	}
	else if(d < (90*60)) {
		return left + '1時間前' + right;
	}
	else if(d < (24*60*60)) {
		return left + (parseInt(d / 3600)).toString() + '時間前' + right;
	}
	else if(d < (48*60*60)) {
		return left + '昨日' + right;
	}
	else {
		return left + (parseInt(d / 86400)).toString() + '日前' + right;
	}
}
//つぶやく
function postwi(status) {
	var d=document;
	var i1=d.createElement('iframe');
	i1.style.display='none';
	d.body.appendChild(i1);
	var tid;
	if(/*@cc_on!@*/false){
		tid='_self';
		setTimeout(function() {
			d.body.removeChild(i1);
		},5000);
	}else{
		tid='yakeistyle';
		var i2 = d.createElement('iframe');
		i2.name=tid;
		i2.style.display='none';
		d.body.appendChild(i2);
		if(navigator.userAgent.indexOf('Firefox')>=0){window[tid+'flg']=true};
		var onunload = function(){
			if(window[tid+'flg']){delete window[tid+'flg'];return;}
			setTimeout(function() {
				d.body.removeChild(i1);
				d.body.removeChild(i2);
			},500);
		};
		var w2=i2.contentWindow;
		if(w2.attachEvent){
			w2.attachEvent("onunload",onunload);
		}else{
			w2.addEventListener("unload",onunload,false);
		}
	}
	d=i1.contentWindow.document;
	d.open();
	d.write(unescape("%3Cbody%3E%3C/body%3E"));
	d.close();
	var iform = d.createElement("form");
	iform.id="iform";
	iform.method="post";
	iform.action="https://twitter.com/statuses/update.xml";
	iform.target=tid;
	iform.setAttribute("Accept-charset","utf-8");
	var ist = d.createElement("input");
	ist.type="hidden";
	ist.name="status";
	ist.value=status;
	iform.appendChild(ist);
	d.body.appendChild(iform);	
	var val = iform.submit();
	setTimeout("getTl(1)", 3000);
}
//つぶやきを削除
function deleteTwitter(status,date){
	if(!window.confirm(date+"の投稿を削除しますか？")){
		return;
	}
	var d=document;
	var i1=d.createElement('iframe');
	i1.style.display='none';
	d.body.appendChild(i1);
	var tid;
	if(/*@cc_on!@*/false){
		tid='_self';
		setTimeout(function() {
			d.body.removeChild(i1);
		},5000);
	}else{
		tid='yakeistyle';
		var i2 = d.createElement('iframe');
		i2.name=tid;
		i2.style.display='none';
		d.body.appendChild(i2);
		if(navigator.userAgent.indexOf('Firefox')>=0){window[tid+'flg']=true};
		var onunload = function(){
			if(window[tid+'flg']){delete window[tid+'flg'];return;}
			setTimeout(function() {
				d.body.removeChild(i1);
				d.body.removeChild(i2);
			},500);
		};
		var w2=i2.contentWindow;
		if(w2.attachEvent){
			w2.attachEvent("onunload",onunload);
		}else{
			w2.addEventListener("unload",onunload,false);
		}
	}
	d=i1.contentWindow.document;
	d.open();
	d.write(unescape("%3Cbody%3E%3C/body%3E"));
	d.close();
	var iform = d.createElement("form");
	iform.id="iform";
	iform.method="post";
	iform.action="https://twitter.com/statuses/destroy/"+status+".xml";
	iform.target=tid;
	iform.setAttribute("Accept-charset","utf-8");
	var ist = d.createElement("input");
	ist.type="hidden";
	ist.name="status";
	iform.appendChild(ist);
	d.body.appendChild(iform);
	var val = iform.submit();
	setTimeout("getTl(1)", 2000);
}
//BACKボタン
function changeTwitterPageBack(){
	var page = parseInt(document.top.twitterPage.value);
	page = page-1;
	if(page == 1){
		document.forms[0].twitterPageBack.disabled=true;
		document.forms[0].twitterPageBack.style.cursor='auto';
	}
	document.forms[0].twitterPageNext.disabled=false;
	document.forms[0].twitterPageNext.style.cursor='pointer';
	document.getElementById('result').innerHTML = "<center><BR><img src=\"./image/loading.gif\" width=\"126\" height=\"22\" border=\"0\"></center>";
	getTl(page);
	document.top.twitterPage.value = page;
}
//NEXTボタン
function changeTwitterPageNext(){
	var page = parseInt(document.top.twitterPage.value);
	page = page+1;
	if(page == 2){
		document.forms[0].twitterPageBack.disabled=false;
		document.forms[0].twitterPageBack.style.cursor='pointer';
	}
	document.getElementById('result').innerHTML = "<center><BR><img src=\"./image/loading.gif\" width=\"126\" height=\"22\" border=\"0\"></center>";
	getTl(page);
	document.top.twitterPage.value = page;
}
//twitterAPI
var TwitterAPI = {
version:'0.9.6',
w:window,
d:document,
e:function(id){return this.d.getElementById(id);},
ce:function(el){return this.d.createElement(el);},
ac:function(e){this.d.body.appendChild(e);},
rc:function(e){try{this.d.body.removeChild(e);}catch(e){}},
ae:function(e,t,f){if(e.attachEvent){e.attachEvent('on'+t,f);}else{e.addEventListener(t,f,false);}},
b:'https://search.twitter.com/',
lout:'https://other:a@twitter.com/',
cnt:0,
tid:function(){return 'TwitterApiID'+(++this.cnt)},
cm:function(c,m,i) {return c+(m&&m.length>0?'/'+m:'')+(i?'/'+i:'');},
isFx:(navigator.userAgent.indexOf('Firefox')>=0),
isIE:(/*@cc_on!@*/false),
isOp:window.opera,
get:function(c,f,p) {
	var t=this;t._get(t.b,c,f,p);
},
_get:function(b,cm,func,param) {
	var t=this;
	var tid=t.tid();
	var s=t.ce('script');
	t.w[tid]=function(obj){
		if(func){func(obj);}
		var t=TwitterAPI;
		setTimeout(function() {
			try{delete t.w[tid];}catch(e){t.w[tid]=null;}
			t.rc(s);
		},100);
	};
	s.src=b+cm+'.json?'+((param)?''+param:'');
	t.ac(s);
},
post:function(cm,func,vals) {
	var t=this;
	var i1=t.ce('iframe');
	i1.style.display='none';
	t.ac(i1);
	var tid;
	if(t.isIE){
		tid='_self';
		setTimeout(function() {
			t.rc(i1);
		},5000);
	}else{
		tid=t.tid();
		var i2=t.ce('iframe');
		i2.name=tid;
		i2.style.display='none';
		t.ac(i2);
		if(t.isFx){t.w[tid+'flg']=true};
		t.ae(i2.contentWindow,'unload',function(){
			var t=TwitterAPI;
			if(t.w[tid+'flg']){delete t.w[tid+'flg'];return;}
			if(func){func();}
			setTimeout(function() {
				t.rc(i1);
				t.rc(i2);
			},100);
		});
	}
	var d=i1.contentWindow.document;
	d.open();
	d.write('<form method="POST" action="'+t.b+cm+'.xml" target="'+tid+'">');
	if (vals) {
		for(var n in vals) {
			var val=vals[n].replace(/&/g,'&amp;').replace(/"/g,'&quot;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
			d.write('<input type="hidden" name="'+n+'" value="'+val+'">');
		}
	}
	d.write('</form>');
	d.write('<script>window.onload=function(){document.forms[0].submit();}</script>');
	d.close();
},
relogin:function(f) {
	var t=this;
	var e=false;
	if(t.isOp){
		e=true;
	}else{
		try{t._get(t.lout,t.cm('statuses','user_timeline'),f);}catch(e){e=true;}
	}
	if(e){alert('Not supported relogin.');}
},
search:function(f,i,p){var t=TwitterAPI;t.get(t.cm('search'),f,p);},
statuses:{
	public_timeline:function(f,i,p){var t=TwitterAPI;t.get(t.cm('statuses','public_timeline'),f,p);},
	friends_timeline:function(f,i,p){var t=TwitterAPI;t.get(t.cm('statuses','friends_timeline',i),f,p);},
	user_timeline:function(f,i,p){var t=TwitterAPI;t.get(t.cm('statuses','user_timeline',i),f,p);},
	show:function(f,i,p){var t=TwitterAPI;t.get(t.cm('statuses','show',i),f,p);},
	update:function(s,f) {var t=TwitterAPI;t.post(t.cm('statuses','update'),f,{'status':s});},
	mentions:function(f,i,p){var t=TwitterAPI;t.get(t.cm('statuses','mentions'),f,p);},
	destroy:function(f,i,p){var t=TwitterAPI;t.get(t.cm('statuses','destroy',i),f);},
	friends:function(f,i,p){var t=TwitterAPI;t.get(t.cm('statuses','friends',i),f,p);},
	followers:function(f,i,p){var t=TwitterAPI;t.get(t.cm('statuses','followers',i),f,p);},
	featured:function(f,i,p){var t=TwitterAPI;t.get(t.cm('statuses','featured'),f);}
},
users:{
	show:function(f,i,p){var t=TwitterAPI;t.get(t.cm('users','show',i),f,p);},
	own:function(f) {
		TwitterAPI.statuses.user_timeline(function(r) {
			if (r&&r.length&&r.length>0) {f(r[0].user);}
		},null,'count=1');
	}
},
direct_messages:{
	show:function(f,i,p){var t=TwitterAPI;t.get(t.cm('direct_messages'),f,p);},
	sent:function(f,i,p){var t=TwitterAPI;t.get(t.cm('direct_messages','sent'),f,p);},
	create:function(u,x,f) {var t=TwitterAPI;t.post(t.cm('direct_messages','new'),f,{'user':u,'text':x});},
	destroy:function(f,i,p){var t=TwitterAPI;t.get(t.cm('direct_messages','destroy',i),f,p);}
},
friendships:{
	create:function(f,i,p){var t=TwitterAPI;t.post(t.cm('friendships','create',i),f,p);},
	destroy:function(f,i,p){var t=TwitterAPI;t.post(t.cm('friendships','destroy',i),f,p);},
	exists:function(f,i,p){var t=TwitterAPI;t.get(t.cm('friendships','exists'),f,p);},
	show:function(f,i,p){var t=TwitterAPI;t.get(t.cm('friendships','show'),f,p);}
},
friends:{
	ids:function(f,i,p){var t=TwitterAPI;t.get(t.cm('friends','ids'),f,p);}
},
followers:{
	ids:function(f,i,p){var t=TwitterAPI;t.get(t.cm('followers','ids'),f,p);}
},
account:{
	verify_credentials:function(f,i,p){var t=TwitterAPI;t.get(t.cm('account','verify_credentials'),f);},
	rate_limit_status:function(f,i,p){var t=TwitterAPI;t.get(t.cm('account','rate_limit_status'),f);},
	end_session:function(f,i,p){var t=TwitterAPI;t.get(t.cm('account','end_session'),f);},
	update_delivery_device:function(f,i,p){var t=TwitterAPI;t.get(t.cm('account','update_delivery_device'),f,p);},
	update_profile_colors:function(f,i,p){var t=TwitterAPI;t.post(t.cm('account','update_profile_colors',i),f,p);},
	update_profile_image:function(f,i,p){var t=TwitterAPI;t.post(t.cm('account','update_profile_image',i),f,p);},
	update_profile_background_image:function(f,i,p){var t=TwitterAPI;t.post(t.cm('account','update_profile_background_image',i),f,p);},
	update_profile:function(f,i,p){var t=TwitterAPI;t.post(t.cm('account','update_profile',i),f,p);}
},
favorites:{
	show:function(f,i,p){var t=TwitterAPI;t.get(t.cm('favorites','',i),f,p);},
	create:function(f,i,p){var t=TwitterAPI;t.post(t.cm('favorites','create',i),f,p);},
	destroy:function(f,i,p){var t=TwitterAPI;t.post(t.cm('favorites','destroy',i),f,p);}
},
notifications:{
	follow:function(f,i,p){var t=TwitterAPI;t.get(t.cm('notifications','follow',i));},
	leave:function(f,i,p){var t=TwitterAPI;t.get(t.cm('notifications','leave',i));}
},
blocks:{
	create:function(f,i,p){var t=TwitterAPI;t.post(t.cm('blocks','create',i),f,p);},
	destroy:function(f,i,p){var t=TwitterAPI;t.post(t.cm('blocks','destroy',i),f,p);},
	exists:function(f,i,p){var t=TwitterAPI;t.get(t.cm('blocks','exists',i),f,p);},
	blocking:{
		show:function(f,i,p){var t=TwitterAPI;t.get(t.cm('blocks','blocking',i),f,p);},
		ids:function(f,i,p){var t=TwitterAPI;t.get(t.cm('blocks','blocking','ids'),f,p);}
	}
},
help:{
	test:function(f,i,p){var t=TwitterAPI;t.get(t.cm('help','test'));},
	downtime_schedule:function(f,i,p){var t=TwitterAPI;t.get(t.cm('help','downtime_schedule'));}
}
}
