/* --------------------------------------------------------------------------------
 *
 *		更新情報ライブラリ
 *
 *		※※注意！！※※
 *		本日のＸＸ，掲示板他のスクリプトを読み込んでいる、という前提になっているため、
 *		以下のスクリプトは他のスクリプト群の後に読み込むこと。
 *		順番も下記の通りとする
 *
 *		whatsnew.js
 *		whatsnew/whatsnew_data.js
 *
 *
 *		Copyright(c) 2004-2008 Atsushi Oguma  <aog206rc@qj9.so-net.ne.jp>
 *		OG-Works
 *		3-11-8-203 Tsumada-kita, Atsugi-shi, Kanagawa-ken 243-0812, Japan
 *
 *		Copyright(c) 2004-2008 Atsushi Oguma
 *		Panorama snow academy ski school
 *		6666-703 Fujimi, Fujimi-cho, Suwa-gun, Nagano-ken 399-0211, Japan
 *
 * -------------------------------------------------------------------------------- */


var	DEF_KEEP_FRESHNESS_TERM			= 7 ;	/*	ネタ鮮度保持期間：ディフォルト＝７日	*/



 /*
 *	コンストラクタ。
 *	個別ニュースクラス。
 *
 *	@param	info	個別ニュース項目文字列。
 */
function WhatsNewItem( info ) {
	var		data ;
	var		ii ;
	data = info.split( "," ) ;

	this.isShow	= (data[0]==1 ) ? true : false ;
	this.isLink	= (data[1]==1 ) ? true : false ;
	this.isNewWin	= (data[2]==1 ) ? true : false ;

	this.date = new Date( data[3] ) ;
	this.url = data[4] ;

	this.word = new Array() ;
	this.word[0] = data[5] ;
	this.word[1] = data[6] ;
	this.word[2] = data[7] ;

	if( data[6] ) {
		this.tip = data[6] ;
	}
	else {
		this.tip = "" ;
		for( ii=0; ii<this.word.length; ii++ ) {
			this.tip += this.word[ii] ;
		}
	}
}


/*
 *	オブジェクトのデバッグ情報を表示する。
 */
WhatsNewItem.prototype.debug = function () {
	var		info="" ;
	var		ii ;
	
	info += sprintf( "date : %04d/%02d/%02d\n", this.date.getYear(), this.date.getMonth()+1, this.date.getDate() ) ;

	info += "url [" + this.url + "]\n" ;
	info += "tip [" + this.tip + "]\n" ;

	for( ii=0; ii<this.word.length; ii++ ) {
		info += "  word" + ii + "[ = " ;
		info += this.word[ii] + "]\n" ;
	}
	info += "\n" ;

	alert( info ) ;
}


/*
 *	ネタ新鮮状態を返す。
 *
 *	@return	新鮮な場合は<code>true</code>、鮮度が落ちた場合は<code>false</code>を返す。
 */
WhatsNewItem.prototype.isFreshness = function () {
	var		ret ;
	/* ネタ鮮度保持期間を超えたらFALSE 期間内ならTRUE */
	ret = ( Math.floor((getTodayInst()).getTime()/DEF_COEF_UTC_DAY) > Math.floor(this.date.getTime()/DEF_COEF_UTC_DAY)+DEF_KEEP_FRESHNESS_TERM ) ? false : true ;
	return( ret ) ;
}


/*
 *	ニュース項目を表示する。
 *
 *	@param		fHideNewMark	情報鮮度"NEW"マークの表示指定。
 *								true	表示する。
 *								false	表示しない。
 */
