var hideMenu = null,
	hotBnr = null,
	recentBnr = null,
	sponBnr = null;

jQuery(function($) {

	var win_w = $(window).width();

	if (win_w > 960) {
//PC	
		setting_pc();

	} else {
//Mobile	
		setting_mobile();
	}

	if (hotBnr != null) {
		hotBnr.destroySlider();
		hotBnr = null;
	}

	$("dl.gnbSearch").on("click", "dt a", function(){
		var sClass = $("dl.gnbSearch dt").attr("class");
		if (sClass != "view") {
			$("ul#gnb > li").removeClass("on");
			$("ul#gnb ul").css('display', 'none');

			$("dl.gnbSearch dt").addClass("view");
			$("dl.gnbSearch dd").show();
		} else {
			$("ul#gnb ul").css('display', '');

			$("dl.gnbSearch dt").removeClass("view");
			$("dl.gnbSearch dd").hide();
		}
	
		return false
	});

	if ($("div.articleBrief").length) {
		$("div#container").addClass("article");
	}

// 탭메뉴
	var tabArea = $(".tabArea");
	
	if (tabArea.length) {
		
		for (var i=0 ; i<tabArea.length ; i++) {
			var tabMenu = tabArea.eq(i).find("ul.tabMenu > li"),
				tabCon = tabArea.eq(i).find(".tabCon");
			
			//tabMenu.removeClass("on").eq(0).addClass("on");
			//tabCon.hide().eq(0).show();
		}

		if ($("div.necEdu").length) 	{
			tabMenu.removeClass("on");
			tabCon.hide();
		}
		
		tabArea.on("click", "ul.tabMenu a", function(){
			var currTabMenu =  $(this).parent().parent().parent().find("ul.tabMenu li"),
				currTabCon =  $(this).parent().parent().parent().find(".tabCon"),
				currIdx = $(this).parent().index();

			if ($(this).parent().attr("class") == "on") {
				currTabMenu.removeClass("on");
				currTabCon.slideUp();
			} else {
				currTabMenu.removeClass("on").eq(currIdx).addClass("on");
				currTabCon.hide().eq(currIdx).slideDown();
			}
		

			return false
		});
		
	}

	var rwTabArea = $(".rwTabArea");
	if (rwTabArea.length) {
		var rwTabMenu = rwTabArea.find("ul.rwTabMenu li"),
			rwTabCon = rwTabArea.find("div.tabCon");

		//rwTabMenu.eq(0).addClass("on");
		//rwTabCon.hide().eq(0).show();

		rwTabMenu.on("click", "a",  function(){
			var win_w = $(window).width(),
				nIdx = $(this).parent().index(),
				menuTxt = $(this).text();

			rwTabArea.find("dl.rwMenu > dt").removeClass("view");
			rwTabArea.find("dl.rwMenu > dt").find("i").attr("class",  function(i){
				var src = $(this).attr("class");
				return src.replace("-up", "-down");
			});
			rwTabArea.find(".toggleCon").slideUp();			

			rwTabArea.find("a.trigger span").text(menuTxt);
			rwTabMenu.removeClass("on").eq(nIdx).addClass("on");
			rwTabCon.hide().eq(nIdx).show();
			
			
			return false
		});
	}

	$(document).on("click","span.trigger",function() {
		if($(this)[0].id=="trigger_c") {
			var _currToggle = $(this).parent().parent().parent(),
			_currToggleCon = $(this).parent().parent().parent().find("dd#toggleCon_c"),
			sClass = $(this).attr("class");
			
			$(this).parent().find("span#trigger_c").removeClass("view");
			$(this).parent().parent().find("dd#toggleCon_c").hide();
		} else {
			var _currToggle = $(this).parent().parent(),
			_currToggleCon = $(this).parent().next(),
			sClass = $(this).attr("class");
		}

		if (sClass != "trigger view") {
			$(this).addClass("view");
			$(this).find("i").attr("class",  function(i){
				var src = $(this).attr("class");
				return src.replace("-down", "-up");
			});
			_currToggleCon.slideDown();
		} else {
			$(this).removeClass("view");
			$(this).find("i").attr("class",  function(i){
				var src = $(this).attr("class");
				return src.replace("-up", "-down");
			});
			_currToggleCon.slideUp();
		}

		return false
	});

	$(document).on("click","a.trigger",function() {

		if($(this)[0].id=="trigger_a") {
			var _currToggle = $(this).parent().parent(),
			_currToggleCon = $(this).parent().parent().find("dd#toggleCon_a"),
			sClass = $(this).attr("class");

			$(this).parent().find("a#trigger_ga").removeClass("view");
			$(this).parent().parent().find("dd#toggleCon_ga").hide();
		} else if($(this)[0].id=="trigger_ga") {
			var _currToggle = $(this).parent().parent(),
			_currToggleCon = $(this).parent().parent().find("dd#toggleCon_ga"),
			sClass = $(this).attr("class");
			
			$(this).parent().find("a#trigger_a").removeClass("view");
			$(this).parent().parent().find("dd#toggleCon_a").hide();
		} else {
			var _currToggle = $(this).parent().parent(),
			_currToggleCon = $(this).parent().next(),
			sClass = $(this).attr("class");
		}

		if (sClass != "trigger view") {
			$(this).addClass("view");
			$(this).find("i").attr("class",  function(i){
				var src = $(this).attr("class");
				return src.replace("-down", "-up");
			});
			_currToggleCon.slideDown();
		} else {
			$(this).removeClass("view");
			$(this).find("i").attr("class",  function(i){
				var src = $(this).attr("class");
				return src.replace("-up", "-down");
			});
			_currToggleCon.slideUp();
		}

		return false
	});

	//filter	
	$(document).on("click","a#filter_category_more",function() {
		
		if($("#filter_category_layer").css("display") == "none"){
			$('#filter_category_layer').show();
		} else {
			$('#filter_category_layer').hide();
		}

		return false
	});

	$(document).on("click","a#filter_topic_more",function() {
		
		if($("#filter_topic_layer").css("display") == "none"){
			$('#filter_topic_layer').show();
		} else {
			$('#filter_topic_layer').hide();
		}

		return false
	});

	$(document).on("click","a#filter_keyword_more",function() {
		
		if($("#filter_keyword_layer").css("display") == "none"){
			$('#filter_keyword_layer').show();
		} else {
			$('#filter_keyword_layer').hide();
		}

		return false
	});

	$(document).on("click","a#filter_author_more",function() {
		
		if($("#filter_author_layer").css("display") == "none"){
			$('#filter_author_layer').show();
		} else {
			$('#filter_author_layer').hide();
		}

		return false
	});

	$(document).on("click","a#filter_year_more",function() {
		
		if($("#filter_year_layer").css("display") == "none"){
			$('#filter_year_layer').show();
		} else {
			$('#filter_year_layer').hide();
		}

		return false
	});

	$(document).on("click","a#filter_fund_more",function() {
		
		if($("#filter_fund_layer").css("display") == "none"){
			$('#filter_fund_layer').show();
		} else {
			$('#filter_fund_layer').hide();
		}

		return false
	});

	$('ul > li > input:checkbox').on("click", function(){		
		var this_name = $(this)[0].name;
		var this_value = $(this)[0].value;
		var this_checked = $(this)[0].checked;

		$('ul > li > input:checkbox').each(function() {
			if($(this)[0].name == this_name && $(this)[0].value == this_value) { 
				$(this)[0].checked = this_checked;
			}
		});
	});

	$('ul[name="filter"] > li > input:checkbox').on("click", function(){	
		$('#page')[0].value = "1";
		$('#filter_form')[0].submit();
	});

	//filer layer
	$(document).on("click","input#filter_layer_submit",function() {
		$('#page')[0].value = "1";
		$('#filter_form')[0].submit();
	});

	$(document).on("click","a#filter_layer_close",function() {
		$('.layerPopup').each(function() {
			$(this).hide();
		});
	});	

	//기타
	$(document).on("click","span[name='funded_link']",function() {
		var this_value = $(this)[0].innerText;

		$("ul > li > input:checkbox[name='p_filter_fund[]']").each(function() {
			if($(this)[0].value == this_value) { 
				$(this)[0].checked = true;
			}
		});

		$('#page')[0].value = "1";
		$('#filter_form')[0].submit();
	});
	
	$("dl.popupCite").on("click", " .close a", function(){
		$("dl.popupCite").hide();
		$(".div_dc_back").hide();
		return false
	});


	$("dl.popupCite").on("click", " .close_ic a", function(){
		$(".div_dc_back").hide();
		return false
	});

		
	$(".div_dc").on("click", "a", function(){
		$(".div_dc_back").hide();
		return false
	});


	$('form[name="cited_btn"] a.btnPoint_down').on("click", function(){
		var cited_popup = $(this).closest("li");
		if(cited_popup.find(".div_dc_back").css("display")=="none") {

			cited_popup.find(".div_dc_back").show();
			cited_popup.find(".popupCite select").trigger("change");
			cited_popup.find(".div_dc_back select").trigger("change");
		} else {
				cited_popup.find(".popupCite").hide();
				cited_popup.find(".div_dc_back").hide();
		}
		return false
	});




	$(".cited_view").on("click", "a", function(){
		var cited_popup = $(this).closest("li");
		if(cited_popup.find(".popupCite").css("display")=="none") {
			cited_popup.find(".popupCite").show();
			cited_popup.find(".popupCite select").trigger("change");
		} else cited_popup.find(".popupCite").hide();
		return false
	});

	if ($('form[name="cited_btn"]').length) {

		$('form[name="cited_btn"] select').change( function(){
			type = $(this).val();
			number = $(this).parent().find('input:hidden[name="number"]').val();
			text = $(this).closest("dd").find('.popupCon')[0];

			$.ajax({
				type: 'POST',
				data: {type:type, number:number},
				url: '../journal/ajax_cited_text.php',
				success: function(result) {
					text.innerHTML = result;
				}
			});
		});

		$('form[name="cited_btn"] a.btnPoint').on("click", function(){
			text = $(this).closest("dd").find('.popupCon')[0].innerText;
			$('#clip_target').val(text);
			$('#clip_target').select();
			
			document.execCommand('copy');
			alert('Citation successfully copied.');
		});

	

		$('form[name="cited_btn"] a.btnDef').on("click", function(){
			type = $(this).val();
			number = $(this).parent().find('input:hidden[name="number"]').val();
			text = $(this).closest("dd").find('.popupCon')[0];

			$.ajax({
				type: 'POST',
				data: {type:type, number:number},
				url: '../journal/ajax_cited_text.php',
				success: function(result) {
					text.innerHTML = result;
				}
			});
		});
	}
});

