/*
 * Thickbox 3.1 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2007 cody lindley
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/

var tb_pathToImage = "/common/img/thickloading.gif";

/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/

//on page load call tb_init

//-ejc-// to html document //start
jQuery.noConflict();
var j$ = jQuery;
//-ejc-// to html document //end

j$(document).ready(function(){
	tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
});


// *******************************************************
// *
// *  ページ読み込み完了時
// *
// *******************************************************
var tb_autoopen;
j$(function(){
	if(tb_autoopen){
		// 関数がすでにある場合、関数の中身を差し替える
		// change-navi.jsでAjax完了時に実行される
		tb_autoopen = tb_open;
	}else{
		// 関数がなかった場合、そのまま実行
		tb_open();
	}
	
});

function tb_nomber(dom) {
	if ( typeof(dom) == "string" ) {
		tb_show(null,dom,false);
	} else {
		var t = dom.title || dom.name || null;
		var a = dom.href || dom.alt;
		var g = dom.rel || false;
		tb_show(t,a,g);
		dom.blur();
		return false;
	}
	//return false;
}
// *******************************************************
// *
// * フローティングウィンドウを自動表示させる
// *
// *******************************************************
function tb_open(){
	var thick=swfobject.getQueryParamValue("thick");
	var pattern=/\w+(\.html|\.htm|\.php|\.jsp|\.cgi)/i;
	if(thick.match(pattern)!=null){
		// ※クエリがHTMLだったとき
		var url=unescape(thick);
		var p=j$.getQueryParams();
		var p_arr=[];
		for(var e in p){
			if(e!="thick")p_arr.push(e+"="+p[e]);
		}
		if(p_arr.length>0)url+="?"+p_arr.join("&");
		var hash=document.location.hash;
		if(hash!="")url+=hash;
		tb_click(url);
	}else{
		switch(thick){
		case 'recommends':
			// ※オススメ商品
			var category = swfobject.getQueryParamValue("category");
			var itemid = swfobject.getQueryParamValue("itemid");
			tb_click('/storetopics/1/'+storenumber+'/info-new.html?category='+category+'&itemid='+itemid);
			break;
		case 'events':
			// ※フェアイベント
			var itemid = swfobject.getQueryParamValue("itemid");
			tb_click('/storetopics/2/'+storenumber+'/info-new.html?itemid='+itemid);
			break;
		case 'features':
			// ※特集掲示板
			var thid = swfobject.getQueryParamValue("thid");
			tb_click('/storetopics/4/'+storenumber+'/info-new.html?thid='+thid);
			break;
		case 'auto':
		case 'price':
			// ※プチプライス
			var category = swfobject.getQueryParamValue("category");
			var itemid = swfobject.getQueryParamValue("itemid");
			var query_arr = [];
			if(category) query_arr.push('category='+category);
			if(itemid) query_arr.push('itemid='+itemid);
			var query_str = query_arr.join('&');
			tb_click('/storetopics/6/'+storenumber+'/info-new.html?'+query_str);
			break;
		case 'floorinfo':
			// ※フロアインフォ
			var locationURL = String(document.location);
			locationURL = locationURL.substring(0,locationURL.indexOf("?"));
			var locationURL_arr = locationURL.split(".");
			locationURL_arr.pop();
			locationURL = locationURL_arr.join(".");
			tb_click(locationURL + 'f.html');
			break;
		case 'brands':
			// ※ブランド検索｜頭文字から検索
			var p=j$.getQueryParams();
			p["brands"]="1";
			var p_arr=[];for(var e in p)p_arr.push(e+"="+p[e]);
			
			var loc=String(document.location);
			loc=loc.substring(0,loc.indexOf("?"));
			var loc_arr=loc.split(".");loc_arr.pop();loc=loc_arr.join(".");
			tb_click(loc+'f.html?'+p_arr.join('&'));
			break;
		case 'category':
			// ※ブランド検索｜カテゴリ名から検索
			var p=j$.getQueryParams();
			p["category"]="1";
			var p_arr=[];for(var e in p)p_arr.push(e+"="+p[e]);
			
			var loc=String(document.location);
			loc=loc.substring(0,loc.indexOf("?"));
			var loc_arr=loc.split(".");loc_arr.pop();loc=loc_arr.join(".");
			tb_click(loc+'f.html?'+p_arr.join('&'));
			break;
		}
	}
}



//add thickbox to href & area elements that have a class of .thickbox
function tb_init(domChunk){
	j$(domChunk).click(function(){
		var t = this.title || this.name || null;
		var a = this.href || this.alt;
		var g = this.rel || false;
		tb_show(t,a,g);
		this.blur();
		return false;
	});
}

function tb_click(dom) {
	if ( typeof(dom) == "string" ) {
		tb_show(null,dom,false);
	} else {
		var t = dom.title || dom.name || null;
		var a = dom.href || dom.alt;
		var g = dom.rel || false;
		tb_show(t,a,g);
		dom.blur();
		return false;
	}
	//return false;
}

function tb_show(caption, url, imageGroup) {//function called when the user clicks on a thickbox link

	try {
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			j$("body","html").css({height: "100%", width: "100%"});
			j$("html").css("overflow","hidden");
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
				j$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay' onclick='tb_remove();return false;'></div><div id='TB_window'></div>");
				j$("#TB_overlay").click(tb_remove);
			}
		}else{//all others
			if(document.getElementById("TB_overlay") === null){
				j$("body").append("<div id='TB_overlay' onclick='tb_remove();return false;'></div><div id='TB_window'></div>");
				j$("#TB_overlay").click(tb_remove);
			}
		}
		
		if(tb_detectMacXFF()){
			j$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			j$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
		}
		
		//if(caption===null){caption="";}
		j$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		j$('#TB_load').show();//show loader
		
		var baseURL;
	   if(url.indexOf("?")!==-1){ //ff there is a query string involved
			baseURL = url.substr(0, url.indexOf("?"));
	   }else{ 
	   		baseURL = url;
	   }
	   
	   var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
	   var urlType = baseURL.toLowerCase().match(urlString);

		if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){//code to show images
				
			TB_PrevCaption = "";
			TB_PrevURL = "";
			TB_PrevHTML = "";
			TB_NextCaption = "";
			TB_NextURL = "";
			TB_NextHTML = "";
			TB_imageCount = "";
			TB_NomberHTML = ""; //番号リンク
			TB_FoundURL = false;
			if(imageGroup){
				TB_TempArray = j$("a[rel="+imageGroup+"]").get();
				for (TB_Counter = 0; ((TB_Counter < TB_TempArray.length) && (TB_NextHTML === "")); TB_Counter++) {
					var urlTypeTemp = TB_TempArray[TB_Counter].href.toLowerCase().match(urlString);
						if (!(TB_TempArray[TB_Counter].href == url)) {						
							if (TB_FoundURL) {
								TB_NextCaption = TB_TempArray[TB_Counter].title;
								TB_NextURL = TB_TempArray[TB_Counter].href;
								TB_NextHTML = "<span id='TB_next'><a href='#'>Next &gt;</a></span>";
							} else {
								TB_PrevCaption = TB_TempArray[TB_Counter].title;
								TB_PrevURL = TB_TempArray[TB_Counter].href;
								TB_PrevHTML = "<span id='TB_prev'><a href='#'>&lt; Prev</a></span>";
							}
						} else {
							TB_FoundURL = true;
							act = (TB_Counter);
							TB_imageCount = "Image " + (TB_Counter + 1) +" of "+ (TB_TempArray.length);											
						}
				}
				for (TB_Counter = 0; (TB_Counter < TB_TempArray.length); TB_Counter++) {
				//alert(TB_TempArray.length);
				//番号リンク生成
				a="";
					if(TB_Counter == act ){
						TB_NomberHTML += "<span id='TB_no"+TB_Counter+"'  class='d'>"+(TB_Counter+1)+"</span>";
					}else{
						TB_NomberHTML += "<span id='TB_no"+TB_Counter+"'><a href='#'>"+(TB_Counter+1)+"</a></span>";
					}
				}
				
			}
			if(!TB_NextHTML&&imageGroup)TB_NextHTML = "<span id='TB_next' class='d'>Next &gt;</span>";
			if(!TB_PrevHTML&&imageGroup)TB_PrevHTML = "<span id='TB_prev' class='d'>&lt; Prev</span>";

			imgPreloader = new Image();
			imgPreloader.onload = function(){
			imgPreloader.onload = null;
				
			// Resizing large images - orginal by Christian Montoya edited by me.
			var pagesize = tb_getPageSize();
			var x = pagesize[0] - 100;// - 150;
			var y = pagesize[1] - 150;// - 150;
			var imageWidth = imgPreloader.width;
			var imageHeight = imgPreloader.height;
			if (imageWidth > x) {
				imageHeight = imageHeight * (x / imageWidth); 
				imageWidth = x; 
				if (imageHeight > y) { 
					imageWidth = imageWidth * (y / imageHeight); 
					imageHeight = y; 
				}
			} else if (imageHeight > y) { 
				imageWidth = imageWidth * (y / imageHeight); 
				imageHeight = y; 
				if (imageWidth > x) { 
					imageHeight = imageHeight * (x / imageWidth); 
					imageWidth = x;
				}
			}
			// End Resizing
			if(caption==null)caption="";
			TB_WIDTH =  685;//  imageWidth + 30;
			TB_HEIGHT = imageHeight + 60;
			j$("#TB_window").append("<div id='ejcTB_top' class='i'><a onclick='tb_remove2();return false;' href='#'><img width='35' height='35' id='ejcTB_close' alt='close'  class='i' src='/stores/guide/common/img/float/thick_close.gif'></a></div><div id='ejcTB_body' class='i' style='text-align:center;width:685px'><a href='' id='TB_ImageOff' title='Close'><img id='TB_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+"'/></a>" + "<div id='TB_caption'><p class='caption'>"+caption+"</p><div id='TB_secondLine'>" + TB_PrevHTML + TB_NomberHTML +  TB_NextHTML + "</div></div></div><div id='ejcTB_bottom' class='i'> </div>"); 		
			
			j$("#TB_closeWindowButton").click(tb_remove2);
		if(imageGroup){	
			
			if (!(TB_PrevHTML === "<span id='TB_prev' class='d'>&lt; Prev</span>")) {
				function goPrev(){
					//if(j$(document).unbind("click",goPrev)){j$(document).unbind("click",goPrev);}
					j$("#TB_window").remove();
					j$("body").append("<div id='TB_window'></div>");
					tb_show(TB_PrevCaption, TB_PrevURL, imageGroup);
					return false;	
				}
				j$("#TB_prev").click(goPrev);
			}
			
			if (!(TB_NextHTML === "<span id='TB_next' class='d'>Next &gt;</span>")) {		
				function goNext(){
					j$("#TB_window").remove();
					j$("body").append("<div id='TB_window'></div>");
					tb_show(TB_NextCaption, TB_NextURL, imageGroup);				
					return false;	
				}
				j$("#TB_next").click(goNext);
				
			}
			if (!(act == 0) && 0< TB_TempArray.length) {		

					function goPage0(){
						j$("#TB_Image").fadeOut("fast", function(){
					j$("#TB_window").remove();
					j$("body").append("<div id='TB_window'></div>");
							tb_show(TB_TempArray[0].title, TB_TempArray[0].href, imageGroup);
						});				
						return false;	
					}
					j$("#TB_no0").click(goPage0);
			}
			if (!(act == 1) && 1< TB_TempArray.length) {		
					function goPage1(){
						j$("#TB_Image").fadeOut("fast", function(){
					j$("#TB_window").remove();
					j$("body").append("<div id='TB_window'></div>");
							tb_show(TB_TempArray[1].title, TB_TempArray[1].href, imageGroup);
						});				
						return false;	
					}
					j$("#TB_no1").click(goPage1);

			}
			if (!(act == 2) && 2< TB_TempArray.length) {		

					function goPage2(){
						j$("#TB_Image").fadeOut("fast", function(){
					j$("#TB_window").remove();
					j$("body").append("<div id='TB_window'></div>");
							tb_show(TB_TempArray[2].title, TB_TempArray[2].href, imageGroup);
						});				
						return false;	
					}
					j$("#TB_no2").click(goPage2);

			}
			if (!(act == 3) && 3< TB_TempArray.length) {		
					function goPage3(){
						j$("#TB_Image").fadeOut("fast", function(){
					j$("#TB_window").remove();
					j$("body").append("<div id='TB_window'></div>");
							tb_show(TB_TempArray[3].title, TB_TempArray[3].href, imageGroup);
						});				
						return false;	
					}
					j$("#TB_no3").click(goPage3);

			}
			if (!(act == 4) && 4< TB_TempArray.length) {		


					function goPage4(){
						j$("#TB_Image").fadeOut("fast", function(){
					j$("#TB_window").remove();
					j$("body").append("<div id='TB_window'></div>");
							tb_show(TB_TempArray[4].title, TB_TempArray[4].href, imageGroup);
						});				
						return false;	
					}
					j$("#TB_no4").click(goPage4);

			}
			if (!(act == 5) && 5< TB_TempArray.length) {		


					function goPage5(){
						j$("#TB_Image").fadeOut("fast", function(){
					j$("#TB_window").remove();
					j$("body").append("<div id='TB_window'></div>");
							tb_show(TB_TempArray[5].title, TB_TempArray[5].href, imageGroup);
						});				
						return false;	
					}
					j$("#TB_no5").click(goPage5);

			}
			if (!(act == 6) && 6< TB_TempArray.length) {		


					function goPage6(){
						j$("#TB_Image").fadeOut("fast", function(){
					j$("#TB_window").remove();
					j$("body").append("<div id='TB_window'></div>");
							tb_show(TB_TempArray[6].title, TB_TempArray[6].href, imageGroup);
						});				
						return false;	
					}
					j$("#TB_no6").click(goPage6);

			}
			if (!(act == 7) && 7< TB_TempArray.length) {		


					function goPage7(){
						j$("#TB_Image").fadeOut("fast", function(){
					j$("#TB_window").remove();
					j$("body").append("<div id='TB_window'></div>");
							tb_show(TB_TempArray[7].title, TB_TempArray[7].href, imageGroup);
						});				
						return false;	
					}
					j$("#TB_no7").click(goPage7);

			}
			if (!(act == 8) && 8< TB_TempArray.length) {		


					function goPage8(){
						j$("#TB_Image").fadeOut("fast", function(){
					j$("#TB_window").remove();
					j$("body").append("<div id='TB_window'></div>");
							tb_show(TB_TempArray[8].title, TB_TempArray[8].href, imageGroup);
						});				
						return false;	
					}
					j$("#TB_no8").click(goPage8);

			}
		}
			document.onkeydown = function(e){ 	
				if (e == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = e.which;
				}
				if(keycode == 27){ // close
					tb_remove2();
				} else if(keycode == 190){ // display previous image
					if(!(TB_NextHTML == "")){
						document.onkeydown = "";
						goNext();
					}
				} else if(keycode == 188){ // display next image
					if(!(TB_PrevHTML == "")){
						document.onkeydown = "";
						goPrev();
					}
				}	
			};
			
			tb_position();
			j$("#TB_load").remove();
			j$("#TB_ImageOff").click(tb_remove2);
			j$("#TB_window").css({display:"block"}); //for safari using css instead of show
			};
			
			imgPreloader.src = url;
			
		}else{//code to show html
			
			// 横幅設定
			TB_WIDTH = 940;
			
			// 高さ設定
			var ejcMinHeight = 360;
			var ejcNewHeight = -10;
			if(window.innerHeight){
				ejcNewHeight += window.innerHeight;
			}else if(document.documentElement && 0 != document.documentElement.clientHeight){
				ejcNewHeight += document.documentElement.clientHeight;
			}else if(document.body){
				ejcNewHeight += document.body.clientHeight;
			}
			if(ejcMinHeight > ejcNewHeight){
				ejcNewHeight = ejcMinHeight;
			}
			TB_HEIGHT = ejcNewHeight;
			
			var ajaxContentLeft=3;
			ajaxContentW = TB_WIDTH - 80 - ajaxContentLeft * 2;
			//ajaxContentW = TB_WIDTH - 110;
			ajaxContentH = TB_HEIGHT - (62 + 65);
			
			// iframe作成
			var iframe_html = "";
			iframe_html += "<div id='ejcTB_top'>";
			iframe_html += "<a href='#' onclick='tb_remove();return false;'>";
			iframe_html += "<img src='/common/img/thick_close.png' width='30' height='30' alt='close' id='ejcTB_close'>";
			iframe_html += "</a>";
			iframe_html += "</div>";
			
			iframe_html += "<div id='ejcTB_body'>";
			iframe_html += "<iframe";
			iframe_html += " frameborder='0' hspace='0'";
			iframe_html += " src='"+url+"'";
			//iframe_html += " src='"+urlNoQuery[0]+"'";
			iframe_html += " id='TB_iframeContent'";
			iframe_html += " name='TB_iframeContent"+Math.round(Math.random()*1000)+"'";
			iframe_html += " onload='tb_showIframe()'";
			iframe_html += " style='width:"+ajaxContentW+"px;height:"+(ajaxContentH+12)+"px;margin-left:"+(40+ajaxContentLeft)+"px;margin-right:"+(900-ajaxContentW-ajaxContentLeft)+"px;'>";
			//iframe_html += " style='width:"+(ajaxContentW)+"px;height:"+(ajaxContentH + 12)+"px;margin-left:40px;margin-right:"+(900 - ajaxContentW)+"px;'>";
			iframe_html += " </iframe>";
			iframe_html += "</div>";
			
			iframe_html += "<div id='ejcTB_bottom'> </div>";
			
			j$("#TB_iframeContent").remove();
			j$("#TB_overlay").unbind();
			j$("#TB_window").append(iframe_html);
			j$("#TB_closeWindowButton").click(tb_remove);
			tb_position();
			if(j$.browser.safari){//safari needs help because it will not fire iframe onload
				j$("#TB_load").remove();
				j$("#TB_window").css({display:"block"});
			}
		}
	} catch(e) {
		//nothing here
	}
}

//helper functions below
function tb_showIframe(){
	j$("#TB_load").remove();
	j$("#TB_window").css({display:"block"});
	
	// hash link
	var src = j$("#TB_iframeContent").attr("src");
	if ( src.indexOf("#") != -1 ) {
		window.open(src,j$("#TB_iframeContent").attr("name"));
	}
}

function tb_remove() {
	//-ejc-// for firefox bug //start
	document.getElementById("TB_window").style.borderWidth = "1px";
	document.getElementById("ejcTB_close").style.display = "none";
	document.getElementById("ejcTB_top").style.backgroundImage = "url(/common/img/float_win_top.gif)";
	document.getElementById("ejcTB_body").style.backgroundImage = "url(/common/img/float_win_body.gif)";
	document.getElementById("ejcTB_bottom").style.backgroundImage = "url(/common/img/float_win_bottom.gif)";
	//-ejc-// for firefox bug //end

	j$("#TB_imageOff").unbind("click");
	j$("#TB_closeWindowButton").unbind("click");
	//-ejc-// fadeout //start
	j$("#TB_window").fadeOut(10,function(){j$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	//	j$("#TB_window").fadeOut("fast",function(){j$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	//-ejc-// fadeout //end
	j$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		j$("body","html").css({height: "auto", width: "auto"});
		j$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}
function tb_remove2() {
	//-ejc-// for firefox bug //start
	document.getElementById("TB_window").style.borderWidth = "1px";
	document.getElementById("ejcTB_close").style.display = "none";
	document.getElementById("ejcTB_top").style.backgroundImage = "url(/stores/guide/common/img/float/float_win_top.gif)";
	document.getElementById("ejcTB_body").style.backgroundImage = "url(/stores/guide/common/img/float/float_win_body.gif)";
	document.getElementById("ejcTB_bottom").style.backgroundImage = "url(/stores/guide/common/img/float/float_win_bottom.gif)";
	
	//-ejc-// for firefox bug //end

	j$("#TB_imageOff").unbind("click");
	j$("#TB_closeWindowButton").unbind("click");
	//-ejc-// fadeout //start
	j$("#TB_window").fadeOut(10,function(){j$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	//	j$("#TB_window").fadeOut("fast",function(){j$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	//-ejc-// fadeout //end
	j$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		j$("body","html").css({height: "auto", width: "auto"});
		j$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}


function tb_position() {
j$("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		j$("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
	}
}

function tb_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}


function tb_detectMacXFF() {
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
		return true;
	}
}


// ******************************************************************************************
//   
//   拡張プラグイン
//   j$.getQueryParams();
//   j$.sortOn(Array,String);
//   
// ******************************************************************************************
(function(j$){
j$.extend({getQueryParams:function(){var r={};var u=document.location.search;if(u){var t=u.substring(1).split('&');var l=t.length;for(var i=0;i<l;i++){var p=t[i].indexOf('=');r[t[i].substring(0,p)]=t[i].substring(p+1);}}return r;}});
j$.extend({sortOn:function(tmp,n){var r=[];r=tmp.slice();var p=n;r.sort(function(d1,d2){var s1=d1[p];var s2=d2[p];if(s1<s2){return -1;}else if(s1>s2){return 1;}else{return 0;}});return r;}});
})(jQuery);

