/*
 *	コンストラクタ。
 *
 *	@param	info	メニュー要素。
 */
function MenuBarItem( info ) {
	var		DEF_MENUBAR_FLAG_SHOW		= 1<<0 ;	/*	表示フラグ	*/
	var		DEF_MENUBAR_FLAG_NEW_WIN	= 1<<1 ;	/*	新規ウィンドウオープンフラグ	*/
	var		data ;
	data = info.split(",") ;

	this.flag	= parseInt(data[0],2) ;	/*	動作定義フラグ	*/
	this.word	= data[1] ;		/*	表示文字列	*/
	this.url	= data[2] ;		/*	対応URL	*/
	this.title	= (data[3]!="") ? data[3] : this.word ;	/*	タイトル文字列	*/

	if( ((getBrowserCode())&DEF_BROWSER_TYPE_IE)==0 ) {
		/*
		 *	IE以外では、close()を無効にする
		 *	Firefoxでスクリプトが動作しない？ため。
		 *	暫定処置としてスクリプト表示を無効にする。要調査。
		 */
		if( this.url == "javascript:window.close()" ) {
			this.flag &= parseInt( "1110", 2 ) ;
		}
	}
}



/*
 *	メニュー要素を表示する。
 * 
 *	@param	fLink	リンク指定。
 *					<code>true</code>の場合、urlへのリンクとして表示する。<br />
 *					<code>false</code>の場合、リンク表示しない。<br />
 */
MenuBarItem.prototype.getMenubarItemHtml = function ( fLink ) {
	var		word="" ;

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

	/* リンク指定が与えられない場合の処理 */
	if( typeof(fLink) == "undefined" ) {
		fLink = true ;
	}

	if( fLink ) {
		/* リンクとして表示 */
		word += "<a href='" + this.url + "' title='" + this.title + "'"  ;
		word += ( this.isNewWindow()!=0 ) ? " target='_blank'" : "" ;	/* 画面切り替え？ */
		word += ">" + this.word + "</a>"  ;
	}
	else {
		/* 非リンクとして表示 */
		word += "<span>" + this.word + "</span>"  ;
	}

	return( word ) ;
}


/*
 *	デバッグ情報文字列を取得する。
 *
 *	@param	no		制御番号。
 *
 *			fLink	リンク指定。
 *					<code>true</code>の場合、urlへのリンクとして表示する。<br />
 *					<code>false</code>の場合、リンク表示しない。<br />
 */
MenuBarItem.prototype.getMenubarItemDebugHtml = function ( no, fLink ) {
	var		word="" ;
	var		jump="" ;
	var		style = "width:auto;text-align:left;padding:2px;" ;
	var		styleC = "width:auto;text-align:center;padding:2px;" ;

	word += "<tr>" ;

	word += sprintf( "<td style='%s'>%d</td>", styleC, no ) ;		/*	制御番号	*/

	word += sprintf( "<td style='%s'>%s</td>", styleC, ( this.isShow() ? "&nbsp;" : "*" ) )  ;/* 表示しない？ */
	word += sprintf( "<td style='%s'>%s</td>", styleC, ( this.isNewWindow() ? "*" : "&nbsp;" ) )  ;/* 画面切り替え？ */
	word += sprintf( "<td style='%s'>%04b</td>", style, this.flag)  ;		/*	動作定義フラグ値	*/
	word += sprintf( "<td style='%s'>%s</td>", style, this.word )  ;		/*	表示文字列	*/

	if( fLink ) {
		/* リンクとして表示 */
		word += sprintf( "<td style='%s'>", style )  ;			/*	対応URL	*/
		word += sprintf( "<a href='%s' title='%s'%s>", this.url, this.title, ( (this.isNewWindow()) ? " target='_blank'" : "" ) ) ;
		word += sprintf( "%s</a>", this.url ) ;
	}
	else {
		word += sprintf( "<td style='%s;color:#555555;'>", style )  ;			/*	対応URL	*/
		word += sprintf( "%s", this.url )  ;		/*	対応URL	*/
	}
		word += "</td>" ;


	word += sprintf( "<td style='%s'>%s</td>", style, this.title )  ;		/*	タイトル文字列	*/
	word += "</tr>" ;

	return( word ) ;
}


/*
 *	表示フラグの状態を返す。
 *
 *	@return	表示する場合は<code>true</code>、表示しない場合は<code>false</code>を返す。
 */
MenuBarItem.prototype.isShow = function () {
	return( (this.flag&this.DEF_MENUBAR_FLAG_SHOW!=0) ? true : false ) ;
}


/*
 *	フラグを変更する（OR演算）。
 *
 *	@param	newFlag	。
 */
MenuBarItem.prototype.bitOrFlag = function ( newFlag ) {
	this.flag |= parseInt(newFlag,2) ;
}

/*
 *	新規ウィンドウで開くかどうかの状態を返す。
 *
 *	@return	新規ウィンドウで開く場合は<code>true</code>、ウィンドウを切り替える場合は<code>false</code>を返す。
 */
MenuBarItem.prototype.isNewWindow = function () {
	return( ((this.flag&this.DEF_MENUBAR_FLAG_NEW_WIN)!=0) ? true : false) ;
}




var		DEF_MENUBAR_STYLE_RIGHT		= 0		/*	右寄せ		*/
var		DEF_MENUBAR_STYLE_CENTER	= 1		/*	中央寄せ	*/
var		DEF_MENUBAR_STYLE_LEFT		= 2		/*	左寄せ		*/



/*
 *	コンストラクタ。
 *	メニューバークラス。
 *
 *	@param	info	メニュー要素。
 */
