/* 
 * common.js 20200123
 */

// 獲取地址欄參數
function getUrl(name) {
	let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	let r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]);
	return null;
};

// let pageGrade = 0 ; //0 根目錄  1 page目錄
const pageFile = window.location.pathname.indexOf('/pages/') != -1 ? '../' : '' ;

/* 
注入左側欄
 */
// sidebarLeft()
// function sidebarLeft(){
// 	let _html = `<div class="sidebar-left">
// 		<div class="side-user">
// 			<div class="user-icon">
// 				<img src="${pageFile}images/sign_in_icon.png" />
// 			</div>
// 			<a href="javascript:;" class="user-name"> Sign In </a>
// 		</div>
// 		<div class="channel-menu">
// 			<div class="menu-item">
// 				<a href="javascript:;" class="m-1 ac">新品上市</a>
// 				<ul class="tree-menu open">
// 					<li> <a href="javascript:;">女鞋新品</a></li>
// 					<li> <a href="javascript:;">爆款新品</a></li>
// 					<li> <a href="javascript:;">聯名新品</a></li>
// 					<li> <a href="javascript:;">本月主打星</a></li>
// 					<li> <a href="javascript:;">熱銷現貨</a></li>
// 				</ul>
// 			</div>
// 			<div class="menu-item">
// 				<a href="javascript:;" class="m-1">活動專區</a>
// 				<ul class="tree-menu ">
// 					<li> <a href="javascript:;">女鞋新品</a></li>
// 					<li> <a href="javascript:;">爆款新品</a></li>
// 				</ul>
// 			</div>
// 			<div class="menu-item">
// 				<a href="javascript:;" class="m-1">快閃折扣</a>
// 				<ul class="tree-menu ">
// 					<li> <a href="javascript:;">女鞋新品</a></li>
// 					<li> <a href="javascript:;">爆款新品</a></li>
// 				</ul>
// 			</div>
// 			<div class="menu-item">
// 				<a href="javascript:;" class="m-1">聯名企劃</a>
// 				<ul class="tree-menu ">
// 					<li> <a href="javascript:;">女鞋新品</a></li>
// 					<li> <a href="javascript:;">爆款新品</a></li>
// 				</ul>
// 			</div>
// 			<div class="menu-item">
// 				<a href="javascript:;" class="m-1">品牌女鞋</a>
// 				<ul class="tree-menu ">
// 					<li> <a href="javascript:;">女鞋新品</a></li>
// 					<li> <a href="javascript:;">爆款新品</a></li>
// 				</ul>
// 			</div>
// 			<div class="menu-item">
// 				<a href="javascript:;" class="m-1">迪士尼聯名</a>
// 				<ul class="tree-menu ">
// 					<li> <a href="javascript:;">女鞋新品</a></li>
// 					<li> <a href="javascript:;">爆款新品</a></li>
// 				</ul>
// 			</div>
// 			<div class="menu-item">
// 				<a href="javascript:;" class="m-1">授權品牌</a>
// 			</div>
// 			<div class="menu-item">
// 				<a href="javascript:;" class="m-1">聯乘包包</a>
// 			</div>
// 			<div class="menu-item">
// 				<a href="javascript:;" class="m-1">口罩 | 生活</a>
// 			</div>
// 			<div class="menu-item">
// 				<a href="javascript:;" class="m-1">Lookbook</a>
// 			</div>
// 			<div class="menu-item">
// 				<a href="javascript:;" class="m-1 ">品牌全系列</a>
// 			</div>
// 		</div>
// 		<div class="money-type select-item">
// 			<select class="form-control">
// 				<option value ="0">新台幣</option>
// 				<option value ="1">美元</option>
// 			</select>
// 		</div>
// 		<div class="language select-item">
// 			<select class="form-control">
// 				<option value ="0">language：中文</option>
// 				<option value ="1">language：English</option>
// 			</select>
// 		</div>
// 		<div class="sign-out"><a href="javascript:;">登出</a></div>
// 	</div>` ;
// 	$('body').append(_html)
// }

/* 
注入右側篩選欄
 */
