// 스크립트 전용 mobile 체크 전역 변수 - 단순 window width size로 체크
window.isUserMobile = window.innerWidth <= 768;

util = (function( w, $ ) {
	'use strict';
	var config = {
		pop : null,
		addr_c_name : 'KCCF_C_ADDR',
		location_id : 'location_set',
		location_to_id : 'location_set_to',
		b_cnt_id : 'strong_b_cnt',
		o_cnt_id : 'strong_o_cnt',
		em_b_cnt_id : 'em_b_cnt',
		em_o_cnt_id : 'em_o_cnt',
		cms_host : 'https://cms.nculture.org',
		newcms_host : 'https://ncms.nculture.org',
		url: {
			activityHstAllCnt:'/cmm/cmmActivityHstAllListTotCnt.do',
			recentlyView:'/cmm/regCmmRecentlyView.do'
		}
	};

	function numberWithCommas(x) {
		x = x+'';
		return x.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
	}
	function replaceAll(val, s1, s2){
		var str =  val.replace( /(\s*)/g, "" );
		return str.split(s1).join(s2);
	}
	function setEnterkeyEnabled( e, callback ) {
		if ( e && e.which === 13 ) {
		callback();
		e.preventDefault();
		}
	}
	function getBytes( s, b, i, c ) {
		for ( b = i = 0; c = s.charCodeAt( i++ ); b += c >> 11 ? 3 : c >> 7 ? 2 : 1 );
		return b;
	}
	function getPath( url ) {
		if ( url === null || url === '' ) {
		return '';
		}
		return url.replace( w.location.protocol + '//' + w.location.host, '' );
	}
	function downloadFile(form, atchFileId, fileSn) {
		var varForm = document.forms[form];
		varForm.action = '/cmm/downloadFile.do?atchFileId='+ atchFileId +'&fileSn='+ fileSn;
		varForm.submit();
	}
	function updateFile(obj) {
		$(obj).parent().parent().find("input").eq(0).val('U');
	}
	function clearFile(obj) {
		var fileObj = $(obj).parent().find("input").eq(2);
		fileObj.parent().html(fileObj.parent().html());
	}
	function deleteFile(obj) {
		$(obj).parent().parent().find('div').eq(1).hide();
		$(obj).parent().parent().find('div').eq(0).show();
		$(obj).parent().parent().find('div').eq(0).html($(obj).parent().parent().find('div').eq(0).html());
		$(obj).parent().parent().find('input').eq(0).val('D');
	}
	function undeleteFile(obj) {
		$(obj).parent().parent().find('div').eq(1).show();
		$(obj).parent().parent().find('div').eq(0).hide();
		$(obj).parent().parent().find('input').eq(0).val('R');
	}
	function cmmActivityReload(){
			w.setTimeout(function() {
				getCmmActivityHstAllListTotCnt();
			}, 5500);
	}
	function getCmmActivityHstAllListTotCnt(){
			$.ajax({
				type : 'GET',
				url : config.url.activityHstAllCnt,
				data: {},
				dataType: 'json',
				cache: false,
				error : function(error) {
					$('#'+config.b_cnt_id).html('0');
					$('#'+config.o_cnt_id).html('0');
					$('#'+config.em_b_cnt_id).html('0');
					$('#'+config.em_o_cnt_id).html('0');
				},
				success : function(response) {
					$('#'+config.b_cnt_id).html('0');
					$('#'+config.o_cnt_id).html('0');
					if(response.successYn != 'N'){
						if(response.resultList.length > 0){
							$('#'+config.b_cnt_id).html(response.resultList[0].cnt);
							$('#'+config.o_cnt_id).html(response.resultList[1].cnt);
						}
					}
					$('#'+config.em_b_cnt_id).html($('#'+config.b_cnt_id).html());
					$('#'+config.em_o_cnt_id).html($('#'+config.o_cnt_id).html());
				},
				complete : function() {
				}
			});
	}
	function fnRecentlyViewContent(id, type, url) {
		if ($.active > 0) {
			return false;
		}
		if(type != 'C'){
			return false;
		}
		$.ajax({
			method: 'POST',
			url: config.url.recentlyView,
			data: {cId:id , cType:type , lUrl:url},
			dataType: 'json'
			})
			.done(function( json ) {
				getCmmActivityHstAllListTotCnt();
			});
	}
	function isNull(data){
		var rtnData;
		if(data == null){
			rtnData = "";
		} else{
			rtnData = data;
		}
		return rtnData;
	}
	function snsShare(snsType, url) {
		var url,name,specs,changedUrl,title;
		if(w.document.title.indexOf("|") > -1){
			title = encodeURIComponent($.trim(w.document.title.split("|")[1]));
		}else{
			if(isNull(url) != '' && url.indexOf('/lib/') > -1){ // 넘겨받은 url
				title = encodeURIComponent($.trim('지역문화Pick')); // 지역문화Pick 레이어에서 공유시
			}else{
				title = encodeURIComponent($.trim(w.document.title));
			}
		}
		if(isNull(url) != ''){
			if(url.indexOf(location.host) == -1  && url.indexOf(config.cms_host) == -1 && url.indexOf(config.newcms_host) == -1){
				url = location.protocol + '//' + location.host + url;
			}
			changedUrl = encodeURIComponent(url);
		}else{
			if($('input[name="contentId"]').length > 0){
				changedUrl = encodeURIComponent(w.document.URL.split("?")[0]+'?contentId='+$('input[name="contentId"]').eq(0).val());
			}else{
				changedUrl = encodeURIComponent(w.document.URL.split("?")[0]+'?targetId='+$('input[name="targetId"]').eq(0).val());		
			}
			if(w.document.URL.indexOf('/cul/') > -1){
				if($('input[name="contentType"]').length > 0){
					changedUrl += encodeURIComponent('&contentType='+$('input[name="contentType"]').eq(0).val());
				}
			}
		}
		
		if(snsType == '1'){
			url = 'https://www.facebook.com/sharer/sharer.php?u=' +changedUrl+'&amp;t='+title;
			name = 'facebooksharedialog';
			specs = "menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600";
		}else if(snsType == '2'){
			url = 'https://twitter.com/intent/tweet?text=[%EA%B3%B5%EC%9C%A0]%20' + title+'&url='+changedUrl;
			name = 'twittersharedialog';
			specs = "menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600";
		}else if(snsType == '3'){
			url = 'http://blog.naver.com/openapi/share?url=' +changedUrl+'&amp;title='+title;
			name = 'naversharedialog';
			specs = "menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600";
		}else if(snsType == '4'){
			url = 'https://story.kakao.com/s/share?url='+changedUrl;
			name = 'kakaostorysharedialog';
			specs = "menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=400,width=600";
		}
		if(config.pop != null){
			config.pop.close();
		}
		config.pop = w.open(url, name, specs);
	}
	function setLocation(){
		if($.cookie(config.addr_c_name).indexOf('세종') != -1){
			$('#'+config.location_id).html($.cookie(config.addr_c_name).split('|')[0]);
		}else{
			$('#'+config.location_id).html($.cookie(config.addr_c_name).split('|').join(' '));	
		}
		if($('#'+config.location_to_id).length>0){
			$('#'+config.location_to_id).html($('#'+config.location_id).html());
		}
	}
	function nl2br(str){
		var breakTag = '<br/>';      
		return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');  
	}
	function isWindowSmall() {
		return $(window).width() <= 768;
	}
	function risDownloadFile(aId, mId, formName) {
		if (typeof(formName) == 'undefined') formName = 'risDownloadForm' // [include_cmm_popup.jsp] 파일에 선언
	
		var varForm = document.forms[formName];
		if (typeof(varForm.aId) != 'undefined') {
			varForm.action = '/cmm/risDownloadFile.do';
			varForm.aId.value = aId;
			varForm.mId.value = mId;
		} else {
			varForm.action = '/cmm/risDownloadFile.do?aId='+aId+'&mId'+mId;
		}
				
		if ( $.cookie('KCCF_E_POP') == 'Y' ){
			varForm.submit();
		}else{
			$('#export-data').show();
		}
	
	}
	function assetImgUrl(filePath, amsAssetsPath, amsThumbnailPath) {
		var linkUrl = amsAssetsPath + amsThumbnailPath;
		var stringVal = util.isNull(filePath);
		if(stringVal != ''){
			if (stringVal.indexOf('http') >= 0) {
				linkUrl = '';
			} else if(stringVal.indexOf("/") !== -1){
				linkUrl = amsAssetsPath;
			}
		}
		var imgUrl = linkUrl + filePath;
		return imgUrl;
	}
	function uploadImgUrl(filePath, uploadHomePath) {
		var stringVal = util.isNull(filePath);
		var imgUrl = stringVal;
		if(stringVal != ''){
			if (stringVal.indexOf('/upload') == 0) {
				imgUrl = uploadHomePath + stringVal;
			}
		}
		return imgUrl;
	}
	function docView(mId, sd) {
		var url = '/npdfView.do?npdf='+mId;
		if (typeof(sd) != 'undefined') url += '&sd='+sd;
		var newWindow = window.open('about:blank');
		newWindow.location.href=url;
	}
	function xssCheck(str) {
        var rtnData;
		if(str == null){
			rtnData = "";
		} else{
			str = str.replace(/\&/g, "&amp;"); 
			str = str.replace(/\//g, "&#x2F;");
			str = str.replace(/</g, "&lt;").replace(/>/g, "&gt;");
	        str = str.replace(/'/g, "&#039;").replace(/"/g, "&quot;");
	        str = str.replace(/\(/g, "&#040;").replace(/\)/g, "&#041;");
	        rtnData = str;
		}
		return rtnData;
	}
	return {
		config:config,
		numberWithCommas:numberWithCommas,
		replaceAll: replaceAll,
		getBytes: getBytes,
		setEnterkeyEnabled: setEnterkeyEnabled,
		getPath: getPath,
		downloadFile:downloadFile,
		updateFile:updateFile,
		clearFile:clearFile,
		deleteFile:deleteFile,
		undeleteFile:undeleteFile,
		cmmActivityReload:cmmActivityReload,
		getCmmActivityHstAllListTotCnt:getCmmActivityHstAllListTotCnt,
		fnRecentlyViewContent:fnRecentlyViewContent,
		isNull:isNull,
		snsShare:snsShare,
		setLocation:setLocation,
		nl2br:nl2br,
		isWindowSmall:isWindowSmall,
		risDownloadFile:risDownloadFile,
		assetImgUrl:assetImgUrl,
		uploadImgUrl:uploadImgUrl,
		docView:docView,
		xssCheck:xssCheck
	};
})( window, jQuery );