/* Body ID
=========================================== */
$(function(){
	$('body').addClass($('#BodyID').attr('class'));
});


/* Mouse Over
========================================== */
$(function(){
  var image_cache = new Object();
  $(".swap").each(function(i) {
    var imgsrc = this.src;
    var dot = this.src.lastIndexOf('.');
    var imgsrc_on = this.src.substr(0, dot) + '_on' + this.src.substr(dot, 4);
    image_cache[this.src] = new Image();
    image_cache[this.src].src = imgsrc_on;
    $(this).hover(
      function() { this.src = imgsrc_on; },
      function() { this.src = imgsrc; });
  });
});


/* Smooth Scroll
========================================== */
$(function(){ 
	$('a[href^=#]:not(a[href$=#]):not(.tabNavi a)').click(function(){
		var href= this.hash;
		var $target = $(href == '#_top' ? 'body' : href);
		
		// アンカーリンクがあるページでだけ以下を実行
		if($target.size()){
			var top = $target.offset().top;
			$($.browser.safari ? 'body' : 'html').animate({scrollTop:top}, 500, 'swing');
		}
		return false;
	});
});


/* Cart Contents
========================================== */
$(function(){
	$('#header #headerNavi ul#headerMyNavi li#cartContents').hover(function(){
		$(this).find('.contents').fadeIn('fast');
		$(this).find('a > img.btn').css('visibility','hidden');
	},function(){
		$(this).find('.contents').fadeOut('fast');
		$(this).find('a > img.btn').css('visibility','visible');
	});
});


/* Navi Recipe
========================================== */
$(function(){
	var naviRecipePrevBtn = $('#navigation .unit#naviRecipe ul.pageNavi li.prev');
	var naviRecipeNextBtn = $('#navigation .unit#naviRecipe ul.pageNavi li.next');
	var naviRecipeContents = $('#navigation .unit#naviRecipe .contents');
	naviRecipeContents.filter(':first').show();
	var naviRecipeShowCount = 0;
	naviRecipePageNaviShow();
	
	
	naviRecipeNextBtn.click(function(){
		naviRecipeContents.filter(':eq('+naviRecipeShowCount+')').hide();
		naviRecipeShowCount += 1;
		naviRecipeContents.filter(':eq('+naviRecipeShowCount+')').show();
		naviRecipePageNaviShow();
	});
	
	naviRecipePrevBtn.click(function(){
		naviRecipeContents.filter(':eq('+naviRecipeShowCount+')').hide();
		naviRecipeShowCount -= 1;
		naviRecipeContents.filter(':eq('+naviRecipeShowCount+')').show();
		naviRecipePageNaviShow();
	});
	
	function naviRecipePageNaviShow(){
		if(naviRecipeShowCount == 0){
			naviRecipePrevBtn.hide();
		}else if(naviRecipeContents.filter(':eq('+(naviRecipeShowCount+1)+')').size() == 0){
			naviRecipeNextBtn.hide();
		}else{
			naviRecipePrevBtn.show();
			naviRecipeNextBtn.show();
		}
	};
});


/* Tooltip Thumbnail
========================================== */
$(function(){
	$('.tooltipThumbnail').hover(function(){
		$(this).find('img.tooltipThumbnailImg').fadeIn('fast');
	},function(){
		$(this).find('img.tooltipThumbnailImg').fadeOut('fast');
	});
	
	// IE対策　重なり調整
	tooltipZindex = 300;
	$('.tooltipThumbnail').parent().each(function(){
		$(this).css('position','relative').css('z-index',tooltipZindex);
		tooltipZindex += -1;
	});
});


/* Tooltip Form
========================================== */
$(function(){
	$('.tooltipForm input, .tooltipForm select, .tooltipForm textarea').focus(function(){
		$(this).closest('.tooltipForm').find('.tooltipFormContents:first').fadeIn('fast');
	});
	
	$('.tooltipForm input, .tooltipForm select, .tooltipForm textarea').blur(function(){
		$(this).closest('.tooltipForm').find('.tooltipFormContents').fadeOut('fast');
	});
});