// screenWords();
// function screenWords(){
// 	let _html = `<div class="sidebar-words">
// 		<div class="words-statis">
// 			<div class="num">- 篩選條件 | <span>1</span> |</div>
// 			<a href="javascript:;" class="clear-words">清除</a>
// 		</div>
// 		<div class="words-content">
// 			<div class="w-item">
// 				<div class="w-title">尺寸</div>
// 				<ul>
// 					<li class=" checked"><a href="javacript:;"> 36 </a> <img src="${pageFile}images/checked.png" /></li>
// 					<li class=""><a href="javacript:;"> 37 </a><img src="${pageFile}images/checked.png" /></li>
// 					<li class="dpnone"><a href="javacript:;"> 38 </a><img src="${pageFile}images/checked.png" /></li>
// 					<li class="dpnone"><a href="javacript:;"> 39 </a><img src="${pageFile}images/checked.png" /></li>
// 					<li class="dpnone"><a href="javacript:;"> 40 </a><img src="${pageFile}images/checked.png" /></li>
// 					<li class="dpnone"><a href="javacript:;"> 41 </a><img src="${pageFile}images/checked.png" /></li>
// 				</ul>
// 				<a href="javacript:;" class="watch-more">+ 查看更多</a>
// 			</div>
// 			<div class="w-item">
// 				<div class="w-title">顏色</div>
// 				<ul>
// 					<li class=" checked"><a href="javacript:;"> 黑色 </a> <img src="${pageFile}images/checked.png" /></li>
// 					<li class=""><a href="javacript:;"> 棕色 </a><img src="${pageFile}images/checked.png" /></li>
// 					<li class="dpnone"><a href="javacript:;"> 紅色 </a><img src="${pageFile}images/checked.png" /></li>
// 					<li class="dpnone"><a href="javacript:;"> 白色 </a><img src="${pageFile}images/checked.png" /></li>
// 				</ul>
// 				<a href="javacript:;" class="watch-more">+ 查看更多</a>
// 			</div>
// 		</div>
// 	</div>` ;
// 	$('body').append(_html)
// }

// 動態加載底部欄
// function setBottom(){
// 	let _html = `<div class="shop-bot">
// 		<ul>
// 			<li><a href="javascript:;" class="bot-a  sign-bot" data-type="1"><img src="${pageFile}images/sign_bot.png" /></a></li>
// 			<li>
// 				<a href="javascript:;" class="bot-a shopping-cart" data-type="2">
// 					<!-- 已加入购物车数量  大于99建议用省略号模式 -->
// 					<span class="g-nums" id="spanCartNum"> 0</span>
// 					<img src="${pageFile}images/shopping_cart.png" />
// 				</a>
// 			</li>
// 			<li><a href="javascript:;" class="bot-a collect-bot" data-type="3"><img src="${pageFile}images/collect_bot.png" /></a></li>
// 			<li><a href="javascript:;" class="bot-a vip-manage-bot"  data-type="4"><img src="${pageFile}images/vip_manage_bot.png" /></a></li>
// 		</ul>
// 	</div>`
// 	$('.shops').append(_html)
// }
// setBottom();


/* 
 商品列表頁
 */
// 調起左側邊欄目
$('body').delegate('.sidebar-btn-l', 'click', function() {
	let _this = $(this);
	_this.addClass('showSide');
	$('body').addClass('showSidebar');
	$('.sidebar-left').animate({
		left: 0
	}, 300)
	return false;
})
// 收起側邊欄
$("html").delegate(".showSidebar", "click", function(e) {
	let _con = null ;
	let diff = 0 ; //0為左側邊欄  1位關鍵詞篩選側邊欄
	if ($('.sidebar-btn-l').hasClass('showSide')) {
		_con = $('.sidebar-left'); // 目标不隐藏区域
	} else {
		_con = $('.sidebar-words');
		diff = 1 ;
	}
	if (!_con.is(e.target) && _con.has(e.target).length === 0) {
		if( diff == 0 ){
			_con.animate({
				left: '-100%',
			}, 500);
			$('.sidebar-btn-l').removeClass('showSide')
		}else{
			_con.animate({
				right: '-100%',
			}, 500);
		}
		$('body').removeClass('showSidebar');
		
	}
	return;
})
// 展開欄目
$('body').delegate('.sidebar-left .m-1', 'click', function() {
	let _this = $(this);

	if (_this.hasClass('real-a')) {
		return true;
	}

	if( ! _this.hasClass('ac')  && _this.next('ul').length > 0 ){
		_this.addClass('ac').next('ul').slideDown(300)
	}else if( _this.hasClass('ac') && _this.next('ul').length > 0 ){
		_this.removeClass('ac').next('ul').slideUp(300);
	}
	return false ;
}) 