WhatsNewItem.prototype.getWhatsNewItemHTML = function (fHideNewMark) {
	var		target_str ;
	var		date ;
	var		msg="" ;

	/* 表示しない？ */
	if ( !this.isShow ) {
		return( msg ) ;
	}

	if ( typeof(fHideNewMark)=="undefined" ) {
		fHideNewMark = false ;
	}

	if( this.tip=="" ) {
		this.tip = this.word ;
	}

	msg += "<tr>" ;

	/*	情報鮮度	*/
	if( fHideNewMark ) {
		msg += "<td class='new'>" ;
		msg += ( this.isFreshness()==true ) ?
			"<img src='./images/shape/new.gif' width='24' height='9' alt='NEW!!' />" :
			"&nbsp;" ;
		msg += "</td>" ;
	}

	/*	掲載日時	*/
	msg += sprintf( "<td class='date'>%04d/%02d/%02d</td>", this.date.getFullYear(), this.date.getMonth()+1, this.date.getDate() ) ;


	/*	記事	*/
	msg += "<td class='comment'>" ;
	if( this.word[0] ) {	msg += this.word[0] ;	}

	if( this.isLink && this.url ) {
		msg += sprintf( "<a href='%s' title='%s' target='%s'>%s</a>", this.url, this.tip, (this.isNewWin?"_blank":"_top"), this.word[1]  ) ;
	}
	else {
		msg += this.word[1] ;
	}

	if( this.word[2] ) {	msg += this.word[2] ;	}
	msg += "</td>" ;


	msg += "</tr>" ;

	return( msg ) ;
}


/*
 *	デバッグ情報文字列を取得する。
 */
WhatsNewItem.prototype.getWhatsNewItemDebugHTML = function (id) {
	var		date ;
	var		word="" ;
	var		ii ;


	word += "<tr>" ;
	word += sprintf( "<td rowspan='3'>%3d</td>", (typeof(id)=="undefined"?-1:id) ) ;

	/*	掲載日時	*/
	word += sprintf( "<td rowspan='3'>%s %04d/%02d/%02d</td>", (this.isFreshness())?"*":"&nbsp;", this.date.getFullYear(), this.date.getMonth()+1, this.date.getDate() ) ;

	/*	URL	*/
	if( this.url ) {
		word += sprintf( "<td rowspan='3'>%s</td>", (this.isNewWin?"*":"&nbsp;") ) ;

		word += "<td rowspan='3'>" ;
		word += sprintf( "<a href='%s' title='%s' target='%s'>", this.url, this.tip, (this.isNewWin?"_blank":"_top") ) ;
		word += this.url + "</a>" ;
		word += "</td>" ;
	}
	else {
		word += "<td rowspan='3'>&nbsp;</td><td rowspan='3'>&nbsp;</td>" ;
	}

	/*	表示文字列	*/
	word += sprintf( "<td>0</td><td>%s</td>", (this.word[0])?this.word[0]:"&nbsp;" ) ;
	word += "</tr>" ;


	/*	表示文字列	*/
	for( ii=1; ii<this.word.length; ii++ ) {
		word += sprintf( "<tr><td>%d</td><td>%s</td></tr>", ii, (this.word[ii])?this.word[ii]:"&nbsp;" ) ;
	}


	return( word ) ;
}






/*
 *	コンストラクタ。
 *	ニュース一覧クラス。
 *
 *	@param	info	ニュース項目文字列の配列。
 */
function WhatsNew( newsInfo ) {
	var		ii ;
	var		cnt=0 ;

	this.item = new Array() ;
	for( ii=0; ii<newsInfo.length; ii++ ) {
		if( (new WhatsNewItem( newsInfo[ii] )).isShow!=true ) {
			/*	非表示	*/
			continue ;
		}

		this.item[cnt] = new WhatsNewItem( newsInfo[ii] ) ;
		cnt++ ;
	}

	/*
	 *	表示ページインデックス
	 *	0=1ページ目
	 */
	this.pageIndex = 0 ;

	/*	表示スタイル（ディフォルト）	*/
	this.style = "whatsnew" ;

	/*	HTML表示用DocID	*/
	this.docID = "" ;

	/*	項目の日付ソート	*/
	this.sortByDate() ;

	/*	１ページあたりの表示項目数	*/
	this.itemNumOfPage = 5 ;
}


/*
 *	全項目を表示するのに必要なページ数を取得する。
 *
 *	@return		全項目を表示するのに必要なページ数。
 */
WhatsNew.prototype.getTotalPageNum = function () {
	return( Math.ceil(this.item.length/this.itemNumOfPage) ) ;
}


/*
 *	１ページあたりの表示項目数を設定する。
 *
 *	@param		num		表示する１ページあたりの項目数。
 */
