var widthO50 = [600,800,1000,1200,1400,1600,1800,2000,2200];
var widthO = [600,800,1000,1200,1400,1600,1800,2000,2400,2800,3200,3600];
var widthY15 = [600,800,1000,1200,1400,1600,1800];
var widthY25 = [880,1080,1280,1480,1680,1800];
var widthVariations = {
	'16':widthO, '25':widthO, '50':widthO50, 'W':widthO, // orgon
	'Y15':widthY15, 'Y25':widthY25 }; // yokota

var heightO = [800,1000,1200,1400,1600,1800,2000,2200];
var heightY15 = [300,500,700,900,1100,1300,1500,1700,1800];
var heightY25 = [980,1180,1380,1580,1800];
var heightVariations = {
	'16':heightO, '25':heightO, '50':heightO, 'W':heightO, // orgon
	'Y15':heightY15, 'Y25':heightY25 }; // yokota

var minWidth = {
	'16':300, '25':300, '50':300, 'W':300, 'Y15':450, 'Y25':450 };
var maxWidth = {
	'16':3600, '25':3600, '50':2200, 'W':3600, 'Y15':1800, 'Y25':1800 };
	
var minHeight = {
	'16':410, '25':410, '50':300, 'W':410, 'Y15':110, 'Y25':110 };
var maxHeight = {
	'16':2200, '25':2200, '50':2200, 'W':2200, 'Y15':1800, 'Y25':1800 };
	
var minFloor = {
	'16':1000, '25':1000, '50':1000, 'W':1000, 'Y15':1600, 'Y25':1600 };
var maxFloor = {
	'16':3000, '25':3000, '50':3000, 'W':3000, 'Y15':2900, 'Y25':2900 };

var CALL = -1;
var NG = -2;

var price16 = [
	[6600,7000,7200,7500,7800,8500,9100,11800,12000,14800,16100,17200], // 41-80
	[7000,7200,7300,8100,8800,9700,10300,12000,12300,15400,17400,19000], // 81-100
	[7400,7900,8100,8900,10100,10900,11600,12200,12600,17200,19600,21500], // 101-120
	[7800,8100,8900,9900,11300,12500,13300,13600,14100,19200,22500,24600], // 121-140
	[8000,8500,9800,10900,12400,13500,14300,14800,15300,21100,24300,26500], // 141-160
	[8100,9100,10600,11900,13300,14800,15600,16100,16800,22600,26700,28900], // 161-180
	[8200,9300,11100,12600,14800,15800,16800,17300,18100,25100,28400,31000], // 181-200
	[8500,10100,11800,13800,15300,16300,17300,18900,22100,26000,29300,32100]]; // 201-220

var price25st = [
	[5500,6000,6300,6800,6900,7400,8200,9500,10000,13000,14000,15500], // 41-80
	[6000,6300,6800,7000,7400,8100,8400,9600,10200,13200,14600,15900], // 81-100
	[6400,6800,6900,7400,7800,8500,9000,9700,10400,13400,15300,16700], // 101-120
	[6800,6900,7300,7800,8500,9300,9800,10400,11400,14500,16700,18100], // 121-140
	[6900,7200,7600,8400,9000,9800,10400,11400,12400,15300,17600,19200], // 141-160
	[7200,7400,8000,8800,9700,10400,10900,12400,13400,16500,18700,20100], // 161-180
	[7600,7900,8500,9200,10000,11000,11600,12900,13900,17000,19800,21500], // 181-200
	[8000,8400,8900,9800,10600,11700,12500,13900,14900,18000,21100,23100]]; // 201-220

var price25sp = [
	[6600,7200,7600,8100,8300,8900,9800,11400,12100,15700,16900,18500], // 41-80
	[7200,7600,8100,8400,8900,9700,10100,11500,12300,15900,17600,18700], // 81-100
	[7700,8100,8300,8900,9500,10200,10800,11600,12500,16100,18300,20000], // 101-120
	[8200,8300,8700,9300,10200,11100,11700,12500,13700,17300,20100,21700], // 121-140
	[8300,8600,9100,10100,10800,11700,12500,13700,14900,18400,21200,23100], // 141-160
	[8600,8900,9600,10500,11600,12500,13100,14900,16100,19800,22500,24200], // 161-180
	[9100,9500,10200,11000,12000,13200,13900,15500,16700,20400,23800,25700], // 181-200
	[9600,10100,10700,11700,12700,14000,15000,16700,17900,21600,25300,27800]]; // 201-220

