var LANG_LIST = ['None','日本語','English','French','German','Spanish'];

function getIconURL( _uid, _size )
{
	if( !_size ) _size = 'b';

	if( _uid != null && _uid.length > 0 )
		return 'http://img.tweetimag.es/i/' + _uid + '_' + _size;
	else
		return 'http://img.tweetimag.es/i/Twiccha_' + _size;
}
////////////////////////////////
function jsonURIDecode( obj )
{
	if( typeof obj == 'string' )
		return decodeURIComponent( obj );
	else
		jQuery.each( obj, function( key, value )
		{
			if( typeof value == 'string' )
				obj[key] = decodeURIComponent( value );
		});

	return obj;
}

//フォロー状態チェックのキャッシュデータ用
var TwFriendshipsCache = {};

//フォロー状態チェックの標準引数
var TwFriendshipsOpts = {
	 "url": 'http://twitter.com/friendships/show.json?callback=?'
	,"type":'GET'
	,"dataType":'json'
	,"async": false //同期通信
	,"cache": true
	,"pageCache" : true
	,"beforeSend":function(xhr)
	{
		//キャッシュを調べる
		var now = new Date();
		var ckey = '_' + xhr.data.source_screen_name + '_' + xhr.data.target_screen_name;
		var ckey2 = String(now.getYear()) + String(now.getMonth()) + String(now.getDate()) + String(now.getHours()) + String(now.getMinutes());

		//分単位のキャッシュ動作をさせるためにパラメータを追加
		xhr.data.k = ckey2;
		this._time = now;

		if( TwFriendshipsCache[ ckey ] && TwFriendshipsCache[ ckey ][ ckey2 ] )
		{
			//appendMessage("キャッシュヒット:" + ckey +"."+ ckey2 );
			return false;
		}
		return true;
	}
	,dataFilter: function(data)
	{
		var ckey = '_' + this.data.source_screen_name + '_' + this.data.target_screen_name;
		var ckey2 =  this.data.k;

		if( !data )
		{
			//キャッシュデータを探して使う
			if( TwFriendshipsCache[ ckey ] && TwFriendshipsCache[ ckey ][ ckey2 ] )
			{
				//appendMessage("キャッシュ取得:" + ckey +"."+ ckey2 );
				return TwFriendshipsCache[ ckey ][ ckey2 ];
			}
		}
		else
		{
			//データがある場合キャッシュ化
			var tmp = {};
			tmp[ ckey2 ] = data;

			if( TwFriendshipsCache[ckey] )
			{
				TwFriendshipsCache[ ckey ] = {}; //対象ユーザーのキャッシュをクリア
			}
			TwFriendshipsCache[ ckey ] = tmp; //キャッシュ化
		}
		return data;
	}
}

//フォロワー同士か？
function isFollower( owner_id, target_id )
{
	var swfobj = jQuery('#Twiccha');

	var opts = {
		"data":{
			 "source_screen_name": owner_id
			,"target_screen_name": target_id
		}
		,"success": function(jsondata, status, xhr)
		{
			if( jsondata.relationship )
			{
				if( jsondata.relationship.target.following && jsondata.relationship.source.following )
				{
					//相互フォロー状態
					if( swfobj.length && swfobj.get(0) )
						swfobj.get(0).okFollowed();
					return true;
				}
				else
				{
					//相互フォロー状態ではない
					if( swfobj.length && swfobj.get(0) )
						swfobj.get(0).ngFollowed();
					return false;
				}
			}
			else
			{
				//twitterとの通信エラーが発生
				if( swfobj.length && swfobj.get(0) )
					swfobj.get(0).errorFollowed(status);
			}
		}
	};

	doCheckFollow( opts );
}

//ターゲットをフォローしているか？
function isFollowing( obj, status )
{
	//フォローしているかチェック
	if( obj.relationship && obj.relationship.source.following )
	{
		//フォロー済みアラート表示
		return true;
	}
	else
	{
		//フォロー許可アラート表示
		return false;
	}
}

//ターゲットにフォローされているか？
function isFollowed( my_id, target_id )
{
	isFollowing( target_id, my_id );
}

//ターゲットをフォローしているか？
function doCheckFollow( opts )
{
	opts = jQuery.extend( {}, TwFriendshipsOpts, opts );
	jQuery.jsonp( opts );

	//obj.relationship.source.following
	//obj.relationship.source.followed
	/*
	jQuery('<h1/>').data('opts',opts).delay(function()
	{
		var opts = jQuery(this).data('opts');
		jQuery.jsonp( opts );
	});
	jQuery.resume(1000);
	*/

}

//入室コマンド発行
function roomin( _roomId )
{
	// todo:エラーの場合、入れないと伝える
	jQuery.ajax(
	{
		 url: '/send.php'
		,data:{
			  'command':'roomin'
			 ,'roomId':_roomId
		}
		,dataType: 'text'
		,cache: false
		,success: function( data, status )
		{
			location.href = '/' + data;
			return false;
		}
	});
	return false;
}

String.prototype.trim = function() {
	var _str = this;
	var length = 0;
	do {
		length = _str.length;
		// 先頭、末尾の半角空白文字の削除
		_str = _str.replace(/^[ ]+|[ ]+$/g, '');
		// 先頭、末尾の全角空白文字の削除
		_str = _str.replace(/^[　]+|[　]+$/g, '');
		// 先頭、末尾の改行コードの削除
		_str = _str.replace(/^[\r\n]+|[\r\n]+$/g, '');
		// 先頭、末尾のNULL文字の削除
		_str = _str.replace(/^[\u0000-\u001f]+|[\u0000-\u001f]+$/g, '');
	} while (length!=_str.length)
 
	return _str;
}

