var link = get_link_path();
var lang = "";

var top_category = null;

//最上位カテゴリ取得
for( idx=0; idx<category_num; idx++ ) {
	category = category_list[ idx ];

	if( category[ "parentid" ] == 0 ) {
		top_category = category;
		break;
	}
}

/**
 *
 */
function write_navi_menu() {
	var idx=0;

	var category = null;

	var href = "";
	var img = "";

	href = "";
	href += link;
	if( lang ) {
		href += lang + "/";
	}

	document.write( '			<table border="0" cellpadding="0" cellspacing="0" width="170">' + "\n" );
	document.write( '				<tr>' + "\n" );
	document.write( '				<td><img name="mnavi01" src="' + href + 'product/img/mnavi/mnavi01.gif" width="170" height="22" border="0" alt=""></td>' + "\n" );
	document.write( '				</tr>' + "\n" );

	//表示カテゴリ取得
	for( idx=0; idx<category_num; idx++ ) {
		category = category_list[ idx ];

		if( category[ "parentid" ] == top_category[ "categoryid" ] ) {
			//関連製品が存在するかチェック
			if( in_product( category[ "categoryid" ] ) ) {
				img = href;
				img += "gnavi/category_" + str_pad_left( category[ "categoryid" ], 3, "0" ) + ".html";

				document.write( '				<tr>' + "\n" );
				document.write( '				<td><div class="lmn_title1"><p><a href="' + img + '">' + category[ "naviname" ] + '</a></p></div></td>' + "\n" );
				document.write( '				</tr>' + "\n" );
				document.write( '				<tr>' + "\n" );
				document.write( '				<td><img src="' + link + 'img/lmn_title1_bordergif.gif" width="170" height="3"></td>' + "\n" );
				document.write( '				<tr>' + "\n" );
			}
		}
	}


	document.write( '				<tr>' + "\n" );
/*	document.write( '				<td><img name="mnavi05" src="img/mnavi/mnavi05.gif" width="170" height="8" border="0"></td>' + "\n" );*/
	document.write( '				</tr>' + "\n" );
	document.write( '			</table>' + "\n" );
}

/**
 *
 */
function write_category_menu() {
	var idx=0;

	var category = null;
	var href = "";
	var img = "";

	href = "";
	href += link;
	if( lang ) {
		href += lang + "/";
	}

	document.write( '<div id="box-products">' + "\n" );

	document.write( '			<table width="170" border="0" cellspacing="0" cellpadding="0">' + "\n" );
	document.write( '				<tr>' + "\n" );
	document.write( '				<td bgcolor="#999999">' + "\n" );
	document.write( '					<div align="center">' + "\n" );
	document.write( '					<table width="170" border="0" cellspacing="1" cellpadding="0">' + "\n" );
	document.write( '						<tr>' + "\n" );
	document.write( '						<td bgcolor="#FFCC66"><img src="' + href + 'product/img/lmn_t01.gif" width="168" height="19" alt=""></td>' + "\n" );
	document.write( '						</tr>' + "\n" );

	//表示カテゴリ取得
	for( idx=0; idx<category_num; idx++ ) {
		category = category_list[ idx ];

		if( category[ "parentid" ] == top_category[ "categoryid" ] ) {
			//関連製品が存在するかチェック
			if( in_product( category[ "categoryid" ] ) ) {
				img = href;
				img += "gnavi/category_" + str_pad_left( category[ "categoryid" ], 3, "0" ) + ".html";

				document.write( '						<tr>' + "\n" );
				document.write( '						<td bgcolor="#DCDEE3"><div class="lmn_title2"><a href="' + img + '">' + category[ "name" ] + '</a></div></td>' + "\n" );
				document.write( '						</tr>' + "\n" );
			}
		}
	}



	document.write( '					</table>' + "\n" );
	document.write( '					</div>' + "\n" );
	document.write( '				</td>' + "\n" );
	document.write( '				</tr>' + "\n" );
	document.write( '			</table>' + "\n" );
}

function in_product( categoryid ) {
	var idx=0;

	var category = null;
	var product = null;

	var id_list = null;
	var parentid = 0;
	var i;

	for( idx=0; idx<product_num; idx++ ) {
		product = product_list[ idx ];

		id_list = product[ "categoryid" ];

		for( i=1; i<id_list.length; i++ ) {
			parentid = id_list[ i ];
			do {
				if( parentid == categoryid ) {
					return( true );
				}

				//親カテゴリをチェック
				parentid = get_parent( parentid );
			} while( parentid > 0 );
		}
	}
	return( false );
}

function get_parent( categoryid ) {
	var idx=0;

	var category = null;

	//対象カテゴリ取得
	for( idx=0; idx<category_num; idx++ ) {
		category = category_list[ idx ];

		if( category[ "categoryid" ] == categoryid ) {
			return( category[ "parentid" ] );
		}
	}

	return( 0 );
}


function write_product_menu() {
	var idx=0;

	document.write( '<div id="box-products">' + "\n" );
	document.write( '<h3><a href="' + link + 'products/index.html"><img src="' + link + 'products/img/lnavi_h2.gif" alt="製品一覧" width="154" height="36" border="0" /></a></h3>' + "\n" );
	document.write( '<ul>' + "\n" );

	var category = null;
	var html = "";
	var href = "";

	for( idx=0; idx<category_num; idx++ ) {
		category = category_list[ idx ];

		href = "";
		href += link;
		href += "products/";
		href += "category_" + str_pad_left( category[ "categoryid" ], 3, "0" ) + ".html";

		html = '';
		html += '<li>';
		html += '<a href="';
		html += href;
		html += '">';
		html += category[ "name" ];
		html += '</a>';
		html += '</li>';
		html += "\n";

		document.write( html );
	}

	document.write( '</ul>' + "\n" );
	document.write( '</div>' + "\n" );
}

/**
 *
 */
function get_link_path() {
//	var reg_host = new RegExp( "^http[s]*://test.ipros.net/~kirintechno/", "i" );
//本番用
	var reg_host = new RegExp( "^http[s]*://www.kirintechno.co.jp/", "i" );

	var link = location.href.substring( 0, location.href.lastIndexOf( "/" ) +1 );
	link = link.replace( reg_host, "" );

	var reg = null;
	var from = 0;
	var pos = 0;
	var end = false;

	do {
		pos = link.indexOf( "/", from );
		if( pos + 1 == link.length ) {
			end = true;
		}

		reg = new RegExp( link.substring( from, pos ), "i" );
		link = link.replace( reg, ".." );

		from += 3;
	} while( !end );

	return( link );
}

/**
 *
 */
function str_pad_left( input, pad_length, pad_string ) {
	var idx=0;

	input = String( input );

	if( pad_string.length > 1 ) {
		pad_string = pad_string.charAt( 0 );
	}

	for( idx=0; idx<=( pad_length - input.length ); idx++ ) {
		input = pad_string + input;
	}

	return( input );
}

