function init() {
	
	// メニューやその他ボタンのマウスイベント
	set_menu_mouse_event();
	set_btn_mouse_event();

	// ご注意くださいのpopupイベント
	set_notice_popup();

	// リサイクル価格テーブルのハイライト
	$('table.price_table tbody tr').bind('mouseover', function() { price_table_on($(this)) });
	$('table.price_table tbody tr').bind('mouseout', function() { price_table_off($(this)) });

	// お取扱店舗の色設定
	set_color_shop_list();
}


function set_notice_popup() {

	$link = $('div#sidebar ul#gmenu a img#btn_notice').parent();
	$link.bind('click', function() { window.open( '/retanaka/notice/index.html', 'notice', 'width=700, height=620, menubar=no, toolbar=no, lacation=yes, status=yes, resizable=yes, scrollbars=yes'); });


}


/*
 *	サイドメニューのマウスオーバー、マウスアウトイベント
 */
function set_menu_mouse_event() {

	// メニューのボタンを取得
	var menu_imgs = $('div#sidebar ul#gmenu a img');
	
	var ii = 0;
	var menu_img_id = '';
	
	// メニューのボタンの数でループ
	for(ii=0; ii<menu_imgs.length; ii++) {
		
		// ボタンのidを取得
		menu_img_id = $(menu_imgs[ii]).attr('id');

		if(menu_img_id != '') {

			// ボタンのidと同じ値のclassを持っているbodyは存在する
			if($('body.' + menu_img_id.replace('btn_', '')).length == 1) {
				
				// ボタンの画像マウスオーバー時の画像に変更
				img_ov($(menu_imgs[ii]));
				
			// ボタンのidと同じ値のclassを持っているbodyは存在しない
			}else {

				// ボタンにマウスオーバー、マウスアウトイベントを設定
				$('img#' + menu_img_id).hover(
					function () {
					 	img_ov($(this));
					},

					function () {
						img_out($(this));
					}
				);
			}
		}
	}
}


/*
 *	ボタンのマウスオーバー、マウスアウトイベント
 */
function set_btn_mouse_event() {

	// class="onmouse"のボタンにマウスオーバー、マウスアウトイベントを設定
	$('img.' + 'onmouse').hover(
		function () {
		 	img_ov($(this));
		},

		function () {
			img_out($(this));
		}
	);
}


function img_ov(img) {
	var src = img.attr('src');
	var file_type = src.substring(src.lastIndexOf('.'), src.length);
	var src_ov = src.replace(file_type, '_ov' + file_type);
	img.attr('src', src_ov);
}

function img_out(img) {
	var src = img.attr('src');
	var file_type = src.substring(src.lastIndexOf('.'), src.length);
	var src_out = src.replace('_ov' + file_type, file_type);
	img.attr('src', src_out);
}


/*
 *	リサイクル価格のテーブルのハイライト
 */

function price_table_on(row) {
	var table_element = row.parent().parent();
	if(table_element.attr('id') == 'ag_price_table') {
		table_element.children().children().addClass('on');
	}else {
		row.addClass('on');
	}
}

function price_table_off(row) {
	var table_element = row.parent().parent();
	if(table_element.attr('id') == 'ag_price_table') {
		table_element.children().children().removeClass('on');
	}else {
		row.removeClass('on');
	}
}


/*
 *	お取扱店舗の色設定
 */
function set_color_shop_list() {

	shop_info = $('body.shop div.shop_info');
	
	ii = 0;

	for(ii=0;ii<shop_info.length;ii++) {

		if(ii%2 == 0) {
			$(shop_info[ii]).addClass('gray');
		}else {
			$(shop_info[ii]).addClass('white');
		}
	}

}


/*
 *	品種の変更
 */
function change_metal() {

	var metal = $('form#frm_price select[name="slt_metal_code"]').val();

	$.post(	'index.html',
			{ action_id: 'change_metal', slt_metal_code: metal },
			function(jsData) { show_select_grade(jsData); } );
}

function show_select_grade(jsData) {

	
	if(jsData.substring(0, 1) == '{') {

		var data = eval('('+jsData+')');

		// 品位名称リスト
		$('table#metal_type td#select_grade').empty();

		if(data.grade_codes) {

			var grade_name_list = data.grade_names.split(',');
			var grade_code_list = data.grade_codes.split(',');
			var options = '';
			
			for(var ii=0;ii<grade_name_list.length;ii++) {
				options += '<option value="' + grade_code_list[ii] + '">' + grade_name_list[ii] + '</option>\n';
			}
			
			$('table#metal_type td#select_grade').append('<select id="slt_grade" name="slt_grade" onchange="simulate();"><option value="">選択してください</option>' + options + '</select>');

		}else {
			$('table#metal_type td#select_grade').html('-');
		}

		$('table#calc_result td#recycle_price').html('&yen;-');
		$('table#calc_result td#buyback_price').html('&yen;-');

	}
}


/*
 *	お買取価格のシミュレーション
 */
function simulate() {

	var metal = $('form#frm_price select[name="slt_metal_code"]').val();
	var grade = $('form#frm_price select[name="slt_grade"]').val();
	var weight = $('form#frm_price input[name="txt_weight"]').val();

	if(!grade) {
		grade = '';
	}

	$.post(	'index.html',
			{ action_id: 'simulate', slt_metal_code: metal, slt_grade: grade, txt_weight: weight },
			function(jsData) { display_price(grade, jsData); } );
}

function display_price(grade, jsData) {

	if(jsData.substring(0, 1) == '{') {

		$('table#calc_result td#weight p.notice').remove();

		var data = eval('('+jsData+')');

		if(data.grade_code) {

			// 1gあたりのリサイクル価格の出力
			$('table#calc_result td#recycle_price').html('&yen;' + number_format(data.price));

			// エラーが発生していない場合
			if(!data.error_messages) {

				var buyback_price = '';

				if(data.weight) {
					buyback_price = number_format(data.price * data.weight);
				}else {
					buyback_price = '-';
				}

				$('table#calc_result td#buyback_price').html('&yen;' + buyback_price);

			}
		
		}else {

			$('table#calc_result td#recycle_price').html('&yen;-');
			$('table#calc_result td#buyback_price').html('&yen;-');
		
		}
		
		// エラーが発生している場合
		if(data.error_messages) {
			$('table#calc_result td#weight').append('<p class="notice">' + data.error_messages + '</p>');
		}
	}
}

function number_format(num) {
	return num.toString().replace( /([0-9]+?)(?=(?:[0-9]{3})+$)/g , '$1,' );
}

function clear_simulation() {
	$('table#metal_type td#select_metal select').val('');
	$('table#metal_type td#select_grade').html('-');
	$('table#calc_result td#recycle_price').html('&yen;-');
	$('table#calc_result td#weight input').val('');
	$('table#calc_result td#weight p.notice').remove();
	$('table#calc_result td#buyback_price').html('&yen;-');
}

function submitStop(e){
	if (!e) {
		var e = window.event;
	}

	if(e.keyCode == 13) {
		return false;
	}
}