// *********************************************
//   記事オブジェクト
// *********************************************

function Entry( newEntry )
{
	// -----------------------------------------------------------
	// メンバ
	
	this.id      = newEntry.id;
	this.x_index = newEntry.x_index;
	this.y_index = newEntry.y_index;
	this.z_index = newEntry.z_index;
	this.width   = newEntry.width;
	this.height  = newEntry.height;
	this.date    = newEntry.date;
	this.message = newEntry.message;
	this.status  = newEntry.status;
	
	this.display    = display;
	this.move       = move;
	this.changeSize = changeSize;
	this.popup      = popup;
	
	
	// -----------------------------------------------------------
	// コンストラクタ
	
	var emptyEntry  = document.getElementById( "contents" );
	
	if ( newEntry == null || newEntry == undefined )
	{
		alert( 'やばいよ BBS バグってるバグってる！' );
		return;
	}
	
	this.display();
	
	emptyEntry.innerHTML += '\n<span id="entry_' + Number( this.id + 1 ) + '"></span>';
	
	
	// -----------------------------------------------------------
	// 表示
	
	function display ()
	{
		var output      = document.getElementById( "entry_" + this.id );
		var htmlBuf     = "";
		
		var taSize      = getTextareaSize( this.width, this.height );
		
		var dateScaled  = this.date.toLocaleString();
		    dateScaled  = dateScaled.replace( /:[0-9]{2}$/g, '' );
		    dateScaled  = dateScaled.replace( /(年|月)/g, '/' );
		    dateScaled  = dateScaled.replace( /日/g, '' );
		    dateScaled += ':';
		
		
		// -----------------------------------------------------------
		// テーブル枠
		htmlBuf += '<table width='  + this.width  + '  ';
		htmlBuf +=        'height=' + this.height + '  ';
		htmlBuf +=        'id="'    + this.id     + '" ';
		htmlBuf +=        'style=\'position: absolute; ';
		htmlBuf +=                 'left: '    + this.x_index + 'px; ';
		htmlBuf +=                 'top: '     + this.y_index + 'px; ';
		htmlBuf +=                 'z-index: ' + this.z_index + '\'';
		htmlBuf +=                 '>';
		htmlBuf += '	<tr>';
		htmlBuf += '		<td id=top' + this.id;
		htmlBuf +=            ' valign=top align=left width=' + ( this.width - 16 ) + '>';
		
		
		// -----------------------------------------------------------
		// エントリの記事
		htmlBuf += '			<div class=top>' + dateScaled + '</div>';
		htmlBuf += '		</td>';
		htmlBuf += '		<td colspan=2 valign=top align=right width=16';
		htmlBuf +=                                             ' onClick="dropEntry( ' + this.id + ', true );">';
		htmlBuf += '			<div class=icon>x</div>';
		htmlBuf += '		</td>';
		htmlBuf += '	</tr>';
		htmlBuf += '	<tr valign=top align=left>';
		htmlBuf += '		<td colspan=3>';
		
		htmlBuf += '			<textarea cols=' + taSize.cols + ' rows=' + taSize.rows;
		htmlBuf +=                      ' id=textarea' + this.id;
		htmlBuf +=                      ' onChange="writeMessage(' + this.id + ')"';
		htmlBuf +=                      ' style="overflow:auto;">' + this.message + '</textarea>';
		
		
		// -----------------------------------------------------------
		// テーブル枠
		htmlBuf += '		</td>';		htmlBuf += '	</tr>';		htmlBuf += '	<tr>';
		htmlBuf += '		<td>';
		htmlBuf += '		</td>';		htmlBuf += '		<td width=16>';
		htmlBuf += '		</td>';		htmlBuf += '		<td height=8 width=5 bgcolor=000000>';
		htmlBuf += '		</td>';		htmlBuf += '	</tr>';
		htmlBuf += '</table>';
		
		
		// -----------------------------------------------------------
		// 画面出力
		output.    innerHTML = htmlBuf;
		
		
		// -----------------------------------------------------------
		// z-index は明示的に保持しておく ( innerHTML にて書き込んだ
		// タグの場合、style 属性に追加した .zIndex が取得出来ない為。
		// 全ブラウザ共通仕様。)
		document.getElementById('entry_' + this.id ).style.zIndex = this.z_index;
	}
	
	
	
	// -----------------------------------------------------------
	// 記事をドラッグで移動
	function move( moveX, moveY )
	{
		this.x_index += moveX;
		this.y_index += moveY;
		
		
		if ( this.x_index < 0 )  { this.x_index = 0; }
		if ( this.y_index < 0 )  { this.y_index = 0; }
		
		document.getElementById( this.id ).style.left = this.x_index;
		document.getElementById( this.id ).style.top  = this.y_index;
	}
	
	
	
	// -----------------------------------------------------------
	// 記事をドラッグで大きさの変更
	function changeSize( moveX, moveY )
	{
		this.width   += moveX;
		this.height  += moveY;
		
		var taSize = getTextareaSize( this.width, this.height );
		
		
		if ( this.width  < 210 )  { this.width  = 210; }
		if ( this.height < 150 )  { this.height = 150; }
		
		document.getElementById( this.id ).width  = this.width;
		document.getElementById( this.id ).height = this.height;
		
		
		// ---------------------------------------------
		// FireFox だと、何故か <object>.height が
		// 反映されない罠なので、重いけどウィンドウを再描画
		if ( !window.createPopup )  { this.display(); }
		
		// ---------------------------------------------
		// 再描画していない IE 用の続き
		else
		{
			document.getElementById( 'textarea' + this.id ).rows = taSize.rows;
			document.getElementById( 'textarea' + this.id ).cols = taSize.cols;
			
			document.getElementById( 'top' + this.id ).width = this.width - 25;
		}		
	}
	
	
	
	// -----------------------------------------------------------
	// 記事を画面の最上部（奥行き）に表示
	function popup()
	{
		var z_indexMax = getZ_indexMax();
		
		// -----------------------------------------------------------
		// z-index を変更して画面に反映
		if ( this.z_index != z_indexMax )
		{
			this.z_index = z_indexMax + 1;
			this.display();
		}
	}
}
