var widthO = [1000,1200,1600,2000,2400];
var widthY = [1200,1600,2000,2400,2800,3200,3600,4000,4500,5000];
var widthVariations = {
	'G1':widthO, 'G2':widthO, 'G3':widthO, 'G4':widthO, // orgon
	'YG':widthY, 'YW':widthY }; // yokota

var heightO = [1000,1400,1800,2200,2600,3000,3400,3800,4200,4600,5000];
var heightY = [1000,1400,1800,2200,2600,3000];
var heightVariations = {
	'G1':heightO, 'G2':heightO, 'G3':heightO, 'G4':heightO, // orgon
	'YG':heightY, 'YW':heightY }; // yokota

var minWidth = {
	'G1':400, 'G2':400, 'G3':400, 'G4':400, // orgon
	'YG':250, 'YW':250 }; // yokota
var maxWidth = {
	'G1':2400, 'G2':2400, 'G3':2400, 'G4':2400, // orgon
	'YG':5000, 'YW':5000 }; // yokota
	
var minHeight = {
	'G1':400, 'G2':400, 'G3':400, 'G4':400, // orgon
	'YG':400, 'YW':400 }; // yokota
var maxHeight = {
	'G1':3400, 'G2':3400, 'G3':3400, 'G4':3400, // orgon
	'YG':3000, 'YW':3000 }; // yokota

var minFloor = {
	'G1':1000, 'G2':1000, 'G3':1000, 'G4':1000, // orgon
	'YG':1000, 'YW':1000 }; // yokota
var maxFloor = {
	'G1':3000, 'G2':3000, 'G3':3000, 'G4':3000, // orgon
	'YG':3000, 'YW':3000 }; // yokota
	
var CALL = -1;
var NG = -2;

// GROUP-1　バランスなし
var price1N = [
	[8700,9400,11400,12700,14900], // 40～100
	[9800,10100,12400,14600,18000], // 101～140
	[10400,11000,13600,16300,19600], // 141～180
	[10800,11900,14500,18200,21700], // 181～220
	[11400,12600,15200,19800,24000], // 221～260
	[12000,12900,16800,20400,25100], // 261～300
	[13400,17300,21000,25600,31000], // 301～340
	[14700,18300,23600,29800,34900], // 341～380
	[16000,19100,25300,32000,37700], // 381～420
	[17300,20100,27000,33800,40300], // 421～460
	[18500,21000,29000,36000,42000]]; // 461～500

// GROUP-1　バランス付き
var price1Y = [
	[9700,10400,12400,13700,15900], // 40～100
	[10800,11100,13400,15600,19000], // 101～140
	[11400,12000,14600,17300,20600], // 141～180
	[11800,12900,15500,19200,22700], // 181～220
	[12400,13600,16200,20800,25000], // 221～260
	[13000,13900,17800,21400,26100], // 261～300
	[14400,18300,22000,26600,32000], // 301～340
	[15700,19300,24600,30800,35900], // 341～380
	[17000,20100,26300,33000,38700], // 381～420
	[18300,21100,28000,34800,41300], // 421～460
	[19500,22000,30000,37000,43000]]; // 461～500

// GROUP-2　バランスなし
var price2N = [
	[9000,9700,12000,14400,17100], // 40～100
	[10300,10800,13900,17300,20600], // 101～140
	[11000,11900,16300,20100,24300], // 141～180
	[11600,13700,18400,22800,27500], // 181～220
	[12800,15300,21000,26000,31300], // 221～260
	[14400,17000,23300,28600,34600], // 261～300
	[15400,18600,25500,31500,37500], // 301～340
	[16700,20200,27800,34100,40900], // 341～380
	[18600,21900,29600,37200,44000], // 381～420
	[20000,23800,31900,39800,47200], // 421～460
	[21000,25500,33900,42300,49900]]; // 461～500

// GROUP-2　バランス付き
var price2Y = [
	[10000,10700,13000,15400,18100], // 40～100
	[11300,11800,14900,18300,21600], // 101～140
	[12000,12900,17300,21100,25300], // 141～180
	[12600,14700,19400,23800,28500], // 181～220
	[13800,16300,22000,27000,32300], // 221～260
	[15400,18000,24300,29600,35600], // 261～300
	[16400,19600,26500,32500,38500], // 301～340
	[17700,21200,28800,35100,41900], // 341～380
	[19600,22900,30600,38200,45000], // 381～420
	[21000,24800,32900,40800,48200], // 421～460
	[22000,26500,34900,43300,50900]]; // 461～500