var priceD = [
	[6600,7000,7200,7500,7800,8500,9100,9600,9800], // 30～80
	[7000,7200,7300,8100,8800,9700,10300,10800,11100], // 81～100
	[7400,7900,8100,8900,10100,10900,11600,12200,12600], // 101～120
	[7800,8100,8900,9900,11300,12500,13300,13600,14100], // 121～140
	[8000,8500,9800,10900,12400,13500,14300,14800,15300], // 141～160
	[8100,9100,10600,11900,13300,14800,15600,16100,16800], // 161～180
	[8200,9300,11100,12600,14800,15800,16800,17300,18100], // 181～200
	[8500,10100,11800,13800,15300,16300,17300,18300,18800]]; // 201～220

var priceE = [
	[7900,8400,8600,9000,9300,10200,10900,11500,11700], // 30～80
	[8400,8600,8700,9700,10500,11600,12300,12900,13300], // 81～100
	[8900,9500,9700,10700,12100,13100,13900,14600,15100], // 101～120
	[9300,9700,10700,11900,13500,15000,15900,16300,16800], // 121～140
	[9600,10200,11700,13100,14900,16200,17100,17700,18300], // 141～160
	[9700,10900,12700,14300,15900,17700,18700,19300,20100], // 161～180
	[9800,11100,13300,15100,17700,18900,20100,20700,21700], // 181～200
	[10200,12100,14100,16500,18300,19500,20700,21900,22500]]; // 201～220

var priceW = [
	[7500,7800,8000,8300,9500,10400,11300,14900,15800,18000,19800,21500], // 41-80
	[7800,8000,8300,9400,10700,11800,12900,15300,16000,18700,21200,23900], // 81-100
	[8000,8300,9400,10800,11700,12600,14000,15600,17200,20000,24700,25900], // 101-120
	[8300,9400,10600,11700,13000,14400,16000,17500,19200,22100,25900,30400], // 121-140
	[8600,9900,11600,12600,14300,16200,17800,19800,21400,24300,29200,33000], // 141-160
	[9200,11100,12700,13900,15800,17300,19500,21400,23300,26700,31100,36100], // 161-180
	[10400,12200,13600,15300,17200,19200,21000,23200,25600,29200,34600,38900], // 181-200
	[11500,13600,14700,16800,18700,21000,23100,25300,27400,31800,37800,42800]]; // 201-220
	
var priceY15 = [
	[7800,8500,8900,9600,10100,10400,10900], // -300
	[8100,8700,9300,10200,10800,11200,12000], // -500
	[8500,9300,9700,10900,11700,12000,12800], // -700
	[8700,9600,10300,11200,12000,12800,13500], // -900
	[8900,10100,10500,11800,12800,13500,14200], // -1100
	[9300,10300,11000,12500,13300,14200,15000], // -1300
	[9500,10600,11700,13200,14000,14900,15800], // -1500
	[9700,11100,12100,13600,14800,15600,16600], // -1700
	[10100,11300,12400,14100,15400,16400,17300]]; // -1800

var priceY25 = [
	[8400,8900,9300,10300,10800,11700], // -980
	[8900,9300,10000,10900,11300,13000], // -1180
	[9200,10000,10500,11700,12400,13500], // -1380
	[9800,10400,11100,12300,13200,14500], // -1580
	[10300,11200,12200,13500,14300,16100]]; // -1800

var variations = {
	'16_S_N':[colorA, price16],
	
	'25_S_N':[colorB, price25st], '25_S_Y':[colorB, price25sp],
	'25_M_N':[colorCM, price25sp], '25_M_Y':[colorCM, price25sp],
	'25_P_N':[colorCP, price25sp], '25_P_Y':[colorCP, price25sp],
	'25_A_N':[colorCA, price25sp], '25_A_Y':[colorCA, price25sp],
	
	'50_S_N':[colorD, priceD], '50_S_Y':[colorD, priceE],
	'50_M_N':[colorEM, priceE], '50_M_Y':[colorEM, priceE],
	'50_P_N':[colorEP, priceE], '50_P_Y':[colorEP, priceE],
	'50_A_N':[colorEA, priceE], '50_A_Y':[colorEA, priceE],
	
	'W_S_N':[colorF, priceW],
	
	'Y15_S_N':[colorY, priceY15],
	'Y25_S_N':[colorY, priceY25]
};

