// インジケーター用画像ファイルのパス
var INDICATOR_GIF = "/common/img/indicator.gif";

var jewelryXmlFilePath = "/jewelry/xml/jewelry.xml";
var xmlData;
var searchRes;

var DISP_MAX = 12;
// サムネイルサイズ
var IMAGE_SIZE = 96;
var pageNo = 1;


var notReady = 2; // 非同期動作未完了カウンタ

$(function(){
	// インジケーター表示
	dispSearchIndicator();

	checkReady();
});

// インジケーター表示
function dispSearchIndicator() {
	$(".favoriteList02").empty();
	// ページナビクリア
	$(".pageNavBox").empty();
	// インジケータを表示
	$(".pageNavBox").html("<div class=\"jewelryNaBox02\" align=\"center\"><img src=\"" + INDICATOR_GIF + "\"/></div>");
}


$.event.add(window, "load",
	function() {
		// 商品情報XMLの読み込み
		// 非同期化
		fdc_util_readJewelryXML(jewelryXmlFilePath,
			function(xml) {
				xmlData = xml;
				checkReady();
			}
		);
	}
);


function checkReady(){
	if(--notReady == 0) {
		finalInitialize();
	}
}

function finalInitialize(){
	searchData();
	dispSearchRes();
	dispPageNavi();
}

function searchData() {
	searchRes = new Array();
	
	var temp = $.cookie('favoriteItemInfo');
	if (temp == null || temp == '') {
		return;
	}

	var favoriteItemAry = temp.split(';');

	$.each(xmlData.jewelryList[0].jewelry,

		function() {
			var itemId = this.id[0];
			
			var flg = false;
			for (i=0; i<favoriteItemAry.length; i++) {
				if (favoriteItemAry[i] == itemId) {
					flg = true;
					break;
				}
			}
			if (!flg) {
				return;
			}

			var temp = new Array();
			temp.id = itemId;
			temp.price = this.price[0];

			temp.dispMaterial = "";
			if (this.dispMaterial != null) {
				temp.dispMaterial = this.dispMaterial[0];
			}

			temp.entrylink = "";
			if (this.entrylink != null) {
				temp.entrylink = this.entrylink[0];
			}

			temp.thumbImageURL = "";
			if (this.thumbnail2 != null) {
				temp.thumbImageURL = this.thumbnail2[0];
			} else {
				var thumbId = 1;
				if (this.imageList[0].thumbnail != null) {
					thumbId = this.imageList[0].thumbnail[0];
					if (this.imageList[0].imageURL.length < thumbId) {
						thumbId = 1;
					}
				}
				temp.thumbImageURL = this.imageList[0].imageURL[thumbId - 1];
			}

			searchRes.push(temp);
		}

	);
}


// お気に入り登録してある商品を表示
function dispSearchRes() {

	// 商品表示クリア
	$(".favoriteList02").empty();

	$.each(searchRes, function(i,val) {

		if (i < (pageNo - 1) * DISP_MAX
		|| pageNo * DISP_MAX - 1 < i) {
			return;
		}

		if (i % 3 == 0) {
			$(".favoriteList02").append($("<ul/>"));
		}

		var dispId = "No." + val.id;
		var dispMaterial = val.dispMaterial;
		var dispPrice = "\\" + fdc_util_insertComma(val.price) + "(税込)";
		
		$(".favoriteList02 > ul:last")
		
			.append($("<li/>")
				.append($("<div/>")
					.addClass("thumbBox")
					.append($("<a/>")
						.attr("href", val.entrylink)
						.append($("<img/>")
							.attr("src", val.thumbImageURL)
							.attr("width", IMAGE_SIZE)
							.attr("height", IMAGE_SIZE)
							.attr("alt", "")
						)
					)
					.append($("<div/>")
						.addClass("iconList")
						.append($("<a/>")
							.attr("class", "del")
							.click(
								function() {
									$(this).parent().parent().parent().fadeOut("slow",
										function() {
											onClickFavoriteDelBtn(val.id);
										}
									);
								}
							)
							.append($("<img/>")
								.attr("src", "/common/img/jewelry/btn_delete.gif")
								.attr("width", "13")
								.attr("height", "12")
								.attr("alt", "削除")
							)
						)
					)
				)
				.append($("<dl/>")
					.append($("<dt/>")
						.text("品番")
					)
					.append($("<dd/>")
						.text(dispId)
					)
				)
			);

		if (dispMaterial != '') {
			$(".favoriteList02 > ul:last > li:last > dl")
				.append($("<dt/>")
					.text("素材")
				)
				.append($("<dd/>")
					.text(dispMaterial)
				);
		}

		$(".favoriteList02 > ul:last > li:last > dl")
			.append($("<dt/>")
				.text("価格")
			)
			.append($("<dd/>")
				.text(dispPrice)
			);

	});

}