window.addEventListener('load', function()
{
	main_resize();	
});


function main_resize() {
	var win_w = $(window).width();
    var main_hot = $("div.mainArticle > div.hot");
	var max_heignt = 0;
	var max_heignt_resize_ck = false;

	if (main_hot.length) {

		max_heignt = main_hot[0].clientHeight;

		$("div.hot li.slick-slide").each(function() {
			if(max_heignt < $(this)[0].clientHeight) {
				max_heignt = $(this)[0].clientHeight;
				max_heignt_resize_ck = true;
			}
		});
		
		$("div.mainArticle > div.hot").css({"height":max_heignt});
		if(win_w + 16 > 960) {

			$max_height=  (main_hot[0].clientHeight / 3) - 15;
			if (win_w + 16 > 1360) $max_height= (main_hot[0].clientHeight / 2) - 7.5;

			$("div.mainArticle > ul.articleList > li > a").each(function() {
				if($max_height < $(this)[0].clientHeight) {
					$max_height = $(this)[0].clientHeight;	
				}
			});

			$("div.mainArticle > ul.articleList > li > a").css({"height":$max_height});

			if (win_w + 16 > 1360) {
				if(main_hot[0].clientHeight < ($max_height * 2) + 15) {
					$("div.mainArticle > div.hot").css({"height":($max_height * 2) + 15});
				}
			} else {
				if(main_hot[0].clientHeight < ($max_height * 3) + 30) {
					$("div.mainArticle > div.hot").css({"height":($max_height * 3) + 30});
				}
			}
		}	
	}
}

