

/**
*  Description: 사용설정저장
**/
var numAcUsed = 1; //자동완성:0(미사용),1(사용)
var numAcDrt = 0; //앞(0)/뒤(1) 단어 맞춤
var debugMode = false;

function initAcOption(){
    if(numAcUsed!=1) tac.close();
    numAcUsed = parseInt(checkNull(getSrchCookie("acUsed"), 1, "1"));
    numAcDrt = parseInt(checkNull(getSrchCookie("acDrt"), 1, "0"));
}

function setAcUsed(numType){
    setSrchCookie("acUsed", numType, "100"); //쿠키저장
    numAcUsed = parseInt(numType);
    initAcOption();
}

function setAcDrt(numType){
    setSrchCookie("acDrt", numType, "100"); //쿠키저장
    numAcDrt = parseInt(numType);
    tac.get($F('qt'));
    initAcOption();
}


var thumbModelArray=new Array();
var thumbImgArray=new Array();
var thumbUrlArray=new Array();
var acListLength =0;
function subBuildAcWordListJson(_classBank, xml){

    parentEl = _classBank.wordListWrap;
    query = _classBank.qt.value.toLowerCase();
    var len = acListLength = xml.xpersearch.length;
    //len = acListLength =(window.netscape) ? len +1 : len ;

    var returnValue="";
    while(parentEl.childNodes.length > 0) {
            parentEl.removeChild(parentEl.childNodes[0]);
    }

    if(len > 0){

        //자동완성 화면출력

        var mistakeQuery = "";

        if(query.charCodeAt(0) < 127) mistakeQuery = assembleHangul(query);

        var ulEl = document.createElement("UL");
            ulEl.style.padding = "5px 0px 5px 0px";
            ulEl.style.height="120px";
        for(i = 0; i < len; i++){
            value = xml.xpersearch[i].TT;


            value = value.toLowerCase().replace(/^\s*/, "").replace(/\s*$/, "");
            var tempValue = replaceRegExp(value, query, _classBank.G_AC_START_HL_TAG + query + _classBank.G_AC_END_HL_TAG);
            if(mistakeQuery.length > 0) tempValue = replaceRegExp(tempValue, mistakeQuery, _classBank.G_AC_START_HL_TAG + mistakeQuery + _classBank.G_AC_END_HL_TAG);

            var liEl = document.createElement("LI");
                liEl.style.color = "#494a4d";
                liEl.style.fontWeight = "bold";
                liEl.style.height = "20px";
                liEl.style.margin = "0 0 0px 0";
                liEl.instance = _classBank;
                liEl.onmouseover = function(){this.instance.setAcWordFocus(this, 1);}

                liEl.onmouseout = function(){this.instance.setAcWordFocus(this, 0);}

            var aEl = document.createElement("A");
                aEl.href = "#";
                aEl.innerHTML	= "<span style='font-size:"+_classBank.Auto.fontSize+"'>"+tempValue+"</span>";
                aEl.instance = _classBank;
                 aEl.onclick = function(e){ this.instance.qt.value = this.innerHTML.stripTags().strip();this.instance.displayoutLayer2();};
                aEl.style.fontSize = _classBank.Auto.fontSize;
                aEl.style.letterSpacing = "-1px";
                liEl.appendChild(aEl);
                liEl.appendChild(document.createElement("BR"));
                ulEl.appendChild(liEl);
        }
        if(140 > i*20){
            _classBank.Auto.listSize = i;
        }
        else{
          _classBank.Auto.listSize = 5;
          _classBank.acListPos = -1;
        }
        ulEl.style.height= _classBank.Auto.listSize * 20 -10 +"px";
        parentEl.style.height = _classBank.Auto.listSize * 20 + 10 +"px";
        parentEl.appendChild(ulEl);
    }else{
        tac.close();
    }
    return returnValue;
}



