/**
 * 対応エリアJS
 *
 * @author Tsue Shogo
 * @version
 *	1.0.0	2018/07/26(木) 04:04	新規作成
 */
(function(){
	/**
	 * コントローラ
	 *
	 * @var Object
	 */
	var Controller = {
		/**
		 * パラメータ
		 *
		 * @var Object
		 */
		params: {
			originPos: {},
			ariaListTextOrg: [],
			updateWidth: null
		},
		/**
		 * イベント
		 *
		 * @var Object
		 */
		events: {
			'window': {
				'resize': 'resizeWindow'
			},
			'svg .clickable': {
				'click': 'clickClickable',
				'mouseover': 'mouseoverClickable',
				'mouseout': 'mouseoutClickable'
			},
			'.map-name li': {
				'mouseover': 'mouseoverMapNameLi',
				'mouseout': 'mouseoutMapNameLi'
				//'mousedown': 'mousedownMapNameLi',
				//'contextmenu': 'contextmenuMapNameLi'
			}
		},
		/**
		 * 初期実行
		 *
		 * @return void
		 */
		init: function(){
			// 行数更新
			//this.updateAreaListText();

			// タイマー登録
			//var _this = this;
			//setInterval(function(){
			//	_this.updateAreaListText();
			//}, 1000);

			//// SVGを調整する
			//this.updateSvg();
		},
		/**
		 * [event] windowをリサイズ
		 *
		 * @param object	event
		 * @param object	target
		 */
		resizeWindow: function(event, target) {
			// 行数更新
			//this.updateAreaListText();

			//// SVGを調整する
			//this.updateSvg();
		},
		/**
		 * [event] 地図をクリック
		 *
		 * @param object	event
		 * @param object	target
		 */
		clickClickable: function(event, target) {
			var href = $(target).data('href');
			location.href = href;
		},
		/**
		 * [event] 地図マウスオーバー
		 *
		 * @param object	event
		 * @param object	target
		 */
		mouseoverClickable: function(event, target) {
			var id, match;
			if (id = $(target).attr('id')) {
				$('.is_' + id + ' a').addClass('hover');
			} else if ($(target).data('href') && (match = $(target).data('href').match(/\/([^\/]+)\/?$/))) {
				id = match[1];
				$('.is_' + id + ' a').addClass('hover');
			}
		},
		/**
		 * [event] 地図マウスアウト
		 *
		 * @param object	event
		 * @param object	target
		 */
		mouseoutClickable: function(event, target) {
			var id, match;
			if (id = $(target).attr('id')) {
				$('.is_' + id + ' a').removeClass('hover');
			} else if ($(target).data('href') && (match = $(target).data('href').match(/\/([^\/]+)\/?$/))) {
				id = match[1];
				$('.is_' + id + ' a').removeClass('hover');
			}
		},
		/**
		 * [event] ボタンマウスオーバー
		 *
		 * @param object	event
		 * @param object	target
		 */
		mouseoverMapNameLi: function(event, target) {
			var match = $(target).attr('class').match(/is_(\S+)/);
			if (match) {
				$('#' + match[1]).addClass('hover');
				$('.cls-1').each(function(){
					if ($(this).data('href') && $(this).data('href').match(new RegExp('\/' + match[1] + '\/?$'))) {
						$(this).addClass('hover');
					}
				});
			}
		},
		/**
		 * [event] ボタンマウスアウト
		 *
		 * @param object	event
		 * @param object	target
		 */
		mouseoutMapNameLi: function(event, target) {
			var match = $(target).attr('class').match(/is_(\S+)/);
			if (match) {
				$('#' + match[1]).removeClass('hover');
				$('.cls-1').each(function(){
					if ($(this).data('href') && $(this).data('href').match(new RegExp('\/' + match[1] + '\/?$'))) {
						$(this).removeClass('hover');
					}
				});
			}
		},
		/**
		 * 行数を取得する
		 * 
		 * @see http://shanabrian.com/web/javascript/element-get-number-of-lines.php
		 */
		getNumberLines: function(element) {
			var fontSize   = 0;
			var count      = 0;
			var calcResult = 0;
		 
			// 要素からスタイルを取得
			var getStyleValue = function(element, styleName, pseudoElt) {
				if (!element) {
					return '';
				}
				if (!pseudoElt) {
					pseudoElt = '';
				}
				var style;
				var toCamelcase = function(str, upper) {
					if (!str) {
						return str;
					}
					var strs = str.split(/[-_ ]+/);
					var i = 1;
					var len = strs.length;
					if (len <= 1) {
						return str;
					}
					if (upper) {
						i = 0;
						str = '';
					} else {
						str = strs[0].toLowerCase();
					}
					for (; i < len; i++) {
						str += strs[i].toLowerCase().replace(/^[a-z]/, function(value) {
							return value.toUpperCase();
						});
					}
					return str;
				};
				if (document.defaultView) {
					style = document.defaultView.getComputedStyle(element, pseudoElt);
				} else {
					style = element.currentStyle;
					styleName = toCamelcase(styleName);
				}
				return (styleName) ? style[styleName] : style;
			};
			// 要素からフォントサイズを取得
			var getFontSize = function(element) {
				var fontSize = getStyleValue(element, 'font-size');
				if (!fontSize.match(/px$/)) {
					var tmpElem = document.createElement('div');
					tmpElem.style.width = '1em';
					element.appendChild(tmpElem);
					fontSize = tmpElem.offsetWidth;
					element.removeChild(tmpElem);
				}
				return parseInt(fontSize);
			};
			var lineHeightVal = getStyleValue(element, 'line-height');
			var heightVal     = element.clientHeight;
			var boxSizing     = getStyleValue(element, 'box-sizing');
			if (boxSizing === 'border-box' || boxSizing === 'padding-box') {
				heightVal -= parseInt(getStyleValue(element, 'padding-top'));
				heightVal -= parseInt(getStyleValue(element, 'padding-bottom'));
			}
			if (isNaN(parseInt(lineHeightVal))) {
				fontSize = getFontSize(element);
				while (calcResult < heightVal) {
					calcResult += fontSize;
					if (calcResult <= heightVal) {
						count++;
					}
				}
			} else {
				count = Math.floor(heightVal / parseInt(lineHeightVal));
			}
			return count;
		},
		/**
		 * 行数を更新する
		 */
		updateAreaListText: function() {
			// 準備
			var isSp = !!(window.matchMedia('screen and (max-width:767px)').matches);
			var lineNumLim = isSp ? 2 : 4;
			var _this = this;

			// ループ
			$('.area-listText').each(function(index){
				// 原文の保持
				if (typeof(_this.params.ariaListTextOrg[index]) === 'undefined') {
					_this.params.ariaListTextOrg[index] = $(this).text();
				}

				// 文字数の削除
				var text = _this.params.ariaListTextOrg[index];
				$(this).text(text);
				var i = 0;
				while (_this.getNumberLines(this) > lineNumLim) {
					text = text.substr(0, text.length - 1);
					$(this).text(text + '…');
					if (i++ > 1000) {
						break;
					}
				}
			});
		}
		///**
		// * [event] ボタンコンテキストメニュー
		// *
		// * @param object	event
		// * @param object	target
		// */
		//contextmenuMapNameLi: function(event, target) {
		//	event.preventDefault();
		//},
		///**
		// * [event] ボタンマウスダウン
		// *
		// * @param object	event
		// * @param object	target
		// */
		//mousedownMapNameLi: function(event, target) {
		//	if (event.button == 2) {
		//		var _this = this;
		//		
		//		// 地図サイズ取得
		//		var $svg = $('.map-area svg');
		//		var defaultWidth = $svg.attr('width');
		//		var defaultHeight = $svg.attr('height');
		//		// W,H計算
		//		var width = $svg.width();
		//		var scale = width / defaultWidth;
		//		var height = defaultHeight * scale;
		//		var maxHeight = $svg.data('max_height') ? $svg.data('max_height') * 1 : 400;
		//		var addScale = 1;
		//		if (height > maxHeight) {
		//			addScale = maxHeight / height;
		//		}
		//		width *= addScale;
		//		height *= addScale;
		//		scale *= addScale;
		//		// 地図とボタン位置との差計算
		//		var wrapWidth = $('.map-wrap').width();
		//		var offsetLeft = (wrapWidth - width) / 2;
		//		
		//		var match;
		//		if (match = $(target).attr('class').match(/(is_\S+)/)) {
		//			var keyName = match[1];
		//			var left = $(target).css('left').substr(0, $(target).css('left').length - 2) * 1;
		//			var top = $(target).css('top').substr(0, $(target).css('top').length - 2) * 1;
		//			var x = $(target).width() / 2 + left;
		//			var y = $(target).height() / 2 + top;
		//			var thisLeft = (x - offsetLeft) / scale;
		//			var thisTop = y / scale;
		//			console.log(thisLeft, thisTop);
		//		}
		//		return false;
		//	}
		//	return true;
		//},
		///**
		// * SVGを調整する
		// *
		// * @return void
		// */
		//updateSvg: function() {
		//	var _this = this;
		//	// オリジナルのポジション取得
		//	if (Object.keys(this.params.originPos).length === 0) {
		//		$('.map-name li').each(function(){
		//			var match;
		//			if (match = $(this).attr('class').match(/(is_\S+)/)) {
		//				var keyName = match[1];
		//				_this.params.originPos[keyName] = {
		//					left: $(this).css('left').substr(0, $(this).css('left').length - 2) * 1,
		//					top: $(this).css('top').substr(0, $(this).css('top').length - 2) * 1
		//				}
		//			}
		//		});
		//	}
		//	// 地図サイズ取得
		//	var $svg = $('.map-area svg');
		//	var defaultWidth = $svg.attr('width');
		//	var defaultHeight = $svg.attr('height');
		//	// W,H計算
		//	var width = $svg.width();
		//	var scale = width / defaultWidth;
		//	var height = defaultHeight * scale;
		//	var maxHeight = $svg.data('max_height') ? $svg.data('max_height') * 1 : 400;
		//	var addScale = 1;
		//	if (height > maxHeight) {
		//		addScale = maxHeight / height;
		//	}
		//	width *= addScale;
		//	height *= addScale;
		//	scale *= addScale;
		//	// 地図サイズ変更
		//	$svg.width(width);
		//	$svg.height(height);
		//	// 地図とボタン位置との差計算
		//	var wrapWidth = $('.map-wrap').width();
		//	var offsetLeft = (wrapWidth - width) / 2;
		//	// アイコンポジション変更
		//	$('.map-name li').each(function(){
		//		var match;
		//		if (match = $(this).attr('class').match(/(is_\S+)/)) {
		//			var keyName = match[1];
		//			var x = (_this.params.originPos[keyName].left * scale + offsetLeft);
		//			var y = _this.params.originPos[keyName].top   * scale;
		//			var left = x - $(this).width() / 2;
		//			var top  = y - $(this).height() / 2;
		//			$(this).css({left: left, top: top});
		//		}
		//	});
		//}
	};
	jQuery(function($){
		// 初期実行
		Controller.init();
		// イベント登録
		Object.keys(Controller.events).forEach(function(selector){
			var events = Controller.events[selector];
			Object.keys(events).forEach(function(event){
				var func = events[event];
				if (selector === 'window') {
					$(window).on(event, function(eve){
						Controller[func](eve, this);
					});
				} else {
					$(document).on(event, selector, function(eve){
						Controller[func](eve, this);
					});
				}
			});
		});
	});
})();

//=============================================
//アコーディオン 都道府県別対応エリア
//=============================================
$(function() {
	$('.item .prefDetail-list .typesquare_option').click(function(){
		$(this).next().slideToggle('fast');
			$(this).toggleClass("is-open");
	});
});
