var fsk1 = 1260;	// 朱色の風呂敷価格
var fsk2 = 1575;	// 紫色の風呂敷価格

var ary_matrix  = new Array();	//箱・風呂敷のマトリックス
var ary_default = new Array();	//デフォルトで選択される箱
var ary_non_opt = new Array();	//「箱の色を変える」のリンク有無
var ary_non_fsk = new Array();	//「風呂敷を購入する」のリンク有無
var ary_def_fsk = new Array();	//デフォルトで選択される風呂敷


function optionInit() {

	$.ajax({
		url: "/gift/cart/options_matrix.js.php",
		dataType: 'json',
		success: function(data){

			ary_matrix  = data['matrix'];
			ary_default = data['default_wrapping'];
			ary_non_opt = data['non_option_flag'];
			ary_non_fsk = data['non_fsk_flag'];
			ary_def_fsk = data['default_fsk'];

			optionInitSet();
			optionMake();

		},
		error: function(){
			if(window.confirm("サーバとの通信に失敗しました。\n【ＯＫ】をクリックすると、ブラウザを再読み込みして再度通信を試みます。\n何度もこのメッセージが出る場合は、しばらく時間を置いてからアクセスし直してください。")){
				window.location.reload();
			}
		}
	});
	
}

function optionInitSet() {

	var str = '';
	var tmp, key, i, j;
	var num = optionNum();
	
	if($("#option").val() == '') {
		// 未設定時	
		for(key in num) {
			str += key + ":" + ary_default[key] + "-" + ary_def_fsk[key] + "_";
		}
		$("#option").val(str.substr(0, str.length - 1));
	} else {
		// 設定済時
		var check = new Array();
		tmp = $("#option").val().split("_");
	
		for(i = 0; i < tmp.length; i++) {
			check[tmp[i].split(":")[0]] = tmp[i].split(":")[1];
		}
	
		for(key in num) {
			if(check[key]){
				str += key + ":" + check[key] + "_";
			}else{
				str += key + ":" + ary_default[key] + "-" + ary_def_fsk[key] + "_";
			}
		}

		$("#option").val(str.substr(0, str.length - 1));
	}
}