// 윈도우 창 size 변경시
$(window).resize(function(){

	var win_w = $(window).width();
	if (win_w > 960) {
		//PC	
		setting_pc();
		main_resize();
	} else {
		//Mobile	
		setting_mobile();
	}

	if (hotBnr != null) {
		hotBnr.destroySlider();
		hotBnr = null;
	}

});

//페이지새로고침
function f_page_reload(){
	location.href = location.href;
}	

//필터 초기화
function f_filter_reset(){

	history.replaceState({}, null, location.pathname);// GET 파라미터가 있는 경우 파라미터 삭제

	$("ul > li > input:checkbox[name='p_filter_topic[]']").each(function() {
		$(this)[0].checked = false;
	});

	$("ul > li > input:checkbox[name='p_filter_category[]']").each(function() {
		$(this)[0].checked = false;
	});
	
	$("ul > li > input:checkbox[name='p_filter_keyword[]']").each(function() {
		$(this)[0].checked = false;
	});

	$("ul > li > input:checkbox[name='p_filter_author[]']").each(function() {
		$(this)[0].checked = false;
	});

	$("ul > li > input:checkbox[name='p_filter_year[]']").each(function() {
		$(this)[0].checked = false;
	});

	$("ul > li > input:checkbox[name='p_filter_fund[]']").each(function() {
		$(this)[0].checked = false;
	});

	$('#filter_form')[0].submit();
	
}	