var TAutoComplete2 = Class.create();
var classBankSub = null;
TAutoComplete2.prototype = {
    initialize: function(initVar,tSearch)
    {
        this.G_AC_WORD_LENGTH = 40;
        this.G_AC_START_HL_TAG = "<font color=#b85c2e>";
        this.G_AC_END_HL_TAG = "</font>";
        this.G_AC_BG_COLOR = "#EFEFEF";
        this.xmlSrc = '/sch/autoJson.do';

         this.lastQuery = "";
         this.obj = new String();
         this.Auto = initVar;

         //자동완성관련정보 초기화
         this.searchInstance = tSearch;
         this.observeAutoArea=0; // 자동완성검색어 레이어 display 상태 값을 저장
         this.observeimgArea =0;
         this.observeAutoOnOff = 1;

         this.scrollTop = 0;
         this.acListPos = 0;
         this.scrollTopId = 0;
         this.scrollBottomId = this.Auto.listSize-1;
         this.height = this.Auto.listSize * 20;

         if(!this.createAutoWap())  //리스트가 뿌려질 div 생성
             if(debugMode){
                 alert("list div Wrap create fail");
             }

        if(!this.initQt())  //검색어 input 개체 세팅
            if(debugMode){
                alert("search input setting fail");
            }
        this.displayoutLayer();

    },
    createObj: function(_objNm){
        try{
            var tmpObj = document.getElementById(_objNm);
            if(!tmpObj)
                if(debugMode){
                    alert(_objNm+" id를 가진 개체가 없습니다. 다시 확인 해 주세요");
                }

            return tmpObj
        }catch(e){
            if(debugMode){
                alert(e);
            }
            return false;
        }
    },
    createAutoWap: function(){

        try{
            //자동완성 리스트 레이어 생성
            this.autoCompleteWrap = document.createElement("DIV");
            this.autoCompleteWrap.style.display = "none";
            this.autoCompleteWrap.style.position = "absolute";
            this.autoCompleteWrap.style.top = "-15px";
            this.autoCompleteWrap.style.left = "257px";
            this.autoCompleteWrap.style.zIndex = "99999";
            this.autoCompleteWrap.style.width = "300px";
            this.autoCompleteWrap.style.background = "#ffffff no-repeat 0 100%";
            this.autoCompleteWrap.style.padding = "0 0 0px 0";
            this.autoCompleteWrap.style.margin = "-2px 0 0 -3px";
            this.autoCompleteWrap.style.border = "0px solid #D0D0D0";
            this.autoCompleteWrap.style.borderTop = "none";



            this.wordListWrap = document.createElement("DIV");
            this.wordListWrap.style.background = "#fff url('global/bg_search_05.gif') no-repeat 0 0"
            this.wordListWrap.style.borderBottom = "1px solid #f2f2f2";
            this.wordListWrap.style.padding = "0px 0 0 8px";
            this.wordListWrap.style.overflowY = "auto";
            this.wordListWrap.style.lineHeight = "18px";
            this.wordListWrap.style.height = (this.height+10)+"px";
            this.wordListWrap.style.overflowX = "hidden";
            this.wordListWrap.style.border = "1px solid #D0D0D0";
            this.wordListWrap.style.scrollbarFaceColor = "#FFFFFF";
            this.wordListWrap.style.scrollbar3dLightColor = "#C6C6C6";
            this.wordListWrap.style.scrollbarHighlightColor = "#C6C6C6";
            this.wordListWrap.style.scrollbarShadowColor = "#C6C6C6";
            this.wordListWrap.style.scrollbarDarkShadowColor = "#FFFFFF";
            this.wordListWrap.style.scrollbarTrackColor = "#F5F3F3";
            this.wordListWrap.style.scrollbarArrowColor = "#C6C6C6";


            this.autoCompleteWrap.appendChild(this.wordListWrap);

            var parent = document.getElementsByTagName("BODY")[0];
            parent.insertBefore(this.autoCompleteWrap, parent.firstChild);

            this.wordListWrap.style.display = "none";
            this.autoCompleteWrap.style.display = "none";
            this.autoCompleteWrap.instance = this;
            this.autoCompleteWrap.onmouseover =function(){this.instance.observeAutoArea=1};
            this.autoCompleteWrap.onmouseout = function(){this.instance.observeAutoArea=0};

            return true;
        }catch(e){
            if(debugMode){
                alert(e);
            }
            return false;

        }

    },
    initQt: function(){ //검색어 입력 input 개체 관련 세팅
        try{
             this.qt = this.createObj(this.Auto.autoKeywordInputId); // 검색어 Input
             this.qt.AutoComplete = "off";
             this.qt.autoInstance = this;

            if(this.qt.onkeydown){
                addEvent(this.qt, 'keydown', function(e){this.autoInstance.open(e , 1)});
            }else{
                this.qt.onkeydown =  function(e){this.autoInstance.open(e , 1)};
            }
             this.qt.onkeyup = function(e){this.autoInstance.open(e , 0)};
             this.qt.onblur = function(e){this.autoInstance.displayoutLayer()};
             this.qt.onfocus = function(e){this.autoInstance.qt.value="";/*this.style.backgroundImage="url('')";*/this.autoInstance.clearVal()};

             return true;

        }catch(e){
            if(debugMode){
                alert(e);
            }
            return false;
        }
    },
    get: function()
    {
        value = this.qt.value.replace(/^\s*/, "").replace(/\s*$/, "");
        if(value == "" || this.observeAutoOnOff == 0){
             this.displayoutLayer();
            return;
        }

        if(this.lastQuery==value) return;

        if(value==""){
            this.lastQuery = value;
            this.wordListWrap.style.display = "none";
            this.autoCompleteWrap.style.display = "none";
            return;
        }

        this.lastQuery = value;
        var url = this.xmlSrc;
        var pars = 'query=' + disassembleHangul(value, numAcDrt) + "&sort=0&colName=AUTOCOMPLETE&pg=1&lc=10";

        this.autoCompleteWrap.style.width = eval(this.qt.offsetWidth + 3)+'px';
        this.autoCompleteWrap.style.left = eval(getRealOffsetLeft(this.qt))+'px';
        this.autoCompleteWrap.style.top = eval(getRealOffsetTop(this.qt) + 20)+'px';

        classBankSub = this;
        createXMLHttpRequest();

           xmlHttp.onreadystatechange = function(){

                    if(xmlHttp.readyState == 4) {
                    if(xmlHttp.status == 200 || xmlHttp.status == 0) {

                xml =  eval('(' + xmlHttp.responseText + ')');
                     if( xmlHttp.responseText && xml.xpersearch.length > 0){
                        subBuildAcWordListJson(classBankSub,xml);
                        classBankSub.wordListWrap.style.display = "";
                        classBankSub.autoCompleteWrap.style.display = "";
                    }else{
                        classBankSub.wordListWrap.style.display = "none";
                        classBankSub.autoCompleteWrap.style.display = "none";
                    }
                }else{
                    if(debugMode){
                        alert(xmlHttp.status );
                    }
                }
            }
            //classBankSub = null;
            };

           xmlHttp.open("post", url, true);
           xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
           xmlHttp.setRequestHeader("Accept","text/xml");
           xmlHttp.send(pars);

        initAcOption();
    },

    open: function(e, arg)
    {

        var key = window.ActiveXObject ? window.event.keyCode : e.which;

        switch (key) {
            case 13:
                 this.displayoutLayer();
            break;
            case 27:
                this.close();
                break;
            case 38:
            case 37:
                if(acListLength > 0 && parseInt(arg) == 1) this.setPosAcword(-1,this.wordListWrap);
                break;
            case 40:
            case 39:

                if(acListLength > 0 && parseInt(arg) == 1) this.setPosAcword(1,this.wordListWrap);
                break;
            default:
                if( parseInt(arg) == 0 && key != 229){
                    this.get();

                    }
            break;


        }

    },
    displayoutLayer: function(){

        chkBlur = 1;

        if((this.wordListWrap && this.observeAutoArea == "0" && this.observeimgArea == "0"))
        {
            this.wordListWrap.style.display = "none";
            this.autoCompleteWrap.style.display = "none";
            //offThumbImg();
        }
    },
    displayoutLayer2 : function (){

            //offThumbImg();
            this.wordListWrap.style.display = "none";
            this.autoCompleteWrap.style.display = "none";


    },
    clearVal: function(){
        var inputQr =this.qt;
        if(inputQr.value=="Enter a Keyword" || inputQr.value=="Enter a Keyword" ){
            inputQr.value="";

        }
    },
    chkAutoOnOff : function (){
        if(this.obj.checkAutoObj.checked == true){
            this.observeAutoOnOff = 1;
            setSrchCookie("autoOnOff", this.observeAutoOnOff, 1);
            layerOnOff(this.observeAutoOnOff);
            this.displayoutLayer2();
            this.open("","");

        }else{
            this.observeAutoOnOff = 0;
            setSrchCookie("autoOnOff", othis.observeAutoOnOff, 1);

            this.displayoutLayer2();
            layerOnOff(this.observeAutoOnOff);
            this.open("", "");

        }

    },
    chkAutoOpen: function(){

          this.observeAutoOnOff = getSrchCookie("autoOnOff");
          this.autoCompleteWrap.style.display="";

           layerOnOff(this.observeAutoOnOff);
           this.open("","");
    },
    setPosAcword: function(value, tmpListSection){

        try{
            var acListPosLast = this.acListPos;
            this.acListPos += parseInt(value);

            if(this.acListPos < 0){
                this.acListPos = 0;
                //tac.close();
            }else{

                if(this.acListPos >= acListLength - 1) this.acListPos = acListLength - 1;
                this.qt.value = tmpListSection.childNodes[0].childNodes[this.acListPos].childNodes[0].innerHTML.stripTags().strip();
                if(acListPosLast >= 0) this.setAcWordFocus(tmpListSection.childNodes[0].childNodes[acListPosLast],  0 , "");
                this.setAcWordFocus(tmpListSection.childNodes[0].childNodes[this.acListPos], 1, this.acListPos);
                if(this.autoCompleteWrap.style.display == "none"){
                      this.wordListWrap.style.display = "";
                      this.autoCompleteWrap.style.display = "";

                      }

                if(value > 0 ){

                    if(this.acListPos > this.scrollBottomId){

                        this.scrollTopId = this.scrollTopId + 1;
                        this.scrollBottomId = this.scrollBottomId + 1;

                        this.wordListWrap.scrollTop = this.acListPos * 20 - (this.height - 20);
                        //alert("top:"+(acListPos * 20 - 120));

                    }
                }else{

                    if(this.acListPos < this.scrollTopId){
                        this.scrollTopId = this.scrollTopId - 1;
                        this.scrollBottomId = this.scrollBottomId - 1;
                        this.wordListWrap.scrollTop = this.wordListWrap.scrollTop - 20;
                    }
                }

            }
        }catch(e){
            if(debugMode){
                alert(e);
            }
        }
    },
    close: function(){
        if(this.listSection.style.display == "" ){
            this.wordListWrap.style.display = "none";
        }
    },
    setAcWordFocus: function(_li,numType, i){
        _li.style.backgroundColor = (parseInt(numType) > 0)? this.G_AC_BG_COLOR : "";
    }

};






