(function($){
	$(window).on({
		'load':function(){
			var 
			configs = {
				cookieName:'popup_new_item', 
				cookieOption:{
					//expires:1, 
					path:'/', 
					secure:true 
				}, 
				scroll:400, 
				device:[
					'sp' 
				], 
				fileList:[
					{
						type:'js', 
						func:$.numberFormat, 
						url:'/js/jquery/jquery.numberFormat.js' 
					}, 
					{
						type:'js', 
						func:$.dateToUnixTime, 
						url:'/js/jquery/jquery.dateToUnixTime.js' 
					}, 
					{
						type:'js', 
						func:$.cookie, 
						url:'/js/jquery/jquery.cookie.js' 
					}, 
					{
						type:'js', 
						func:$.fn.sliceStrLimit, 
						url:'/plugin/jquery/slice_str_limit/jquery.sliceStrLimit.js' 
					}, 
					{
						type:'js', 
						func:$.fn.popup, 
						url:'/plugin/jquery/popup/jquery.popup.js' 
					}, 
					{
						type:'css', 
						url:'/plugin/jquery/popup/default.css' 
					}, 
					{
						type:'css', 
						url:'/item/new_item_popup/css/index.css' 
					} 
				], 
				cache:false, 
				cacheParam:'r', 
				sliceStrLimit:14 
			}, 
			funcConfirmDevice = () => {
				var 
				nav = navigator.userAgent.toLowerCase();
				return nav.indexOf('iphone') > 0 || nav.indexOf('ipad') > 0 || nav.indexOf('android') > 0 || nav.indexOf('mobile') > 0?'sp':'pc';
			};
			if($.inArray(funcConfirmDevice(), configs.device) === -1)
			return false;
			var 
			flagExecute = false, 
			funcExecute = () => {
				$.ajax({
					url:'/configs/popup/new_item.json?r=' + Math.random(), 
					dataType:'json', 
					error:function(jqXHR, textStatus, errorThrown){
						console.log(textStatus);
						return;
					}, 
					success:function(response, textStatus, jqXHR){
						var 
						tax = 1.1, 
						currentDate = '', 
						funcGetItemName = (itemName) => {
							var 
							mchSaleTime = itemName.match(/縲�(.+?)雋ゥ螢イ譁ー菴懊€�/);
							if(mchSaleTime === null)
							return [
								itemName, 
								'' 
							];
							var 
							arrItemName = itemName.split(mchSaleTime[0]);
							return [
								arrItemName[0], 
								mchSaleTime[0] 
							];
						}, 
						elePopupNewItemList = $('#popup_new_item_list');
						elePopupNewItemList.empty();
						for(var i = 0, l = response.length;i < l;i++){
							if(currentDate === ''){
								currentDate = response[i].sale_time;
							}else 
							if(currentDate !== response[i].sale_time){
								break;
							}
							var 
							arrItemName = funcGetItemName(response[i].item_name);
							elePopupNewItemList.append('\
								<li class="clearfix" data-popup_new_item_sale_time="' + response[i].sale_time + '">\
									<a href="/fs/alamode/' + response[i].item_code + '" target="_blank" title="' + response[i].item_name + '">\
										<img src="/img/item/thumbnail/400/' + response[i].item_code + '.jpg" alt="' + response[i].item_name + '">\
									</a>\
									<div>\
										<h4>\
											<!--' + arrItemName[1] + '<br>-->\
											<span class="slice_str_limit" data-slice_str_limit="' + configs.sliceStrLimit + '">' + arrItemName[0] + '</span>\
										</h4>\
										<span class="popup_new_item_discount_icon">\
											5%OFF\
										</span>\
										<p class="popup_new_item_regular_price">\
											' + $.numberFormat(response[i].init_price) + '蜀�(遞手セシ:' + $.numberFormat(Math.floor(response[i].init_price * tax)) + '蜀�)\
										</p>\
										<p class="popup_new_item_sale_price">\
											' + $.numberFormat(response[i].init_price === response[i].price?Math.floor(response[i].price * 0.95):response[i].price) + '蜀�(遞手セシ:' + $.numberFormat(Math.floor((response[i].init_price === response[i].price?Math.floor(response[i].price * 0.95):response[i].price) * tax)) + '蜀�)\
										</p>\
										<div>\
											<a href="/fs/alamode/' + response[i].item_code + '" target="_blank" title="' + response[i].item_name + '" class="popup_new_item_item_button">\
												隧ウ邏ー縺ッ縺薙■繧噂
											</a>\
										</div>\
									</div>\
								</li>\
							');
						}
						elePopupNewItemList.find('.slice_str_limit').sliceStrLimit();
						var 
						oneDaySecond = 86400;
						funcGetDate = (timestamp) => {
							var 
							date = new Date(timestamp * 1000), 
							month = date.getMonth() + 1, 
							day = date.getDate(), 
							hour = date.getHours(), 
							minute = date.getMinutes(), 
							dayOfWeek = date.getDay(), 
							dayOfWeekStr = [
								'譌・', 
								'譛�', 
								'轣ォ', 
								'豌エ', 
								'譛ィ', 
								'驥�', 
								'蝨�' 
							][dayOfWeek];
							return month + '/' + day + '(' + dayOfWeekStr + ')' + hour + ':' + (minute < 10?'0':'') + minute;
						}, 
						elePopupNewItemDateComment = $('#popup_new_item_date_comment'), 
						elePopupNewItemList = $('#popup_new_item_list'), 
						unixTimeNewItemSaleTime = $.dateToUnixTime(elePopupNewItemList.find('> li').eq(0).data('popup_new_item_sale_time')), 
						unixTimeCurrent = $.dateToUnixTime();
						if(unixTimeNewItemSaleTime + oneDaySecond - 1 < unixTimeCurrent)
						return false;
						elePopupNewItemDateComment.html(funcGetDate(unixTimeNewItemSaleTime) + '��' + funcGetDate(unixTimeNewItemSaleTime + oneDaySecond - 1));
						$('#popup_new_item')
						.popup({
							callbackFadeOutFinish:() => {
								$.cookie(configs.cookieName, '1', configs.cookieOption);
							} 
						})
						.trigger('popup.show');
					}
				});
			}, 
			funcPutJs = (
				location, 
				callback 
			) => {
				$.getScript(location + (configs.cache !== false?'':(location.indexOf('?') === -1?'?':'&') + configs.cacheParam + '=' + Math.random()))
				.done((script, textStatus) => {
					callback(true, location);
				})
				.fail((jqxhr, settings, exception) => {
					callback(false, location);
				});
			}, 
			funcPutLink = (
				location, 
				callback 
			) => {
				var 
				eleHead = $('head'), 
				eleLink = document.createElement('link');
				eleLink.rel = 'stylesheet';
				eleLink.href = location + (configs.cache !== false?'':(location.indexOf('?') === -1?'?':'&') + configs.cacheParam + '=' + Math.random());
				eleHead.append(eleLink);
				eleLink.addEventListener('load', function(){
					callback(true, location);
				});
				eleLink.addEventListener('error', function(){
					eleLink.parentNode.removeChild(eleLink);
					callback(false, location);
				});
			}, 
			funcLoadFile = () => {
				var 
				cntImg = 0, 
				lenFile = configs.fileList.length, 
				funcPutResult = function(
					result, 
					location 
				){
					if(result === false)
					console.log('popup_new_item:load_file => errer url(' + location + ')');
					cntImg++;
					if(lenFile === cntImg)
					funcLoadComplete();
				};
				for(var i = 0;i < lenFile;i++)
				switch(configs.fileList[i].type){
					case 'js':
						if(typeof configs.fileList[i].func === 'function'){
							funcPutResult(
								true, 
								configs.fileList[i].url 
							);
							break;
						}
						funcPutJs(
							configs.fileList[i].url, 
							function(
								result, 
								location 
							){
								funcPutResult(
									result, 
									location 
								);
							} 
						);
					break;
					case 'css':
						funcPutLink(
							configs.fileList[i].url, 
							function(
								result, 
								location 
							){
								funcPutResult(
									result, 
									location 
								);
							} 
						);
					break;
					default:
						funcPutResult(false, configs.fileList[i]);
					break;
				}
			}, 
			funcPutHtml = (eleParent) => {
				$(eleParent).append('\
				<div class="popup" id="popup_new_item" data-popup_show_trigger="#popup_new_item_show_trigger" data-popup_hide_trigger="#popup_new_item_hide_trigger">\
					<span class="popup_hide_trigger" id="popup_new_item_hide_trigger"></span>\
					<div class="font_min" id="popup_new_item_parent">\
						<div id="popup_new_item_inner">\
							<h3>\
								<span>\
									豈朱€ア譛�/豌エ/譛ィ/蝨滓屆譌・20譎ゑス杤
								</span>\
								<span>\
									譁ー菴懃匱陦ィ莨喀
								</span>\
							</h3>\
							<p id="popup_new_item_5off_comment">\
								24譎る俣髯仙ョ�5%OFF\
							</p>\
							<p id="popup_new_item_date_comment"></p>\
							<ul id="popup_new_item_list"></ul>\
							<a href="/feature/new_item/" id="popup_new_item_more_button">\
								譁ー菴懊い繧、繝�Β繧偵b縺」縺ィ隕九k\
							</a>\
						</div>\
					</div>\
				</div>\
				');
			}, 
			funcLoadComplete = () => {
				var 
				eleNewItemPopupParent = $('#new_item_popup_parent');
				if(!eleNewItemPopupParent[0]){
					console.log('popup_new_item:undefined ele => #new_item_popup_parent');
					return false;
				}
				var 
				dataNewItemPopupParent = eleNewItemPopupParent.data('new_item_popup_parent');
				if(typeof dataNewItemPopupParent === 'undefined'){
					console.log('popup_new_item:undefined data => new_item_popup_parent');
					return false;
				}
				funcPutHtml(dataNewItemPopupParent);
				if(configs.scroll !== 0){
					$(window).on({
						'scroll':function(){
							var 
							scroll = $(this).scrollTop();
							if(
								flagExecute || 
								configs.scroll > scroll 
							)return false;
							flagExecute = true;
							
							var 
							displayFlag = $.cookie(configs.cookieName);
							if(
								typeof displayFlag !== 'undefined' && 
								displayFlag === '1' 
							)
							return false;
							funcExecute();
						} 
					});
				}else{
					var 
					displayFlag = $.cookie(configs.cookieName);
					if(
						typeof displayFlag !== 'undefined' && 
						displayFlag === '1' 
					)
					return false;
					funcExecute();
				}
			};
			funcLoadFile();
		} 
	});
})(jQuery);