var images = new Array();

function preload() {
	for (i = 0; i < preload.arguments.length; i++) {
		images[i] = new Image();
		images[i].src = preload.arguments[i];
	}
}

function tsearch_submit(f) {
	if (f.stx.value.length < 2) {
		alert("검색어는 두글자 이상 입력하십시오.");
		f.stx.focus();
		return false;
	}
	return true;
}

$(document).on("mouseover", '[data-toggle="popover-hover"]', function () {
	var e = $(this);
	e.off('hover');
	e.popover({
	  html: true,
	  trigger: 'hover',
	  placement: 'left',
	  content: function () { return '<img src="' + e.data('img') + '" class="w-100 na-r1"/>'; }
	}).popover('show');
});

$(document).ready(function() {

	// 즐겨찾기
    $(document).on('click', '#favorite', function(e) {
        var bookmarkURL = window.location.href;
        var bookmarkTitle = document.title;
        var triggerDefault = false;

        if (window.sidebar && window.sidebar.addPanel) {
            // Firefox version < 23
            window.sidebar.addPanel(bookmarkTitle, bookmarkURL, '');
        } else if ((window.sidebar && (navigator.userAgent.toLowerCase().indexOf('firefox') > -1)) || (window.opera && window.print)) {
            // Firefox version >= 23 and Opera Hotlist
            var $this = $(this);
            $this.attr('href', bookmarkURL);
            $this.attr('title', bookmarkTitle);
            $this.attr('rel', 'sidebar');
            $this.off(e);
            triggerDefault = true;
        } else if (window.external && ('AddFavorite' in window.external)) {
            // IE Favorite
            window.external.AddFavorite(bookmarkURL, bookmarkTitle);
        } else {
            // WebKit - Safari/Chrome
            alert((navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 'Cmd' : 'Ctrl') + '+D 키를 눌러 즐겨찾기에 등록하실 수 있습니다.');
        }

        return triggerDefault;
    });

	// 툴팁
    $('body').tooltip({
		selector: "[data-toggle='tooltip']"
    });
	
	// 위젯 탭 전환 - 마우스오버
	$(".wg_head_tab").hover(function(){
		console.log($(this).parent().parent());
		const wg_wrapper = $(this).attr('data-parent');
		const target_id = $(this).attr("data-target");
		const more_href = $(this).attr("data-url");

		$("#"+wg_wrapper+" .wg_head_tab").removeClass('active');
		$("#"+wg_wrapper+" .wg_head_tab").each(function(){
			if ($(this).attr("data-target") == target_id) {
				$(this).addClass('active');
			}
			else {
				$(this).removeClass('active');
			}
		});

		if (more_href) {
			$("#"+wg_wrapper+" .wg_head_more a").attr("href", more_href);
			//$("#"+wg_wrapper+" .wg_head_more_m a").attr("href", more_href);
		}

		$("#"+wg_wrapper+" .wg_content_wrap").each(function(){
			if ($(this).attr("id") == target_id) {
				$(this).addClass('active');
			}
			else {
				$(this).removeClass('active');
			}
		});
	});
	
	// 위젯 탭 전환 - 클릭
	$(".wg_head_tab").click(function(){
		const wg_wrapper = $(this).attr('data-parent');
		const target_id = $(this).attr("data-target");
		const more_href = $(this).attr("data-url");

		$("#"+wg_wrapper+" .wg_head_tab").removeClass('active');
		$("#"+wg_wrapper+" .wg_head_tab").each(function(){
			if ($(this).attr("data-target") == target_id) {
				$(this).addClass('active');
				if (more_href) {
					if ($(this).parent().parent().attr('id') == 'wg_main_board_head_m') {
						$(this).attr("onclick", "location.href='"+more_href+"'");
					}
				}
			}
			else {
				$(this).removeClass('active');
				if ($(this).parent().parent().attr('id') == 'wg_main_board_head_m') {
					$(this).removeAttr('onclick');
				}
			}
		});
	
		if (more_href) {
			$("#"+wg_wrapper+" .wg_head_more a").attr("href", more_href);
			//$("#"+wg_wrapper+" .wg_head_more_m a").attr("href", more_href);
		}

		$("#"+wg_wrapper+" .wg_content_wrap").each(function(){
			if ($(this).attr("id") == target_id) {
				$(this).addClass('active');
			}
			else {
				$(this).removeClass('active');
			}
		});
	});
	
	// 닉네임 복사
	$(document).on('click', '.btn_mb_nick_copy', function(){
		const copyTextarea = document.createElement("textarea");
		copyTextarea.style.position = "fixed";
		copyTextarea.style.opacity = "0";
		copyTextarea.textContent = $(this).attr("data-value");

		document.body.appendChild(copyTextarea);
		copyTextarea.select();
		document.execCommand("copy");
		document.body.removeChild(copyTextarea);

		const toast_msg = $(".toast_msg", this);

		toast_msg.fadeIn();
		setTimeout(function(){
			toast_msg.fadeOut();
		}, 2000);
	});

	// 스크랩시 댓글 내용 삭제
	// 댓글 입력창을 주석처리 해놓았지만, 혹시 모를 악용을 위해..
	$("form[name='f_scrap_popin']").submit(function(){
		$("#wr_content", this).val("");
	});
});