/* ↓2重送信対応↓ */
var postingflg= false;
function posingprv(){
if (postingflg) {return(false);}
	postingflg= true;
	setTimeout("clrpostingflg();", 3000);
	return(true);
}
function clrpostingflg(){postingflg= false;}
/* ↑2重送信対応↑ */

/* ↓ヘッダ制御↓ */
function setHeaderBrandCode(code) {
  document.SearchProducts.brdgp.value=code;
}

function setHeaderBrand(code, name) {
  document.SearchProducts.elements[0].value=name;
  document.SearchProducts.brdgp.value=code;
  document.BrandLayer.brdgpname.value=name;
  listClose();
  submitHeader();
  return false;
}

function setHeaderItemCode(code1, code2) {
  document.SearchProducts.item.value=code1;
  document.SearchProducts.sitem.value=code2;
}

function setHeaderItem(code1, code2, name) {
  document.SearchProducts.elements[1].value=name;
  document.SearchProducts.item.value=code1;
  document.SearchProducts.sitem.value=code2;
  document.ItemLayer.itemname.value=name;
  listClose();
  submitHeader();
  return false;
}

function submitHeader() {
var	s1='';

	if (""!=document.SearchProducts.brdgp.value) {
		s1 += ((""!=s1)?"\n":"") + 'sidebrdgp'
			+ "\t" + document.SearchProducts.brdgp.value
			+ "\t" + document.BrandLayer.brdgpname.value;
	}
	if (""!=document.SearchProducts.item.value) {
		s1 += ((""!=s1)?"\n":"") + 'sideitem'
			+ "\t" + document.SearchProducts.item.value
			+ "\t" + document.ItemLayer.itemname.value;
	}
	if (""!=document.SearchProducts.sitem.value) {
		s1 += ((""!=s1)?"\n":"") + 'sidesitem'
			+ "\t" + document.SearchProducts.sitem.value
			+ "\t" + document.ItemLayer.itemname.value;
	}
	document.cookie= "selectValues=" + escape(s1) + ";path=/user/";

	$("#brdgpflg").val(brandListAlphaShowFlg);

  var brdgp = document.SearchProducts.brdgp.value;
  var item = document.SearchProducts.item.value;
  if (brdgp != "") {
	document.SearchProducts.main.value = "brand";
  } else if (item != "") {
	document.SearchProducts.main.value = "item";
  } else {
	document.SearchProducts.main.value = "none";
  }
  document.SearchProducts.submit();
  return false;
}
function do_search_detail() {
	document.MagSearchDetail.submit();
}

function do_search_free() {
	if ( (($("#freeword").css("color") == "rgb(102, 102, 102)")||($("#freeword").css("color") == "#666666")||($("#freeword").css("color") == "#666"))
			   && ($("#freeword").val() == "フリーワード") ) {

		document.MagSearchFree.q.value = "";
	}
	document.MagSearchFree.submit();
}

function setFreeWord(freeword) {
	document.MagSearchFree.q.value = freeword;
}
/* ↑ヘッダ制御↑ */

/* ↓検索ヘッダ↓ */
var pldnFlg= false;			// iframe読み込みフラグ
var menuDne= false;			// HTMLロードフラグ
var loadFinish = false;
var brandMnuClk= false;
var itemMnuClk= false;

var timeID;						// タイマーID保持
var onMouseFlg = true;			// マウスカーソル位置フラグ
var brandListShowFlg;			// ブランドリスト表示フラグ
var brandListAlphaShowFlg = "";	// ブランドリスト（カナ/アルファベット）フラグ
var itemListShowFlg;			// アイテムリスト表示フラグ

/* 外部HTML読み込み */
function pldnclk() {
	if ( !menuDne ) {
		pldnFlg= true;
		document.getElementById("pldnfrme").src= pldnfrmesrc;
	}
}
function pldncmpl() {
	var framedoc;
	if ( pldnFlg ) {
		menuDne= true;
		var frameel = document.getElementById("pldnfrme");
		if (document.all) {
			this.onreadystatechange = null;
			framedoc= frameel.contentWindow.document;
		} else {
			framedoc= frameel.contentDocument;
		}

		$("#searchWindow_nav_left").html(framedoc.getElementById("searchWindow_nav_left").innerHTML.replace(/((\%3c)|(\%3C)|(\&lt\;)|(\<))ep\:imgsrv((\%20)|( ))\/((\%3e)|(\%3E)|(\&gt\;)|(\>))/g, "img04.magaseek.com"));

		$("#searchWindow_list_kana").html(framedoc.getElementById("searchWindow_list_kana").innerHTML.replace(/((\%3c)|(\%3C)|(\&lt\;)|(\<))ep\:imgsrv((\%20)|( ))\/((\%3e)|(\%3E)|(\&gt\;)|(\>))/g, "img04.magaseek.com"));
		$("#searchWindow_list_alpha").html(framedoc.getElementById("searchWindow_list_alpha").innerHTML.replace(/((\%3c)|(\%3C)|(\&lt\;)|(\<))ep\:imgsrv((\%20)|( ))\/((\%3e)|(\%3E)|(\&gt\;)|(\>))/g, "img04.magaseek.com"));
		$("#searchWindow_list_item").html(framedoc.getElementById("searchWindow_list_item").innerHTML.replace(/((\%3c)|(\%3C)|(\&lt\;)|(\<))ep\:imgsrv((\%20)|( ))\/((\%3e)|(\%3E)|(\&gt\;)|(\>))/g, "img04.magaseek.com"));

		brandListShowFlg = false;
		brandListAlphaShowFlg = $("#brdgpflg").val() != "" ? $("#brdgpflg").val() : "kana";
		itemListShowFlg = false;
		loadFinish = true;


		if(brandMnuClk) {
			brandListBoxShow();
		} else if(itemMnuClk) {
			itemListBoxShow();
		}
	}
}