var colorType16 = [
	['S','スタンダードカラー']];

var colorType25 = [
	['S','スタンダードカラー'],
	['M','メタリックカラー'],
	['P','プリント柄'],
	['A','穴あき模様']];

var colorType50 = [
	['S','スタンダードカラー'],
	['M','メタリックカラー'],
	['P','プリント柄'],
	['A','穴あき模様']];

var colorTypeW = [
	['S','スタンダードカラー']];

var colorTypeY = [
	['S','スタンダードカラー']];

var colorTypeList = {
	'16':colorType16, '25':colorType25, '50':colorType50, 'W':colorTypeW, // orgon
	'Y15':colorTypeY, 'Y25':colorTypeY }; // yokota

var tapes = [
	['900','ホワイト'],
	['106','アイボリー'],
	['42','サフラン'],
	['134','カーキ'],
	['117','ブラウン'],
	['187','プラム'],
	['197','グリーン'],
	['189','レンガ'],
	['168','アッシュグレイ'],
	['178','インディゴ'],
	['9274','ダークブラウン'],
	['165','ブラック']];

var labels = {
	'wingType':['アルミブラインドタイプ', {
		'16':'オルゴン スタンダード16',
		'25':'オルゴン スタンダード25',
		'50':'オルゴン スタンダード50',
		'W':'オルゴン ウェービィ',
		'Y15':'国産浴室用ブラインド15',
		'Y25':'国産浴室用ブラインド25'
	}],
	'colorType':['カラータイプ', {
		'S':'スタンダードカラー',
		'M':'メタリックカラー',
		'P':'プリント柄',
		'A':'穴あき模様'
	}],
	'ladderTapeYN':['ラダーテープ有無', {
		'Y':'あり',
		'N':'なし'
	}],
	'slatColor':['スラット色', {}],
	'ladderTape':['ラダーテープ色', {
		'N':'ラダーテープなし'
	}],
	'orderWidth':['製品サイズ幅(ミリ)', {}],
	'orderHeight':['丈(ミリ)', {}],
	'handleType':['操作タイプ', {
		'P':'パーフェクトポール',
		'C':'クリアポール'
	}],
	'upDownCodePosition':['羽昇降コード', {
		'right':'右側',
		'left':'左側',
		'std':'標準'
	}],
	'rotateCodePosition':['羽回転ポール', {
		'right':'右側',
		'left':'左側',
		'std':'標準',
		'combined':'羽昇降コードと一体',
		'sameside':'羽昇降コードと同じサイド'
	}],
	'attachStyle':['窓枠への取付', {
		'std':'標準仕様',
		'ceiling':'天井',
		'wall':'正面'
	}],
	'floorHeight':['床より本体上部高(ミリ)', {
		'u1500':'1500mm以下',
		'o3000':'3000mm以上'
	}]
};

var pids = {
	'16':'AOS901', '25':'AOS901', '50':'AOS901', 'W':'AOW901', // orgon
	'Y15':'AY9901', 'Y25':'AY9901' }; // yokota

function getWingType() { return $('#wingType').val(); }
function getColorType() { return $('#colorType').val(); }
function getLadderTapeYN() { return $('#ladderTapeYN').val(); }
function getSlatColor() { return $('#slatColor').val(); }
function getLadderTape() { return $('#ladderTape').val(); }
function getAttachStyle() { return $('#attachStyle').val(); }
function getOrderWidth() { return $('#orderWidth').val(); }
function getOrderHeight() { return $('#orderHeight').val(); }
function getHandleType() { return $('#handleType').val(); }
function getUpDownCodePosition() { return $('#upDownCodePosition').val(); }
function getRotateCodePosition() { return $('#rotateCodePosition').val(); }
function getFloorHeight() { return $('#floorHeight').val(); }
function getNumItems() { return $('#numItems').val(); }

