// Skip Nav
$(document).on('click', '#skip a', function (e) {
	var tg = $(this).attr('href');
	$(tg).attr('tabindex', '0').focus();
	e.preventDefault();
});

// LNB
$('.lnb-list a').on('click', function (e) {
	// e.preventDefault();
	$(this).parent('li').addClass('on').siblings('li').removeClass('on');
});
if ($('.lnb-list').outerWidth() > $('.lnb-container').outerWidth()) {
	var lnbNum = $('.lnb-list .on').index('.lnb-list li');
	$('.lnb-container').animate({
		scrollLeft: $('.lnb-list .on').position().left
	}, 0);
}

// 탭메뉴
var tabCurrentIndex;
$('.tab-list .on a').each(function () {
	var tg = $(this).attr('href');
	if (tg !== '#' && tg.charAt(0) === '#') {
		$(tg + '.tab-content').css('display', 'block');
	}
});

$('.tab-list a').on('click', function (e) {
	var tg = $(this).attr('href');
	$(this).parent('li').addClass('on').siblings('li').removeClass('on');

	if (tg === '#' || tg === '' || tg === '#;') {
		e.preventDefault();
	} else if (tg.charAt(0) === '#') {
		if ($(tg).hasClass('tab-content')) {
			$(tg).show().siblings('.tab-content').hide();
			e.preventDefault();
		}
	}
});

// 맨 위로
(function toTop() {
	var $body = $('.wrapper');

	$(window).scroll(function () {
		if ($(this).scrollTop() > 10) {
			$('#btn-totop').addClass('show');
			$body.addClass('scrolling')
		} else {
			$('#btn-totop').removeClass('show');
			$body.removeClass('scrolling');
		}
	});

	$(document).on('click', '#btn-totop', function (e) {
		e.preventDefault();
		$('body, html').animate({
			scrollTop: 0
		}, 300);
		return false;
	});
	
	$(document).on('click', '.rental_customer_btn', function (e) {
		e.preventDefault();
		scrollPage(0);
		return false;
	});
})();

// 데이트피커 적용
$('.form-datepicker').each(function () {
	setDatepicker($(this));
});

$(document).on('click', '.btn-datepicker', function (e) {
	e.preventDefault();
	openCalendar($(this).prev());
});

function setDatepicker(obj) {
	$(obj).wrap('<span class="datepicker"></span>');
	$(obj).after('<button type="button" class="btn btn-datepicker"><i class="ico ico-datepicker-gray"></i><span class="sr-only">달력</span></button>');
}

// 모달 레이어
$(function () {
	$('.modal-popup.show').each(function () {
		openModal($(this), null);
	});
});

var modalOpener = null;
$(document).on('click', 'a.js-layer-open', function (e) {
	var tg = $(this).attr('href');
	openModal(tg, $(this));
	e.preventDefault();
}).on('click', '.js-layer-close, .modal-popup .btn-close-popup, .modal-popup .js-layer-close, .modal-popup .dimed', function () {
	var target = $(this).closest('.modal-popup').attr('id');
	closeModal('#' + target, modalOpener);
}).on('keydown', '.modal-popup .popup-inner', function (e) {
	if ($('.popup-inner').is(e.target) && e.keyCode == 9 && e.shiftKey) { // shift + tab
		e.preventDefault();
		$(this).find('.btn-close-popup').focus();
	}
}).on('keydown', '.modal-popup .btn-close-popup', function (e) {
	if (e.keyCode == 9 && !e.shiftKey) { // tab
		e.preventDefault();
		$(this).closest('.popup-inner').attr('tabindex', '0').focus();
		$(this).unbind('keydown').keydown();
	}
});

function openModal(_target, _opener) {
	modalOpener = _opener;
	$(_target).insertAfter('.wrapper');

	setTimeout(function () {
		$('body').addClass('modal-pop-modal-pop-disable-scrolling')
		$(_target).addClass('show').removeClass('hide');
	}, 100);
	setTimeout(function () {
		$('.popup-inner', _target).attr('tabindex', '0').focus();
	}, 300);
	bodyScroll(true, $('body').width());

	//show-modal 이벤트 발생
	// $(_target)[0].dispatchEvent(showEvent);

}

function closeModal(_target, _opener) {
	bodyScroll(false);
	var tg = null;

	if (_opener) {
		tg = $(_target);
		modalOpener = $(_opener);
	} else {
		tg = $('.modal-popup.show');
		modalOpener = null;
	}

	$(tg).addClass('hide').removeClass('show');
	if (modalOpener !== null) {
		modalOpener.focus();
		modalOpener = null;
	}

	//hidden-modal 이벤트 발생
	// tg[0].dispatchEvent(hiddenEvent);
}

var modalScrollTop;

function bodyScroll(_status, _orgWidth) {
	var $fixedModal = $('body');
	//바디를 담음

	if (_status) {
		modalScrollTop = $(window).scrollTop();
		//모달 스크롤 탑은 윈도우 객체의 스크롤 탑값임
		$fixedModal.addClass('modal-open');
		//바디에 어드클레스 모달 오픈
		$fixedModal.css({
			'top': -1 * modalScrollTop,
			//바디에 탑 값을 스크롤 값만큼 땡김

		});
		if ($('html').get(0).scrollWidth > $('html').width() === false) {
			$fixedModal.css('margin-right', $('body').width() - _orgWidth);

		}
	} else {
		$fixedModal.removeClass('modal-open');
		$fixedModal.css({
			'margin-right': '',
			'top': '',
		});
		$(window).scrollTop(modalScrollTop);
	}
}

// 추가
document.ontouchmove = function (event) {
	//터치시
	var isTouchMoveAllowed = true,
		//
		// target = event.target;
		target = document.querySelector('body')

	//타겟임
	while (target !== null) {
		if (target.classList && target.classList.contains('modal-pop-disable-scrolling')) {
			//클레스 검사
			isTouchMoveAllowed = false;

			break;
		}
		target = target.parentNode;
	}
	if (!isTouchMoveAllowed) {
		event.preventDefault();
	}
};


// 윈도우 팝업 오픈
var popOpenBtn = null;

function openWindow(_obj, popName, w, h, _opener) {
	var popW = 900;
	var popH = 600;
	var myUrl = _obj;

	if (typeof _obj !== 'string') {
		if ($(_obj).prop('tagName') === 'A') {
			popOpenBtn = $(_obj);
			myUrl = $(_obj).attr('href');
		} else if (_opener) {
			popOpenBtn = $(_opener);
		}
	}

	if (w) popW = w;
	if (h) popH = h;
	var left = window.screenX + (window.outerWidth - popW) / 2;
	var top = window.screenY + (window.outerHeight - popH) / 2;
	window.open(myUrl, popName, 'width=' + popW + ', height=' + popH + ', left=' + left + ', top=' + top + ', scrollbars=yes');
}

function closeWindow() {
	if (window.opener != null) {
		window.opener.openerFocus();
	}
	window.close();
}

function openerFocus() {
	if (popOpenBtn != null) {
		$(popOpenBtn).focus();
		popOpenBtn = null;
	}
}