// 搜索
$('body').delegate('.search-btn-r', 'click', function() {
	let _this = $(this);
	if (!_this.hasClass('clicked')) {
		_this.addClass('clicked');
		$('.search-box.mobile').animate({
			left: '0.2rem',right:"1.5rem",
		}, 100);
	} else {
		_this.removeClass('clicked');
		$('.search-box.mobile').animate({
			left: '7.5rem',right:'7.5rem',
		}, 100);
	}
	return false;
})

// $('body').on('keyup', '.search_keyword', function (e) {
// 	if (13 == e.keyCode) {
// 		let value = $(this).val().trim();
// 		if ('' == value) {
// 			return ;
// 		}

// 		gtag('event', 'search', {
// 			search_term: value
// 		});

// 		location.href = '/products/search?q=' + encodeURIComponent(value);
// 	}
// });


/* 
關鍵詞篩選部分
 */
// 調起右側邊欄
// $('body').delegate('.screen-words .add-words', 'click', function() {
// 	let _this = $(this);
// 	$('body').addClass('showSidebar');
// 	$('.sidebar-words').animate({
// 		right: 0
// 	}, 500)
// 	return false;
// })
//查看更多
// $('body').delegate('.sidebar-words .watch-more', 'click', function() {
// 	let _this = $(this);
// 	_this.closest(".w-item").find('.dpnone').removeClass('dpnone');
// 	_this.remove() ;
// 	return false ;
// }) 
//切換關鍵詞選中取消
// $('body').delegate('.sidebar-words li', 'click', function() {
// 	let _this = $(this);
// 	if( ! _this.hasClass('checked') ){
// 		_this.addClass('checked') ;
// 	}else{
// 		_this.removeClass('checked') ;
// 	}
// 	return false ;
// }) 
//關鍵詞清除
// $('body').delegate('.sidebar-words .clear-words', 'click', function() {
// 	let _this = $(this);
// 	$('.sidebar-words').find('li.checked').removeClass('checked') ;
// 	return false ;
// })


// 底部鏈接跳轉
$('body').delegate('.bot-a', 'click', function() {
	let _this = $(this);
	let _index = _this.attr("data-type")
	
	let pathname = window.location.pathname  ;
	let lacationHref = "" ;
	
	if( _index == 1){
		lacationHref = "" ;
	}else if( _index == 2){
		lacationHref = pageFile+"pages/shopping_cart.html"
	}else if( _index == 3){
		lacationHref = pageFile+"pages/collect_list.html"
	}else if( _index == 4){
		lacationHref = pageFile+"pages/vip_center.html"
	}
	if( lacationHref == "" ||  pathname.indexOf(lacationHref) != -1 ){
		// 已經進入對應詳情頁，點擊底部不再跳轉
		return  ;
	}
	window.location.href = lacationHref ;
	return false ;
})


 
// 關閉彈窗
 $('body').delegate('.modal-close', 'click', function() {
 	let _this = $(this); 
 	let modal = _this.closest(".modal") ;
	modal.hide() ;
 	return false ;
 }) 
 
 
 //pc 關閉彈窗
 $('body').delegate('.modal-pc-close', 'click', function() {
 	let _this = $(this); 
 	let modal = _this.closest(".modal-pc") ;
	$('body').removeClass('overhidden')
 	modal.hide() ;
 	return false ;
 }) 
 
// 轉化時間格式
 let timeFormat = (time) => {
     let timeStr = '';
     let stringFormat = (i) => {
         return i < 10 ? `0${i}` : `${i}`;
     }
     let minuteTime = 0;
     let secondTime = 0;
     let hourTime = 0;
     if(time < 60) {
         timeStr = `00:${stringFormat(time)}`
     } else if(time >= 60 && time < 3600) {
         minuteTime = parseInt(time / 60);
         secondTime = parseInt(time % 60);
         timeStr = `${stringFormat(minuteTime)}:${stringFormat(secondTime)}`;
     } else if(time >= 3600) {
         let _t = parseInt(time % 3600);
         hourTime = parseInt(time / 3600);
         minuteTime = parseInt(_t / 60);
         secondTime = parseInt(_t % 60);
         timeStr = `${stringFormat(hourTime)}:${stringFormat(minuteTime)}:${stringFormat(secondTime)}`
     }
     return timeStr;
 }
