$jq = jQuery.noConflict();

var SERAI = {};

SERAI.VOICE = {
	path   : window.location.pathname,
	file   : "index.html",
	domain : "http://serai-bimi.jp/",
	styles : ["","star1","star2","star3","star4","star5"],
	stars  : ["","★","★★","★★★","★★★★","★★★★★"]
};

SERAI.POPUP = {} ;



$jq(function(){
	
	//body要素のid・Classの取得
	var BODYCLASS = $jq("body").attr("class");
	var BODYID = $jq("body").attr("id");
	
	//http://〜から始まるリンクに自動でtarget="_blank"を付加する
	$jq('a[href^="http://"]').attr("target","_blank");	
	$jq('a[href^="https://"]').attr("target","_blank");	

/*
	//ポップアップ制御
	$jq("a[rel='popup-magazine']").click(function(){
		var URL = this.href ;
		$jq(this).removeAttr("target");
		new_window = window.open(URL,"magazine","width=666,height=550,resizable=yes,scrollbars=yes");
		if(window.focus){new_window.focus()};
		return false;
	});
*/

	//ポップアップ制御
	$jq('a[href*="?popup="]').click(function(e){
														  
		$jq(this).removeAttr("target");
		
		var val = this.search ;
		var url = (this.href).replace(val,"");
		
		var vals = (val.split("=")[1]).split("+");
		
		//alert(url + " : " + val +  " : " + vals );
		
		if(!SERAI.POPUP[vals[2]] || SERAI.POPUP[vals[2]].closed) SERAI.POPUP[vals[2]] = window.open(url,"","width=" + vals[0] +",height=" + vals[1] +",resizable=0,toolbar=0,scrollbars=yes");
		if(window.focus) SERAI.POPUP[vals[2]].focus();
				
		if(e.preventDefault)e.preventDefault();
		else e.returnValue = false ;
		if(e.stopPropagation)e.stopPropagation();
		else e.cancelBubble = true ;
	});


	//アンカーリンクのスムーズスクロール（同一ページのみ）
	$jq("a[href*='#']:not(a[href*='/'])").click(function(){
												  
		var Anchor = $jq(this).attr("href");
		var hash = $jq(this)[0].hash ;
		//alert(($jq(this) instanceof jQuery) + " : " + hash);
		if(hash.indexOf("webitem") != -1) return ;
		
		if(Anchor !== "javascript:void(0);"){
			var Place = $jq(Anchor).offset().top;
			
			var WinH = $jq(window).height();
			
			if($jq.browser.msie && $jq.browser.version == 6){
				var BodyH = $jq(document).height();
			} else {
				var BodyH = $jq("body").height();
			}
			var AnchorB = BodyH - Place ;
			
			var AnchorPlace = WinH - AnchorB;
			
			//$jq("<p>").html("BodyH" + BodyH).appendTo("body").css("color","#009999");
			//$jq("<p>").html("AnchorB" + AnchorB).appendTo("body").css("color","#009900");
			//$jq("<p>").html("WinH" + WinH).appendTo("body");
			//$jq("<p>").html("AnchorPlace" + AnchorPlace).appendTo("body").css("color","#990000");
			//$jq("<p>").html("Place" + Place).appendTo("body").css("color","#000099");
						
			if(AnchorB < WinH && 0 < (Place - AnchorPlace)){
				if($jq.browser.safari || $jq.browser.msie && $jq.browser.version == 6){
					$jq("body").animate({scrollTop: (Place - AnchorPlace) },{queue:false,duration:800,easing:"easeOutQuart"});
				} else {
					$jq("html").animate({scrollTop: (Place - AnchorPlace) },{queue:false,duration:800,easing:"easeOutQuart"});
				}
			} else {
				if($jq.browser.safari || $jq.browser.msie && $jq.browser.version == 6){
					$jq("body").animate({scrollTop: Place},{queue:false,duration:800,easing:"easeOutQuart"});
				} else {
					$jq("html").animate({scrollTop: Place},{queue:false,duration:800,easing:"easeOutQuart"});
				}
			}
			return false ;
		}
		
	});
		

	//グローバルナビのエフェクト
	$jq("#globalNavi li").hover(
		function(){
			$jq(this).children().animate({"opacity" : 0 },{queue:false,duration:400,easing:"easeOutQuart"}).css("text-indent","-9999px");
		},
		function(){
			$jq(this).children().animate({"opacity" : 1.0 },{queue:false,duration:500,easing:"easeOutQuart"});
		}
	);	
	
	/* 簡易商品検索 ------------------------------------------- */

	//検索フォーム生成
	var Searchbox = $jq("<div>").attr("id","side-searchbox").addClass("sidebox");

	if(document.getElementById("newitems")) Searchbox.insertBefore("#side-inqbox");
	else Searchbox.prependTo("#side");
	
	var SearchboxChild = $jq("<div>").addClass("sidebox-child").appendTo(Searchbox);
	
	var Searchtitle = $jq("<h2 class=\"replace\"><em>商品検索</em></h2>").appendTo(SearchboxChild);
	var WrapSearch = $jq("<div>").addClass("wrapSearch").appendTo(SearchboxChild);
		
	var SearchIpt = $jq("<input type=\"text\" id=\"searchInput\" value=\"キーワードを入力\" />").appendTo(WrapSearch);
	if($jq.browser.mozilla){
		SearchIpt.css({
			haight : "auto",
			padding : "1px 0 0"
		});
	}
	SearchIpt.focus(function(){
		SearchIpt.addClass("JsInputFocus");
		if(SearchIpt.val() == "キーワードを入力") SearchIpt.val("");
	});
	SearchIpt.blur(function(){
		SearchIpt.removeClass("JsInputFocus");
		if(SearchIpt.val() == "") SearchIpt.val("キーワードを入力");
	});
	
	var SearchBtn = $jq("<div id=\"searchBtn\" class=\"multiButton\"><a href=\"#\" title=\"商品検索\"><span>検索</span></a></div>").appendTo(SearchboxChild);
	var SearchAlert = $jq("<p id=\"searchAlert\">※キーワードが入力されていません。</p>").appendTo(SearchboxChild).hide();

	//検索結果表示部分を生成
	var resultParseBox = $jq("<div id=\"resultParseBox\">").prependTo("#contents").hide();
	
	var SearchResultHead = function(keyword_array,keyword_length){
		$jq("<h1 id=\"resultTitle\" class=\"replace\"><em>商品検索</em></h1>").appendTo(resultParseBox);
		$jq("<p class=\"SearchResultHead\">").text("キーワード：「" + keyword_array.join("」「") + "」の検索結果(" + keyword_length + "件)").appendTo(resultParseBox);
	}
	
	var insertReturnP = function(){
		var ReturnPageP = $jq("<p class=\"LinkReturn\"></p>").appendTo(resultParseBox);
		var ReturnPageA = $jq("<a href=\"#\">元のページに戻る</a>").appendTo(ReturnPageP);
		ReturnPageA.click(function(Event){
			var _e = Event || window.event ;
			
			$jq("#contents > :not(:first-child)").show();
			resultParseBox.fadeOut(200);
			
			if(_e.preventDefault)_e.preventDefault();
			else _e.returnValue = false ;
			if(_e.stopPropagation)_e.stopPropagation();
			else e.cancelBubble = true ; 
		});
	}
	
	//検索処理
	SearchBtn.click(function(Event){
		
		var _e = Event || window.event ;
						
		var _Word = SearchIpt.val();
		if(_Word == "キーワードを入力") _Word = "" ;
		if(_Word == ""){
			SearchAlert.show();
			
			if(_e.preventDefault)_e.preventDefault();
			else _e.returnValue = false ;
			if(_e.stopPropagation)_e.stopPropagation();
			else e.cancelBubble = true ; 
			return ;
		}
		else SearchAlert.hide();
		
		_Word = _Word.replace(/</g,"&lt;");
		_Word = _Word.replace(/>/g,"&gt;");
		_Word = _Word.replace(/　/g," ");
		_Word = _Word.replace(/\b\s+/g,"");
		_Word = _Word.replace(/\b\s+\b/g," ");
		_Word = _Word.split(/\s/);

		$jq("#contents > :not(:first-child)").hide();

		$jq.ajax({
			url           : "http://serai-bimi.jp/js/serai_search_data.json",
			//url           : "http://ingsnet.com/marunaga/serai_script_test/serai_search_data.json",
			dataType      : "jsonp" ,
			jsonp         : "callback" ,
			type          : "GET" ,
			cache         : true ,
			timeout       : 3000 ,
			scriptCharset : "utf-8",
			beforeSend  : function(data){
				resultParseBox.empty();
				resultParseBox.fadeIn(200);
				$jq("<p id=\"loading\">データを取得中です。</p>").appendTo(resultParseBox);
			},
			error       : function(data){
				resultParseBox.empty();
				insertReturnP();
				$jq("<p id=\"error\">データが取得できませんでした。</p>").appendTo(resultParseBox);
				
				$jq("#contents > :not(:first-child)").hide();
			},
			success     : SiteSearch = function(data,status){
				
				if(data.entries){
			
					resultParseBox.empty();
					
					var _searchItems = new Array();
					
					for(var i = 0 ; i < data.entries.length ; i++){
							
						var _in_word = false ;
	
						var alltxt = new Array();
						for(var prop in data.entries[i]) alltxt += data.entries[i][prop];
						
						var _count = 0 ;
						for(var j = 0 ; j < _Word.length ; j++){
							if(alltxt.indexOf(_Word[j]) == -1) continue ;
							_count++ ;
							if(_Word.length == _count) _in_word = true ;
						}
						
						if(_in_word) _searchItems.push(data.entries[i]);
					
					}
					
					if(_searchItems.length != 0){
						
						SearchResultHead(_Word,_searchItems.length);
						insertReturnP();
						
						for(var k = 0 ; k < _searchItems.length; k++){
							$jq("<div class=\"SearchSection\"><h2><a href='" +
								  _searchItems[k].link +"'>" +
								  _searchItems[k].title + "[" + _searchItems[k].category + "]</a></h2><p class=\"SearchDescription\">「" + 
								  _searchItems[k].description + "」</p><p>" +
								  _searchItems[k].contents.substring(0,100) + "…" + "</p>" + 
								  "<p class=\"SearchLink\"><a href='" + _searchItems[k].link +"'>「" + _searchItems[k].title + "」詳細ページ</a><p>" +
								  "</div>").appendTo(resultParseBox);
						}
					}
					else {
						
						SearchResultHead(_Word,_searchItems.length);
						insertReturnP();
						$jq("<div class=\"SearchAttention\"><h2>" + "該当の商品が見つかりませんでした。" + "</h2></div>").appendTo(resultParseBox);
					}
				}
				else{ 
					resultParseBox.empty();
					$jq("<div class=\"SearchAttention\"><h2>" + data.error + "</h2></div>").appendTo(resultParseBox);
					insertReturnP();
				}

			resultParseBox.fadeIn(200);
			}
		});
		
		if(_e.preventDefault)_e.preventDefault();
		else _e.returnValue = false ;
		if(_e.stopPropagation)_e.stopPropagation();
		else e.cancelBubble = true ; 
	});

});