WhatsNew.prototype.setItemNumOfPage = function ( num ) {
	this.itemNumOfPage = num  ;
}


/*
 *	表示するページインデックスを設定する。
 *
 *	@param		index		表示するページインデックス。
 */
WhatsNew.prototype.setPageIndex = function ( index ) {
	this.pageIndex = index  ;
}


/*
 *	HTMLを表示する文書IDを設定する。
 *
 *	@param		id	表示するページインデックス。
 */
WhatsNew.prototype.setDocId = function ( id ) {
	this.docID = id  ;
}


/*
 *	新着情報HTMLを表示する。
 */
WhatsNew.prototype.putFullItem = function () {
	document.write( this.getWhatsNewHTMLFull() ) ;
}
WhatsNew.prototype.putByDocID = function () {
	document.getElementById(this.docID).innerHTML = this.getWhatsNewHTML() ;
}


/*
 *	新着情報表示用HTMLを取得する。（表示ページ単位）
 */
WhatsNew.prototype.getWhatsNewHTML = function () {
	var	ii ;
	var	msg = "" ;
	var	naviHtml = "" ;
	var	fFreshExist = false ;

	/*	表示ページが複数ある場合はリンク情報を追加	*/
	naviHtml = this.getWhatsNewNaviHTML() ;

	start	= this.pageIndex * this.itemNumOfPage ;
	end		= start + this.itemNumOfPage ;
	if ( start<0 ) { start = 0 ; }
	if ( end>=this.item.length ) { end = this.item.length ; }


	fHideNewMark = this.isFreshNewsExist( start, end ) ;
	msg += "<table class='" + this.style + "' cellspacing='0' cellpadding='0' border='0'>" ;
	for( ii=start; ii<end; ii++ ) {
		msg += this.item[ii].getWhatsNewItemHTML(fHideNewMark) ;
	}
	msg += "</table>" ;

	/*	表示ページが複数ある場合はリンク情報を追加	*/
	msg += naviHtml ;


	return( msg ) ;
}


/*
 *	指定範囲内に新着情報があるか確認する。
 *
 *	@param		start, end		確認範囲id
 *
 *	@return		確認結果
 *				true		あり。
 *				false		なし。
 */
WhatsNew.prototype.isFreshNewsExist = function ( start, end ) {
	var	ii ;
	var	numFreshness = 0 ;

	for( ii=start; ii<end; ii++ ) {
		if ( !this.item[ii].isShow ) {
			continue ;
		}
		if( this.item[ii].isFreshness()==true ) {
			numFreshness++ ;
		}
	}

	return( (numFreshness>0) ? true : false ) ;
}




/*
 *	新着情報表示用HTMLを取得する。（全項目）
 */
WhatsNew.prototype.getWhatsNewHTMLFull = function () {
	var	ii ;
	var	msg = "" ;
	msg += "<table class='" + this.style + "' cellspacing='0' cellpadding='0' border='0'>" ;
	for( ii=0; ii<this.item.length; ii++ ) {
		msg += this.item[ii].getWhatsNewItemHTML(true) ;
	}
	msg += "</table>" ;
	return( msg ) ;
}


/*
 *	新着情報表示用HTMLを取得／表示する。
 *	新着情報HTMLを表示する。
 */
WhatsNew.prototype.getWhatsNewNaviHTML = function () {
	var	naviHtml = "" ;

	if( this.getTotalPageNum()<=1 ) {
		return(naviHtml) ;
	}

	/*	表示ページが複数ある場合はリンク情報を追加	*/
	naviHtml += "<p class='whatsnew-navi'>" ;

	/*	<<	*/
	if( this.pageIndex>0 ) {
		naviHtml += "<a href='javascript:decrementPageIndex();'>&lt;&lt;</a>" ;
	}
	else {
		naviHtml += "<span class='unlink'>&lt;&lt;</span>" ;
	}

	/*	page navi	*/
	for( ii=0; ii<this.getTotalPageNum(); ii++ ) {
		if( ii==this.pageIndex ) {
			naviHtml += sprintf( "<span class='current'>%d</span>", ii+1 ) ;
		}
		else {
			naviHtml += sprintf( "<a href='javascript:setPageIndex(%d);'>%d</a>", ii, ii+1 )  ;
		}
	}

	/*	>>	*/
	if( this.pageIndex<this.getTotalPageNum()-1 ) {
		naviHtml += "<a href='javascript:incrementPageIndex();'>&gt;&gt;</a>" ;
	}
	else {
		naviHtml += "<span class='unlink'>&gt;&gt;></span>" ;
	}
	naviHtml += "</p>" ;

	return( naviHtml ) ;
}


