

/**
*  Description: »ç¿ë¼³Á¤ÀúÀå
**/
var numAcUsed = 1; //ÀÚµ¿¿Ï¼º:0(¹Ì»ç¿ë),1(»ç¿ë)
var numAcDrt = 0; //¾Õ(0)/µÚ(1) ´Ü¾î ¸ÂÃã

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 buildAcWordListJson(_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 12px 5px 12px";
			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);






		}
		parentEl.appendChild(ulEl);
		_classBank.acListPos = -1;

	}else{
		tac.close();
	}
	return returnValue;
}



var TAutoComplete = Class.create();
var classBank = null;
TAutoComplete.prototype = {
	initialize: function(initVar)
	{
		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 = '/search/autoJson.jsp';

		 this.lastQuery = "";
		 this.obj = new String();
		 this.Auto = initVar;

		 //ÀÚµ¿¿Ï¼º°ü·ÃÁ¤º¸ ÃÊ±âÈ­

		 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 »ý¼º
		 		alert("list div Wrap create fail");


		if(!this.initQt())  //°Ë»ö¾î input °³Ã¼ ¼¼ÆÃ
		 		alert("search input setting fail");

		this.displayoutLayer();
















		 //this.obj.thumbDiv = this.createObj(this.Auto.thumbDiv); // °Ë»ö¾î display
		 //this.obj.checkAutoObj = this.createObj(this.Auto.checkAuto); // °Ë»ö¾î display
		 //this.obj.checkAutoOnObj = this.createObj(this.Auto.checkAutoOn); // °Ë»ö¾î display


//		if(getSrchCookie("autoOnOff") != "" && getSrchCookie("autoOnOff") != null){
//	     this.observeAutoOnOff = getSrchCookie("autoOnOff");
//	    }
	    //layerOnOff(this.observeAutoOnOff);

		//addEvent(tac.obj.thumbDiv, 'mouseover', function(e){tac.observeimgArea=1; });
		//addEvent(tac.obj.thumbDiv, 'mouseout', function(e){tac.observeimgArea=0;});
		//addEvent(tac.obj.checkAutoObj, 'click', function(e){tac.chkAutoOnOff();});
		//addEvent(tac.obj.checkAutoOnObj, 'click', function(e){tac.chkAutoOnOff();});
		//ÀÌ¹ÌÁö¹× ²ô±â ÄÑ±â±â


		//initAcOption();



	},
	createObj: function(_objNm){
		try{
			var tmpObj = document.getElementById(_objNm);
			if(!tmpObj)
				alert(_objNm+" id¸¦ °¡Áø °³Ã¼°¡ ¾ø½À´Ï´Ù. ´Ù½Ã È®ÀÎ ÇØ ÁÖ¼¼¿ä");

			return tmpObj
		}catch(e){
			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 0 0";
			this.autoCompleteWrap.style.margin = "-2px 0 0 0px";
			this.autoCompleteWrap.style.border = "0px solid #D0D0D0";
			this.autoCompleteWrap.style.borderTop = "none";



			this.wordListWrap = document.createElement("DIV");
			this.wordListWrap.style.background = "#ffffff no-repeat 0 0"
			this.wordListWrap.style.borderBottom = "1px solid #f2f2f2";
			this.wordListWrap.style.padding = "1px";
			this.wordListWrap.style.overflowY = "auto";
			this.wordListWrap.style.lineHeight = "18px";
			this.wordListWrap.style.height = (this.height+4)+"px";
			this.wordListWrap.style.padding = "1px";
			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){
			alert(e);
			return false;

		}

	},
	initQt: function(){ //°Ë»ö¾î ÀÔ·Â input °³Ã¼ °ü·Ã ¼¼ÆÃ
		try{
			 this.qt = this.createObj(this.Auto.autoKeywordInputId); // °Ë»ö¾î Input
			 this.qt.AutoComplete = "off";
			 this.qt.autoIntance = this;

			if(this.qt.onkeydown){
				addEvent(this.qt, 'keydown', function(e){this.autoIntance.open(e , 1)});
			}else{
				this.qt.onkeydown =  function(e){this.autoIntance.open(e , 1)};
			}
			 this.qt.onkeyup = function(e){this.autoIntance.open(e , 0)};
			 this.qt.onblur = function(e){this.autoIntance.displayoutLayer()};
			 this.qt.onfocus = function(e){this.autoIntance.clearVal()};

			 return true;

		}catch(e){
			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=' + disassembleHangul2(value, numAcDrt) + "&sort=0&colName=AUTOCOMPLETE&pg=1&lc=10";


		if(this.Auto.autoKeywordInputId == "query"){
			this.autoCompleteWrap.style.width = this.qt.offsetWidth + 150;
		}else{
			this.autoCompleteWrap.style.width = this.qt.offsetWidth;
		}

		this.autoCompleteWrap.style.left = getRealOffsetLeft(this.qt);
		this.autoCompleteWrap.style.top = getRealOffsetTop(this.qt) + 20;



		classBank = 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){
						buildAcWordListJson(classBank,xml);
					    classBank.wordListWrap.style.display = "";
						classBank.autoCompleteWrap.style.display = "";
					}else{
						classBank.wordListWrap.style.display = "none";
						classBank.autoCompleteWrap.style.display = "none";
					}
				}else{
					alert(xmlHttp.status );
				}
			}
			//classBank = 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){
			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 qt1 = new String();

 	 qt1.fontSize="12px"; // ÀÚµ¿¿Ï¼º ÆùÆ®Å©±â
 	 qt1.listSize="7";
 	 qt1.autoKeywordInputId = "query"; // °Ë»ö¹Ú½º id

 	 qt1.thumbDiv = "thumbDiv";
 	 qt1.checkAuto = "checkAuto"; //ÀÚµ¿¿Ï¼º º¸±â±â´É Ã¼Å©¹Ú½º
 	 qt1.checkAutoOn = "checkAutoOn"; //ÀÚµ¿¿Ï¼º º¸±â±â´É Ã¼Å©¹Ú½º

 	 qt1.autoOpen = "autoOpen"; // ¿­±â¹öÆ°
 	 qt1.autoOn = "autoOn"; // ¿­±â¹öÆ°
 	 qt1.autoOff = "autoOff"; // ¿­±â¹öÆ°
}





var autoInit =  function(){
		 var objtoId = document.all.query;
         objtoId.id = "query";
		 objtoId.Autocomplete = "off";
	 	new TAutoComplete(qt1);
}
addEvent(window, 'load', autoInit);