$jq(function(){
	if($jq("body").attr("id") != "item-detail-page") return ;
	
	if($jq.browser.msie){	//IE7以下 かごに入れるボタンhover active
		
		$jq("#button-buy button").hover(
			function(){
				$jq("#button-buy button").addClass("hover"); 
			},
			function(){
				$jq("#button-buy button").removeClass("hover"); 
			}
			);
		$jq("#button-buy button").mousedown(
			function(){
				$jq("#button-buy button").addClass("active"); 
			});
		$jq("#button-buy button").mouseup(
			function(){
				$jq("#button-buy button").removeClass("active"); 
			});
	
		$jq(".button-buy button").hover(
			function(){
				$jq(this).addClass("hover"); 
			},
			function(){
				$jq(this).removeClass("hover"); 
			}
			);
		$jq(".button-buy button").mousedown(
			function(){
				$jq(this).addClass("active"); 
			});
		$jq(this).mouseup(
			function(){
				$jq(".button-buy button").removeClass("active"); 
			});
	}	//IE7以下 かごに入れるボタンhover active
	
	//side-list 伸縮・改
	$jq("#side-list dt").css("cursor","pointer");
	$jq("#side-list dt").toggle(
		function(){
			$jq(this).nextAll("dd").hide(0);
			$jq(this).removeClass("minus");
			$jq(this).addClass("plus");
		},
		function(){
			$jq("#side-list dt:has('dd:visible')").trigger("click");
			$jq(this).removeClass("plus");
			$jq(this).addClass("minus");
			$jq(this).nextAll("dd").show(0);
		}
	);
	$jq("#side-list dt").trigger("click");
	$jq("#side-list h2+dl>dt").trigger("click");
	

	//お客様の声 ///////////////////////////////////////////////////////////////
	var maintext = $jq("#detail-maintext");
	
	$jq.ajax({
			url           : "../../../js/customers_voice.txt",
			dataType      : "text" ,
			type          : "GET" ,
			cache         : true ,
			timeout       : 3000 ,
			beforeSend : function(res){

			},
			error : function(res){

			},
			success : function(res){

				if (navigator.appVersion.indexOf( "KHTML" ) > -1){
					var esc = escape(res);
					if (esc.indexOf("%u") < 0 && esc.indexOf("%") > -1) res = decodeURIComponent(esc);
				}

				var lines = res.split("\n");
				lines.reverse() ;

				var commentbox = $jq('<div id="detail-commentbox"></div>');

				for(var i = 0 ; i<lines.length; i++){
					var data = lines[i].split(",");
					if(!data[0]) continue ;
					if(data[0] == "非公開") continue ;
					if(SERAI.VOICE.path.indexOf((data[2].replace(SERAI.VOICE.domain,""))) == -1) continue ;

					$jq(
						'<div class="commentbox-s">' +
						'<h3>' + data[5] + '</h3>' +
						'<p class="replace icon-stars ' + SERAI.VOICE.styles[data[3]] + '"><em>おすすめ度：' + SERAI.VOICE.stars[data[3]] + '</em></p>' +
						'<p>' + data[6] + '</p>' +
						'<p class="commentname">【' + data[4] + '】</p>' +
						'</div>'
					).appendTo(commentbox);
					
				}
				
				if(commentbox.children().length == 0){
					commentbox = null ;
					return ;
				}
				else 	commentbox.insertAfter(maintext);

				$jq(
					'<ul id="commentboxtab">' +
					'<li class="multiButton" id="tab-maintext"><a href="#detail-maintext" title="ナビゲーション：商品の詳細"><span>商品の詳細</span></a></li>' +
					'<li class="multiButton" id="tab-comment"><a href="#detail-commentbox" title="ナビゲーション：お客様からの声"><span>お客様からの声</span></a></li>' +
					'</ul>'
				).insertBefore(maintext);
				
				changeTab();
			}
	});
	

	//コメントタブ切替
	function changeTab(){
		var CommentboxHeight = $jq("#detail-commentbox").height();
		var MaintextHeight = $jq("#detail-maintext").height();
		
		$jq("#detail-commentbox .icon-stars").css("color","#ffffff");
	
		$jq("#commentboxtab li a").attr("href","javascript:void(0);");
		$jq("#commentboxtab #tab-maintext").addClass("jsActive");
		$jq("#detail-commentbox").css("opacity","0").hide();
		$jq("#tab-maintext").click(function(){
			$jq("#tab-comment").removeClass("jsActive");
			$jq(this).addClass("jsActive");
			$jq("#detail-maintext:hidden").css("height",CommentboxHeight).animate({"height" : MaintextHeight , "opacity" : 1.0 },{queue:false,duration:300,easing:"easeInOutSine"});
			$jq("#detail-commentbox:visible").animate({"opacity" : 0},{queue:false,duration:100,easing:"easeInOutSine"}).hide();
		});
		$jq("#tab-comment").click(function(){
			$jq("#tab-maintext").removeClass("jsActive");
			$jq(this).addClass("jsActive");
			$jq("#detail-commentbox:hidden").css("height",MaintextHeight).animate({"height" : CommentboxHeight , "opacity" : 1.0  },{queue:false,duration:300,easing:"easeInOutSine"});
			$jq("#detail-maintext:visible").animate({"opacity" : 0},{queue:false,duration:100,easing:"easeInOutSine"}).hide();
		});
	}

});



