 var nav_type=get_nav();
 var brw_type=get_browser();
 var dom_type=chk_rt(nav_type);

 //var search_box=document.getElementById("TopPage1_txtSearchBox") ;
 //var search_option=document.getElementById("qdomain");
 
 var search_box;
 var search_option;

 var m_on=0,m_now=0,s_now=0,shl=0,a_now=0,a_on=0,arr_on=0,frm_on=0 ;
 var cn_use="use_ac" ;
 var bkImg = 1;
 var wi_len = 2;
 var wi_int=500 ;
 var max_row=4;
 var B="block",I="inline",N="none",UD="undefined" ;
 var bak="",old="" ;

 var qs_ac_list="",qs_ac_id="",qs_q="",qs_m=0,qs_ac_len=0 ;
// bak=old=search_box.value ;

 var acuse=1;	//ÀÚµ¿¿Ï¼º»ç¿ë¿©ºÎ
 var cc= new Object() ;
 var ac_layer_visibility=0;

 var goGoodsNo;


 function get_nav() {
	var ver=navigator.appVersion ;
	
	 if (navigator.appName.indexOf("Microsoft")!=-1 && ver.indexOf("MSIE 4")==-1 && ver.indexOf("MSIE 3")==-1) {
		return 1;
	 } else if (navigator.appName.indexOf("Netscape")!=-1) return 2;
			else return 0;
 }
 
 function get_browser(){
	if($.browser.msie )	return "IE";
	else if($.browser.safari )	return "SA";
	else if($.browser.mozilla) return "FF";
	else	return "";
	
 }
 
 function chk_rt(nav_type) {
	try { 
		var tmp=document.getElementById("TopPage1_txtSearchBox");
	} catch (e) { 
		return 0;
	} 
	
	return 1;
 } 

 

//±â´É²ô±â ¹öÆ°À» ´­·¶À»¶§
 function ac_off() 
 {	
	if ($("#TopPage1_txtSearchBox").val() == "") {
		popup_ac(0);
		//document.all.noquery_ac_body.style.display = "none";
	}else{
		ac_hide();
//		search_box.autocomplete = "off" ;
	}
	acuse = 0;
 }

//±â´ÉÄÑ±â ¹öÆ°À» ´­·¶À»¶§
 function ac_on()
 {
	acuse = 1;
	popup_ac(1);
	
	if ($("#TopPage1_txtSearchBox").val()  != "") 
		wd();
		
	setTimeout("wi()", wi_int);
	$("TopPage1_txtSearchBox").focus();
	//ÆûµéÀ» °¨Ãá´Ù.
	hide_behind_forms();
}


	  //type=0 : ¸ðµÎ °¨ÃáÁØ´Ù.
	 //type=1 : °Ë»ö¾î°¡ ÀÖÀ»¶§ ÀÚµ¿¿Ï¼ºÃ¢ º¸ÀÌ±â
	 //type=2 : ±â´ÉÀÌ ²¨Á®ÀÖÀ»¶§ ÀÚµ¿¿Ï¼ºÃ¢ º¸ÀÌ±â
	 //type=3 : '°Ë»ö¾î¸¦ ÀÔ·ÂÇØ´Þ¶ó'´Â ÀÚµ¿¿Ï¼ºÃ¢ º¸ÀÌ±â
	 function popup_ac(type){
		
		if(type==0){
			$("#ac_body").css("display", "none");
			$("#off_ac_body").css("display", "none");
			$("#noquery_ac_body").css("display", "none");
			
			//°Ë»öÃ¢³» ¼¼¸ð ÀÌ¹ÌÁöº¯°æ
			switch_image(0);
		}else if(type==1){
			$("#ac_body").css("display", "block");
			$("#off_ac_body").css("display", "none");
			$("#noquery_ac_body").css("display", "none");
			
			switch_image(1);
		}else if(type==2){
			$("#ac_body").css("display", "none");
			$("#off_ac_body").css("display", "block");
			$("#noquery_ac_body").css("display", "none");
			
			switch_image(1);
		}else if(type==3){
			$("#ac_body").css("display", "none");
			$("#off_ac_body").css("display", "none");
			$("#noquery_ac_body")[0].style.display= "block";
			
			switch_image(1);
		}
	 }
	 
		