function optionSet(id,type) {
	// CSS の影響？position:absolute の top が異常値になる
	//var posY = document.documentElement.scrollTop + (document.documentElement.clientHeight / 2);
	var posY    = "500px";	
	var matrix  = optionMatrix();

	//alert(matrix[id][2]);
	
	var num     = optionNum();
	var opt_all = $("#option").val().split("_");
	var tmp;
	
	for(var i = 0; i < opt_all.length; i++) {
		if(opt_all[i].split(":")[0] == id) {
			tmp = opt_all[i].split(":")[1].split("-");
			for(var j = 0; j < tmp.length; j++) {
				$('input[@name=opt_' + j + ']').val([tmp[j]]);
			}
			break;
		}
	}

	// 黒箱の設定
	try {
		switch(matrix[id][1]) {
			case "1":
				$("li.lbl_0_3").css("display", "block");
				$("#opt_0_3").removeAttr("disabled");
				break;
			default:
				$("li.lbl_0_3").css("display", "none");
				$("#opt_0_3").attr("disabled", "disabled");
				break;
		}
	} catch(err) {
		$("li.lbl_0_3").css("display", "none");
		$("#opt_0_3").attr("disabled", "disabled");
	}

	// 桐箱の設定
	try {
		switch(matrix[id][2]) {
			case "1":
				$("li.lbl_0_4").css("display", "block");
				$("#opt_0_4").removeAttr("disabled");
				break;
			default:
				$("li.lbl_0_4").css("display", "none");
				$("#opt_0_4").attr("disabled", "disabled");
				break;
		}
	} catch(err) {
		$("li.lbl_0_4").css("display", "none");
		$("#opt_0_4").attr("disabled", "disabled");
	}

	// 金箱の設定
	try {
		switch(matrix[id][3]) {
			case "1":
				$("li.lbl_0_5").css("display", "block");
				$("#opt_0_5").removeAttr("disabled");
				break;
			default:
				$("li.lbl_0_5").css("display", "none");
				$("#opt_0_5").attr("disabled", "disabled");
				break;
		}
	} catch(err) {
		$("li.lbl_0_5").css("display", "none");
		$("#opt_0_5").attr("disabled", "disabled");
	}

	// 白箱の設定
	try {
		switch(matrix[id][4]) {
			case "1":
				$("li.lbl_0_6").css("display", "block");
				$("#opt_0_6").removeAttr("disabled");
				break;
			default:
				$("li.lbl_0_6").css("display", "none");
				$("#opt_0_6").attr("disabled", "disabled");
				break;
		}
	} catch(err) {
		$("li.lbl_0_6").css("display", "none");
		$("#opt_0_6").attr("disabled", "disabled");
	}

	// ピンク箱の設定
	try {
		switch(matrix[id][5]) {
			case "1":
				$("li.lbl_0_7").css("display", "block");
				$("#opt_0_7").removeAttr("disabled");
				break;
			default:
				$("li.lbl_0_7").css("display", "none");
				$("#opt_0_7").attr("disabled", "disabled");
				break;
		}
	} catch(err) {
		$("li.lbl_0_7").css("display", "none");
		$("#opt_0_7").attr("disabled", "disabled");
	}

	// 風呂敷の設定
	try {
		switch(matrix[id][0]) {
			case "1":	

				$("li.lbl_1_2").css("display", "none");
				$("li.lbl_1_1").css("display", "block");
				
				$("#opt_1_1").removeAttr("disabled");
				$("#opt_1_2").attr("disabled", "disabled");
				$("#optionPrice").css("display", "block");

				//$("#optionInsite").css("display", "block");
				$("#optionInsite").html("風呂敷(朱)");

				$("#optionQuantity").html(num[id]);
				$("#optionTotal").html(numberFormat(taxAdd(fsk1 * num[id])));
				
				if (type == 2) {
					$("input[name='opt_1']").val(["1"]);
				}

				optionFskColor();
				break;

			case "2":	

				$("li.lbl_1_1").css("display", "none");
				$("li.lbl_1_2").css("display", "block");
				
				$("#opt_1_1").attr("disabled", "disabled");
				$("#opt_1_2").removeAttr("disabled");
				$("#optionPrice").css("display", "block");
				
				//$("#optionInsite").css("display", "block");
				$("#optionInsite").html("風呂敷(紫)");

				$("#optionQuantity").html(num[id]);
				$("#optionTotal").html(numberFormat(taxAdd(fsk2 * num[id])));

				if (type == 2) {
					$("input[name='opt_1']").val(["2"]);
				}

				optionFskColor();
				break;

			case "3":	

				$("li.lbl_1_2").css("display", "none");
				$("#opt_1_1").attr("disabled", "disabled");
				$("#opt_1_2").attr("disabled", "disabled");
				$("#optionPrice").css("display", "none");

				break;

			case "4":	

				$("li.lbl_1_2").css("display", "none");
				$("#opt_1_1").attr("disabled", "disabled");
				$("#opt_1_2").attr("disabled", "disabled");
				$("#optionPrice").css("display", "none");

				break;

			default:

				$("#opt_1_1").attr("disabled", "disabled");
				$("#opt_1_2").attr("disabled", "disabled");
				//$("#optionPrice").css("display", "none");
				$("#optionInsite").css("display", "block");
				$("#optionInsite").html("この商品には風呂敷はつけられません。");
				break;

		}

	} catch(err) {

		$("#opt_1_1").attr("disabled", "disabled");
		$("#opt_1_2").attr("disabled", "disabled");
		$("#optionPrice").css("display", "none");
		$("#optionInsite").css("display", "block");
		$("#optionInsite").html("この商品には風呂敷はつけられません。");
	}
	
	$("#opt_focus").val(id);
	
	if (type == 1) {
		$("#optionLayer").css("top", posY).fadeIn();
	}

	if ((type == 2) || (type == 3)) {
		$("li.lbl_1_0").css("display", "none");
		$(".fsk_radio").css("display", "none");
		
		if (type == 2) {
			$("#kounyu_midasi").css("display", "block");
			$("#kounyu_btn").css("display", "inline");
			$("#cansel_midasi").css("display", "none");
			$("#cansel_btn").css("display", "none");
		}

		if (type == 3) {
			$("#kounyu_midasi").css("display", "none");
			$("#kounyu_btn").css("display", "none");
			$("#cansel_midasi").css("display", "block");
			$("#cansel_btn").css("display", "inline");
		}
		
		$("#optionLayer2").css("top", posY).fadeIn();

	}

}

function optionAdd(type) {
	var opt_all = $("#option").val().split("_");
	var box = $("input[@name=opt_0]:checked").val();
	var fsk = $("input[@name=opt_1]:checked").val();
	var str = '';
	
	var opt = '';
	
	if(box == undefined) box = 2;	//「箱なし」は2
	if(fsk == undefined) fsk = 0;

	for(var i = 0; i < opt_all.length; i++) {
		
		opt = opt_all[i].split(":")[1];
		
		if(opt_all[i].split(":")[0] == $("#opt_focus").val()){

			if (type == 1) {
				str += $("#opt_focus").val() + ":" + box + "-" + opt.split("-")[1] + "_";
			}

			if (type == 2) {
				str += $("#opt_focus").val() + ":" + opt.split("-")[0] + "-" + fsk + "_";
			}

			if (type == 3) {
				str += $("#opt_focus").val() + ":" + opt.split("-")[0] + "-" + "0" + "_";
			}
			
		}else{
			str += opt_all[i] + "_";
		}
	}
	
	$("#option").val(str.substr(0, str.length - 1));
	$("#test_result .disp").load("/gift/cart/options.js.php", { send: $("#option").val() });
	optionMake();
	$("#optionLayer").fadeOut();
	$("#optionLayer2").fadeOut();
}