// GROUP-3　バランスなし
var price3N = [
	[10000,11300,13800,17200,20500], // 40～100
	[11300,12700,16900,20900,24500], // 101～140
	[12400,14900,19700,24600,29400], // 141～180
	[14300,17000,22600,28300,34100], // 181～220
	[16000,19600,26000,29000,35300], // 221～260
	[18100,21800,29200,35900,43000], // 261～300
	[19900,23800,32000,39500,47600], // 301～340
	[22000,26000,34900,43500,51800], // 341～380
	[23700,28400,38000,47000,55400], // 381～420
	[25500,30700,40900,50200,59700], // 421～460
	[27400,33200,43900,54000,64500]]; // 461～500

// GROUP-3　バランス付き
var price3Y = [
	[11000,12300,14800,18200,21500], // 40～100
	[12300,13700,17900,21900,25500], // 101～140
	[13400,15900,20700,25600,30400], // 141～180
	[15300,18000,23600,29300,35100], // 181～220
	[17000,20600,27000,30000,36300], // 221～260
	[19100,22800,30200,36900,44000], // 261～300
	[20900,24800,33000,40500,48600], // 301～340
	[23000,27000,35900,44500,52800], // 341～380
	[24700,29400,39000,48000,56400], // 381～420
	[26500,31700,41900,51200,60700], // 421～460
	[28400,34200,44900,55000,65500]]; // 461～500

// GROUP-4　バランスなし
var price4N = [
	[10500,11700,15000,18600,22200], // 40～100
	[11600,14100,18400,22800,27100], // 101～140
	[13500,16400,41800,27200,32700], // 141～180
	[10800,19200,25500,31300,37800], // 181～220
	[18000,21400,28700,35700,43100], // 221～260
	[20400,24000,32300,40100,48500], // 261～300
	[22500,26800,35600,44600,50300], // 301～340
	[24800,29500,39400,49000,58600], // 341～380
	[27000,32000,43000,53400,64000], // 381～420
	[29700,34900,46100,57900,69600], // 421～460
	[31000,37400,49600,62000,74700]]; // 461～500

// GROUP-4　バランス付き
var price4Y = [
	[11500,12700,16000,19600,23200], // 40～100
	[12600,15100,19400,23800,28100], // 101～140
	[14500,17400,42800,28200,33700], // 141～180
	[11800,20200,26500,32300,38800], // 181～220
	[19000,22400,29700,36700,44100], // 221～260
	[21400,25000,33300,41100,49500], // 261～300
	[23500,27800,36600,45600,51300], // 301～340
	[25800,30500,40400,50000,59600], // 341～380
	[28000,33000,44000,54400,65000], // 381～420
	[30700,35900,47100,58900,70600], // 421～460
	[32000,38400,50600,63000,75700]]; // 461～500
	
var priceYG89 = [
	[21700,21700,21700,21700,21700,23100,26000,28900,32600,36200], // -1000
	[21700,21700,21700,24300,28400,32400,36500,40500,45600,50700], // -1400
	[21700,21700,26000,31200,36500,41700,46900,52100,58600,65200], // -1800
	[21700,25500,31800,38200,44600,51000,57300,63700,71700,79600], // -2200
	[22600,30100,37600,45200,52700,60200,67800,75300,84700,94100], // -2600
	[26000,34700,43400,52100,60800,69500,78200,86900,97800,108600]]; // -3000

var priceYW89 = [
	[17400,21600,26000,29600,33800,38000,42400,46600,52200,57800], // -1000
	[18900,23800,28900,33400,38300,43200,48600,53300,59800,66200], // -1400
	[20600,26400,32400,37600,43400,49400,55200,60700,68100,75400], // -1800
	[22300,29000,35900,41800,48700,55200,61800,68300,76600,84800], // -2200
	[24000,31600,39400,46400,53700,61100,68700,75900,85100,94100], // -2600
	[25600,34200,43100,50600,58700,66900,75100,83000,93100,103100]]; // -3000