//페이지 선택
function page_search(page){
	$('#page')[0].value = page;
	$('#filter_form')[0].submit();
}	

//Mobile
function setting_mobile() {
	$("div.wrapper, div#headerWrap, ul#gnb ul, ul.articleList li").removeAttr("style");
	$("div.mainArticle div.hot, ul.articleList a").removeAttr("style");
	$("ul#gnb > li").removeClass("on");
	$("ul#gnb > li, ul#gnb > li > a").off();
	//$("dl.gnbSearch dt").removeClass("view");
	//$("dl.gnbSearch dd").hide();


	setTimeout(function(){
		var win_h = $(window).height(),
			header_h = $("div#headerWrap").outerHeight(),
			footer_h = $("div#footerWrap").outerHeight(),
			con_minH = win_h - header_h - footer_h;

		$("div#container").css({"min-height":con_minH})

	}, 300);


	$("ul#gnb > li > a").on("click", function(){
		var sClass = $(this).parent().attr("class");
		if (sClass == "on") {
			$(this).parent().removeClass("on");
			$(this).parent().find("ul").slideUp();
		} else {
			//$("dl.gnbSearch dt").removeClass("view");
			//$("dl.gnbSearch dd").hide();

			$("ul#gnb > li").removeClass("on");
			$("ul#gnb ul").hide();

			$(this).parent().addClass("on");
			$(this).parent().find("ul").slideDown();
		}
		return false
	});


	if ($("div.lnbWrap").length) {

		$("div.lnbWrap dt").removeClass();
		$("div.lnbWrap dl, div.lnbWrap dd").removeAttr("style");

		$("div.lnbWrap dt a").on("click", function(){
			var sClass = $(this).parent().attr("class"),
				currCon = $(this).parent().next();

			if (sClass == "view") {
				$(this).parent().removeClass("view");
				currCon.slideUp();
			} else {
				$(this).parent().addClass("view");
				currCon.slideDown();
			}
			return false
		});


		$("body:not(div.lnbWrap)").on("click", function(){
			$("div.lnbWrap dt").removeClass("view");
			$("div.lnbWrap dd").hide();
		});

	}


	if (recentBnr != null) {
		recentBnr.destroySlider();
		$("div.recent ul, div.recent li").removeAttr("style");
		recentBnr = null;
	}

	if (sponBnr != null) {
		sponBnr.destroySlider();
		$("div.bnrzone ul, div.bnrzone li").removeAttr("style");
		sponBnr = null;
	}
}