function MenuBar( info ) {
	var		ii ;


	this.item = new Array() ;
	for( ii=0; ii<info.length; ii++ ) {
		this.item[ii] = new MenuBarItem( info[ii] ) ;
	}
	this.num = ii ;

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

	/*	表示位置（ディフォルト：左寄せ）	*/
	this.showPosition = DEF_MENUBAR_STYLE_LEFT ;

	/*	区切り指定（ディフォルト：あり） 	*/
	this.delimit = true ;

	/*
	 *	現在位置（ナビ要素番号）
	 *	指定番号に一致する番号の要素は非リンク表示
	 */
	this.assignTracking = 0 ;
	this.fAssignTracking = true ;
}


/*
 *	現在位置追従フラグ設定。
 */
MenuBar.prototype.setCurrentAssignTracking = function () {
	this.fAssignTracking = true ;
}
MenuBar.prototype.resetCurrentAssignTracking = function () {
	this.fAssignTracking = false ;
}


/*
 *	表示スタイルを定義する。
 *
 *	@param	style	メニューバーを表示するスタイルのスタイル名。
 */
MenuBar.prototype.setShowStyle = function ( style ) {
	this.style = style ;
}


/*
 *	区切り指定を設定する。
 *
 *	@param	delimit	区切り指定。
 *					<code>true</code>は、区切り文字を挿入する。<br />
 *					<code>false</code>は、区切り文字を挿入しない。<br />
 */
MenuBar.prototype.setDelimit = function ( delimit ) {
	if( delimit==true || delimit==false ) {
		this.delimit = delimit ;
	}
}


/*
 *	表示位置を定義する。
 *
 *	@param	position	メニューバーを表示する位置。
 *						DEF_MENUBAR_STYLE_RIGHT		右寄せ
 *						DEF_MENUBAR_STYLE_CENTER	中央寄せ
 *						DEF_MENUBAR_STYLE_LEFT		左寄せ
 */
MenuBar.prototype.setShowPosition = function ( position ) {
	switch( position ) {
	case DEF_MENUBAR_STYLE_RIGHT:	/*	右寄せ		*/
	case DEF_MENUBAR_STYLE_CENTER:	/*	中央寄せ	*/
	case DEF_MENUBAR_STYLE_LEFT:	/*	左寄せ		*/
		this.showPosition = position ;
		break ;
	}
}


/*
 *	ニューバーを表示する。
 *
 *	@param	assign_no	現在位置（ナビ要素番号）。
 */
MenuBar.prototype.put = function ( assign_no ) {
	document.write( this.getMenubarHtml(assign_no) ) ;
}
MenuBar.prototype.getMenubarHtml = function ( assign_no ) {
	var		html="" ;
	var		ii ;
	var		fCheckNotShow=FALSE ;

	if( this.countAvailableItem()==0 ) {
		/*	有効リンクがない場合はそのままreturn	*/
		return( html ) ;
	}

	/*	現在位置設定	*/
	if( typeof(assign_no)!="undefined" || assign_no ) {
		/*	フラグ=TRUEの場合は、現在位置を再設定	*/
		this.assignTracking = ( this.fAssignTracking ) ? assign_no : -1 ;
	}

	html += "<table class='" + this.style + "' cellspacing='0' cellpadding='0' border='0'><tr>" ;

	html += "<td style='text-align:" ;
	switch( this.showPosition ) {
	case DEF_MENUBAR_STYLE_RIGHT:	html += "right;'" ;		break ;	/*	右寄せ		*/
	case DEF_MENUBAR_STYLE_CENTER:	html += "center;'" ;	break ;	/*	中央寄せ	*/
	case DEF_MENUBAR_STYLE_LEFT:
	default:						html += "left;'" ;		break ;	/*	左寄せ		*/
	}
	html += ">" ;

	if( this.delimit ) {
		html += "|" ;
	}

	for( ii=0; ii<this.num; ii++ ) {
		if( !this.item[ii].isShow() ) {
			if( this.fAssignTracking&&ii==this.assignTracking ) {
				/*	非表示項目の場合、現在位置を１つ進める	*/
				this.assignTracking++ ;
			}
			continue ;
		}

		html += this.item[ii].getMenubarItemHtml( ((this.fAssignTracking&&ii==this.assignTracking)?false:true) ) ;
		if( this.delimit ) {
			html += "|" ;
		}
	}
	html += "</td></tr></table>" ;


	if( this.fAssignTracking ) {
		this.assignTracking++ ;
	}

	return( html ) ;
}


/*
 *		有効なリンク数を計数する。
 */
MenuBar.prototype.countAvailableItem = function () {
	var		cnt=0 ;
	var		ii ;
	for( ii=0; ii<this.num; ii++ ) {
		if( this.item[ii].isShow() ) {
			cnt++ ;
		}
	}
	return( cnt ) ;
}


/*
 *		デバッグモードで表示する。
 *
 *	@param	assign_no	現在位置（ナビ要素番号）。
 */
MenuBar.prototype.debug = function ( assign_no ) {
	var		html="" ;
	var		ii ;
	var		fLink ;
	var		unlink_no = -1 ;

	/* 非リンク番号指定が与えられない場合の処理 */
	if( typeof(assign_no) == "undefined" ) {
		unlink_no = -1 ;
	}

	html += "<table class='" + this.style + "' cellspacing='0' cellpadding='0' border='1' style='width:auto;'>" ;
	html += "<caption>debug mode</caption>" ;
	html += "<tr><th>no</th><th>link</th><th>switch</th>" ;
	html += "<th>flag</th><th>html</th><th>URL</th><th>title</th></tr>" ;
	for( ii=0; ii<this.num; ii++ ) {
		fLink = ( ii==unlink_no ) ? false : true ;
		html += this.item[ii].getMenubarItemDebugHtml(ii, fLink) ;
	}
	html += "</table>" ;

	document.write( html ) ;
}