var priceYW127 = [
	[16400,20500,24700,28200,32100,36100,40300,44200,49600,54900], // -1000
	[17900,22600,27500,31700,36300,41000,46100,50700,56800,62900], // -1400
	[19500,25000,30800,35700,41200,46900,52400,57700,64600,71600], // -1800
	[21100,27600,34100,39700,46200,52500,58700,64900,72700,80500], // -2200
	[22800,30000,37400,44000,51000,58100,65200,72000,80700,89400], // -2600
	[24400,32500,40900,48000,55800,63500,71300,78100,88400,97900]]; // -3000

var priceList = {
	'G1_N_89':price1N, 'G1_Y_89':price1Y, 'G1_N_127':price1N, 'G1_Y_127':price1Y,
	'G2_N_89':price2N, 'G2_Y_89':price2Y, 'G2_N_127':price2N, 'G2_Y_127':price2Y,
	'G3_N_89':price3N, 'G3_Y_89':price3Y, 'G3_N_127':price3N, 'G3_Y_127':price3Y,
	'G4_N_89':price4N, 'G4_Y_89':price4Y, 'G4_N_127':price4N, 'G4_Y_127':price4Y,
	'YG_S_89':priceYG89, 'YG_S_127':priceYG89,
	'YW_S_89':priceYW89, 'YW_S_127':priceYW127
};

var colorTypeList = {
	'G1':color1, 'G2':color2, 'G3':color3, 'G4':color4,
	'YG':colorYG, 'YW':colorYW };

var labels = {
	'group':['バーチカルブラインドタイプ', {
		'G1':'オルゴン スタンダード',
		'G2':'GROUP-2',
		'G3':'オルゴン デザイン',
		'G4':'GROUP-4',
		'YG':'国産遮光ブラインド Glass',
		'YW':'国産遮光ブラインド Waffle'
	}],
	'productType':['製品タイプ', {
		'H':'ハープ',
		'HCP':'ハープ コード/ポール',
		'OC':'ワンチェーン'
	}],
	'colorType':['生地の種類', {}],
	'louverWidth':['ルーバーの幅', {}],
	'railColor':['レイルカラー', {
		'white':'ホワイト',
		'silver':'シルバー'
	}],
	'balanceYN':['バランスの有無', {
		'S':'標準仕様',
		'Y':'バランス付き',
		'N':'バランスなし'
	}],
	'handleType':['操作タイプと納まり位置', {
		'BLR':'バトン式・左操作・右納まり',
		'BRL':'バトン式・右操作・左納まり',
		'2RR':'2コード式・右操作・右納まり',
		'2RL':'2コード式・右操作・左納まり',
		'2LL':'2コード式・左操作・左納まり',
		'2LR':'2コード式・左操作・右納まり',
		'2RW':'2コード式・右操作・両納まり',
		'2LW':'2コード式・左操作・両納まり',
		'RR':'右操作・右納まり',
		'RL':'右操作・左納まり',
		'RB':'右操作・両側納まり',
		'RC':'右操作・中央納まり',
		'LR':'左操作・右納まり',
		'LL':'左操作・左納まり',
		'LB':'左操作・両側納まり',
		'LC':'左操作・中央納まり'
	}],
	'attachStyle':['窓枠への取付方法', {
		'std':'標準仕様',
		'ceiling':'天井',
		'wall':'正面'
	}],
	'orderWidth':['製品サイズ幅(ミリ)', {}],
	'orderHeight':['製品サイズ丈(ミリ)', {}],
	'floorHeight':['床より本体上部高(ミリ)', {
		'u1500':'1500mm以下',
		'o3000':'3000mm以上'
	}]
};

var pids = {
	'G1':'VOPL01', 'G2':'VOPL01', 'G3':'VOPT01', 'G4':'VOPT01', // orgon
	'YG':'VYG901', 'YW':'VYW901' }; // yokota
	