//ÀÎÇ²¹Ú½ºÀÇ ¼¼¸ð ¹öÆ°À» ´­·¶À»¶§ ÀÚµ¿¿Ï¼ºÃ¢À» º¸¿©ÁØ´Ù.
 function show_ac()
 {	
	if (acuse == 0) {
		 if($("#ac_body")[0].style.display == "block") 
			popup_ac(0);	
		 else 
			popup_ac(2);
	}
	else
	{
		if ($("#TopPage1_txtSearchBox").val() == "") 
		{
			if($("#noquery_ac_body")[0].style.display == "block") 
				popup_ac(0);	
			else 
				popup_ac(3);
		}
		else 
		{
			//wd();
			//alert(document.all.TopPage1_txtSearchBox.value);
			req_ipc();
		}
	}
 }
 
 
	function wd() { 
		
		search_box.onclick = req_ipc ;
		document.body.onclick = dis_p ;
		
	}
	
	 var dnc=0;
	 function req_ipc() { 
		dnc=1;
		frm_on=0;
		req_ac2(1) ;
	 }

	 function dis_p() {
		 //alert("dnc:"+dnc+" arr_on:"+arr_on+" frm_on:"+frm_on);
		 
		 if (dnc) { 
			 dnc=0;
			return ;
		} 
		
		if (arr_on) {
//			mp_layer() ;
			return ;
		}
		if (frm_on) { 
			return ;
		} 
		alw=0 ;
		ac_hide() ;
		//alert(88);
		
	 } 

	 function req_ac2(me) { 	 
		 if (search_box.value == "" || acuse==0 ) return ;
		 		
		 if (a_on && dnc) { 
			ac_hide() ;
			return ;
		} 
		var o = get_cc(me) ;
		
		 if (o && o[1][0] != "" ){ac_show(o[0], o[1], o[2], me) ;}
		 else reqAC(me) ;
	 } 

	 var _req = null;
	 function get_req() {
		 if(_req && _req.readyState!=0) { 
			 _req.abort() ;
		 } 
		 try {
			 _req = new ActiveXObject("Msxml2.XMLHTTP") ;
		 } catch (e) { 
			 try { 
				 _req = new ActiveXObject("Microsoft.XMLHTTP") ;
			 } catch (e) { 
				 _req = false ;
			 } 
		}
		if (!_req && typeof XMLHttpRequest!=UD) _req = new XMLHttpRequest() ;
		return _req ;
	 } 

	 function showAC() {
	 
		 if (_req.readyState==4 && _req.status==200 ) { //sang
			 
			 eval(_req.responseText);
			 set_cc(qs_q, qs_ac_list, qs_ac_id, qs_m) ;
			 ac_show(qs_q, qs_ac_list, qs_ac_id, qs_m) ;
		 }
	 } 
	 

	 function reqAC(me) { 
		var sv ;
		var ke=trim_space(search_box.value, me) ;
		var sTarget="°ø¿¬";
		
		ke = ke.replace(/ /g, "%20") ;
		if (ke == "") { 
			ac_hide() ;
			return ;
		} 
		
		if(brw_type == "SA")
			sv = "/Home/SearchNew/get_ac_2.aspx?p=" + me + "&q=" + ke + "&t=" + sTarget;
		else
			sv = "/Home/SearchNew/get_ac_2.aspx?p=" + me + "&q=" + escape(ke) + "&t=" + escape(sTarget);
		
		_req = get_req() ;
		if (_req) { 
			_req.open("GET", sv, true) ;
			_req.onreadystatechange = showAC ;
		} 
		try {
			_req.send() ;
		} catch (e) { 
			return 0 ;
		} 
		 
	 } 

	function ac_show(aq, al, ai, am) {
	 
		 if (aq && aq!="" && aq!=trim_space(search_box.value, am)) return ;
		 qs_q = aq ;
		 qs_m = am ;
		 qs_ac_list = al ;
		 qs_ac_id = ai ;
		 qs_ac_len = qs_ac_list.length ;
		 var h = (qs_ac_len > 7) ? 7 : qs_ac_len ;
		 h = h * 19 ;

		 print_ac() ;

		 if (qs_ac_list[0] == "" && (qs_m==1 || qs_m==2)) { 
			 qs_ac_len=1;
			 h=19;
			 if (qs_ac_list[0] == "") h = h + 19;
		 } 
		 h = 140;
		 scrol.style.height = h + 4 ;

		 if (qs_ac_len) { 
			 h+=38;
			 //a_on=1;
		 } else { 
			 //a_on=0;
		 }
		 a_on=1;
		 ac_body.width = 330 ;
		 ac_body.height = h ;

		 popup_ac(1);

		 if (a_on) { 
			 set_acpos(0,0);
			 scrol.scrollTop=0;
			 
			 search_box.onkeydown = ackhl;
		 } 
		 
		 //¹ØÀÇ ÆûµéÀÌ ÀÚµ¿¿Ï¼º ·¹ÀÌ¾î¸¦ °¡¸®¹Ç·Î Àá½Ã ÆûµéÀ» ¾Èº¸ÀÌ°Ô ÇÑ´Ù.
		 hide_behind_forms();
	 } 

	 function set_acpos(v, bookImgsrc) { 
		a_now = v;
		setTimeout('set_ahl();', 10);
		
	     var g;
	     
	     if ( v > 0 && bookImgsrc)
		{
			
			document.all.bookImg.style.display =  "block";
			document.all.bookImg.src = bookImgsrc;
			g = document.getElementById("gNo" + a_now);
			goGoodsNo = g.value;	
		} else {
			//document.all.bookImg.style.display =  "none";
		}
	 }
	 
	 function goGoods()
	 {	
		//location.href = "/Home/Perf/PerfDetailInfo.aspx?IdPerf="+(parseInt(goGoodsNo, 10) + 100000);
	     location.href = "/Home/Perf/PerfDetailInfo.aspx?IdPerf=" + (goGoodsNo);
	 }

	 function set_ahl() { 
		 if (!a_on) return;
		 
		 var o1, o2;
		 for (i=0;  i<qs_ac_len; i++) { 
			o1 = document.getElementById('ac' + (i+1));
			if ((i+1) == a_now) {
				o1.style.backgroundColor = '#E3EDF5';
			} else {
				o1.style.backgroundColor = '';
//				document.all.bookImg.src = '';
			}
		}
	 } 
	 
	function Keycode(e){
		var result;
		if(window.event)
			result = window.event.keyCode;
		else if(e)
			result = e.which;
		return result;
	}

	 
	 //Å°¸¦ ´©¸¦¶§ ÀÌº¥Æ® °Ë»çÇÏ´Â ÇÔ¼ö
	 function ackhl(event) { 
		 
		 var key = Keycode(event);
		 var o1, o2 ;
		 var img;
		
		 if (key==39) { 
			 req_ac2(1) ;
		 }
		 if (key==13) {
			 //if (a_now>0) search_keyword(qs_ac_list[a_now]);
		 }
		 if (key==40 || (key==9 )) { 
			 if (m_on) return ;
			 if (!a_on) {	
				 req_ac2(1) ;
				 return ;
			}
			if (a_now < qs_ac_len) { 
				if (a_now == 0) bak = search_box.value ;
			
				a_now++ ;
				if (a_now > max_row) scrol.scrollTop = parseInt((a_now-1)/max_row)*max_row*19 ;
				 o1 = document.getElementById('ac' + a_now) ;
				 //o2 = document.getElementById('acq' + a_now) ;
				 //img = document.getElementById("img" + a_now) ;
				 //old = search_box.value = o2.outerText ;
				 //set_acpos(a_now, img.outerText) ;
				 
				 old = search_box.value = $('#acq' + a_now).text();
				 set_acpos(a_now, $('#img' + a_now).text()) ;
				 search_box.focus() ;
				 set_ahl() ;
				 //e.returnValue = false;
			 } 
		 }
		 if (a_on && (key==38 || key==9 )) {
			 if (!a_on) return ;
			 if (a_now <= 1) { 
				 ac_hide() ;
				 old = search_box.value = bak ;
			 } 
			 else {
				 a_now-- ;
				 if ((qs_ac_len-a_now)+1 > max_row) scrol.scrollTop = (qs_ac_len-(parseInt((qs_ac_len-a_now)/max_row)+1)*4)*19 ;
				 //o1 = document.getElementById('ac'+ a_now) ;
				 //o2 = document.getElementById('acq' + a_now) ;
				 //img = document.getElementById('img' + a_now) ;
				 old = search_box.value = $('#acq' + a_now).text() ;
				 set_acpos(a_now, $('#img' + a_now).text()) ;
				  search_box.focus() ;
				 set_ahl() ;
				 //e.returnValue = false ;
			 }
		 }
		 
	 } 

	 function print_ac() { 
	 
		 if (qs_ac_list[0] == "") {
			 scrol.innerHTML = get_ac0() ;
		 }
		 else {
			 scrol.innerHTML = get_aclist() ;
			 //alert(scrol.innterHTML);
		 }
		 //alert(scrol.innerHTML);
		 popup_ac(1); //ÀÚµ¿¿Ï¼ºÃ¢ º¸¿©ÁÜ.
		 //document.all.ac_body.style.display = B ;
		 setTimeout('set_ahl();', 10) ;
	 } 

	 function get_aclist() { 
		 var d="", ds="",l=0, s="", cnt=0, pos=0, qlen=0, img="";
		 if (qs_ac_list[0] != "") { 
			 s += "<table width=100% cellpadding=0 cellspacing=0 border=0>" ;
			 s += "<tr><td width=70% valign=top>";
			 s += "<table width=100% cellpadding=0 cellspacing=0 border=0>" ;
			 for (i=0; i<qs_ac_len;	i++) { 
				 var query = qs_ac_list[i].split("^");
				 ds = d = query[0];
				 goodsNo = query[2];
				 age_lmt_yn = query[3];
				 lmt_age = query[4];
				 img = query[1];

				 l = js_strlen(d) ;
				 
			     if (l > 20) ds = js_substring(d, 0, 20) + ".." ;
			     
				pos = d.indexOf(search_box.value);
				
				if (pos >= 0) {
					if (pos == 0) {
						ds = js_highlight (ds, search_box.value,  0);
					}
					else if (pos == d.length - 1)
					{
						ds = js_highlight (ds, search_box.value,  -1);
					}else 
					{
						ds = js_highlight (ds, search_box.value,  pos);
					}
				}
				 s += "<input type='hidden' name='gNo"+(i+1)+"' value='"+goodsNo+"'>";
				 s += "<a href='javascript:search_item();'>" ;
				 s += "<tr id='ac" + (i+1) + "' onmouseover=\"set_acpos('" + (i+1) + "', '"+img+"');\" onmouseout=\"set_acpos(0,0); \" onclick=\"set_acinput('" + (i+1) + "')\" style=\"this.style.backgroundColor=''\" style='CURSOR:hand'>" ;
				 s += "<td height=20 align=left title=\""+d+"\">" + ds +"</td>" ;
				 s += "<td height=20 align=right></td>" ;
				 s += "</tr></a>" ;
				 s += "<span id='acq" + (i+1) + "' style='display:none'>" + d + "</span>" ;
				 s += "<span id='img" + (i+1) + "' style='display:none'>" + img + "</span>" ;
			 } 
			 s += "</table>" ;
			 s += "</td><td>";
			 s += "<table width=100% cellpadding=0 cellspacing=0 border=0>" ;
			 s += "<tr><td valign='top' style='padding : 10 5 5 5;' height=140 width=70><img border=0 id='bookImg' width=70 style='display:none' onClick=\"goGoods();\" style=\"CURSOR:hand\">&nbsp;</td></tr></table>";
			 s += "</td></tr></table>"
		 }

		 return s ;
	 } 

	 function js_makehigh_pre(s, t) { 
		 var d="";
		 var s1=s.replace(/ /g, "");
		 var t1=t.replace(/ /g, "");
		 t1=t1.toLowerCase();
		 if (t1==s1.substring(0, t1.length)) {
			d="<font color=#75A4CC>";
			for (var i=0,j=0; j<t1.length; i++) 
			{
				if (s.substring(i, i+1)!=" ") j++;
				d+=s.substring(i, i+1)
			 }
			 d+="</font>"+s.substring(i, s.length)
		 } 
		 return d;
	 } 
	 
	 function js_makehigh_suf(s, t) { 
		 var d="";
		 var s1=s.replace(/ /g, "");
		 var t1=t.replace(/ /g, "");
		 t1=t1.toLowerCase();
		 if (t1==s1.substring(s1.length-t1.length))  { 
			for (var i=0,j=0 ; j<s1.length-t1.length ;  i++) { 
				if (s.substring(i, i+1)!=" ") j++;
				d+=s.substring(i, i+1);
			 } 
			d+="<font color=#75A4CC>";
			 for (var k=i,l=0;  l<t1.length;  k++) {
				if (s.substring(k, k+1)!=" ") l++;
				 d+=s.substring(k, k+1);
			 }
			 d+="</font>";
		 }
		 return d;
	 } 

	 function js_makehigh_mid(s, t, pos) { 
		 var d="";
		 var s1=s.replace(/ /g, "");
		 var t1=t.replace(/ /g, "");
		 t1=t1.toLowerCase();

		d=s.substring(0, pos);
		d+="<font color=#75A4CC>"; 
		for (var i=pos,j=0; j < t1.length; i++) 
		{
			if (s.substring(i, i+1)!=" ") j++;
			d+=s.substring(i, i+1);
		}
		d+="</font>"+s.substring(i, s.length);
		 return d;
	 } 


	 function js_highlight(s, d, is_suf) {
		 var ret="";
		 if (is_suf == 0) { 
			ret=js_makehigh_pre(s, d);
		 } 
		 else if (is_suf == -1) {
		 	ret=js_makehigh_suf(s, d);
		 }
		 else {
			ret=js_makehigh_mid(s, d, is_suf);
		 }

		 if (ret=="") return s;
		 else return ret;
	 } 

	 function set_acinput(v) { 
		 if (!a_on) return ;
		 //var o = document.getElementById('acq' + a_now) ;
		 //old = search_box.value = o.outerText ;
		 old = search_box.value = $('#acq' + a_now).text();
		 
		 search_box.focus() ;
		 ac_hide() ;
	 } 

	 function get_ac0() { 
		 var s="",ment="" ;
		 if (qs_m==1) ment="ÃßÃµ »óÇ°¸íÀÌ ¾ø½À´Ï´Ù.";
		 else if (qs_m==2) ment="ÃßÃµ »óÇ°¸íÀÌ ¾ø½À´Ï´Ù.";
		 s += "<table width=100% cellpadding=0 cellspacing=0 border=0>" ;
		 s += "<tr id=ac1 onmouseover=\"set_acpos(1,0); \" onmouseout=\"set_acpos(0,0); \" style=\"backgroundColor=''\">" ;
		 s += "<td height=20 align=left>" + ment + "<img border=0 id='bookImg' style='display:none'></td></tr>" ;
		 s += "</table>" ;
		 s += "<span id=acq1 style='display:none'>" + old + "</span>" ;
		 return s ;
	 }

	 function js_strlen(s) { 
		 var i,l=0;
		 for (i=0; i<s.length; i++) 
			 if (s.charCodeAt(i) > 127) l+=2;
			 else l++;
		 return l;
	 }

	 function js_substring(s, start, len) { 
		 var i,l=0;d="" ;
		 for (i=start; i<s.length && l<len; i++) {
			 if (s.charCodeAt(i) > 127) l+=2 ;
			 else l++ ;
			 d+=s.substr(i, 1) ;
		 } 
		 return d ;
	 } 

	function trim_space(ke, me) { 
	
		if (me!=2) {
			ke = ke.replace(/^ +/g, "") ;
			ke = ke.replace(/ +$/g, " ") ;
		} else { 
			ke = ke.replace(/^ +/g, " ") ;
			ke = ke.replace(/ +$/g, "") ;
		} 
		ke = ke.replace(/ +/g, " ") ;
	
		return ke ;
	 } 

	 function get_cc(me) { 
		 var ke=trim_space(search_box.value, me) + me ;
		 return cc[ke];
//		 alert(typeof(cc[ke]));
//		 return typeof(cc[ke])==UD ? null : cc[ke] ;
	 } 

	 function set_cc(aq, al, ai, me) { 
		 cc[aq+me] = new Array(aq, al, ai) ;
	 } 

	 function ac_hide() {	
		if (document.all.ac_body.style.display == N) return ;
		popup_ac(0); //hide all
		//document.all.ac_body.style.display = N ;
		a_on = a_now = 0 ;
		
		//ÆûµéÀ» ´Ù½Ãº¸ÀÌ°Ô ÇÑ´Ù.
		show_behind_forms();
	} 
	
	 function wi() {
		 if (acuse==0) return ;
		 if (m_on) { 
			 setTimeout("wi()", wi_int) ;
			return ;
		} 

		var now = search_box.value;
		if (now == "" && now != old) ac_hide() ;

		if(now != "" && bkImg == 1) 
		{
			fnClearGNBImage();
			bkImg = 0;
		}

		if (now.length >= wi_len && now != "" && now != old) { 

			var o=null, me=1 ;
			o = get_cc(me) ;
			
			if (o && o[1][0] != ""){ac_show(o[0], o[1], o[2], me); }
			else {reqAC(me) ;}
		} 
		old = now ;
		setTimeout("wi()", wi_int) ;
	 } 

	 function set_mouseon(f) { 
		 if (f==1) arr_on = 1 ;
		 else if (f==2) frm_on = 1 ;
	 }

	 function set_mouseoff(f) {
		 if (f==1) arr_on = 0 ;
		 else if (f==2) frm_on = 0 ;
	 } 

	 function req_pf() {
		 frm_on=1;
		 req_ac2(1);
		 search_box.focus();
		 cursor_end();
	 }

	 function req_sf() {
		 frm_on=1;
		 req_ac2(2);
		 search_box.focus();
		 cursor_end();
	 } 

	 function cursor_end() { 
		 if ((nav_type==1 || nav_type==2)  && dom_type==1) { 
			 var rng=search_box.createTextRange();
			 if (rng!=null) { 
				 rng.move("textedit");
				 rng.select();
			 }
		 }
	 }
	 
	 
	 
	 //ÀÚµ¿¿Ï¼º ·¹ÀÌ¾îµÚÀÇ ÆûµéÀ» °¨Ãá´Ù.
	 function hide_behind_forms(){
//		document.FTCategoryMain.sort.style.visibility="hidden";
//		document.FTCategoryMain.fetSizeTop.style.visibility="hidden";
//		document.FTCategoryMain.statGbTop.style.visibility="hidden";
	 }
	 
	 //ÀÚµ¿¿Ï¼º ·¹ÀÌ¾îµÚÀÇ ÆûµéÀ» ´Ù½Ã º¸¿©ÁØ´Ù.
	 function show_behind_forms(){
//		document.FTCategoryMain.sort.style.visibility="visible";
//		document.FTCategoryMain.fetSizeTop.style.visibility="visible";
//		document.FTCategoryMain.statGbTop.style.visibility="visible";
	 }
	 
	 //°Ë»ö¾îÀÔ·ÂÃ¢ÀÇ ÀÚµ¿¿Ï¼º È­»ìÇ¥¸¦ À§, ¾Æ·¡·Î º¯°æÇÑ´Ù.
	 //type 0 : Ã¢ÀÌ ´ÝÇûÀ»¶§ È­»ìÇ¥ ¾Æ·¡·Î.
	 //type 1 : Ã¢ÀÌ ÆîÃ³Á³À»¶§ À§·Î
	 function switch_image(type){
		org_src=document.all.imgIntro0.src;
		former_part=org_src.substring(0,org_src.length-6);
		if(type==0){
			document.all.imgIntro0.src=former_part+"Dn.gif";
		}else if(type==1){
			document.all.imgIntro0.src=former_part+"Up.gif";
		}
	 }
	
	 function debug(msg){
		window.status=msg;
	 }

 
 
 $(document).ready(function(){
	search_box=document.getElementById("TopPage1_txtSearchBox") ;
	search_option=document.getElementById("qdomain");
	
    bak=old=search_box.value ;
	 
	wd();
	setTimeout("wi()", wi_int);
});


