/**
 *	@filename		lib.js
 *	@charset		utf-8
 *	@created		August 21, 2009
 *	@lastmodified	October 14, 2009
 *	@description	共通js
 */
(function(){



var document = window.document;



//
//	ルートパス取得
//
var path = document.getElementsByTagName("script");
path = path[path.length-1].src.replace(/\/js\/common\/lib\.js$/, "/");



//
//	スクロール用
//
var moveObject = function(){
	var y = document.body.scrollTop || document.documentElement.scrollTop;
	var target = Math.max(Math.floor(y - (y / 8)));
	scrollTo(0, target);
	if(target > 0){
		setTimeout(moveObject,10);
	}
};



//
//	IE用objectタグ関係
//
var removeData = function(obj){
	var data = obj.getAttribute("data");
	if(data){
		obj.removeAttribute("data");
	}
};



//
//	リンクの点線を消す
//
var disableLinkOutline = function() {
	$(document.links).each(function(){
		$(this).focus(function(){
			this.blur();
		});
	});
};



//
//	フォントサイズ変更
//
var FSC=function(){this.id="_FSC_";this.fl={};this.fl.change=[];this.fl.smaller=[];this.fl.larger=[];this.init();this.ir=true;var ms=100,os=this.gcs(),ocs=this.gccs(),me=this;this.timer=setInterval(function(){if(me.ir){var cs=me.gcs(),ccs=me.gccs();if(os!=cs){me.cf("change");if(os>cs){me.cf("smaller");}else if(os<cs){me.cf("larger");}}else if(ocs!=ccs){me.cf("change");if(ocs>ccs){me.cf("smaller");}else if(ocs<ccs){me.cf("larger");}}os=cs;ocs=ccs;}},ms);};FSC.prototype.init=function(){var _de=document.getElementById(this.id);if(_de){this.de=_de;}else{this.de=document.createElement("span");this.de.style.display="block";this.de.style.visibility="hidden";this.de.style.position="absolute";this.de.style.zIndex="-9999";this.de.style.left="-9999px";this.de.style.top="-9999px";this.de.style.margin="0";this.de.style.padding="0";this.de.style.minWidth="1px";this.de.style.minHeight="1px";if(document.all&&!window.opera&&!window.XMLHttpRequest){this.de.style.height="1px";}this.de.style.lineHeight="1em";this.de.style.overflow="visible";this.de.style.fontSize="1000%";this.de.style.textIndent="0";this.de.innerHTML="B";this.de.setAttribute("id",this.id);var body=document.getElementsByTagName("body").item(0);body.appendChild(this.de);}};FSC.prototype.addListener=function(en,f){if(en=="change"||en=="smaller"||en=="larger"){if(typeof f!="function"){return;}else{this.fl[en].push(f);}}};FSC.prototype.cf=function(en){this.ir=false;for(var i=0;i<this.fl[en].length;i++){if(typeof this.fl[en][i]=="function"){(this.fl[en][i])();}}this.ir=true;};FSC.prototype.gcs=function(){return this.de.clientHeight;};FSC.prototype.gccs=function(){if(document.defaultView){return document.defaultView.getComputedStyle(this.de,null).getPropertyValue("font-size");}else{return 0;}};FSC.prototype.removeListener=function(en){if(en=="change"||en=="smaller"||en=="larger"){this.fl[en]=null;}};



//
//	サンデーCM
//
var setSundayCMHandler = function(){
	$("#buttonViewCM").click(function(){
		var win = window.open(this.href, "", "width=450,height=400,scrollbars=no,resizable=yes,menubar=no,toolbar=no");
		win.focus();
		return false;
	});
};



////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	DOM読み込み完了後に実行
//
////////////////////////////////////////////////////////////////////////////////////////////////////
$(function(){



//
//	このページの先頭へボタン動作
//
$(".anchorTop a").click(function(){
	moveObject();
	return false;
});
$("#anchorTop a").click(function(){
	moveObject();
	return false;
});



//
//	IE用objectタグ関係
//
if(document.all && !window.opera){
	$("object").each(function(){
		removeData(this);
		this.outerHTML = this.outerHTML;
	});
}



//
//	リンクの点線を消す
//
disableLinkOutline();



//
//	ポップアップリンク用
//
$(".popup").click(function(){
	var url = this.href;
	if(url){
		var win = window.open(url);
		win.focus();
	}
	return false;
});



//
//	ポップアップリンク用
//
/*
$(".sslPopup").click(function(){
	var url = this.href;
	if(url){
		var win = window.open(url, "", "width=800,resizable=yes,scrollbars=yes");
		win.focus();
	}
	return false;
});
*/



//
//	プレゼント当選者
//
$("#buttonPresentWinners").click(function(){
	var url = this.href;
	if(url){
		var win = window.open(url, "", "width=800,resizable=yes,scrollbars=yes");
		win.focus();
	}
	return false;
});

$("#buttonClose").click(function(){
	window.close();
	return false;
});

//
//	input type="image" ロールオーバー用
//
$("input").each(function(){
	if(this.type === "image" && this.src){
		var self = this;
		var src = this.src;
		var src_ov = this.src.replace(/^(.+)\.([^\.]+)$/, "$1_ov.$2");
		var hoverImage = new Image();
		$(hoverImage).load(function(){
			$(self).hover(
				function(){
					this.src = src_ov;
				},
				function(){
					this.src = src;
				}
			);
		});
		hoverImage.src = src_ov;
	}
});



//
//	中カテゴリ一覧
//
var subCats = $("#mainCategory div.section div").get();//divs

//奇数番目でフロートクリア
for(var i=0, len=subCats.length; i<len; i+=2){
	subCats[i].style.clear = "left";
}

subCats = $("ul", subCats).get();//ulの配列／ノードリスト

//高さ調整
var resize2 = function(nodes){
	//一度全ての高さをリセット
	for(var i=0, height=0, len=nodes.length; i<len; i++){
		nodes[i].style.height = "auto";
	}
	for(i=0, height=0, len=nodes.length; i<len; i+=2){
		if(nodes[i+1]){
			height = Math.max($(nodes[i]).height(), $(nodes[i+1]).height());
			if(document.all && !window.opera && typeof XMLHttpRequest === "undefined"){//IE6
				height = height + 20;
			}
			$(nodes[i]).height(height);
			$(nodes[i+1]).height(height);
		}
	}
};
resize2(subCats);

//フォントサイズ変更時にも高さを調整する
(new FSC()).addListener("change", function(){
	resize2(subCats);
});



//
//	商品詳細 写真
//
/*
(function(){
	var productPhoto = document.getElementById("productPhoto");
	if(productPhoto){
		var aTags = productPhoto.getElementsByTagName("a");
		
		//商品写真 サムネイルに虫眼鏡画像を追加
		for(var i=0, len=aTags.length; i<len; i++){
			aTags [i].innerHTML = ['<span></span>', aTags [i].innerHTML].join("");
		}
		
		//商品写真切り替え
		var mainImage = productPhoto.getElementsByTagName("p")[0].getElementsByTagName("img")[0];
		productPhoto = null;
		$(aTags).click(function(){
			var img = this.getElementsByTagName("img")[0];
			var _src = mainImage.src;
			var _alt = mainImage.alt;
			mainImage.src = img.src;
			mainImage.alt = img.alt;
			img.src = _src;
			img.alt = _alt;
			img = null;
			return false;
		});
	}
})();
*/



//
//	商品詳細 写真 サイズ調整＆thickbox
//
(function(){
	var THICKBOX_REL = "productPhotos";
	var productPhoto = document.getElementById("productPhoto");
	if(productPhoto){

		//
		//	メイン画像
		//
		var mainImageTag = productPhoto.getElementsByTagName("p")[0].getElementsByTagName("img")[0];
		var mainImagePhoto = new Image();
		var MAIN_IMAGE_WIDTH = 232;
		mainImagePhoto.onload = function(){
			
			//虫眼鏡画像表示用にaタグをpに挿入
			var aTag = document.createElement("a");
			mainImageTag.parentNode.insertBefore(aTag, mainImageTag);
			
			//幅設定
			mainImageTag.width = MAIN_IMAGE_WIDTH;
			
			//高さ設定
			var height = parseInt(mainImagePhoto.height * MAIN_IMAGE_WIDTH / mainImagePhoto.width, 10);
			mainImageTag.height = height;
			
			aTag.style.width = MAIN_IMAGE_WIDTH + 1 + "px";
			aTag.style.height = height + 1 + "px";
			aTag.href = mainImageTag.src;
			aTag.title = mainImageTag.alt;
			aTag.rel = THICKBOX_REL;
			aTag.parentNode.style.display = "block";
			tb_init(aTag);
			aTag = null;
		};
		mainImagePhoto.src = mainImageTag.src;//画像読み込み
		
		//
		//	サムネイル
		//
		var MAX_SIZE = 113;
		var thumbnails = productPhoto.getElementsByTagName("ul")[0].getElementsByTagName("img");
		var image, aTag;
		for(var j=0, len=thumbnails.length; j<len; j++){
			
			//左右フロート設定
			if(j % 2 === 0){
				thumbnails[j].parentNode.className += " left";
			}else{
				thumbnails[j].parentNode.className += " right";
			}
			
			//虫眼鏡画像表示用にspanタグをliに挿入
			aTag = document.createElement("a");
			thumbnails[j].parentNode.insertBefore(aTag, thumbnails[j].parentNode.firstChild);
			
			//サイズ設定
			image = new Image();
			image.onload = (function(imgTag, aTag){
				return function(){
					//横長の場合
					if(this.width > this.height){
						//alert("横長");
						imgTag.width = MAX_SIZE;
						imgTag.height = parseInt(this.height * MAX_SIZE / this.width, 10);
						imgTag.style.marginTop = parseInt( (MAX_SIZE - parseInt(this.height * MAX_SIZE / this.width, 10)) / 2, 10) + "px";
					//縦長の場合
					}else{
						//alert("縦長");
						imgTag.height = MAX_SIZE;
						imgTag.width = parseInt(this.width * MAX_SIZE / this.height , 10);
					}
					//aTag.style.width = imgTag.width + "px";
					//aTag.style.height = imgTag.height + "px";
					aTag.style.width = aTag.style.height = MAX_SIZE + "px";
					aTag.href = imgTag.src;
					aTag.title = imgTag.alt;
					aTag.rel = THICKBOX_REL;
					aTag.parentNode.style.display = "block";
					tb_init(aTag);
					aTag = null;
				};
			})(thumbnails[j], aTag);
			image.src = thumbnails[j].src;//画像読み込み
		}
		
		//参照削除
		productPhoto = thumbnails = aTag = null;
	}
})();





//
//	各種 ストライプ
//
$("dl.stripe").each(function(){
	$("dd:even", this).addClass("even");
});

$("table.stripe").each(function(){
	$("tr:even", this).addClass("even");
});

$("ul.stripe").each(function(){
	$("li:even", this).addClass("even");
});

$("ol.stripe").each(function(){
	$("li:even", this).addClass("even");
});



//
//	バナー読み込み
//
var siteId = document.getElementById("siteId");
var subContent = document.getElementById("subContent");//バナーA、バナーC挿入先
var mainContentLeft = document.getElementById("mainContentLeft");//バナーB挿入先
var load = function(url, handler){
	$.ajax({
		url : url,
		dataType : "html",
		success : function(data){
			data = data.
				replace(/<\?xml version=\"1\.0\" encoding=\"utf-8\"\?>/, "").//1行目のxml宣言削除
				replace(/<!--(?:[^\-]|-(?!->))+-->/g, "").//htmlコメント削除
				replace(/\="\//g, "=\"" + path);//ルートパス設定
			handler(data);
		}
	});
};

//トップページ以外（h1#siteIdが存在するのはトップのみ）
if(siteId && siteId.tagName.toLowerCase()!=="h1"){
	
	//Net得通販（bodyに"EC"のクラス名あり）
	if(/\bEC\b/.test(document.body.className)){
		
		//バナーB読み込み
		if(mainContentLeft){
			load(path + "banners/banner_b.html", function(data){
				var div = document.createElement("div");
				div.innerHTML = data;
				mainContentLeft.appendChild(div);
				mainContentLeft = div = null;
			});
		}
		
		//バナーC読み込み
		if(subContent){
			load(path + "banners/banner_c.html", function(data){
				var div = document.createElement("div");
				div.innerHTML = data;
				subContent.appendChild(div);
				subContent = div = null;
				setSundayCMHandler();
			});
		}
		
	//Net得通販以外の下層ページ
	}else{
		
		//バナーA読み込み
		if(subContent){
			load(path + "banners/banner_a.html", function(data){
				var div = document.createElement("div");
				div.innerHTML = data;
				subContent.appendChild(div);
				subContent = div = null;
				setSundayCMHandler();
			});
		}
		
	}
}else{
	setSundayCMHandler();
}

siteId = null;//参照クリア



//
//	キーワード検索
//
var fQuery = $("#fQuery");
var fSearch = $("#fSearch");
var fSearchParent = $("#fQuery").parent();
var searchDefaultText = "キーワードを入力";
fQuery.focus(function(){
	if(this.value === searchDefaultText){
		this.value = "";
	}
	fSearchParent.addClass("focus");
});

fQuery.blur(function(){
	if(!/\S/.test(this.value)){
		this.value = searchDefaultText;
	}
	fSearchParent.removeClass("focus");
});

fSearch.submit(function(){
	if(fQuery.val() === searchDefaultText){
		fQuery.val("");
	}
});




//
//	店舗情報トップ
//
if(document.body.id === "shop"){
	(function(){
		var prefectureNav = document.getElementById("prefectureNav");
		var mapImage = prefectureNav.getElementsByTagName("img")[0];
		var prefectures = prefectureNav.getElementsByTagName("area");
		for(var i=0, len=prefectures.length; i<len; i++){
			prefectures[i].onmouseover = (function(obj){
				var src = "./images/map_" + obj.getAttribute("rel") + ".gif";
				return function(){
					mapImage.src = src;
				};
			})(prefectures[i]);
			prefectures[i].onmouseout = function(){
				mapImage.src = "./images/map.gif";
			};
			prefectures[i].onfocus = function(){
				this.blur();
			};
		}
		prefectureNav = prefectures = null;
		
		
		var cells = document.getElementById("section2").getElementsByTagName("div");
		
		//高さ調整
		var resize3 = function(nodes){
			//一度全ての高さをリセット
			for(var i=0, height=0, len=nodes.length; i<len; i++){
				nodes[i].style.height = "auto";
			}
			for(i=0, height=0, len=nodes.length; i<len; i+=3){
				if(nodes[i+1]){
					if(nodes[i+2]){
						height = Math.max($(nodes[i]).height(), $(nodes[i+1]).height(), $(nodes[i+2]).height());
						$(nodes[i]).height(height);
						$(nodes[i+1]).height(height);
						$(nodes[i+2]).height(height);
					}else{
						height = Math.max($(nodes[i]).height(), $(nodes[i+1]).height());
						$(nodes[i]).height(height);
						$(nodes[i+1]).height(height);
					}
				}
			}
		};
		resize3(cells);
		
		(new FSC).addListener("change", function(){
			resize3(cells);
		});
	})();
}



//
//	HeightController[ver. October 16, 2009]
//
var HeightController=function(){var self=this;this.isIE=document.all&&!window.opera;this.isFX=navigator.userAgent.indexOf("Firefox")>-1;this.head=document.getElementsByTagName("head")[0];this.count=0;this.elementSet=[];if(this.isIE){this.styleSheet=document.createStyleSheet();}else{this.styleSheet=document.createElement("style");this.head.appendChild(this.styleSheet);}(new FSC()).addListener("change",function(){self.setHeight();});};HeightController.CLASS_NAME_PREFIX="height_controller_";HeightController.prototype.addElements=function(elements){var className=HeightController.CLASS_NAME_PREFIX+this.count;this.count++;this.elementSet.push({elements:elements,className:className});var i=0,len=elements.length;while(i<len){elements[i].className+=(" "+className);i++;}this.setHeight();};HeightController.prototype.setHeight=function(){this.resetHeight();var rules=[];var maxHeight,_h;var i=0,len=this.elementSet.length;while(i<len){maxHeight=_h=0;var j=0,len2=this.elementSet[i].elements.length;while(j<len2){_h=this.calcHeight(this.elementSet[i].elements[j]);maxHeight=maxHeight<_h?_h:maxHeight;j++;}rules.push("."+this.elementSet[i].className+"{height:"+maxHeight+"px!important;}");i++;}if(this.isIE){this.styleSheet.cssText=rules.join("");}else if(this.isFX){this.styleSheet.innerHTML=rules.join("");}else{var k=0;var len3=rules.length;while(k<len3){this.styleSheet.sheet.insertRule(rules[k],k);k++;}}};HeightController.prototype.calcHeight=function(obj){var height=0,padding=0,style;if(this.isIE){style=obj.currentStyle;if(typeof XMLHttpRequest!=="undefined"){padding=parseInt(style.getAttribute("paddingTop"),10)+parseInt(style.getAttribute("paddingBottom"),10);}}else{style=document.defaultView.getComputedStyle(obj,null);padding=parseInt(style.getPropertyValue("padding-top"),10)+parseInt(style.getPropertyValue("padding-bottom"),10);}height=obj.offsetHeight;return height-padding;};HeightController.prototype.resetHeight=function(){if(this.isIE){this.styleSheet.cssText="";}else if(this.isFX){this.styleSheet.innerHTML="";}else{this.head.removeChild(this.styleSheet);this.styleSheet=document.createElement("style");this.head.appendChild(this.styleSheet);}};


//
//	店舗サービス詳細
//
if(document.body.id === "services"){
	(function(){
		var cells = [];
		var isIE = document.body.currentStyle;
		
		$("div.section ul").each(function(){
			cells.push(this.getElementsByTagName("div"));
		});
		var cellsLength = cells.length;
		
		//高さ調整
		var resize = function(){
			var i, j, len, maxHeight, _h;
			for(j=0; j<cellsLength; j++){
				
				//最大高さを計算
				i = 0;
				len = cells[j].length;
				maxHeight = 0;
				while(i<len){
					cells[j][i].style.height = "auto";
					_h = cells[j][i].offsetHeight;
					maxHeight = maxHeight < _h ? _h : maxHeight;
					i++;
				}
				
				//高さを設定
				i = 0;
				while(i<len){
					cells[j][i].style.height = [maxHeight, "px"].join("");
					i++;
				}
			}
		};
		resize();
		(new FSC).addListener("change", function(){
			//var now = (new Date()).getTime();
			resize();
			//alert((new Date()).getTime() - now + "msec");
		});
	})();
}



//
//	おトクな情報トップ
//
if(document.body.id === "otoku"){
	var hController = new HeightController();
	var section1 = document.getElementById("section1");
	hController.addElements(section1.getElementsByTagName("span"));
	hController.addElements(section1.getElementsByTagName("li"));
}





});
////////////////////////////////////////////////////////////////////////////////////////////////////



})();