var validations = [
	['required', '#wingType', 'ブラインドタイプを選択してください。'],
	['required', '#colorType', 'カラータイプを選択してください。'],
	['required', '#ladderTapeYN', 'ラダーテープの有無を選択してください。'],
	['required', '#slatColor', 'スラットの色を選択してください。'],
	['required', '#ladderTape', 'ラダーテープの色を選択してください。'],
	['required', '#attachStyle', '窓枠への取付方法を選択してください。'],
	['required', '#orderWidth', '製品サイズ・幅を指定してください。'],
	['required', '#orderHeight', '製品サイズ・丈を指定してください。'],
	['required', '#handleType', '操作タイプを指定してください。'],
	['required', '#upDownCodePosition', '羽昇降コードの位置を選択してください。'],
	['required', '#rotateCodePosition', '羽回転ポールの位置を選択してください。'],
	['required', '#floorHeight', '床上高を指定してください。'],
	['f', validateFloorHeight]];

function getMtxPos(w, h) {
	var t = getWingType();
	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 priceList = getPriceList();
	var unitPrice = Math.floor(priceList[pos[0]][pos[1]] / 100) * 100;
	
	return unitPrice;
}

function getPrice() {
	var unitPrice = getUnitPrice();
	if (!unitPrice) return false;
	
	var price = unitPrice * (
		$('#numItems').size() > 0 ? Number($('#numItems').val()) : 1);
	
	return price;
}

function isReadyToCalculatePrice() {
	return (
		getWingType() != ''
		&& getOrderWidth() != ''
		&& getOrderHeight() != ''
		&& getColorType() != ''
		&& getLadderTapeYN() != '');
}

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 changeUpDownCodePosition(v) {
	var o = $('#upDownCodePosition');
	o.empty();
	
	function appendStd() {
		o.append(newOption('', '▽ ここをクリックしてお選びください'));
		o.append(newOption('right', '右側', v));
		o.append(newOption('left', '左側', v));
	}
	
	var wingType = getWingType();
	if (wingType == '16' || wingType == '25' || wingType == 'W') {
		var width = getOrderWidth();
		if (width == '') {
			o.append(newOption('', '↑ 先に製品サイズをお選びください'));
		}
		else {
			if (width >= 1810) {
				o.append(newOption('std', '標準位置', v));
			}
			else {
				appendStd();
			}
		}
	}
	else {
		appendStd();
	}
}

function changeRotateCodePosition(v) {
	var o = $('#rotateCodePosition');
	o.empty();
	
	function appendStd() {
		o.append(newOption('', '▽ ここをクリックしてお選びください'));
		o.append(newOption('right', '右側', v));
		o.append(newOption('left', '左側', v));
	}
	
	var handleType = getHandleType();
	var wingType = getWingType();
	if (wingType == '') {
		o.append(newOption('', '↑ 先にブラインドタイプをお選びください'));
	}
	else if (wingType.charAt(0) == 'Y') {
		if (handleType == 'C') {
			o.append(newOption('sameside', '昇降コードと同じサイド', v));
		}
		else if (handleType == 'P') {
			o.append(newOption('combined', '羽の昇降コードと一体', v));
		}
		else {
			o.append(newOption('', '↑ 先に操作タイプをお選びください'));
		}
	} 
	else if (wingType == '16' || wingType == '25' || wingType == 'W') {
		var width = getOrderWidth();
		if (width == '') {
			o.append(newOption('', '↑ 先に製品サイズをお選びください'));
		}
		else {
			if (width >= 1810) {
				o.append(newOption('std', '標準位置', v));
			}
			else {
				appendStd();
			}
		}
	}
	else {
		appendStd();
	}
}

function changeSlatColor(v) {
	var o = $('#slatColor');
	hideSample(o);
	o.empty();
	
	if (getLadderTapeYN() == '') {
		o.append(newOption('', '↑ 先にラダーテープの有無をお選びください'));
		return;
	}

	o.append(newOption('', '▽ ここをクリックしてお選びください'));
	var colors = getSlatColorList();
	console.log("colors.length="+ colors.length);
	for (var i = 0; i < colors.length; i++) {
		if (!colors[i]) break;
		o.append(newOption(
			colors[i][0],
			colors[i][0] +' - '+ colors[i][1],
			v));
	}
}