// 倒計時
let countdown = 90;
let setTime = (obj) => {
    if(countdown == 0) {
         obj.html("获取验证码");
         countdown = 90; //90秒过后button上的文字初始化,计时器初始化;
         return;
    } else {
         obj.html( timeFormat(countdown).toString() + "秒" );  //此处可以拼接汉字
         countdown--;
    }
	console.log(countdown)
	setTimeout(()=>{
	  setTime(obj)
	},1000)
}

// 倒計時調用示例
//setTime(obj) // obj为jq对象，如$('.codebox')


/* 
pc版 代碼
 */
// 調出搜索框
 $('body').delegate('#web-search-btn', 'click', function() {
 	let _this = $(this); 
 	let searchBox = _this.closest('.R-Top-tool').find('.search-box') ;
	if(searchBox.hasClass('show')){
		searchBox.removeClass('show')
	}else{
		searchBox.addClass('show').find('input').focus();
	}
 	return false ;
 })


 $(".web-top .back-top").click(function () {
	if($('body').hasClass('pc-model')){
		$("html,body").animate({scrollTop:'0px'},300); //回到顶部
	}else{
		$(".shop-content").animate({scrollTop:'0px'},300); //回到顶部 
	}
	return false ;
});


$(window).on('scroll', function(){
	if ($(window).scrollTop() > 450) {
		$('.web-top').fadeIn();
	} else {
		$('.web-top').fadeOut();
	}
});
$('.shop-content').on('scroll', function(){
	if ($('.shop-content').scrollTop() > 450) {
		$('.web-top').fadeIn();
	} else {
		$('.web-top').fadeOut();
	}
});

//iframe 高度自適應
function changeFrameHeight(_this) {
    if(navigator.userAgent.indexOf("MSIE")>0||navigator.userAgent.indexOf("rv:11")>0||navigator.userAgent.indexOf("Firefox")>0){
		$(_this).get(0).style.height = $(_this).get(0).contentWindow.document.body.scrollHeight+"px";
	}else{
		$(_this).get(0).style.height = $(_this).get(0).contentWindow.document.documentElement.scrollHeight+"px";
	}
}

// h5與pc版切換
function judgePc(){
	var winW  = $(window).width() ;
	if( winW > 768 ){
		$('body').addClass('pc-model')
		try {
			$('#content_frame_pc')[0].contentWindow.location.reload(true); 
		} catch (e) {
			console.log(e)
		}
	}else{
		$('body').removeClass('pc-model');
		try {
			$('#content_frame_monile')[0].contentWindow.location.reload(true); 
		} catch (e) {
			console.log(e)
		}
	}
	$('html').show() ;
}
judgePc() ;

$(window).on('resize',function(){
	judgePc() ;
})


$('body').on('change', '.web_language_select', function() {
	let link = '';

	if ('' == location.search) {
		link = location.origin + location.pathname + '?locale=' + $(this).val();
		// location.href += '?locale=' + $(this).val();
	} else {
		link = location.origin + location.pathname + location.search + '&locale=' + $(this).val();
	}

	location.href = link + location.hash;
});

$('body').on('change', '.web_currency_select', function() {
	let link = '';

	if ('' == location.search) {
		link = location.origin + location.pathname + '?currency=' + $(this).val();
		// location.href += '?currency=' + $(this).val();
	} else {
		link =  location.origin + location.pathname + location.search + '&currency=' + $(this).val();
	}

	location.href = link + location.hash;
});

// 頂部個人信息點擊事件
$('body').delegate('.shortcut','click',function(){
	const _this = $(this) ;
	const shortcutBox = _this.find('.shortcut-box') ;
	if(shortcutBox.is(':hidden')){
		shortcutBox.show()
	}else{
		shortcutBox.hide();
	}
	return false ;
})
// 阻止事件冒泡
$('body').delegate('.shortcut-box','click',function(e){
	e.stopPropagation() ;
})
// 點擊空白區域關閉下拉框
$(document).on('click',function(e){
	var cont = $('.shortcut,.shortcut-box');
	if ( !cont.is(e.target) && cont.has(e.target).length === 0) {
		$('.shortcut-box').hide();
	}
})

// 檢查cookie隱私權
$.get('/cookie/privacy/check', function (res) {
	if (200 == res.code) {
		return;
	}

	$('.privacy').show();
});
$('body').on('click', '.privacy-get', function (e) {
	e.preventDefault();
	$('.privacy').hide();
});