function dispPageNavi() {
	var lastPageNo = Math.ceil(searchRes.length / DISP_MAX);

	$(".pageNavBox").empty();

	$(".pageNavBox").append($("<p/>").addClass("txtCenter"));

	if (pageNo >=2) {
		$(".pageNavBox").children("p")
			.append($("<a/>")
				.addClass("prev")
				.attr("href","#")
				.click(
					function() {
						changePage(pageNo-1);
						return false;
					}
				)
				.append($("<img/>").attr("src", "/common/img/jewelry/pnav_products_prev.gif")
					.attr("width", "65")
					.attr("height", "16")
					.attr("alt", "前の商品")
				)
			);
	}

	var startNo;
	var endNo;
	if (lastPageNo < 10) {
		startNo = 1;
		endNo = lastPageNo;
	} else {
		if (pageNo <= 6) {
			startNo = 1;
			endNo = 10;
		} else {
			if ( pageNo + 4 <= lastPageNo) {
				startNo = pageNo - 5;
				endNo = pageNo + 4;
			} else {
				startNo = pageNo -5 - ((pageNo + 4) - lastPageNo);
				endNo = lastPageNo;
			}
		}
	}

    var createChangePage = function(index) {
    	return function() {
    		changePage(index);
    	}
    }

	for (var i = startNo; i <= endNo; i++) {
		if (i == pageNo) {
			$(".pageNavBox").children("p")
				.append($("<span/>")
					.text(i)
				);
		} else {
			$(".pageNavBox").children("p")
				.append($("<a/>")
					.attr("href","#")
					.click(
						createChangePage(i)
					)
					.text(i)
				);
		}
	}


	if (pageNo < lastPageNo) {
		$(".pageNavBox").children("p")
			.append($("<a/>")
				.addClass("next")
				.attr("href","#")
				.click(
					function() {
						changePage(pageNo+1);
						return false;
					}
				)
				.append($("<img/>").attr("src", "/common/img/jewelry/pnav_products_next.gif")
					.attr("width", "67")
					.attr("height", "16")
					.attr("alt", "次の商品")
				)
			);
	}
	
}


// 指定したページに切り替える
function changePage(no) {
	pageNo = no;
	// 検索条件はそのままで、結果をページ番号にしたがって表示する
	dispSearchRes();
	// ページナビ表示
	dispPageNavi();
}

// お気に入りから指定した商品IDを削除する
function onClickFavoriteDelBtn(id) {
	
	var temp = $.cookie('favoriteItemInfo');
	var favoriteItemAry = temp.split(';');

	// favoriteItemAryから商品ID=idの要素を削除
	for(i = 0; i < favoriteItemAry.length; i++){
		if (favoriteItemAry[i] == id){
			favoriteItemAry.splice(i,1);
		}
	}
	var favoriteItemInfo = favoriteItemAry.join(';');
	$.cookie('favoriteItemInfo', favoriteItemInfo, { expires: FDC_COOKIE_EXP, path:FDC_COOKIE_PATH });

	// searchResから商品ID=idの要素を削除
	for(i = 0; i < searchRes.length; i++){
		if (searchRes[i].id == id){
			searchRes.splice(i,1);
		}
	}

	var lastPageNo = Math.ceil(searchRes.length / DISP_MAX);
	if (pageNo > lastPageNo) {
		pageNo = pageNo - 1;
	}

	changePage(pageNo);
}

