﻿/**
 * @fileoverview
 *    
 * @requires jquery.js
 */
/* ----------------------------------------------------------------------------------- */



// __DEBUG__
if (typeof window.console != 'object') {
	window.console = {
		log : function(aaa){}
	}
}


/* ----------------------------------------------------------------------------------- */
/**
 * 
 * 
 * 
*/
$(function(){

	/**
	 * 開閉の処理を行なうブロックの外枠のブロックid。
	 * 
	 * @param {String} target_block_id  対象のブロックのセレクタ。id属性値を指定。
 	 * 
	 */
	var target_blocks = [
			  '#expandableBlock'   // 商品検索結果
	];


	/**
	 * 対象ブロック内の各要素に設定するclass属性値。
	 * 
	 * @param {String} node_cname.btns      ボタン用のa要素のセレクタ
	 * @param {String} node_cname.openbtn   開くボタンのセレクタ
	 * @param {String} node_cname.closebtn  閉じるボタンのセレクタ
	 * @param {String} node_cname.content   開閉するブロックのセレクタ
	 * 
	 */
	var node_cname = {
			  btns           : '.expandableBlockBtn'
			, openbtn        : '.openBtn'
			, closebtn       : '.closeBtn'
			, content        : '.expandableBlockContent'
	};


	/**
	 * @param {String|Number} slidedown_speed スライドダウン時の速度またはイージング指定
	 */
	var slidedown_speed = 400;

	/**
	 * @param {String|Number} slidedown_speed スライドアップ時の速度またはイージング指定
	 */
	var slideup_speed   = "easeOutExpo";




/* __DEBUG__
var h = $("#expandableBlockNewBookPatrol .expandableBlockContent").height();
var h_table = $("#expandableBlockNewBookPatrol .expandableBlockContent table").height();
var h_col = $("#expandableBlockNewBookPatrol .expandableBlockContent col").height();
var h_td = $("#expandableBlockNewBookPatrol .expandableBlockContent td").height();
console.log(h + " : " + h_table + " : " + h_col + " : " + h_td);
*/




	/**
	 * Fx3.6にて、開閉するブロック内の要素内容によっては、スライドがスムーズに行なわれないため、
	 * 予めブロックの高さを計算し、heighto属性値にセットしておく。
	 * 文字サイズの変更後に再度ブロックの高さを再計算するようにフォントサイズの変更監視が必要。
	 * 
	 * 
	 * 
	 */
/*
	//	if (jQuery.browser.mozilla) {
		$(".expandableBlockContent").each(function(){
			var $el = $(this);
			var h = $el.height();
				$el.css({"height": h+10});
			$el.hide();
		});
	//	} else {
	//		$(".expandableBlockContent").hide();
	//	}
*/


/*
	EHON_LIB.fontsizeObserverId = "e-hon-fontsize-observer-block";
	var $fsob = $("#"+EHON_LIB.fontsizeObserverId);
	if (!$fsob.get(0)) {
		$("body").append('<ins id="' + EHON_LIB.fontsizeObserverId + '">M</ins>');
		$fsob = $("#"+EHON_LIB.fontsizeObserverId);
		$fsob.css({
			  'position'       : 'absolute'
			, 'left'           : '-10000px'
			, 'top'            : '-10000px'
			, 'display'        : 'block'
			, 'visibility'     : 'hidden'
			, 'border'         : 'none'
			, 'margin'         : '0'
			, 'padding'        : '0'
		});
	}



	EHON_LIB.fontsizeObserverId = "e-hon-fontsize-observer-block";
	var $fsob = $("#"+EHON_LIB.fontsizeObserverId);
	if (!$fsob.get(0)) {
		$("body").append('<ins id="' + EHON_LIB.fontsizeObserverId + '">M</ins>');
		$("body").append('<ins id="'+ EHON_LIB.fontsizeObserverId + '"></ins>').css({
			  'position'       : 'absolute'
			, 'left'           : '-10000px'
			, 'top'            : '-10000px'
			, 'display'        : 'block'
			, 'visibility'     : 'hidden'
			, 'border'         : 'none'
			, 'margin'         : '0'
			, 'padding'        : '0'
		}).text('M');
		$fsob = $("ins#"+EHON_LIB.fontsizeObserverId);
	}


	var fsob_size = $fsob.offsetHeight;
	
	var tid = setInterval(function(){
		var size = $fsob.height();
		var diff = size - fsob_size;
		if (diff != 0) {
			console.log(fsob_size);
			// ここで高さ再計算。隠れている場合は一旦移動して計算。見えている場合はそのまま計算してheight属性に設定。
			$(".expandableBlockContent").each(function(){
				var $el = $(this);
				//
				if ($el.css('display') != 'none') {
					var h = $el.height();
					$el.css({"height": h+10});
				}
				else {
					$el.css({'display': 'block'});
					var h = $el.height();
					$el.css({"height": h+10});
					console.log(h + "pxxx");
					$el.css({'display': 'none'});
				}
			});
			
			fsob_size = size;
		}
		
	}, 2000);
*/



	$.each(target_blocks, function(){
		
		var block = this;
		
		if ($(block).size() == 1) {
			
			var flg_expanded      = false;
			var flg_slide_started = false;
			
			var $content   = $(block + ' ' + node_cname.content);
			var $btns      = $(block + ' ' + node_cname.btns);
			var $close_btn = $(block + ' ' + node_cname.closebtn);
			var $open_btn  = $(block + ' ' + node_cname.openbtn);
			
			
			var content_width = $content.parent().width() - 38;
			$content.css({'width': content_width});
			
			
			$btns.click(function(e) {
				e.preventDefault();
				//this.blur();
				if (!flg_slide_started) {
					flg_slide_started = true;
					toggle();
				}
			});
			
			
			function toggle() {
				if (!flg_expanded) {
					expand();
				}
				else {
					collapse();
				}
			}
			
			function expand() {
				flg_expanded = true;
				toggleButton();
				$content.slideDown(slidedown_speed, function(){
					flg_slide_started = false;
				});
			}
			
			function collapse() {
				flg_expanded = false;
				toggleButton();
				$content.slideUp(slideup_speed, function(){
					flg_slide_started = false;
				});
			}
			
			function toggleButton() {
				if (!flg_expanded) {
					$close_btn.hide();
					$open_btn.show();
				}
				else {
					$open_btn.hide();
					$close_btn.show();
				}
			}
		}
	});

});


/* ----------------------------------------------------------------------------------- */
/**
 * JavaScript無効時は閉じるボタン非表示、検索条件欄を表示
 */
(function(){

var expand_block_style = ''
+'<style type="text/css">'
+'a.expandableBlockBtn {'
+'	display: inline; text-decoration: none;'
+'}'
+'a.expandableBlockBtn .closeBtn {'
+'	display: none;'
+'}'
+'.expandableBlockContent {'
+'	display: none;'
+'}'
+'</style>';

document.write(expand_block_style);

})()

/* ----------------------------------------------------------------------------------- */