//PC 
function setting_pc() {
	$("body").removeClass("overHidden");
	$("div.wrapper, ul.headerMenu, div.contents, .toggleCon, ul.subMenu, dl.rwMenu dd, dl.rwMenu li, dl.toggleArea_rw > dd").removeAttr("style");
	$("div.mainArticle div.hot, ul.articleList a").removeAttr("style");
	$("ul#gnb > li").removeClass("on");
	$("ul#gnb > li, ul#gnb > li > a").off();
	//$("dl.gnbSearch dt").removeClass("view");
	//$("dl.gnbSearch dd").hide();

	/*
	setTimeout(function(){
		for (var i=0; i<$("ul#gnb > li").length ; i++) {
			var re_poLeft = $("ul#gnb > li > a").eq(i).position().left;
			$("ul#gnb > li").eq(i).find("ul").css({
				"left":re_poLeft
			});
		}
	}, 300);
	*/

	if ($("ul.articleList a").length) {
		for (var i=0; i<$("ul.articleList a").length; i++) {
			//var re_paddingB = $("ul.articleList a").eq(i).find("span.type").outerHeight() + 40;
			var re_paddingB = $("ul.articleList a").eq(i).find("span.type").outerHeight();
			$("ul.articleList a").eq(i).css({
				"padding-bottom":re_paddingB
			});
		}
	}
	/*
	$("ul#gnb > li").on("mouseenter", function(){
		if (hideMenu != null) {
			clearTimeout(hideMenu);
		}
		//$("dl.gnbSearch dt").removeClass("view");
		//$("dl.gnbSearch > dd").hide();

		$("ul#gnb > li").removeClass("on");
		$("ul#gnb ul").hide();

		
		$(this).addClass("on");
		if ($(this).find("ul").length) {
			$(this).find("ul").show();
		}

	});

	$("ul#gnb ul").on("mouseenter", function(){
		if (hideMenu != null) {
			clearTimeout(hideMenu);
		}
	});

	$("ul#gnb").on("mouseleave", function(){
		hideMenu = setTimeout(function(){
			$("ul#gnb > li").removeClass("on");
			$("ul#gnb ul").hide();
		}, 500);
	});
	*/

	if (recentBnr != null) {
		recentBnr.destroySlider();
		$("div.recent ul, div.recent li").removeAttr("style");
		recentBnr = null;
	}

	if (sponBnr != null) {
		sponBnr.destroySlider();
		$("div.bnrzone ul, div.bnrzone li").removeAttr("style");
		sponBnr = null;
	}

//반응형 메뉴
	$(document).ready(function () {
		main_menu_fixed();

		// 현재 URL에서 #supplementary-material이 있는지 확인
		if (window.location.hash === '#supplementary-material') {
			
			var targetHref = $('a[name="supplementary-materialLink"]').attr('href');
			
			if (targetHref) {
				var targetElement = $(targetHref);
				var closestH4 = targetElement.closest('div.panel').prevAll('h4.link-target').first();

				// h4.link-target이 존재하면 해당 위치로 스크롤 이동
				if (closestH4.length) {
					$('html, body').animate({
						scrollTop: closestH4.offset().top - 223
					}, 0);
				}
			}
		}
	});

	if ($("dl.rwMenu").length) {

		var sClass = $("dl.rwMenu ul").attr("class");
		if (sClass == null) 	{
			var reW = 100 / $("dl.rwMenu li").length;
			$("dl.rwMenu li").css({
				"width" : reW + "%"
			});
		}
	}

	if ($("div.articleBrief").length) {
		menu_fixed();

		 $("ul.conMenu a").click(function(event){
			event.preventDefault();

			var move_h = $(this.hash).offset().top - 123,
				nIdx = $(this).parent().index();

			if ($(".fixedArea").length) {
				//move_h = move_h;
			}
			$('html,body').animate({scrollTop:move_h}, 500);

			$("ul.conMenu li").removeClass("on").eq(nIdx).addClass("on");
		});

		$(window).on('scroll', function() {
			var inView = false;

			$('.link-target').each(function(index, element) {
				var preBasicH = 0;
				var basicH = $(element).offset().top - 100;
				if (index > 0) {
					preElement = $('.link-target').eq(index - 1);
					preBasicH = preElement.offset().top - 100;
				} else {
					preElement = $(element);
				}

				var preBasicH = preElement.offset().top - 220;
				var basicH = $(element).offset().top - 220;
				var scrollTop = $(window).scrollTop();

				if(basicH > scrollTop && scrollTop >= preBasicH) {
					inView = true;
					var sID = preElement.attr('id');
					$('ul.conMenu a').removeClass('on');
					var selectedElement = $('ul.conMenu a[href=#'+ sID +']');
					selectedElement.addClass('on');

					var ulTop = $('ul.conMenu').offset().top;
					var elementTop = selectedElement.offset().top;
					if(elementTop < ulTop) {
						$('ul.conMenu').scrollTop($('ul.conMenu').scrollTop() - (ulTop - elementTop));
					}

					var ulBottom = $('ul.conMenu').offset().top + $('ul.conMenu').outerHeight();
					var elementBottom = selectedElement.offset().top + selectedElement.outerHeight();
					if(elementBottom > ulBottom) {
						$('ul.conMenu').scrollTop($('ul.conMenu').scrollTop() + (elementBottom - ulBottom));
					}
					return false;
				}

				var scrollValue = $(window).scrollTop(),
				basic_h = $("div#headerWrap").outerHeight() + $("div.titArea").outerHeight() + $("div.articleBrief").outerHeight();

				
				if (index === ($('.link-target').length - 1) && scrollValue > basic_h) {
					inView = true;
					var sID = $(element).attr('id');
					$('ul.conMenu a').removeClass('on');
					var selectedElement = $('ul.conMenu a[href=#'+ sID +']');
					selectedElement.addClass('on');

					var ulTop = $('ul.conMenu').offset().top;
					var elementTop = selectedElement.offset().top;
					if(elementTop < ulTop) {
						$('ul.conMenu').scrollTop($('ul.conMenu').scrollTop() - (ulTop - elementTop));
					}

					var ulBottom = $('ul.conMenu').offset().top + $('ul.conMenu').outerHeight();
					var elementBottom = selectedElement.offset().top + selectedElement.outerHeight();
					if(elementBottom > ulBottom) {
						$('ul.conMenu').scrollTop($('ul.conMenu').scrollTop() + (elementBottom - ulBottom));
					}
				}
			});

			if (!inView) {
				$('ul.conMenu a').removeClass('on');
			}
		});
	}
}