$jq(function(){
	
	var target = $jq("#multiForms");
	if(!target) return ;
	
	$jq(
		'<form id="setSelector" action="#"><ul>' +
		'<li><input type="radio" name="setSelect" value="0" checked="checked" />' + target.find("h2").eq(0).text() + '　(' + target.find("dl:first dd:first").text() + ')</li>' +
		'<li><input type="radio" name="setSelect" value="1" />' + target.find("h2").eq(1).text() + '　(' + target.find("dl:eq(1) dd:first").text() + ')</li>' +
		'<li><input type="radio" name="setSelect" value="2" />' + target.find("h2").eq(2).text() + '　(' + target.find("dl:eq(2) dd:first").text() + ')</li>' +
		'</ul></form>'
		).prependTo(target);
	
	var selector = $jq("#setSelector");
	var forms = target.children("form").not("#setSelector")
	
	var radios =  selector.find("input[type=radio]");
	$jq.each(radios,function(i){
		if(this.checked){
			forms.eq(this.value).show();
			forms.not(":eq(" + this.value + ")").hide();
		}
	});
	
	radios.click(function(e){
		var index = parseInt(this.value);
		
		//alert(index);
		
		forms.eq(index).fadeIn(300);
		forms.not(":eq(" + index + ")").hide();
		
		e.stopPropagation();
	});
	
	
	//window._forms = forms ;
	//window._radios = radios ;
	
	
});