/* 送信はこちらをクリックで表示/非表示切替
========================================== */
$(function(){
function sendFormSwitch2() {
	$('.switch a').toggle(function(){
		$(this).text(defaultSwitchText);
		$(this).closest('.sendForm').find('.body').hide();
		return false;
	},function(){
		$(this).text('閉じる');
		$(this).closest('.sendForm').find('.body').show();
		flatHeightsAction();
		return false;
	});
}

	$('.sendForm').each(function(){
		if($(this).find('.errorMessage').size() != 0){
			$(this).find('.body').show();
			defaultSwitchText = $(this).find('.switch a').text();
			$(this).find('.switch a').text('閉じる');
			sendFormSwitch2();
			flatHeightsAction();
		}else if($(this).find('input').size() == 0){
			$(this).find('.body').show();
			defaultSwitchText = $(this).find('.switch a').text();
			$(this).find('.switch a').text('閉じる');
			sendFormSwitch2();
			flatHeightsAction();
		}else{
			$(this).find('.switch a').toggle(function(){
				defaultSwitchText = $(this).text();
				$(this).text('閉じる');
				$(this).closest('.sendForm').find('.body').show();
				flatHeightsAction();
				return false;
			},function(){
				$(this).text(defaultSwitchText);
				$(this).closest('.sendForm').find('.body').hide();
				return false;
			});
		};
	});
});


/* ラジオボタンで表示/非表示切替
========================================== */
$(function(){
	$('.radioShowHide input').each(function(){
		if($(this).filter(':checked').attr('class') == 'inputRadio show'){
			$(this).closest('.radioShowHide').find('.radioShowHideContents').show();
			flatHeightsAction();
		}else if($(this).filter(':checked').attr('class') == 'inputRadio hide'){
			$(this).closest('.radioShowHide').find('.radioShowHideContents').hide();
		}
	});
	
	$('.radioShowHide input:radio').change(function(){
		if($(this).attr('class') == 'inputRadio show'){
			$(this).closest('.radioShowHide').find('.radioShowHideContents').show();
			flatHeightsAction();
		}else if($(this).attr('class') == 'inputRadio hide'){
			$(this).closest('.radioShowHide').find('.radioShowHideContents').hide();
		}
	});
	
	$('input.cardInfoSelect').change(function(){
		if($(this).attr('value') == '新規・情報変更'){
			$('.cardInfo_edit').show();
			$('.cardInfo_past').hide();
		}else if($(this).attr('value') == '過去決済カード'){
			$('.cardInfo_edit').hide();
			$('.cardInfo_past').show();
		}
	});
	
	if($('input.cardInfoSelect:checked').attr('value') == '新規・情報変更') {
		$('.cardInfo_edit').show();
		$('.cardInfo_past').hide();
	}else if($('input.cardInfoSelect:checked').attr('value') == '過去決済カード'){
		$('.cardInfo_edit').hide();
		$('.cardInfo_past').show();
	}
});


/* Table Open Close
========================================== */
$(function(){
	$('#deliveryDetails .table08 .btnClose a').closest('tr').nextAll('tr').hide();
	
	$('#deliveryDetails .table08 .btnClose a').toggle(function(){
		$(this).closest('tr').nextAll('tr').show();
		$(this).text('閉じる');
		return false;
	},function(){
		$(this).closest('tr').nextAll('tr').hide();
		$(this).text('開く');
		return false;
	});
});

/* Tab Display
========================================== */
$(function(){
	$('.tabDisplay ul.tabNavi li a').click(function(){
		$(this).closest('.tabDisplay').find('.tabBody').hide();
		$(this).closest('.tabDisplay').find('li').removeClass('active');
		$($(this).attr('href')).show();
		$(this).parent().addClass('active');
		flatHeightsAction();
		return false;
	});
});


/* Pop Up
========================================== */
$(function(){
	$('.popupLink').click(function(){
		window.open($(this).attr('href'),"","width=500,height=500,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0").focus();
		return false;
	});
});


/* Pop Up wide
========================================== */
$(function(){
	$('.popupLink_wide').click(function(){
		window.open($(this).attr('href'),"","width=640,height=600,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0").focus();
		return false;
	});
});


/* Window Close
========================================== */
$(function(){
	$('.windowClose').click(function(){
		window.close();
		return false;
	});
});