function optionClose() {
	$("#optionLayer").fadeOut();
	$("#optionLayer2").fadeOut();
}

function optionMake() {
	var option = $("#option").val().split("_");
	var opt    = 0;
	var total  = 0;
	//var matrix = optionMatrix();
	var num    = optionNum();
	var names  = new Array();
	names[0]   = new Array("通常の箱", "金の箱" ,"なし" , "黒", "桐", "金", "白", "ピンク");
	names[1]   = new Array("なし", "朱", "紫", "朱", "紫");

	for(i = 0; i < option.length; i++) {

		key = option[i].split(":")[0];
		tmp = option[i].split(":")[1].split("-");

		
		if($("#opt_focus").val() != undefined){
			str = '<table cellpadding="0" cellspacing="0" border="0" class="option_cart">';
		}else{
			str = '<table cellpadding="0" cellspacing="0" border="0" class="option">';
		}

		for(j = 0; j < tmp.length; j++) { 
			opt = 0;
			switch(j) {
				// 箱のhtml作成
				case 0:

					if($("#opt_focus").val() != undefined){
						
												
						str += '<tr><th>箱の色：' + names[j][tmp[j]] + '</th>';

						if (ary_non_opt[key] == 1){
							str += '<td class="no_link">箱の色を変える</td></tr>';
						}else{
							str += '<td><a href="javascript:optionSet(' + key + ',1);">箱の色を変える</a></td></tr>';
						}
						

					}else{
						if(tmp[j] != 2) {
							str += '<tr><th>箱の色：' + names[j][tmp[j]] + '×' + num[key] + '</th>';
							str += '<td>無料</td></tr>';
						}
					}
	
					break;

				// 風呂敷のhtml作成
				case 1:

						if($("#opt_focus").val() != undefined){
							

							str += '<tr><th>風呂敷：' + names[j][tmp[j]] + '</th>';

							if (ary_non_fsk[key] == 1){
								str += '<td class="no_link">風呂敷を購入する</td></tr>';
							}else{
								if (ary_def_fsk[key] == 0) {
									if (tmp[j] == 0) {
										str += '<td><a href="javascript:optionSet(' + key + ',2);">風呂敷を購入する</a></td></tr>';
									}else{
										str += '<td><a href="javascript:optionSet(' + key + ',3);">購入をキャンセル</a></td></tr>';
									}
								}else{
									str += '<td class="no_link">商品内容に含まれます</td></tr>';
								}
							}
							
						
						}else{

							if(tmp[j] != 0) {
								str += '<tr><th>風呂敷：' + names[j][tmp[j]] + '×' + num[key] + '</th>';
								switch(parseInt(tmp[j])) {
									case 1:
										opt  = taxAdd(fsk1 * num[key]);
										str += '<td>' + numberFormat(opt) + '円</td></tr>';
										break;
									case 2:
										opt  = taxAdd(fsk2 * num[key]);
										str += '<td>' + numberFormat(opt) + '円</td></tr>';
										break;
									case 3:	str += '<td>無料</td></tr>';	break;
									case 4:	str += '<td>無料</td></tr>';	break;
								}
							}
						}						


					break;
			}
			
			total += opt;
		}
		
		str += '</table>';
		$("#option_" + key).html(str);
		$("#total_" + key).html(numberFormat(parseInt($("#def_" + key).val()) + opt));
		$("#total_small").html(numberFormat(parseInt($("#def_small").val()) + total));
		$("#total_all").html(numberFormat(parseInt($("#def_all").val()) + total));
	}
}

function optionFskColor() {
	if($("#opt_fsk_3").attr("checked")) {
		$("#optionPrice").css("color", "#CCC");
		$("#optionQuantity").css("color", "#CCC");
		$("#optionTotal").css("color", "#CCC");
	} else {
		$("#optionPrice").css("color", "#333");
		$("#optionQuantity").css("color", "#F00");
		$("#optionTotal").css("color", "#F00");
	}
}

function optionNum() {
	var pdt_all = $("#pdt_all").val().split("_");
	var num = new Array();
	
	for(i = 0; i < pdt_all.length; i++) {
		num[pdt_all[i].split("-")[0]] = pdt_all[i].split("-")[1];
	}
	
	return num;
}


function optionMatrix() {

	var matrix = new Array();
		
	matrix = ary_matrix;
	
	return matrix;
}

function numberFormat(int) {
	var temp = String(int);
	var cnt  = 0;
	var str  = "";

	for(var i = temp.length - 1; i >= 0; i--) {
		if(cnt > 0 && cnt % 3 == 0) str = "," + str;
		str = temp.substr(i, 1) + str;
		cnt++;
	}

	return str;
}

function taxAdd(price) {
	var tax   = $("#tax").val();
	var value = price * (tax / 100 + 1);
	
	switch($("#tax_rule").val()) {
		case '1':	value = Math.round(value);	break;
		case '2':	value = Math.floor(value);	break;
		case '3':	value = Math.ceil(value);	break;
	}
	
	return value;
}
