/*
 *	headline.js
 * 
 *	@auther		A.OG
 *	@history	1.00	2009/03/18		create new
 *				1.50	2009/12/xx		オブジェクト定義型記述採用
 *	@description	
 * 
 */


/*
 *	配列の記述順
 *	1:	表示フラグ（0=非表示、1=表示）
 *	2:	識別ID
 *	3:	掲載日
 *	4:	鮮度保持日（new!マーク表示期間）
 *	5:	隠蔽開始日
 *	6:	表示するhtml（タイトル）
 *	7:	表示するhtml（本文）
 */
var	TopHeadlineItem = new Array(

[	/*
	 *		DUMMY
	 */
	/*	表示フラグ		*/	0,
	/*	識別ID			*/	"dummy",
	/*	掲載日			*/	"2008/12/31",
	/*	鮮度保持期間	*/	"2009/01/01",
	/*	表示期間		*/	"2009/01/01",

	/*	タイトル	*/		"dummy",

	/*	本文	*/
	"dummy"
]

) ;


/*
 *	コンストラクタ。
 *	ヘッドラインクラス。
 *
 *	@param	headline	ヘッドライン配列
 */
function HeadlineItem( info ) {
	var		termFreshness ;
	var		termConceal ;

	this.fShow		= parseInt( info[0], 10 ) ;
	this.ID			= info[1] ;
	this.dateEnter	= info[2] ;
	termFreshness	= info[3] ;
	termConceal		= info[4] ;
	this.htmlTitle	= info[5] ;
	this.htmlBody	= info[6] ;

	if( typeof(termFreshness) != "undefined" && termFreshness.length>0 ) {
		this.fFreshness = !isTimeupByDateTime(termFreshness) ;
	}
	else {
		this.fFreshness = false ;
	}

	if( typeof(termConceal) != "undefined" && termConceal.length>0 ) {
		this.fConceal = isTimeupByDateTime(termConceal) ;
	}
	else {
		this.fConceal = false ;
	}
}

/*
 *	表示内容HTMLを取得する。
 *
 *	@param	parent	ヘッドライン管理クラスインスタンス
 *			force	強制表示フラグ
 *
 *	@return	表示内容HTML
 */
HeadlineItem.prototype.getHtml = function (parent,force) {
	var		msgHTML = "" ;
	var		fForce= false ;
	var		style = parent.style ;

	if( typeof(force) == "undefined" ) {
		/*	条件判別	*/
		fForce = false ;
	}
	else if( force != true ) {
		fForce = false ;
	}
	else {
		fForce = true ;
	}

	if( this.fShow==0 && fForce==false ) {
		/*	表示フラグが0の場合はreturn	*/
		return( "" ) ; 
	}
	if( this.fConceal ) {
		/*	表示期間を超えた場合はreturn	*/
		return( "" ) ; 
	}

	msgHTML += "<p class='" + style + "'>" ;
	if( this.fFreshness ) {
		/*	鮮度保持期間内の場合はNEWマークを付与	*/
		msgHTML += "<img src='./images/shape/new.gif' width='24' height='9' alt='NEW!!' /> " ;
	}
	if( this.htmlTitle.length>0 ) {
		msgHTML += "<span class='title'>※" ;
		msgHTML += this.htmlTitle ;
		if( this.dateEnter.length>0 ) {
			msgHTML += " (" + this.dateEnter + "掲載)" ;
		}
		msgHTML += "</span><br />" ;
	}
	msgHTML += this.htmlBody ;
	msgHTML += "</p>" ;

	return( msgHTML ) ;
}


/*
 *	指定する識別タグが同じかどうか判定する。
 *
 *	@return		TRUE	同じID
 *				FALSE	違うID
 */
HeadlineItem.prototype.isIdenticalID = function ( id ) {
	return( ( id==this.ID ) ? true : false ) ;
}




/*
 *	コンストラクタ。
 *	ヘッドラインクラス。
 *
 *	@param	headline	ヘッドライン配列
 */
function Headline( headline ) {
	this.item = new Array() ;
	for( ii=0; ii<headline.length; ii++ ) {
		this.item.push( new HeadlineItem( headline[ii] ) ) ;
	}

	/*	表示スタイル（ディフォルト）	*/
	this.setDispStyle( "headline" ) ;
}


/*
 *	ヘッドラインデータ配列の有効項目数を返す
 *
 *	@param	headline	ヘッドライン情報配列
 *
 *	@return	有効項目数
 */
Headline.prototype.getAvailableItemCount = function () {
	var		cnt=0 ;
	for( ii=0; ii<this.item.length; ii++ ) {
		if( this.item[ii].fShow==1 && !this.item[ii].fConceal ) {
			cnt++ ;
		}
	}
	return( cnt ) ;
}


/*
 *	デバッグ情報を表示する。
 * 
 * @param	id	ID
 *			ID指定した場合、ID比較して有効・無効情報を追加する。
 */