function main_menu_fixed() {
	$(window).scroll(function() {
		var scrollValue = $(window).scrollTop(),
		basic_h = $("div#headerWrap").outerHeight()-57;
		if (scrollValue > basic_h) {
			$("div.header__container").addClass("fixed");
			$("dl.gnbSearch").addClass("fixed");
			$(".article_paging").addClass("fixed");
			$(".top_btn").addClass("fixed");
		} else {
			$("div.header__container").removeClass("fixed");
			$("dl.gnbSearch").removeClass("fixed");
			$(".article_paging").removeClass("fixed");
			$(".top_btn").removeClass("fixed");
		}
	});
}

    $(document).ready(function(){
        $('.top_btn').on('click', function(){
            $('html, body').animate({scrollTop: '0'}, 500);
        })
    })
    $(window).scroll(function() {
        var scroll = $(window).scrollTop();
        //console.log(scroll);
        if (scroll >= 500) {
            //console.log('a');
            $(".top_btn").addClass("on");
        } else {
            //console.log('a');
            $(".top_btn").removeClass("on");
        }
    });

function menu_fixed() {
	$(window).scroll(function() {
		var scrollValue = $(window).scrollTop(),
		basic_h = $("div#headerWrap").outerHeight() + $("div.titArea").outerHeight() + $("div.articleBrief").outerHeight();
		if (scrollValue > basic_h) {
			$("div.articleMenu").addClass("fixed")
		} else {
			$("div.articleMenu").removeClass("fixed")
		}
	});

	$(window).scroll(function() {
		var menuPosition = ($(window).scrollTop() + $("ul.conMenu").height());
		var containerHeight = ($('#container').height() + 113);
		var bottomLimit = 109 - (menuPosition - containerHeight);

		if(menuPosition > containerHeight) {
			$("ul.conMenu").css("top", bottomLimit + 'px');
		} else if (bottomLimit != 109) {
			$("ul.conMenu").css("top", "");
		}
	});
}