function chkAutoOpen(_obj,_autoKeywordDisplayId,_autoKeywordParentDisplayId, _tac){

       observeAutoOnOff = getSrchCookie("autoOnOff");
      document.getElementById(_autoKeywordParentDisplayId).style.display="";

       layerOnOff(observeAutoOnOff);
       _tac.open("","");
}

function layerOnOff(_observeAutoOnOff){

//    if(_observeAutoOnOff == 1){
//        //document.getElementById(autoOn).style.display = "";
//        //document.getElementById(autoOff).style.display = "none";
//       // document.getElementById(checkAuto).style.display = "";
//        document.getElementById(checkAutoOn).style.display = "none";
//        document.getElementById(checkAuto).checked = true;
//        document.getElementById(checkAutoOn).checked = false;
//    }else{
//        //document.getElementById(autoOn).style.display = "none";
//        //document.getElementById(autoOff).style.display = "";
//       // document.getElementById(checkAuto).style.display = "none";
//
//        document.getElementById(checkAutoOn).style.display = "";
//        document.getElementById(checkAuto).checked = false;
//        document.getElementById(checkAutoOn).checked = false;
//    }
}


/***************************************************************************************/
/*	검색어 자동완성 Ver 1.0(Pure javascript)
/*	제 작 일 : 2008/11/27
/*	제	 작: 연구2팀 최제홍
/*	Copyright ⓒ 2008. EXPERNET AND 최제홍. All rights Reserved
/***************************************************************************************/
{
     var qt2 = new String();

      qt2.fontSize="12px"; // 자동완성 폰트크기
      qt2.listSize="5";
      qt2.autoKeywordInputId = "topquery"; // 검색박스 id

      qt2.thumbDiv = "thumbDiv";
      qt2.checkAuto = "checkAuto"; //자동완성 보기기능 체크박스
      qt2.checkAutoOn = "checkAutoOn"; //자동완성 보기기능 체크박스

      qt2.autoOpen = "autoOpen"; // 열기버튼
      qt2.autoOn = "autoOn"; // 열기버튼
      qt2.autoOff = "autoOff"; // 열기버튼
}





var autoInit2 =  function(){
         var tSearch = null;
          new TAutoComplete2(qt2, tSearch);
}
addEvent(window, 'load', autoInit2);