function getSlatColorList() {
	var cd = getVariationTypeCode();
	if (! cd) return false;
	return variations[cd][0];
}

function getPriceList() {
	var cd = getVariationTypeCode();
	if (! cd) return false;
	return variations[cd][1];
}

function changeColorType(v) {
	var o = $('#colorType');
	o.empty();
	
	if (getWingType() == '') {
		o.append(newOption('', '↑ 先にブラインドタイプをお選びください'));
		return;
	}
	
	var s = colorTypeList[getWingType()];
	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 changeLadderTapeYN(v) {
	var o = $('#ladderTapeYN');
	o.empty();

	if (getColorType() == '') {
		o.append(newOption('', '↑ 先にカラータイプをお選びください'));
		return;
	}
	
	var wc = getWingType() +'_'+ getColorType() +'_';
	if (variations[wc +'Y']) o.append(newOption('Y', 'ラダーテープあり', v));
	if (variations[wc +'N']) o.append(newOption('N', 'ラダーテープなし', v));
	if (o.children().size() > 1) {
		o.prepend(newOption('', '▽ ここをクリックしてお選びください'));
		if (!v) o.get(0).selectedIndex = 0;
	}
}

function getVariationTypeCode() {
	if (getWingType() == '') return false;
	if (getColorType() == '') return false;
	if (getLadderTapeYN() == '') return false;
	
	var v = getWingType() +'_'+ getColorType() +'_'+ getLadderTapeYN();
	console.log('variationTypeCode='+ v);
	return v;
}

function changeLadderTape(v) {
	var o = $('#ladderTape');
	hideSample(o);
	o.empty();
	
	if (getWingType() == '') {
		o.append(newOption('', '↑ 先にブラインドタイプをお選びください'));
		return;
	}
	if (getLadderTapeYN() == 'N') {
		o.append(newOption('N', 'ラダーテープなし'));
		return;
	}
	
	o.append(newOption('', '▽ ここをクリックしてお選びください'));
	for (var i = 0; i < tapes.length; i++) {
		o.append(newOption(
			tapes[i][0],
			tapes[i][0] +' - '+ tapes[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[getWingType()];
	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();
			
			var wingType = getWingType();
			if (wingType == '16' || wingType == '25' || wingType == 'W') {
				if (getOrderWidth() >= 1810) value += ' (セパレート)';
			}
		}
		if (o.name == 'orderHeight') continue;
		
		if (isYokota()) {
			if (o.name == 'colorType'
				|| o.name == 'ladderTapeYN'
				|| o.name == 'ladderTape'
				|| o.name == 'attachStyle') continue;
		}
		else {
			if (o.name == 'handleType') 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 getWingType().charAt(0) == 'Y';
}

function onChangeSlatColor() {
	$.cookie('slatColor', getSlatColor());
	
	var t = this;
	var d = $(t).parent().find('.sample');
	
	var showIt = function () {
		if (!t.value) return;
		if (getWingType().charAt(0) == 'Y') return; // yokota なし
		d.text($(t.options[t.selectedIndex]).text());
		d.css('background-image', 'url(slatimg/'+ t.value +'.jpg)');
		d.show('normal');
	}
	
	if (d.css('display') != 'none') {
		d.hide('normal', showIt);
	}
	else {
		showIt(t);
	}
}

function onChangeLadderTape() {
	$.cookie('ladderTape', getLadderTape());
	
	var t = this;
	var d = $(t).parent().find('.sample');
	
	var showIt = function () {
		if (!t.value) return;
		if (isNaN(t.value)) return;
		d.text($(t.options[t.selectedIndex]).text());
		d.css('background-image', 'url(tapeimg/tape'+ t.value +'.jpg)');
		d.show('normal');
	}
	
	if (d.css('display') != 'none') {
		d.hide('normal', showIt);
	}
	else {
		showIt(t);
	}
}

function onChangeLadderTapeYN() {
	$.cookie('ladderTapeYN', getLadderTapeYN());
	changeSlatColor();
	changeLadderTape();
	doUpdatePrice();
}

function onChangeHandleType() {
	$.cookie('handleType', getHandleType());
	changeRotateCodePosition();
}

function onChangeOrderWidth() {
	$.cookie('orderWidth', getOrderWidth());
	changeUpDownCodePosition();
	changeRotateCodePosition();
	doUpdatePrice();
}

function onChangeOrderHeight() {
	$.cookie('orderHeight', getOrderHeight());
	doUpdatePrice();
}

function onChangeFloorHeight() {
	$.cookie('floorHeight', getFloorHeight());
}

function onChangeAttachStyle() {
	$.cookie('attachStyle', getAttachStyle());
}

function onChangeUpDownCodePosition() {
	$.cookie('upDownCodePosition', getUpDownCodePosition());
}

function onChangeRotateCodePosition() {
	$.cookie('rotateCodePosition', getRotateCodePosition());
}

var preloadedImages = new Array();
function preload() {
	console.log('preloading...');
	
	var colors = [
		colorA,
		colorB,
		colorCM, colorCP, colorCA,
		colorD,
		colorEM, colorEP, colorEA,
		colorF
	];

	for (var i = 0; i < colors.length; i++) {
		var color = colors[i];
		for (var j = 0; j < color.length; j++) {
			var img = new Image();
			img.src = 'slatimg/'+ color[j][0] +'.jpg';
			preloadedImages.push(img);
		}
	}
	
	for (var i = 0; i < tapes.length; i++) {
		var img = new Image();
		img.src = 'tapeimg/tape'+ tapes[i][0] +'.jpg';
		preloadedImages.push(img);
	}
}

function onChangeNumItems() {
	$.cookie('numItems', getNumItems());
	doUpdatePrice();
}

function onChangeWingType() {
	$.cookie('wingType', getWingType());
	changeColorType();
	changeSlatColorImg();
	changeLadderTapeYN();
	changeSlatColor();
	changeLadderTape();
	changeAttachStyle();
	changeOrderWidth();
	changeOrderHeight();
	changeHandleType();
	changeUpDownCodePosition();
	changeUpDownCodePositionImg();
	changeRotateCodePosition();
	changeRotateCodePositionImg();
	changeFloorHeight();
	doUpdatePrice();
}

function onChangeColorType() {
	$.cookie('colorType', getColorType());
	changeSlatColorImg();
	changeLadderTapeYN();
	doUpdatePrice();
}

function changeSlatColorImg() {
	if (!getWingType()) return;
	
	var src0 = {
		'16': {
			'S':'alumi16.jpg',
			'M':'',
			'P':'',
			'A':'' },
		'25': {
			'S':'alumi25st.jpg',
			'M':'alumi25meta.jpg',
			'P':'alumi25print.jpg',
			'A':'alumi25hole.jpg' },
		'50': {
			'S':'alumi50st.jpg',
			'M':'alumi50meta.jpg',
			'P':'alumi50print.jpg',
			'A':'alumi50hole.jpg' },
		'W': {
			'S':'wavy-color.jpg',
			'M':'',
			'P':'',
			'A':'' }
		}[getWingType()];		
	if (!src0) return;
	
	if (!getColorType()) return;
	
	var src = src0[getColorType()];
	if (!src) return;
	
	$('#slatColorImg').attr('src', 'images/'+ src);
}

function changeHandleType(v) {
	var o = $('#handleType');
	o.empty();
	
	if (getWingType() == '') {
		o.append(newOption('', '↑ 先にブラインドタイプをお選びください'));
		return;
	}
	
	if (getWingType().charAt(0) == 'Y') {
		o.append(newOption('', '▽ ここをクリックしてお選びください'));
		o.append(newOption('P', 'パーフェクトポール', v));
		o.append(newOption('C', 'クリアポール', v));
	}
	else {
		o.append(newOption('S', '標準仕様'));
	}
}

function changeUpDownCodePositionImg() {
	var src = {
		'16':'control-16_25.gif',
		'25':'control-16_25.gif',
		'50':'control-50a.gif',
		'W':'control-classic.gif'
		}[getWingType()];
	if (!src) return;
	
	$('#upDownCodePositionImg').attr('src', 'images/'+ src);
}

function changeRotateCodePositionImg() {
	var src = {
		'16':'control-16_25.gif',
		'25':'control-16_25.gif',
		'50':'control-50b.gif',
		'W':'control-16_25.gif'
		}[getWingType()];
	if (!src) return;
	
	$('#rotateCodePositionImg').attr('src', 'images/'+ src);
}

function changeOrderWidth(v) {
	var o = $('#orderWidth');
	o.empty();
	
	var wingType = getWingType();
	if (wingType == '') {
		o.append(newOption('', '↑ 先にブラインドタイプをお選びください'));
		return;
	}
	
	var separatable = (wingType == '16' || wingType == '25' || wingType == 'W');
	
	o.append(newOption('', '▽'));
	
	var min = minWidth[wingType];
	var max = maxWidth[wingType];
	for (var i = min; i <= max; i += 10) {
		var t = i + (separatable && i >= 1810 ? '　(セパレート)' : '');  
		o.append(newOption(i, t, v));
	}
}

function changeOrderHeight(v) {
	var o = $('#orderHeight');
	o.empty();
	
	if (!getWingType()) {
		o.append(newOption('', '↑ 先にブラインドタイプをお選びください'));
		return;
	}
	
	o.append(newOption('', '▽'));
	
	var min = minHeight[getWingType()];
	var max = maxHeight[getWingType()];
	for (var i = min; i <= max; i += 10) {
		o.append(newOption(i, i, v));
	}
}

function changeFloorHeight(v) {
	var o = $('#floorHeight');
	o.empty();
	
	if (!getWingType()) {
		o.append(newOption('', '↑ 先にブラインドタイプをお選びください'));
		return;
	}
	
	o.append(newOption('', '▽'));
	
	var t = getWingType().charAt(0);
	if (t == 'Y') {
		o.append(newOption('u1500', '1500mm 以下', v));
	}
		
	var min = minFloor[getWingType()];
	var max = maxFloor[getWingType()];
	for (var i = min; i <= max; i += 100) {
		o.append(newOption(i, i, v));
	}

	if (t == 'Y') {
		o.append(newOption('o3000', '3000mm 以上', v));
	}
}

function changeAttachStyle(v) {
	var o = $('#attachStyle');
	o.empty();
	
	if (getWingType().charAt(0) == 'Y') {
		o.append(newOption('std', '標準仕様'));
	}
	else {
		o.append(newOption('', '▽ ここをクリックしてお選びください'));
		o.append(newOption('ceiling', '天井付け', v));
		o.append(newOption('wall', '正面付け', v));
	}
}

function changeNumItems(v) {
	$('#numItems').val(v ? v : '1');
}

function initSelectors() {
	$('#wingType').val($.cookie('wingType'));
	changeColorType($.cookie('colorType'));
	changeLadderTapeYN($.cookie('ladderTapeYN'));
	changeSlatColor($.cookie('slatColor'));
	changeLadderTape($.cookie('ladderTape'));
	changeAttachStyle($.cookie('attachStyle'));
	changeOrderWidth($.cookie('orderWidth'));
	changeOrderHeight($.cookie('orderHeight'));
	changeHandleType($.cookie('handleType'));
	changeUpDownCodePosition($.cookie('upDownCodePosition'));
	changeRotateCodePosition($.cookie('rotateCodePosition'));
	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); });
	
	$('#wingType').change(onChangeWingType);
	$('#colorType').change(onChangeColorType);
	$('#ladderTapeYN').change(onChangeLadderTapeYN);
	$('#slatColor').change(onChangeSlatColor);
	$('#ladderTape').change(onChangeLadderTape);
	$('#attachStyle').change(onChangeAttachStyle);
	$('#orderWidth').change(onChangeOrderWidth);
	$('#orderHeight').change(onChangeOrderHeight);
	$('#handleType').change(onChangeHandleType);
	$('#upDownCodePosition').change(onChangeUpDownCodePosition);
	$('#rotateCodePosition').change(onChangeRotateCodePosition);
	$('#floorHeight').change(onChangeFloorHeight);
	$('#numItems').change(onChangeNumItems);
	$('#intoCart').click(onClickIntoCart);
	
	initSelectors();
	doUpdatePrice();
	run(preload);
});