/* Table 03
========================================== */
$(function(){
	// borderを設定（tr以下のソースを同じにするためスクリプトで指定）
	function table03Borders(){
		$('#contents table.table03 tr:eq(1)').find('td').addClass('borderTop');
		$('#contents table.table03 tr').find('td:first').addClass('borderLeft');
		$('#contents table.table03 tr').find('td:last').addClass('borderRight');
		
		$('#contents table.table03 tr td.notes').parent().prev().find('td').addClass('borderBtm');
		$('#contents table.table03 tr.separate').prev().find('td').addClass('borderBtm');
	}
	
	// boder初期指定
	table03Borders();
	
	// 削除ボタンクリックで行削除
	$('#contents table.table03 a.delete').click(function(){
		$(this).closest('tr').remove();
		// boder再指定
		table03Borders();
		return false;
	});
});


/* Table 04
========================================== */
$(function(){
	$('#contents table.table04 tr').find('td:first').addClass('borderLeft');
	$('#contents table.table04 tr').find('td:last').addClass('borderRight');
});


/* Table 06
========================================== */
$(function(){
	// borderを設定（tr以下のソースを同じにするためスクリプトで指定）
	function table06Borders(){
		$('#contents table.table06 tr:eq(1)').find('td').addClass('borderTop');
		$('#contents table.table06 tr').find('td:first').addClass('borderLeft');
		$('#contents table.table06 tr').find('td:last').addClass('borderRight');
		
		$('#contents table.table06 tr td.notes').parent().prev().find('td').addClass('borderBtm');
		$('#contents table.table06 tr.separate').prev().find('td').addClass('borderBtm');
	}
	
	// boder初期指定
	table06Borders();
	
	// 削除ボタンクリックで行削除
	$('#contents table.table06 a.delete').click(function(){
		$(this).closest('tr').remove();
		// boder再指定
		table06Borders();
		return false;
	});
});

/* Flatheights
========================================== */
/*
======================================================================
	$.changeLetterSize.addHandler(func)
	文字の大きさが変化した時に実行する処理を追加
======================================================================
*/

jQuery.changeLetterSize = {
	handlers : [],
	interval : 500,//定期処理を行う時間間隔。一応0.5秒で設定して有ります。1秒 = 1000
	currentSize: 0
};

(function($) {

	var self = $.changeLetterSize;

	/* 文字の大きさを確認するためのins要素 */
	var ins = $('<ins>M</ins>').css({
		display: 'block',
		visibility: 'hidden',
		position: 'absolute',
		padding: '0',
		top: '0'
	});

	/* 文字の大きさが変わったか */
	var isChanged = function() {
		
		ins.appendTo('body');
		var size = ins[0].offsetHeight;
		ins.remove();
		
		if (self.currentSize == size) return false;
		self.currentSize = size;
		
		return true;
	};

	/* 文書を読み込んだ時点で
	   文字の大きさを確認しておく */
	$(isChanged);

	/* 文字の大きさが変わっていたら、
	   handlers中の関数を順に実行 */
	var observer = function() {
		if (!isChanged()) return;
		
		$.each(self.handlers, function(i, handler) {
			handler();
		});
	};

	/* ハンドラを登録し、
	   最初の登録であれば、定期処理を開始 */
	self.addHandler = function(func) {
		self.handlers.push(func);
		if (self.handlers.length == 1) {
			setInterval(observer, self.interval);
		}
	};

})(jQuery);

/*
======================================================================
	$(expr).flatHeights()
	$(expr)で選択した複数の要素について、それぞれ高さを
	一番高いものに揃える
======================================================================
*/

(function($) {

	/* 対象となる要素群の集合 */
	var sets = [];

	/* 高さ揃えの処理本体 */
	var flatHeights = function(set) {
		var maxHeight = 0;
		set.each(function(){
			var height = this.offsetHeight;
			if (height > maxHeight) maxHeight = height;
		});
		set.css('height', maxHeight + 'px');
	};

	/* 要素群の高さを揃え、setsに追加 */
	jQuery.fn.flatHeights = function() {
		if (this.length > 1) {
			flatHeights(this);
			sets.push(this);
		}
		return this;
	};

	/* 文字の大きさが変わった時に、
	   setsに含まれる各要素群に対して高さ揃えを実行 */
	$.changeLetterSize.addHandler(function() {
		$.each(sets, function() {
			this.height('auto');
			flatHeights(this);
		});
	});

})(jQuery);