function getGroup() { return $('#group').val(); }
function getProductType() { return $('#productType').val(); }
function getColorType() { return $('#colorType').val(); }
function getLouverWidth() { return $('#louverWidth').val(); }
function getHandleType() { return $('#handleType').val(); }
function getBalanceYN() { return $('#balanceYN').val(); }
function getAttachStyle() { return $('#attachStyle').val(); }
function getOrderWidth() { return $('#orderWidth').val(); }
function getOrderHeight() { return $('#orderHeight').val(); }
function getFloorHeight() { return $('#floorHeight').val(); }
function getNumItems() { return $('#numItems').val(); }

var validations = [
	['required', '#group', 'バーチカルブラインドの種類を選択してください。'],
	['required', '#productType', '製品タイプを選択してください。'],
	['required', '#colorType', '生地の種類を選択してください。'],
	['required', '#louverWidth', 'ルーバーの幅を選択してください。'],
	['required', '#balanceYN', 'バランスの有無を選択してください。'],
	['required', '#handleType', '操作タイプと納まり位置を選択してください。'],
	['required', '#attachStyle', '窓枠への取付方法を選択してください。'],
	['required', '#orderWidth', '製品サイズ・幅を指定してください。'],
	['required', '#orderHeight', '製品サイズ・丈を指定してください。'],
	['required', '#floorHeight', '床上高を指定してください。'],
	['f', validateFloorHeight]];

function getMtxPos(w, h) {
	var t = getGroup();
	if (!t || !w || !h) return false;
	
	var hv = heightVariations[t];
	var wv = widthVariations[t];
	for (var i = 0; i < hv.length; i++) {
		if (h <= hv[i]) {
			for (var j = 0; j < wv.length; j++) {
				if (w <= wv[j]) return [i, j];
			}
			return false;
		}
	}
	return false;
}

function getUnitPrice() {
	if (!isReadyToCalculatePrice()) return false;
	
	var pos = getMtxPos(getOrderWidth(), getOrderHeight());
	if (!pos) return false;
	
	var priceTable = getGroup() +'_'+ getBalanceYN() +'_'+ getLouverWidth();
	console.log('priceTable='+ priceTable);
	
	var groupPrices = priceList[priceTable];
	return groupPrices[pos[0]][pos[1]];
}

function getPrice() {
	var unitPrice = getUnitPrice();
	if (!unitPrice) return false;
	
	var price = unitPrice * (
		$('#numItems').size() > 0 ? Number($('#numItems').val()) : 1);
	
	return price;
}

function isReadyToCalculatePrice() {
	return (
		getGroup() != ''
		&& getLouverWidth() != ''
		&& getBalanceYN() != ''
		&& getOrderHeight() != ''
		&& getOrderWidth() != '');
}

function doUpdatePrice() {
	console.log('doUpdatePrice()');
	
	var changeIt = function (s) {
		$('#estimatePrice').text(s);
		$('#estimateTotalPrice').text(s);
		$('#estimateShipping').text(s == '―' ? '―' : '0');
	};
	
	if (!isReadyToCalculatePrice()) {
		changeIt('―');
		showCallme(false);
		return;
	}

	var priceLabel = getPrice() == -1 ? "―" : addComma(getPrice());
	console.log('priceLabel='+ priceLabel);
	changeIt(priceLabel);
	
	if (getPrice() == -1) {
		$('#callme').text('この寸法での価格については、お問い合わせください。');
		$('#intoCart').attr('disabled', 'disabled');
		showCallme(true);
	}
	else {
		$('#intoCart').removeAttr('disabled');
		showCallme(false);
	}
}

function showCallme(b) {
	var o = $('#callme');
	if (o.css('display') == 'none') {
		if (b) o.show('normal');
	}
	else {
		if (!b) o.hide('normal');
	}
}

function changeColorType(v) {
	var o = $('#colorType');
	o.empty();
	
	if (getGroup() == '') {
		o.append(newOption('', '↑ 先にバーチカルブラインドの種類をお選びください'));
		return;
	}
	
	var s = colorTypeList[getGroup()];
	if (s.length > 1) {
		o.append(newOption('', '▽ ここをクリックしてお選びください'));
	}
	for (var i = 0; i < s.length; i++) {
		o.append(newOption(s[i][0], s[i][1], v));
	}
}

function hideSample(o) {
	var d = $(o).parent().find('.sample');
	if (d.css('display') != 'none') {
		d.hide('normal');
	}
}