function journal_download(type, sid, filename) {
	var increase_column;

	if(type == "pdf") {
		var openWindow = window.open("about:blank");
		openWindow.location.href = '/upload/pdf/' + filename;

		increase_column = "pdf_down";

	} else if(type == "pdf_a") {
		var openWindow = window.open("about:blank");
		openWindow.location.href = '/upload/article/' + filename;

		increase_column = "pdf_down";

	} else if(type == "epub") {		
		location.href = '/func/download.php?' + filename;		
		increase_column = "epub_down";
	}

	$.ajax({
		type: 'POST',
		data: {table: 'journal_tbl', column: increase_column, count: 1, condition_column: 'sid', condition_value: sid},
		url: '/func/count_increase.php'
	});
}


function openPhotoView(obj,xml_file,img_file) {
	if (window.outerWidth >= 1361) {
		$.ajax({
			type: 'POST',
			data: {xml_file: xml_file,img_file:img_file},
			url: '/journal/image_view_text.php',
			success: function(result) {
				$(".main_image_txt").append(result);
			}
		});

		var y = $(this).scrollTop();
		$("#overlay").css("margin-top", y-250);
		$("#overlay").css("z-index", 101);
		$("#overlay").show();
		thumb_array = new Array();


		console.log(obj);
		$(obj).parent().parent().find('img.article_image').each(function (index) {
			thumb_array.push($(this).attr("src"));
			if($(obj).attr("src") == $(this).attr("src")) {
				thumb_index = index;
				//썸네일 이미지 확대시 원본 이미지를 보여주기 위함.
				img_name = $(this).attr("src");
				

				
				if (img_name.match('thum_')) { //썸네일 이미지일 경우 
					value = img_name.replace(/thum_/,''); //특정문자 제거
					changeMainImage(value);
				}else { //원본 이미지 일 경우
					changeMainImage($(this).attr("src"));
				}


				//changeMainImage($(this).attr("src"));
			}
		});
	}
}


function openPhotoView_MainPage(obj,xml_file,img_file) {


	if (window.outerWidth >= 1361) {

		event.preventDefault();

		$.ajax({
			type: 'POST',
			data: {xml_file: xml_file,img_file:img_file},
			url: '/journal/image_view_text.php',
			success: function(result) {
				$(".main_image_txt").append(result);
			}
		});

		var y = $(this).scrollTop();
		//$("#overlay").css("margin-top", y-250);
		$("#overlay").css("margin-top", y+100);
		$("#overlay").css("z-index", 101);
		$("#overlay").show();
		thumb_array = new Array();


		console.log(obj);
		$(obj).parent().parent().find('img.article_image').each(function (index) {
			thumb_array.push($(this).attr("src"));
			if($(obj).attr("src") == $(this).attr("src")) {
				thumb_index = index;
				//썸네일 이미지 확대시 원본 이미지를 보여주기 위함.
				img_name = $(this).attr("src");
				

				
				if (img_name.match('thum_')) { //썸네일 이미지일 경우 
					value = img_name.replace(/thum_/,''); //특정문자 제거
					changeMainImage(value);
				}else { //원본 이미지 일 경우
					changeMainImage($(this).attr("src"));
				}


				//changeMainImage($(this).attr("src"));
			}
		});

		
	}
}