/*
 *	記事を日付順に並び替える。
 */
WhatsNew.prototype.sortByDate = function () {
	var		ii, jj ;
	var		dt1;
	var		dt2;
	var		item ;

	for( ii=this.item.length-1; ii>=0; ii-- ) {
		dt1 = this.item[ii] ;
		for( jj=this.item.length-1; jj>=ii; jj-- ) {
			dt2 = this.item[jj] ;

			if( dt2.date.getTime() >= dt1.date.getTime() ) {
				item = this.item[ii] ;
				this.item[ii] = this.item[jj] ;
				this.item[jj] = item ;
			}
		}
	}
}


/*
 *	更新最新日時を取得する。
 */
WhatsNew.prototype.getLatestDate = function () {
	var		ii, jj ;
	var		dt1 ;
	var		latest ;
	var		latest_index ;
	var		msg=""
	var		date ;


	for( ii=0; ii<this.item.length; ii++ ) {
		if( !this.item[ii].isShow ) {
			continue ;
		}
		latest_index = ii ;
		latest = this.item[ii].date.getTime() ;
		break ;
	}

	/* 最新日時比較 */
	for( ii=1; ii<this.item.length; ii++ ) {
		if( !this.item[ii].isShow ) {
			continue ;
		}
		dt1 = this.item[ii].date.getTime() ;

		if( dt1 > latest ) {
			latest_index = ii ;
			latest = dt1 ;
		}
	}

	date = this.item[ latest_index ].date ;
	return	sprintf( "%4d/%02d/%02d", date.getYear(), date.getMonth()+1, date.getDate() ) ;
}


/*
 *	デバッグモードで表示する。
 */
WhatsNew.prototype.debug = function ( unlink_no ) {
	var		word="" ;

	word += "<a name='_debug' id='_debug'></a>" ;
	word += sprintf( "<p>total page=%d  page index=%d  item=%d</p>", this.getTotalPageNum(), this.pageIndex, this.item.length ) ;
	word += "<table class='" + this.style + "' cellspacing='0' cellpadding='0' border='1' style='width:auto;'>" ;
	word += "<tr><th>id</th><th>date</th><th>new win</th><th>URL</th><th>word no</th><th>word</th></tr>" ;
	for( ii=0; ii<this.item.length; ii++ ) {
		word += this.item[ii].getWhatsNewItemDebugHTML(ii) ;
	}
	word += "</table>" ;

	document.write( word ) ;
	document.location = "#_debug" ;
}


/*
 *	ページインデックスを更新する。
 */
function setPageIndex(index) {
	var		inst = getInst() ;
	if( index<0 ) {
		index = 0 ;
	}
	if( index > inst.getTotalPageNum()-1 ) {
		index = inst.getTotalPageNum()-1 ;
	}
	inst.setPageIndex(index) ;
	document.getElementById(inst.docID).innerHTML = inst.getWhatsNewHTML() ;
}


/*
 *	ページインデックスをインクリメントする。
 */
function incrementPageIndex() {
	var		inst = getInst() ;
	var		index = inst.pageIndex ;
	index++ ;
	if( index > inst.getTotalPageNum()-1 ) {
		index = inst.getTotalPageNum()-1 ;
	}
	inst.setPageIndex(index) ;
	document.getElementById(inst.docID).innerHTML = inst.getWhatsNewHTML() ;
}


/*
 *	ページインデックスをデクリメントする。
 */
function decrementPageIndex() {
	var		inst = getInst() ;
	var		index = inst.pageIndex ;
	index-- ;
	if( index < 0 ) {
		index = 0 ;
	}
	inst.setPageIndex(index) ;
	document.getElementById(inst.docID).innerHTML = inst.getWhatsNewHTML() ;
}