/* ブランドリスト表示 */
function brandListBoxShow() {
	if(loadFinish && !brandListShowFlg) {	/* リスト再表示 */
		if(timeID) {
			clearTimeout(timeID);
		}
		if(itemListShowFlg) {
			$(".searchWindow_list_item").each(function() { $(this).hide(); });
		}
		if(brandListAlphaShowFlg.match(/alpha/)) {
			$(".searchWindow_list_kana").each(function() { $(this).hide(); });
			$(".searchWindow_list_alpha").each(function() { $(this).show(); });
		} else {
			$(".searchWindow_list_alpha").each(function() { $(this).hide(); });
			$(".searchWindow_list_kana").each(function() { $(this).show(); });
		}
		$(".searchWindow_list_brand").each(function() { $(this).show(); });
		$("#searchWindow, #lay_bg").show();

		brandListShowFlg = true;
		itemListShowFlg = false;
		buttonReset();
		setScrollTop();
	} else if(loadFinish && brandListShowFlg) {	/* リスト非表示 */
		listClose();
	} else if(!menuDne) {	/* 初回リスト表示 */
		brandMnuClk = true;
		itemMnuClk = false;
		pldnclk();
	}
	return false;
}

/* アイテムリスト表示 */
function itemListBoxShow() {
	if(loadFinish && !itemListShowFlg) {	/* リスト再表示 */
		if(timeID) {
			clearTimeout(timeID);
		}
		if(brandListShowFlg) {
			$(".searchWindow_list_alpha, .searchWindow_list_kana, .searchWindow_list_brand").each(function() { $(this).hide(); });
		}
		$(".searchWindow_list_item").each(function() { $(this).show(); });
		$("#searchWindow, #lay_bg").show();

		brandListShowFlg = false;
		itemListShowFlg = true;
		buttonReset();
		setScrollTop();
	} else if(loadFinish && itemListShowFlg) {	/* リスト非表示 */
		listClose();
	} else if(!menuDne) {	/* 初回リスト表示 */
		brandMnuClk = false;
		itemMnuClk = true;
		pldnclk();
	}
	return false;
}

/* ブランドリスト（カナ/アルファベット）表示切り替え */
function listChange(img,list) {
	if(!img.match(/_dead\.gif/)) {
		if(timeID) {
			clearTimeout(timeID);
		}
		$("ul#searchWindow_nav_center img:visible").each(function() {
			$(this).attr("src",$(this).attr("src").replace(/_dead\.gif/,"_def.gif"));
		});
		$("ul#searchWindow_nav_center img:visible").each(function() {
			$(this).attr("src",$(this).attr("src").replace(/_ovr\.gif/,"_dead.gif"));
		});
		if(list.match(/kana/)) {
			$(".searchWindow_list_alpha").each(function() {
				$(this).hide();
			});
			$(".searchWindow_list_kana").each(function() {
				$(this).show();
			});
			brandListAlphaShowFlg = "kana";
		} else if(list.match(/alpha/)) {
			$(".searchWindow_list_kana").each(function() {
				$(this).hide();
			});
			$(".searchWindow_list_alpha").each(function() {
				$(this).show();
			});
			brandListAlphaShowFlg = "alpha";
		}
		buttonReset();
		setScrollTop();
	}
	return false;
}

/* リストを閉じる */
function listClose() {
	if(timeID) {
		clearTimeout(timeID);
	}
	buttonReset();
	if(brandListShowFlg) {
		$(".searchWindow_list_alpha, .searchWindow_list_kana, .searchWindow_list_brand").each(function() { $(this).hide(); });
	}
	if(itemListShowFlg) {
		$(".searchWindow_list_item").each(function() { $(this).hide(); });
	}
	$("#lay_bg, #searchWindow").hide();
	brandListShowFlg = false;
	itemListShowFlg = false;
	return false;
}