function newOption(v, t, d) {
	var o = document.createElement('option');
	if (v == d) o.setAttribute('selected', 'selected');
	return $(o).val(v).text(t);
}

function addComma(value) {
	var s = String(value);
	for (var i = 0; i < s.length / 3; i++) {
		s = s.replace(/^([+-]?\d+)(\d\d\d)/, "$1,$2");
	}
	return s;
}

function validateFloorHeight() {
	if (getFloorHeight() == 'o3000') return true;
	
	var floor = getFloorHeight() == 'u1500' ? 1500 : Number(getFloorHeight());
	var order = Number(getOrderHeight());
	if (floor < order) {
		alert('床上高は製品サイズ・丈よりも大きい値を指定してください。');
		$('#floorHeight').get(0).focus();
		return false;
	}
	return true;
}

function validateChooser() {
	for (var i = 0; i < validations.length; i++) {
		var entry = validations[i];
		
		if (entry[0] == 'required') {
			if (!required(entry[1], entry[2])) return false;
		}
		else if (entry[0] == 'f') {
			if (!entry[1]()) return false;
		}
	}
	return true;
}

function required(id, msg) {
	var v = $(id).val();
	if (!v || v == '') {
		alert(msg);
		$(id).parent().ScrollTo(800, 'easeout');
		setTimeout(function () { $(id).get(0).focus(); }, 1000);
		return false;
	}
	return true;
}

function showDetail(o) {
	var d = $(o).parent().parent().find('.detail');
	if (!d) return;
	
	if (d.css('display') == 'none') {
		d.show('normal');
	}
	else {
		d.hide('normal');
	}
}

function onClickIntoCart() {
	doUpdatePrice();
	if (!validateChooser()) return false;
	
	document.cartForm.PID.value = pids[getGroup()];
	document.cartForm.TANKA.value = getUnitPrice();
	document.cartForm.QTY.value = document.chooserForm.numItems.value;
	
	removeOrderOptions();
	
	var cf = $('#cartForm');
	var options = []
	for (var i = 0; i < document.chooserForm.length; i++) {
		var o = document.chooserForm[i];
		if (o.type == 'button') continue;
		if (!labels[o.name]) continue;
		
		var key = labels[o.name][0];
		var value = labels[o.name][1][o.value];
		if (!value) value = o.value;

		if (o.name == 'orderWidth') {
			key = labels['orderWidth'][0] +'×'+ labels['orderHeight'][0];
			value = getOrderWidth() +'×'+ getOrderHeight();
		}
		if (o.name == 'orderHeight') continue;
		
		if (isYokota()) {
			if (o.name == 'balanceYN') continue;
		}
		else {
			if (o.name == 'productType'
				|| o.name == 'railColor') continue;
		}

		options.push(key +': '+ value);
	}
	
	document.cartForm.MEMO.value = options.join('\n');
	
	for (var i = 0; i < document.cartForm.length; i++) {
		var o = document.cartForm[i];
		console.log(o.name +':'+ o.value);
	}
	
	document.cartForm.submit();
}

function hiddenNameValue(cf, name, value) {
	cf.append('<input type="hidden" name="'+ name +'" />');
	$('input[name='+ name +']', cf).val(value);
}

function removeOrderOptions() {
	$('#cartForm input').each(function () {
		var o = $(this);
		if (o.attr('name').indexOf('option') === 0) o.remove(); 
	});
}

function isYokota() {
	return getGroup().charAt(0) == 'Y';
}

function onChangeProductType() {
	$.cookie('productType', getProductType());
}

function onChangeRailColor() {
	$.cookie('railColor', getRailColor());
}

function onChangeHandleType() {
	$.cookie('handleType', getHandleType());
}

function onChangeBalanceYN() {
	$.cookie('balanceYN', getBalanceYN());
	doUpdatePrice();
}

function onChangeOrderWidth() {
	$.cookie('orderWidth', getOrderWidth());
	doUpdatePrice();
}

function onChangeOrderHeight() {
	$.cookie('orderHeight', getOrderHeight());
	doUpdatePrice();
}

function onChangeAttachStyle() {
	$.cookie('attachStyle', getAttachStyle());
}

