/*=================================================================*/
/* 「小細工」を表示するロジック                                    */
/*    2003/10/22 written by ReiFukami(paming__@hotmail.com)        */
/*                                                                 */
/* このロジックは、別ファイルで設定されたデータ(words)を、         */
/* ランダムに選択し、HTMLに出力する機能を実装している。            */
/*                                                                 */
/* 参考URL: http://tohoho.wakusei.ne.jp/js/index.htm               */
/*=================================================================*/


/*-----------------------------------------------------------------*/
/* ランダムに１つを選択して、表示                                  */
/*-----------------------------------------------------------------*/
function includeWords(){

	/* 乱数生成 */
	var index = Math.floor( Math.random() * words.length );

	/* 極々まれに Math.random()が1を返した時の処理 */
	if( index == words.length ){
		index = words.length - 1;
	}

	/* データが未定義の場合は何もしない */
	if( _isUndefinedWords( index ) ){
		return;
	}

	/* htmlの生成 */
	var html = _buildHTML( index );

	/* 生成したhtmlの表示 */
	document.write( html );
}

/*-----------------------------------------------------------------*/
/* データすべてを表示                                              */
/*-----------------------------------------------------------------*/
function includeWordsList(){

	/* データすべてについて、繰り返す */
	for( index=words.length-1; 0<=index; index-- ){
		
		/* もしデータが未定義なら、飛ばす */
		if( _isUndefinedWords( index ) ){
			continue;
		}

		/* htmlを生成して、表示 */
		var html = _buildHTML( index );
		document.write( html );
		document.write( "\n<br><br><br>\n" );
	}
}

/*-----------------------------------------------------------------*/
/* データの項目すべてを表示(デバック用)                            */
/*-----------------------------------------------------------------*/
function includeWordsDebug(){

	var html = "<table border=1><tr><th>インデックス番号</th><th>文章</th>"+
				"<th>リンクの名前</th><th>リンク先URL</th><th>サンプル</th></tr>";
	for( index=0; index<words.length; index++ ){

		if( _isUndefinedWords( index ) ){
			_errorMsgUndefined( index );
			continue;
		}

		var word     = _replaceNullStringToKara( words[index][0] );
		var linkname = _replaceNullStringToKara( words[index][1] );
		var linkurl  = _replaceNullStringToKara( words[index][2] );
		var sample   = _buildHTML( index );
		html += "<tr>";
		html += "<td>" + index    + "</td>";
		html += "<td>" + word     + "</td>";
		html += "<td>" + linkname + "</td>";
		html += "<td>" + linkurl  + "</td>";
		html += "<td>" + sample   + "</td>";
		html += "</tr>";
	}
	html += "</table>";
	document.write( html );
}

/*-----------------------------------------------------------------*/
/* ユーティリティ: 指定された番号のデータから、htmlを生成          */
/*-----------------------------------------------------------------*/
function _buildHTML( index ){

	/* undefinedを""に置き換え */
	words[index][0] = _replaceUndefinedToZeroString( words[index][0] );
	words[index][1] = _replaceUndefinedToZeroString( words[index][1] );
	words[index][2] = _replaceUndefinedToZeroString( words[index][2] );
		

	/* 文章は無条件に出す */
	var html = words[index][0] + '<br>\n';

	/* もし、リンクの名前があれば、"source:xxxx"を表示 */
	if( words[index][1] != "" ){

		/*               ↓CSSを使用 */
		html += '<font class="inside_words_source">source:';

		/* もし、リンク先があれば、リンクを張る(aタグをつける) */
		/* リンク先が無ければ、文字だけ出す。                  */
		if( words[index][2] != "" ){
			html += '<a href="' + words[index][2] + '">' + words[index][1] + '</a>';
		}else{
			html += words[index][1];
		}
		html += '</font>';
	}
	return html;
}


/*-----------------------------------------------------------------*/
/* ユーティリティ: 指定されたインデックス番号が定義済みかどうか    */
/*-----------------------------------------------------------------*/
function _isUndefinedWords( index ){
	var ret = false;
	if( words[index] == undefined ){
		ret = true;
	}
	return ret;
}

/*-----------------------------------------------------------------*/
/* ユーティリティ: 空文字ならば"空"を返す                          */
/*                 そうでなければ受け取った値を返す                */
/*-----------------------------------------------------------------*/
function _replaceNullStringToKara( value ){
	var ret = value;
	if( ( ret == undefined )||
		( ret == null ) ||
		( ret == "" ) ){
		ret = "(空)";
	}
	return ret;
}

/*-----------------------------------------------------------------*/
/* ユーティリティ: undefinedを""に変換                             */
/*                 そうでなければ受け取った値を返す                */
/*-----------------------------------------------------------------*/
function _replaceUndefinedToZeroString( value ){
	var ret = value;
	if( ( ret == undefined )||
		( ret == null ) ){
		ret = "";
	}
	return ret;
}

/*-----------------------------------------------------------------*/
/* エラーメッセージ: 未定義                                        */
/*-----------------------------------------------------------------*/
function _errorMsgUndefined( index ){
	alert( "word_sizeが正しいか,またはインデックス番号に抜けが無いかチェック!\n インデックス番号="+index );
}