/* ボタン画像処理 */
function buttonChange(imgTag) {
	if(!imgTag.src.match(/_dead\.gif/)) {
		$("ul#searchWindow_nav_left img:visible").each(function() {
			if($(this).attr("src").match(/_dead\.gif/)) {
				$(this).attr("src",$(this).attr("src").replace(/_dead\.gif/,"_def.gif"));
			} else if($(this).attr("src").match(/_ovr\.gif/)) {
				$(this).attr("src",$(this).attr("src").replace(/_ovr\.gif/,"_dead.gif"));
			}
		});
		return true;
	}
	return false;
}

/* ボタン画像処理 */
function buttonReset() {
	$("ul#searchWindow_nav_left li img").each(function() {
		if($(this).attr("src").match(/_dead\.gif/)) {
			$(this).attr("src",$(this).attr("src").replace(/_dead\.gif/,"_def.gif"));
		} else if($(this).attr("src").match(/_ovr\.gif/)) {
			$(this).attr("src",$(this).attr("src").replace(/_ovr\.gif/,"_def.gif"));
		}
	});
	return false;
}

/* スクロール処理 */
function Set_moveElement(elementID){
	if(timeID) {
		clearTimeout(timeID);
	}

	// 移動先のoffsetTop取得
	var end_pos = $("#" + elementID).offset().top;

	// 基準のoffsetTop取得
	var y_pos = $("#searchWindow_list_holder").offset().top;

	// 表示位置変更
	base_obj = document.getElementById('searchWindow_list_holder');
	var before_pos = base_obj.scrollTop;

	// 小数点除去
	if (end_pos > 0) {
		end_pos = Math.floor(end_pos);
	}else{
		end_pos = Math.ceil(end_pos);
	}
	if (y_pos > 0) {
		y_pos = Math.floor(y_pos);
	}else{
		y_pos = Math.ceil(y_pos);
	}

	// 位置の差分
	var distance = end_pos - y_pos;
	if(distance < 0) distance -= 1;

	// 実行速度設定
	var interval = new Number(30);

	// 実行処理
	do_moveElement(distance,interval,before_pos);
}

/*
 * スクロール処理
 */
function do_moveElement(distance,interval,before_pos){
	// 移動距離保持
	var move = 0;
	if(distance > 0 ) move = Math.ceil((distance)/8);
	if(distance < 0 ) move = Math.floor((distance)/8);
	distance -= move;

	// 表示位置変更
	base_obj = document.getElementById('searchWindow_list_holder');
	base_obj.scrollTop += move;
	now_pos = base_obj.scrollTop

	// 終了条件を満たしていれば終わり
	if(distance == 0 || before_pos == now_pos) return false;

	// 実行処理繰り返し
	var repeat = "do_moveElement("+distance+","+interval+","+now_pos+")";
	timeID = setTimeout(repeat,interval);
}

// 検索スクロール処理
function indexSerch(imgTag,elementID){
	if(buttonChange(imgTag)) {
		Set_moveElement(elementID);
	}
	return false;
}

/* マウスホイール時、スクロールの中断 */
function wheel(event){
	if(timeID) {
		clearTimeout(timeID);
	}
}

/* 表示位置リセット */
function setScrollTop() {
	base_obj = document.getElementById('searchWindow_list_holder');
	base_obj.scrollTop = 0;
}
/* ↑検索ヘッダ↑ */

/* ready */
$(function() {
	/* マウスカーソル位置フラグ */
	$("#searchWindow,#brandlistbox,#itemlistbox,.BrandLayer_pack,.ItemLayer_pack").mouseout(function() {
		onMouseFlg = false;
	});
	$("#searchWindow,#brandlistbox,#itemlistbox,.BrandLayer_pack,.ItemLayer_pack").mouseover(function() {
		onMouseFlg = true;
	});

	/* ウインドウ除去 */
	$("body").click(function() {
		if(!onMouseFlg && (brandListShowFlg || itemListShowFlg)) {
			listClose();
		}
	});

	/* マウルホイールイベント設定 */
	if(window.addEventListener) {
		window.addEventListener('DOMMouseScroll', wheel, false);
	} else {
		window.onmousewheel = document.onmousewheel = wheel;
	}

	/* 2重送信対応 */
	postingflg= false;
	$("form").submit(function(){return(posingprv());});
	$("a").click(function(){return(posingprv());});

	/* フリーワードデフォルト制御 */
	$("#freeword").focus(function() {
		if($(this).val() == $(this).attr('defaultValue'))
			$(this).val('');
			$(this).css({"color":"#000000"});
	}).blur(function() {
		if(jQuery.trim($(this).val()) == "") {
			$(this).val($(this).attr('defaultValue'));
			$(this).css({"color":"#666666"});
		}
	});
});
