

////////////////////////////////////////
//	XML
////////////////////////////////////////


//	ec詳細取得用xml
var ecItemXml;
var std1Element;
var std2Element;
var stockElement;


var detail;

////////////////////////////////////////
//	ID書換変数
////////////////////////////////////////

/* メイン画像 */
var mainPhotoLinkId = "#ec_main_photo_original";
var mainPhotoImgId = "#ec_main_photo";

var noimage = "/rs/images/sample/320x240.jpg";

/* 参考価格 */
var conItemInfoPriceId = "#item_info_price_content";
var itemInfoPriceId = "#item_info_price";

/* 商品価格 */
var itemPriceId = "#item_price";

/* OFF */
var conItemOffPriceId = "#item_off_content";
var itemOffPriceId = "#item_off";

/* 在庫状況 */
var stockFlgId = "#is_stock_flg";

var conStockNumContentId = "#stock_num_content";
var stockNumId = "#stock_num";

/* 商品分類ID */
var itemStdTypeId1 = "#itemStdTypeId1";
var itemStdTypeId2 = "#itemStdTypeId2";
var itemStdTypeName1 = "#add_cart_item_std_name1";
var itemStdTypeName2 = "#add_cart_item_std_name2";

var itemStdTypeErrId = "#ec_item_std_err";


/* サブ画像 */
var conSubPhoto = "#ec_sub_photo_content";

/* 追加ボタン */
var cartButtonId = "#add_cart_button";

////////////////////////////////////////
//	メッセージ
////////////////////////////////////////

var errorMsg1 = "只今大変混雑しております。\nしばらく立ってから再度アクセスしてください。";
var errorMsg2 = "選択した商品は現在取り扱っておりません。";


/////////////////////////////////////////
//	初期化関数
//	当メソッドを最初に必ず呼ぶ必要あり
/////////////////////////////////////////

function loadEcItem(urlQuery) {
	
	$.ajax({
	    url: urlQuery,
	    cache:false,
	    type: 'GET',
	    dataType: 'xml',
	    timeout: 10000,
	    error: function(){
			alert(errorMsg1);
	    },
	    success: function(xml){

		    $(xml).find('code').each(function(){
		        var errorCode = $(this).attr("id");
				
				if(errorCode != "0") { alert($(this).text()); return; }
				
				ecItemXml = xml;

				
				//	商品規格エレメントを設定
				std1Element = $(xml).find('itemStd1');
				std2Element = $(xml).find('itemStd2');
				
				
				//	在庫エレメントを設定
				stockElement = $(xml).find('stock-item');
				
				 detail = new EcDetail();
				 
				//	買い物カゴボタンをDisableにする
				$(cartButtonId).attr("disabled", true);
		    });
	    }
	});
}


//	商品詳細
function EcDetail() {
}