function openPhotoView_btn(obj,xml_file,img_file) {
	if (window.outerWidth >= 1361) {
		img_file = "/upload/thumbnails/"+img_file;
		changeMainImage(img_file);


		var y = $(this).scrollTop();
		$("#overlay").css("margin-top", y+150);
		$("#overlay").css("z-index", 101);
		$("#overlay").show();
		thumb_array = new Array();
		$(obj).parent().parent().find('img.article_image').each(function (index) {
			thumb_array.push($(this).attr("src"));
			if($(obj).attr("src") == $(this).attr("src")) {
				thumb_index = index;
				console.log(thumb_index);

				img_name = $(this).attr("src");
				if (img_name.match('thum_')) { //썸네일 이미지일 경우 
					value = img_name.replace(/thum_/,''); //특정문자 제거
					changeMainImage(value);
				}else { //원본 이미지 일 경우
					changeMainImage($(this).attr("src"));
				}
				//170302_저널_김마루_변경	
			}
		});
	}
}


function closePhotoView() {
	$('#overlay').hide();
	$(".change_txt").remove();
	thumb_array = new Array();
}

function changeMainImage(src) {

	$("<img />").attr("src", src).load(function() {
		var ratio = 800 / this.width;
		var width = this.width * ratio;
		var height = this.height * ratio;
		if(height > 448) {
			ratio = 448 / this.height;
			width = this.width * ratio;
			height = this.height * ratio;
		}
		$("#overlay #main_image_wrap #main_image").attr('src', this.src);
		$("#overlay #main_image_wrap #main_image").attr('width', width);
		$("#overlay #main_image_wrap #main_image").attr('height', height);
	});
}


$(window).resize(function (){  
	if (window.outerWidth <= 1361) {
		closePhotoView();
	}
})

$(function() {
    // 스크롤 로직을 함수로 분리
    function scrollToTarget(targetHash) {
        var target = $(targetHash);
        if (target.length) {
            var header_height = $('.fixed').height() || 0; // 높이가 정의되지 않은 경우 0으로 처리
            var articleMenu_height = $('.articleMenu').height() || 0; // 높이가 정의되지 않은 경우 0으로 처리
            var offset_top;

            // 목표 위치 계산
            offset_top = target.offset().top - header_height - articleMenu_height;

            // 스크롤 이동
            $('html, body').animate({ scrollTop: offset_top }, 1, function() {
                window.location.hash = targetHash; // URL에 앵커 추가
                
                // 목표 위치에 도달한 후 추가로 300px 아래로 스크롤
                $('html, body').animate({ scrollTop: offset_top - 8}, 1);
            });
        } else {
            console.error("Target element not found:", targetHash); // 오류 로그 출력
        }
    }

    // 페이지 로드 시 앵커로 이동
    if (window.location.hash) {
        scrollToTarget(window.location.hash);
    }

    // 링크 클릭 시 앵커로 이동
    $('a[href*="#"]:not([href="#"])').click(function(event) {
        var thisDomain = window.location.hostname;
        var linkDomain = this.hostname;

        if (thisDomain === linkDomain && this.pathname === window.location.pathname) {
            event.preventDefault(); // 기본 동작 막기
            var targetHash = this.hash;
            scrollToTarget(targetHash); // 함수 호출
        } else {
            // 다른 페이지로 이동 (기본 동작 수행)
            window.location.href = this.href;
        }
    });
});