Headline.prototype.debug = function (id)
{
	var		ii ;
	var		item ;
	var		msgHTML = "" ;

	msgHTML += "<table cellspacing='0' cellpadding='0' border='1'>" ;
	msgHTML += "<caption>" + id + "</caption>" ;
	msgHTML += "<tr><td>no.</td><td>ID</td><td>dateEnter</td><td>htmlTitle</td><td>htmlBody</td></tr>" ;
	for( ii=0; ii<this.item.length; ii++ ) {
		item = this.item[ii] ;
		msgHTML += "<tr>" ;
		msgHTML += sprintf( "<td>%d</td>", ii ) ;

		msgHTML += "<td>" ;
		msgHTML += sprintf( "%s", item.ID ) ;
		if( typeof(id)!="undefined" && item.isIdenticalID(id)==true) {
			msgHTML += "*" ;
		}
		msgHTML += sprintf( "<br />fShow=%d", item.fShow ) ;
		msgHTML += sprintf( "<br />fFreshness=%d", item.fFreshness ) ;
		msgHTML += sprintf( "<br />fConceal=%d", item.fConceal ) ;
		msgHTML += "</td>" ;

		msgHTML += sprintf( "<td>%s</td>", item.dateEnter ) ;
		msgHTML += sprintf( "<td>%s</td>", item.htmlTitle ) ;
		msgHTML += sprintf( "<td>%s</td>", item.htmlBody ) ;
		msgHTML += "</tr>" ;
	}
	msgHTML += "</table>" ;

	document.write( msgHTML ) ;
}



/*
 * 表示スタイルを定義する。
 * 
 * @param	style	表示スタイル名。
 */
Headline.prototype.setDispStyle = function ( style ) {
	this.style = style ;
}


/*
 *	ヘッドライン／緊急のお知らせを表示する
 *	リスト登録された全件に対して日時情報・フラグに基づき表示内容を自動判定する
 */
Headline.prototype.putAuto = function () {
	var		ii ;
	var		msgHTML = "" ;

	if( this.getAvailableItemCount()==0 ) {
		return ;
	}

	for( ii=0; ii<this.item.length; ii++ ) {
		msgHTML += this.item[ii].getHtml(this) ;
	}

	document.write( msgHTML ) ;
}


/*
 *	ヘッドライン／緊急のお知らせを表示する
 *	指定されたタグに該当する項目を表示する。
 *
 *	@param	headline	ヘッドライン情報配列
 *			id			表示対象ID
 *			fForce		強制表示条件
 *						TRUE	強制的に表示する
 *						FLASE	条件を適用して表示する
 */
Headline.prototype.putByID = function (id,fForce)
{
	var		ii ;
	var		fExist = false ;
	var		msgHTML = "" ;
	for( ii=0; ii<this.item.length; ii++ ) {
		if( (this.item[ii].isIdenticalID(id))==false ) {
			continue ;
		}

		if( fExist==true ) {
			alert( sprintf( "ID \"%s\" is already existed!!", id ) ) ;
			break ;
		}
		msgHTML += this.item[ii].getHtml(this,fForce) ;
		fExist = true ;
	}

	document.write( msgHTML ) ;
}




/*
 *	ヘッドライン／緊急のお知らせを表示する
 *	リスト登録された全件に対して日時情報・フラグに基づき表示内容を自動判定する
 *
 *	@param	headline	ヘッドライン情報配列
 */
function putHeadlineAuto(headline) {
	(new Headline(headline)).putAuto() ;
}


/*
 *	指定されたタグに該当するヘッドラインを表示する。
 *
 *	@param	headline	ヘッドライン情報配列
 *			id			表示対象ID
 *			fForce		強制表示条件
 *						TRUE	強制的に表示する
 *						FLASE	条件を適用して表示する
 */
function putHeadlineByID( headline, id, fForce ) {
	(new Headline(headline)).putByID(id, fForce) ;
}


/*
 *	指定されたタグに該当する緊急のお知らせを表示する。
 *
 *	@param	headline		ヘッドライン情報配列
 *			id				表示対象ID
 *			fForce			強制表示条件
 *							TRUE	強制的に表示する
 *							FLASE	条件を適用して表示する
 *			HeadlineArray	緊急お知らせヘッドラインインスタンス配列
 */
function HeadlineArrayItem( headline, id, fForce ) {
	this.headline	= headline ;
	this.id			= id ;
	this.fForce		= fForce ;
}
function putEmergencyNotice( HeadlineArray ) {
	var		ii ;
	for( ii=0; ii<HeadlineArray.length; ii++ ) {
		putEmergencyNoticeItem( HeadlineArray[ii].headline, HeadlineArray[ii].id, HeadlineArray[ii].fForce ) ;
	}
}
function putEmergencyNoticeItem( headline, id, fForce ) {
	var instEmergency = new Headline(headline) ;
	instEmergency.setDispStyle( "additional-remarks" ) ;
	instEmergency.putByID(id, fForce) ;
}