EcDetail.prototype = {
	
	//	商品価格
	showItemPrice : function (price) {
		$(itemPriceId).text(replaceComma(price));	
		$("#item_price2").text(replaceComma(price));	
	},

	//	参考価格表示
	showItemInfoPrice : function (price) {
		$(conItemInfoPriceId).show();
		$("#item_info_price_content2").show();
		$(itemInfoPriceId).text(replaceComma(price));	
	},
	
	//	参考価格非表示
	hideItemInfoPrice : function () {
		$(conItemInfoPriceId).hide();
		$("#item_info_price_content2").hide();
	},
	
	//	OFF表示
	showOffPrice : function (price) {
		$(conItemOffPriceId).show();
		$("#item_off_content2").show();
		$(itemOffPriceId).text(replaceComma(price));
	},
	
	
	//	OFF非表示
	hideOffPrice : function () {
		$(conItemOffPriceId).hide();
		$("#item_off_content2").hide();
	},
	
	//	在庫個数表示
	showStockNum : function (num) {
		$(conStockNumContentId).show();
		$(stockNumId).text(replaceComma(num));
	},

	//	在庫個数非表示
	hideStockNum : function () {
		$(conStockNumContentId).hide();
	},

	//	在庫個数非表示
	hideStockNum : function () {
		$(conStockNumContentId).hide();
	},
	
	//	在庫状況を設定
	setStockFlg : function (isStockFlg) {
		if(isStockFlg == "true") {
			$(stockFlgId).text("在庫あり");
		} else {
			$(stockFlgId).text("在庫なし");
		}
		
	},
	
	//	メイン画像追加
	setMainPhoto : function (photoOriginalUrl, photoBigUrl, photoItemName) {
		$(mainPhotoLinkId).attr("href", photoOriginalUrl);
		$(mainPhotoImgId).attr("src", photoBigUrl);
		$(mainPhotoImgId).attr("alt", photoItemName);
	},
	
	
	//	サブ画像追加
	appendSubPhoto : function (increment, photoOriginalUrl, photoBigUrl, photoSmallUrl, photoItemName) {
		
		var img = "<a href=\"" + photoOriginalUrl + "\" target=\"_blank\">";
		img    +=    "<img id=\"ec_sub_photo" + increment + "\" src=\""+ photoSmallUrl + "\" alt=\"" + photoItemName + "\" />";
		img    += "</a>";
		
		$(conSubPhoto).append(img);
		
		var myThis = this;
		
		//	メイン画像へ貼り付け
		$("#ec_sub_photo" + increment).mouseover(function (){
			myThis.setMainPhoto(photoOriginalUrl, photoBigUrl, photoItemName);
		});
	},
	
	//	サブ画像すべて削除
	removeSubPhoto : function () {
		$(conSubPhoto).html("");
	},

	
	//	カートフォームに反映
	setCartFormStdType1 : function (type1, typeName1) {
		$(itemStdTypeId1).attr("value", type1);
		$(itemStdTypeName1).text(typeName1);
	},
	
	//	カートフォームに反映
	setCartFormStdType2 : function (type2, typeName2) {
		$(itemStdTypeId2).attr("value", type2);
		$(itemStdTypeName2).text(typeName2);
	},
	
	//	商品規格エラーメッセージ
	setItemStdErr : function (msg) {
		$(itemStdTypeErrId).show();
		$(itemStdTypeErrId).text(msg);
	},
	
	//	エラーメッセージ非表示
	hideItemStdErr : function () {
		$(itemStdTypeErrId).hide();
	},
	
	
	//	メイン画像及びサブ画像のフリップ
	flipPhoto : function(type) {
		
		var myThis = this;
		$(ecItemXml).find("type[id=" + type + "]").each(function (){
		
			if($(this).find("photoItem").size() == 0) {
				//	メイン画像にnoimage画像の貼り付け
				myThis.setMainPhoto(noimage, noimage, "");
			} else {
				
				//	メイン画像の貼り付け
				$(this).find("photoItem").each(function () {
				
					var big = $(this).find("item[sizeType=big]").text();
					var original = $(this).find("item[sizeType=original]").text();
					var small = $(this).find("item[sizeType=small]").text();
					var photoItemName = $(this).attr("alt");
					
					myThis.setMainPhoto(original, big, photoItemName);
				
					return false;
				});

				//	サブ画像の貼り付け
				myThis.removeSubPhoto();
				
				if($(this).find("photoItem").size() != 1) {
					
						var increment = 1;
						$(this).find("photoItem").each(function () {
						
						var big = $(this).find("item[sizeType=big]").text();
						var original = $(this).find("item[sizeType=original]").text();
						var small = $(this).find("item[sizeType=small]").text();
						var photoItemName = $(this).attr("alt");
						
						myThis.appendSubPhoto(increment, original, big, small, photoItemName);
						
						increment ++;
					});
				}
			}
		});
	},
	
	//	商品基本情報の更新
	updateItem : function (type1, type2) {
		
		var isEcItem = false;
		
		var myThis = this;
		
		//	在庫から適合する分類を検索
		stockElement.each(function(){
			
			var t1 = $(this).attr("typeId1");
			var t2 = $(this).attr("typeId2");
			
			//	商品規格１・２が設定されている	
			if( (std2Element.size() == 0 && type1 == t1) || (type1 == t1 && type2 == t2) ) {
			
				//	商品価格
				myThis.showItemPrice($(this).find("itemPrice").text());
				
				//	参考価格
				if($(this).find("itemInfoPrice").attr("showFlag") == 'true') {
					myThis.showItemInfoPrice($(this).find("itemInfoPrice").text());
				} else {
					myThis.hideItemInfoPrice();
				}
					
				//	OFF価格
				if($(this).find("itemOffPrice").attr("showFlag") == 'true') {
					myThis.showOffPrice($(this).find("itemOffPrice").text());
				} else {
					myThis.hideOffPrice();
				}
					
				//	在庫状況
				myThis.setStockFlg($(this).find("stock").attr("isStockFlg"));
				
				//	在庫個数
				if($(this).find("stock").attr("showStockNumFlag") == 'true') {
					myThis.showStockNum($(this).find("stock").text());
				} else {
					myThis.hideStockNum();
				}
				
				isEcItem = true;		//	取扱商品
				
				return false;
			}
			
			
		});
		
		//	商品取り扱いか？
		if(isEcItem) {
			//	買い物カゴボタンをDisableにする
			$(cartButtonId).attr("disabled", false);
		
			this.hideItemStdErr();
		} else {
			//	買い物カゴボタンを使用可能にする
			$(cartButtonId).attr("disabled", true);
		
			this.setItemStdErr(errorMsg2);
		}
	}
	
	
}


//	商品規格選択
function selectStdItem() {
	var type1 = $("input:radio[name='selItemStdTypeId1']:checked").val();
	var type2 = $("input:radio[name='selItemStdTypeId2']:checked").val();
	
	if(type1 == undefined && type2 == undefined) {
		//	セレクトボックス
		type1 = $("#itemStdTypeId1").val();
		type2 = $("#itemStdTypeId2").val();
		
	} else {
		//	商品分類の設定(ラジオボタン)
		if(type1 != undefined) {
			var typeName = std1Element.find("type[id=" + type1 + "]").attr("value");
			
			detail.setCartFormStdType1(type1, typeName);
		}
		if(type2 != undefined) {
			var typeName = std2Element.find("type[id=" + type2 + "]").attr("value");
		
			detail.setCartFormStdType2(type2, typeName);
		}
	}
	
	
	//	商品写真の切替
	if(type1 != undefined && type1 != '') {
		if(std1Element.attr("thumbnailFlag") == "true") {
			detail.flipPhoto(type1);
		}
	}
	if(type2 != undefined && type2 != '') {
		if(std2Element.attr("thumbnailFlag") == "true") {
			detail.flipPhoto(type2);
		}
	}
	
	//	規格が正常に設定されている場合のみ商品の基本情報を設定する
	if( (std2Element.size() == 0 && type1 != undefined) || (type1 != undefined && type2 != undefined) ) {
		detail.updateItem(type1, type2);
	}
	
}

//	画像切換え
function flipMain(photoOriginalUrl, photoBigUrl, photoItemName) {
	new EcDetail().setMainPhoto(photoOriginalUrl, photoBigUrl, photoItemName);
}

//	3桁カンマ変換
function replaceComma(str) {
	if(str == null) { return null; }
	
	var num = new String(str).replace(/,/g, "");
	while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
	return num;
}

