
//-------------------------------------
// JSONPによるアクセス
//
//	author: Toyohiko Yoshimine
//-------------------------------------
function etLoadJSONP(_url, _callback) {
	var script = document.createElement('script');
    script.charset = 'utf-8';
    script.type = 'text/javascript';
    script.src = _url + (_url.match(/\?/) ? '&' : '?') + 'callback=' + _callback + '&time='+(new Date().getTime());
    document.lastChild.appendChild(script);
};

//-------------------------------------
// CSSルールの追加
//
//	author: Toyohiko Yoshimine
//-------------------------------------
function etAddCSSRule(_rule) {
	var ss = document.styleSheets[document.styleSheets.length-1];
	if (ss.insertRule) {
		ss.insertRule(_rule, 0);
	}
	else if (ss.addRule) {
		var selector = _rule.substring(0, _rule.indexOf('{'));
		var styles   = _rule.substring(_rule.indexOf('{')+1, _rule.indexOf('}'));
		ss.addRule(selector, styles);
	}
}

//-------------------------------------
//	Twitterガジェット埋め込み
//
//	author: Toyohiko Yoshimine
//-------------------------------------
var etTwitterId = 0;
var etTwitterCallbacks = new Array();

function etTwitter(_params, _userId) {

	var domain = 'http://twitterproxyservice.appspot.com/api';
//	var domain = 'http://twitter.com';

	this.IE6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false;

	this.id = 'etTwitter'+etTwitterId;
	this.shellLinkCSS = 'etTwitterShellLink'+etTwitterId;
	this.tweetLinkCSS = 'etTwitterTweetLink'+etTwitterId;
	++etTwitterId;

	this.params = _params;
	this.userId = _userId;

	this.width  = _params.width;
	this.height = _params.height;

	this.scrollbar   = _params.features.scrollbar;
	this.loadingicon = _params.features.loadingicon;

	// リンク色変更のためのクラス追加
	if (document.styleSheets) {
		if (_params.theme.shell.link) {
			etAddCSSRule('.'+this.shellLinkCSS+' A { text-decoration:none; color:'+_params.theme.shell.link+' }');
			etAddCSSRule('.'+this.shellLinkCSS+' A:active { text-decoration:underline; color:'+_params.theme.shell.link+' }');
			etAddCSSRule('.'+this.shellLinkCSS+' A:hover { text-decoration:underline; color:'+_params.theme.shell.link+' }');
		}
		if (_params.theme.tweets.link) {
			etAddCSSRule('.'+this.tweetLinkCSS+' A { text-decoration:none; color:'+_params.theme.tweets.link+' }');
			etAddCSSRule('.'+this.tweetLinkCSS+' A:active { text-decoration:underline; color:'+_params.theme.tweets.link+' }');
			etAddCSSRule('.'+this.tweetLinkCSS+' A:hover { text-decoration:underline; color:'+_params.theme.tweets.link+' }');
		}
	}

	// ガジェット全体
	document.write('<div id="'+this.id+'"></div>');
	this.gadget = document.getElementById(this.id);
	this.gadget.style.width      = this.width+'px';
	this.gadget.style.height     = (this.height+54+30)+'px';
	this.gadget.style.background = _params.theme.shell.background;
	this.gadget.style.color      = _params.theme.shell.color;
	this.gadget.style.margin     = 0;
	this.gadget.style.padding    = 0;
	this.gadget.style.wordWrap   = 'break-word';
	this.gadget.style.position   = 'relative';
	this.gadget.style.display    = 'block';

	// ツイート表示枠
	this.showDiv = document.createElement('div');
	this.showDiv.style.position   = 'absolute';
	this.showDiv.style.left       = 0;
	this.showDiv.style.top        = '54px';
	this.showDiv.style.width      = this.width+'px';
	this.showDiv.style.height     = this.height+'px';
	this.showDiv.style.background = _params.theme.tweets.background;
	this.showDiv.style.overflowY  = this.scrollbar?'auto':'hidden';
	this.gadget.appendChild(this.showDiv);

	// Loadingアイコン
	if (this.loadingicon) {
		this.loadingDiv = document.createElement('div');
		this.loadingDiv.style.position = 'relative';
		this.loadingDiv.style.left     = parseInt((this.width -32)/2)+'px';
		this.loadingDiv.style.top      = parseInt((this.height-32)/2)+'px';
		this.loadingDiv.style.width    = '32px';
		this.loadingDiv.style.height   = '32px';
		this.loadingDiv.style.backgroundColor = '#fff';
		this.loadingDiv.style.backgroundImage = 'url(http://exittunes.com/Scripts/etTwitter_loading.gif)';
		this.showDiv.appendChild(this.loadingDiv);
	}

	// ツイートテキスト
	this.textDiv = document.createElement('div');
	this.textDiv.style.width      = 'auto';
	this.textDiv.style.height     = 'auto';
	this.textDiv.style.color      = _params.theme.tweets.color;
	this.textDiv.style.margin     = 0;
	this.showDiv.appendChild(this.textDiv);

	// ツイート取得コールバック
	var that = this;
	this.callback = function(_obj) {

		var now = parseInt(new Date().getTime() / 1000);

		if (_obj.length > 1) {

			// ユーザーアイコン枠
			var userIconDiv = document.createElement('div');
			userIconDiv.style.position = 'absolute';
			userIconDiv.style.left     = '2px';
			userIconDiv.style.top      = '2px';
			userIconDiv.style.width    = '48px';
			userIconDiv.style.height   = '48px';
			userIconDiv.style.border   = 'solid 1px white';
			that.gadget.appendChild(userIconDiv);
			// ユーザーアイコンイメージ
			var userIdonImg = document.createElement('img');
			userIdonImg.src = _obj[0].user.profile_image_url;
			userIconDiv.appendChild(userIdonImg);

			// ユーザー名
			var userNameDiv = document.createElement('div');
			userNameDiv.style.position = 'absolute';
			userNameDiv.style.left     = '64px';
			userNameDiv.style.top      = '10px';
			userNameDiv.style.fontSize = '12px';
			userNameDiv.innerHTML = _obj[0].user.name;
			that.gadget.appendChild(userNameDiv);

			// ユーザーID
			var userIdDiv = document.createElement('div');
			userIdDiv.style.position = 'absolute';
			userIdDiv.style.left     = '64px';
			userIdDiv.style.top      = '26px';
			userIdDiv.style.fontSize = '20px';
			userIdDiv.className = that.shellLinkCSS;
			userIdDiv.innerHTML = '<a href="http://twitter.com/'+_obj[0].user.screen_name+'" target="_blank">'+_obj[0].user.screen_name+'</a>';
			that.gadget.appendChild(userIdDiv);

			// ツイッターロゴ
			var logoDiv = document.createElement('div');
			logoDiv.style.position = 'absolute';
			logoDiv.style.left     = '5px';
			logoDiv.style.top      = (that.height+52+8)+'px';
			logoDiv.style.width    = '100px';
			logoDiv.style.height   = '19px';
			if (!that.IE6) {
				logoDiv.style.backgroundImage = 'url(http://exittunes.com/Scripts/etTwitter_logo.png)';
			}
			else {
				logoDiv.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="./etTwitter_logo.png", sizingMethod="scale")';
			}
			that.gadget.appendChild(logoDiv);

			// ExitTunes
			var etDiv = document.createElement('div');
			etDiv.style.position = 'absolute';
			etDiv.style.left     = (that.width-100)+'px';
			etDiv.style.top      = (that.height+52+14)+'px';
			etDiv.style.fontSize = '11px';
			etDiv.className = that.shellLinkCSS;
			etDiv.innerHTML = '<a href="http://exittunes.com/" target="_blank">ExitTunes Gadgets</a>';
			that.gadget.appendChild(etDiv);

			// ツイートをぶちこむ
			for (var i=0; i<_obj.length; ++i) {
				// ツイート部分
				var tweetDiv = document.createElement('div');
				tweetDiv.style.fontSize   = '13px';
				tweetDiv.style.lineHeight = '15px';
				tweetDiv.style.padding = '5px 5px 5px 5px';
				tweetDiv.className = that.tweetLinkCSS;
				var t;
				t = _obj[i].text.replace(/(https?:\/\/[a-zA-Z0-9_\.\!\~\*\(\);\/\?\:\@\&\=\+\$,%#-]+)/g,'<a href="$1" target="_blank">$1</a>');
				t = t.replace(/@([a-zA-Z0-9_]+)/g, '@<a href="http://twitter.com/$1" target="_blank">$1</a>');
				t = t.replace(/#([a-zA-Z0-9_]+)/g, '<a href="http://twitter.com/search?q=%23$1" target="_blank">#$1</a>');
				tweetDiv.innerHTML = t;
				that.textDiv.appendChild(tweetDiv);

				// 日付、reply部分
				var dateDiv = document.createElement('div');
				dateDiv.style.fontSize   = '11px';
				dateDiv.style.padding = '0px 5px 5px 5px';
				dateDiv.style.borderBottom = '1px dotted white';
				dateDiv.className = that.tweetLinkCSS;
				var id = _obj[i].user.screen_name;
				t  = '<a href="';
				t += 'http://twitter.com/'+id+'/status/'+_obj[i].id_str;
				t += ' target="_blank">';
				var ago = now - parseInt(new Date(_obj[i].created_at).getTime()/1000);
				if (!ago) {
					var d = _obj[i].created_at.split(' ');
					var d2 = 
					ago = now - parseInt(new Date(d[0]+', '+d[2]+' '+d[1]+' '+d[5]+' '+d[3]+' GMT').getTime() / 1000);
				}
				if (ago < 60) {
					t += parseInt(ago)+'秒前';
				}
				else if (ago < 60*60) {
					t += parseInt(ago/60)+'分前';
				}
				else if (ago < 24*60*60) {
					t += parseInt(ago/(60*60))+'時間前';
				}
				else {
					t += parseInt(ago/(24*60*60))+'日前';
				}
				t += '</a> - <a href="';
				t += 'http://twitter.com/?status=@'+id+'%20';
				t += '" target="_blank">reply<a/>';
				dateDiv.innerHTML = t;
				that.textDiv.appendChild(dateDiv);
			}
			if (that.loadingicon) {
				// Loadingアイコンを消す
				that.showDiv.removeChild(that.loadingDiv);
			}
		}
		else {
			etLoadJSONP(that.url, that.callback);
		}
	};
	etTwitterCallbacks.push(this.callback);

	// ツイート情報の読み込み
	this.url = domain + '/statuses/user_timeline/'+this.userId+'.json';
	this.callback = 'etTwitterCallbacks['+(etTwitterCallbacks.length-1)+']';
	etLoadJSONP(this.url, this.callback);
}