var preloadedImages = new Array();
function preload() {
	console.log('preloading...');
	
	var colors = [ color1, color2, color3, color4 ];
	
	for (var i = 0; i < colors.length; i++) {
		var color = colors[i];
		for (var j = 0; j < color.length; j++) {
			if (color[j][0] == '') continue;
			var img = new Image();
			img.src = 'vertical_img/'+ color[j][0] +'.jpg';
			preloadedImages.push(img);
		}
	}
}

function onChangeNumItems() {
	$.cookie('numItems', getNumItems());
	doUpdatePrice();
}

function onChangeGroup() {
	$.cookie('group', getGroup());
	changeProductType();
	changeColorType();
	changeLouverWidth();
	changeRailColor();
	changeBalanceYN();
	changeHandleType();
	changeOrderWidth();
	changeOrderHeight();
	changeFloorHeight();
	doUpdatePrice();
}

function onChangeColorType() {
	$.cookie('colorType', getColorType());
	
	var t = this;
	var d = $(t).parent().find('.sample');
	
	var showIt = function () {
		if (!t.value) return;
		
		if (isYokota()) return; // yokota なし
		
		d.find('img').attr('src', 'vertical_img/'+ t.value +'.jpg');
		d.show('normal');
	}
	
	if (d.css('display') != 'none') {
		d.hide('normal', showIt);
	}
	else {
		showIt(t);
	}
}

function onChangeFloorHeight() {
	$.cookie('floorHeight', getFloorHeight());
}

function onChangeLouverWidth() {
	$.cookie('louverWidth', getLouverWidth());
	doUpdatePrice();
}

function changeOrderWidth(v) {
	var o = $('#orderWidth');
	o.empty();
	
	if (getGroup() == '') {
		o.append(newOption('', '↑ 先にブラインドタイプをお選びください'));
		return;
	}
	
	o.append(newOption('', '▽'));
	
	var min = minWidth[getGroup()];
	var max = maxWidth[getGroup()];
	for (var i = min; i <= max; i += 10) {
		o.append(newOption(i, i, v));
	}
}

function changeOrderHeight(v) {
	var o = $('#orderHeight');
	o.empty();
	
	if (!getGroup()) {
		o.append(newOption('', '↑ 先にブラインドタイプをお選びください'));
		return;
	}
	
	o.append(newOption('', '▽'));
	
	var min = minHeight[getGroup()];
	var max = maxHeight[getGroup()];
	for (var i = min; i <= max; i += 10) {
		o.append(newOption(i, i, v));
	}
}

function changeFloorHeight(v) {
	var o = $('#floorHeight');
	o.empty();
	
	if (!getGroup()) {
		o.append(newOption('', '↑ 先にブラインドタイプをお選びください'));
		return;
	}
	
	o.append(newOption('', '▽'));
	
	var t = getGroup().charAt(0);
	if (t == 'Y' || t == 'S') {
		o.append(newOption('u1500', '1500mm 以下', v));
	}
		
	var min = minFloor[getGroup()];
	var max = maxFloor[getGroup()];
	for (var i = min; i <= max; i += 100) {
		o.append(newOption(i, i, v));
	}

	if (t == 'Y' || t == 'S') {
		o.append(newOption('o3000', '3000mm 以上', v));
	}
}

function changeLouverWidth(v) {
	var o = $('#louverWidth');
	o.empty();
	
	if (getGroup() =='YG') {
		o.append(newOption('89', '89mm'));
		return;
	}
	
	o.append(newOption('', '▽ ここをクリックしてお選びください'));
	o.append(newOption('89', '89mm', v));
	o.append(newOption('127', '127mm', v));
}

function changeBalanceYN(v) {
	var o = $('#balanceYN');
	o.empty();
	
	if (!getGroup()) {
		o.append(newOption('', '↑ 先にブラインドタイプをお選びください'));
		return;
	}
	
	if (getGroup().charAt(0) == 'Y') {
		o.append(newOption('S', '標準仕様'));
	}
	else {
		o.append(newOption('', '▽ ここをクリックしてお選びください'));
		o.append(newOption('Y', 'バランス付き', v));
		o.append(newOption('N', 'バランスなし', v));
	}
}