/* 4.Flatheights Setting
========================================== */
$(function(){
	
	
	/* 要素を2つずつの組に分ける
	---------------------------------------- */
		/* 
		---------------------------------------- */
		var sets = [], temp = [];
		$('#faqBlock .section01').each(function(i) {
				temp.push(this);
				if (i % 2 == 1) {
						sets.push(temp);
						temp = [];
				}
		});
		if (temp.length) sets.push(temp);
	
		$.each(sets, function() {
				$(this).flatHeights();
		});
	
	/* 要素を3つずつの組に分ける
	---------------------------------------- */
		/* supportDetails
		---------------------------------------- */
		var sets = [], temp = [];
		$('#contents .supportCenterUnit .supportDetails .section01, #contents .shippingContents .section06 p').each(function(i) {
				temp.push(this);
				if (i % 3 == 2) {
						sets.push(temp);
						temp = [];
				}
		});
		if (temp.length) sets.push(temp);
	
		$.each(sets, function() {
				$(this).flatHeights();
		});
		
		/* Powder Recipe List
		---------------------------------------- */
		var sets = [], temp = [];
		$('#contents #powderRecipeList ul li h4').each(function(i) {
				temp.push(this);
				if (i % 3 == 2) {
						sets.push(temp);
						temp = [];
				}
		});
		if (temp.length) sets.push(temp);
	
		$.each(sets, function() {
				$(this).flatHeights();
		});
		
		var sets = [], temp = [];
		$('#contents #powderRecipeList ul li p.name').each(function(i) {
				temp.push(this);
				if (i % 3 == 2) {
						sets.push(temp);
						temp = [];
				}
		});
		if (temp.length) sets.push(temp);
	
		$.each(sets, function() {
				$(this).flatHeights();
		});
		
		/* How To Make
		---------------------------------------- */
		var sets = [], temp = [];
		$('#contents #powderRecipeDetails #howToMake ul li p.text').each(function(i) {
				temp.push(this);
				if (i % 3 == 2) {
						sets.push(temp);
						temp = [];
				}
		});
		if (temp.length) sets.push(temp);
	
		$.each(sets, function() {
				$(this).flatHeights();
		});
	
	flatHeightsAction();
});

function flatHeightsAction(){
	$('body#home #contents .section#purchaseReason ul.reasonList li p.contents').height('auto').flatHeights();
	$('body#home #contents .section#purchaseReason ul.reasonList li p.title').height('auto').flatHeights();
	
	$('body#home #contents .section#satisfaction ul li p.title').wrap('<div class="bg"></div>');
	$('body#home #contents .section#satisfaction ul li p.title').height('auto').flatHeights();
	
	$('#contents #buyReasonList ul li p.text').wrap('<div class="bgTop"></div>');
	$('#contents #buyReasonList ul li p.name').wrap('<div class="dottedLine"></div>');
	$('#contents #buyReasonList ul li p.date').wrap('<div class="bgBtm"></div>');
	$('#contents #buyReasonList ul li p.text').height('auto').flatHeights();
	$('#contents #buyReasonList ul li p.name').height('auto').flatHeights();
	$('#contents #buyReasonList ul li p.date').height('auto').flatHeights();
	
	$('#satisfactionList ul li p.name').wrap('<div class="bg"></div>');
	$('#satisfactionList ul li p.name').height('auto').flatHeights();
	
	$('#contributionExample .contributionList ul li > p.name').wrap('<div class="bg"></div>');
	$('#contributionExample .contributionList ul li p.name').height('auto').flatHeights();
	
	$('#contents #reasonContribution .step #reasonExample .contributionList ul li > p.contribution').wrap('<div class="bgTop"></div>');
	$('#contents #reasonContribution .step #reasonExample .contributionList ul li > p.name').wrap('<div class="bg"></div>');
	$('#contents #reasonContribution .step #reasonExample .contributionList ul li p.contribution').height('auto').flatHeights();
	$('#contents #reasonContribution .step #reasonExample .contributionList ul li p.name').height('auto').flatHeights();
	
	$('#contents #powderRecipeList ul li h4').wrap('<div class="bg"></div>');	
	$('#contents ul.indexList li .content p').not('.detailLink').height('auto').flatHeights();
	$('#contents #supportCenter #supportBlock .section01').height('auto').flatHeights();
}
