



$(function() {


	//로그아웃
	$('#btn-logout').on('click', function(e) {
		$.ajax({
			url: '/api/logincenter/logout',
			type: 'GET',
			dataType: 'JSON',
			success: data => {
				if(data.result){
					document.location = "/";	
				}				
			}
		});
	});
	
	
	
    $('.inputAutocompleteSubmit').autocomplete({
            
        source: function (request, response) {
            $.ajax({
                url: "/api/common/getKeywords",
                type: "POST",
                dataType: "json",
                data: { keyword: request.term },
                success: function (data) {
                    response(
                        $.map(data.data, function (item) {
                            return {
                                label: item.word,
                                value: item.word,
                                idx: item.word,
                            }
                        })
                    )
                }
            })
        },
            
        focus: function (event, ui) {
          return false;
        },
        select: function (event, ui) {
            $(this).val(ui.item.label);            
            $(this).closest('form').submit();
        },
        minLength: 1,
        delay: 100,
        autoFocus: false,  //true : 자동으로 첫번째 포커스로 이동
    });

	
	
	

	$.summernoteImageUpdate = function(apiUrl, files, editor) {
        var formData = new FormData();
        var fileArr = Array.prototype.slice.call(files);
        fileArr.forEach(function(f){
            if(f.type.match("image/jpg") || f.type.match("image/jpeg" || f.type.match("image/jpeg"))){
                alert("JPG, JPEG, PNG 확장자만 업로드 가능합니다.");
                return;
            }
        });
        for(var xx=0;xx<files.length;xx++){
            formData.append("file[]", files[xx]);
        }

        $.ajax({
            url : apiUrl,
            data: formData,
            cache: false,
            contentType: false,
            processData: false,
            enctype	: 'multipart/form-data',
            type: 'POST',
            success : function(result) {

                //항상 업로드된 파일의 url이 있어야 한다.
                if(result === -1){
                    alert('이미지 파일이 아닙니다.');
                    return;
                }
                var data = JSON.parse(result);
                for(x=0;x<data.length;x++){
                    var img = $("<img>").attr({src: data[x], width: "100%"});
                    $(editor).summernote('pasteHTML', "<img src='"+data[x]+"' style='max-width:100%;' />");
                }
            }
        });
    }


});





/*
  summerNote Default Option
 */
	var summernoteDefaultOption ={
        tabsize: 2,
		fontname: 'nanumsquare_neo',
        height: 500,
		lang: 'ko-KR',
        toolbar: [
            ['style', ['style', 'bold','italic', 'underline', 'clear']],
            ['fontsize', ['fontname', 'fontsize', 'fontsizeunit']],            
            ['height', ['height']],
            ['font', ['strikethrough', 'superscript', 'subscript']],
            ['color', ['forecolor', 'color', 'backcolor']],
            ['para', ['ul', 'ol', 'paragraph']],
            ['table', ['table']],
            ['insert', ['link', 'picture', 'video']],
            ['hr', ['hr']],
            ['view', ['fullscreen', 'codeview', 'undo','redo', 'help']]
        ],
        fontNames: ['nanumsquare_neo', 'Arial', 'Arial Black', 'Comic Sans MS', 'Courier New','맑은 고딕','궁서','굴림체','굴림','돋음체','바탕체'],
		fontSizes: ['8','9','10','11','12','13','14','15','16','17','18','19','20','24','30','36','48','64','82','150'],
        fontSizeUnits: ['px', 'pt'],
		//lineHeights: ['0.2', '0.3', '0.4', '0.5', '0.6', '0.8', '1.0', '1.2', '1.4', '1.5', '2.0', '3.0'],
		tableClassName: 'table-summernote table-bordered-summernote',     
        popover: {
          image: [
            ['image', ['resizeFull', 'resizeHalf', 'resizeQuarter', 'resizeNone']],
            ['float', ['floatLeft', 'floatRight', 'floatNone']],
            ['remove', ['removeMedia']]
          ],
          link: [
            ['link', ['linkDialogShow', 'unlink']]
          ],
 		  table: [
            ['add', ['addRowDown', 'addRowUp', 'addColLeft', 'addColRight', 'toggle']],
            ['delete', ['deleteRow', 'deleteCol', 'deleteTable']],
            /*['custom', ['tableStyles']]*/
          ],
          air: [
            ['color', ['color']],
            ['font', ['bold', 'underline', 'clear']],
            ['para', ['ul', 'paragraph']],
            ['table', ['table']],
            ['insert', ['link', 'picture']]
          ]
        }
    };


	function summernoteReplace(content){
    
    	//한글 파일 태그 제거
    	if( content.indexOf('<div id="hwpEditorBoardContent"') != -1 ){
        	var targetElementStartIndex = content.indexOf('<div id="hwpEditorBoardContent"');
            var targetElementEndIndex = -1;
            if( targetElementStartIndex != -1) {
            	targetElementEndIndex = content.indexOf('</div>',targetElementStartIndex);
                        	
            	if( targetElementEndIndex != -1 ){
            		var contentS = content.slice(0, targetElementStartIndex);
            		var contentE = content.slice(targetElementEndIndex+6, content.length);
            		content = contentS+contentE;
            	}
            	        	
            }
        }
        return content;
	}