function changeHandleType(v) {
	var o = $('#handleType');
	o.empty();
	
	if (!getGroup()) {
		o.append(newOption('', '↑ 先にブラインドタイプをお選びください'));
		return;
	}
	
	o.append(newOption('', '▽ ここをクリックしてお選びください'));
	
	var orgon = [
		['BLR','バトン式・左操作・右納まり'],
		['BRL','バトン式・右操作・左納まり'],
		['2RR','2コード式・右操作・右納まり'],
		['2RL','2コード式・右操作・左納まり'],
		['2LL','2コード式・左操作・左納まり'],
		['2LR','2コード式・左操作・右納まり'],
		['2RW','2コード式・右操作・両納まり'],
		['2LW','2コード式・左操作・両納まり']];
	var yokota = [
		['RR','右操作・右納まり'],
		['RL','右操作・左納まり'],
		['RB','右操作・両側納まり'],
		['RC','右操作・中央納まり'],
		['LR','左操作・右納まり'],
		['LL','左操作・左納まり'],
		['LB','左操作・両側納まり'],
		['LC','左操作・中央納まり']];
	
	var opt = getGroup().charAt(0) == 'Y' ? yokota : orgon;
		
	for (var i = 0; i < opt.length; i++) {
		o.append(newOption(opt[i][0], opt[i][1], v));
	}
}

function changeProductType(v) {
	var o = $('#productType');
	o.empty();
	
	if (!getGroup()) {
		o.append(newOption('', '↑ 先にブラインドタイプをお選びください'));
		return;
	}

	if (isYokota()) {
		o.append(newOption('', '▽ ここをクリックしてお選びください'));
		o.append(newOption('H', 'ハープ', v));
		o.append(newOption('HCP', 'ハープ コード/ポール', v));
		o.append(newOption('OC', 'ワンチェーン', v));
	}
	else {
		o.append(newOption('S', '標準仕様'));
	}
}

function changeRailColor(v) {
	var o = $('#railColor');
	o.empty();
	
	if (!getGroup()) {
		o.append(newOption('', '↑ 先にブラインドタイプをお選びください'));
		return;
	}

	if (isYokota()) {
		o.append(newOption('', '▽ ここをクリックしてお選びください'));
		o.append(newOption('white', 'ホワイト', v));
		o.append(newOption('silver', 'シルバー', v));
	}
	else {
		o.append(newOption('S', '標準仕様'));
	}
}

function isYokota() {
	return getGroup().charAt(0) == 'Y';
}

function changeAttachStyle(v) {
	$('#attachStyle').val(v ? v : '');
}

function changeNumItems(v) {
	$('#numItems').val(v ? v : '1');
}

function initSelectors() {
	$('#group').val($.cookie('group'));
	changeProductType($.cookie('productType'));
	changeColorType($.cookie('colorType'));
	changeLouverWidth($.cookie('louverWidth'));
	changeRailColor($.cookie('railColor'));
	changeBalanceYN($.cookie('balanceYN'));
	changeHandleType($.cookie('handleType'));
	changeAttachStyle($.cookie('attachStyle'));
	changeOrderWidth($.cookie('orderWidth'));
	changeOrderHeight($.cookie('orderHeight'));
	changeFloorHeight($.cookie('floorHeight'));
	changeNumItems($.cookie('numItems'));
}

function run(f) {
	setTimeout(f, 10);
}

$(document).ready(function() {
	console.log('document.ready');
	
	initbox(); // dropdown box
	
	$('.guideButton').click(function () { showDetail(this); });

	$('#group').change(onChangeGroup);
	$('#productType').change(onChangeProductType);
	$('#colorType').change(onChangeColorType);
	$('#louverWidth').change(onChangeLouverWidth);
	$('#railColor').change(onChangeRailColor);
	$('#balanceYN').change(onChangeBalanceYN);
	$('#handleType').change(onChangeHandleType);
	$('#attachStyle').change(onChangeAttachStyle);
	$('#orderWidth').change(onChangeOrderWidth);
	$('#orderHeight').change(onChangeOrderHeight);
	$('#floorHeight').change(onChangeFloorHeight);
	$('#numItems').change(onChangeNumItems);
	$('#intoCart').click(onClickIntoCart);
	
	initSelectors();
	doUpdatePrice();
	run(preload);
});
