﻿var objAjaxReq = null;
if(window.XMLHttpRequest){ //기타브라우저
	   objAjaxReq = new XMLHttpRequest();
 }else if(window.ActiveXObject){ //IE 브라우저
	   objAjaxReq = new ActiveXObject("Microsoft.XMLHTTP");
 }

var SearchMap = Class.create();

SearchMap.prototype = {
	initialize: function(valSearchMap)
	{
		this.cateObj = new Array();
		this.cateTemplet = new Array();
		this.cateCount = new Array();
		this.cateTab = new Array();
		this.cateTabCount = new Array();
		this.search = valSearchMap;

		this.keyword = this.createObj(this.search.inputId); // 검색어 Input
		this.keyword.value = this.search.query;
		this.keyword.instance = this;
		this.keyword.onkeydown = function(e){this.instance.get(e);}

		this.searchBtn = this.createObj(this.search.searchBtn); // 검색버튼
		this.searchBtn.instance = this;
		this.searchBtn.onclick = function(e){this.instance.get(e);}

		this.cateNm = this.search.cateNm;

		this.MAPresult = this.createObj(this.search.MAPresult);
		this.MAPresult.instance = this;

		this.get(this.keyword.value);
	},
	createObj: function(_objNm){
		try{
			var tmpObj = document.getElementById(_objNm);
			if(!tmpObj && this.search.debug)
				alert(_objNm+" id를 가진 개체가 없습니다. 다시 확인 해 주세요");

			return tmpObj
		}catch(e){
			alert(e);
			return false;
		}
	},

	enterKey: function(e)
	{
		var key = window.ActiveXObject ? window.event.keyCode : e.which;

		if(key == 13 || key == 1 || key == 0){
			this.pg = 1;
			if(this.cateNm=="ALL"){
				this.lc = 3;
			}else{
				this.lc = 10;
			}
			this.goTab("?");
		}
	},

	clickInResult: function(e, obj){},
	clickSort: function(e, sort){},

	get: function(){
		value = this.keyword.value;
		lastQuery = value;
		var url = jsonMappingSrc;

//		if(this.keyword.value==""){
//			this.setQtMsg('',0);
//			return false;
//		}

		//Mapping Search
		var parsMap = 'query=' + value + "&sort=1&colName=MAPVILL&pg=1&lc=10&mapping=YES";
    
    objAjaxReq.open("POST", url, true);
    objAjaxReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=uft-8");
    objAjaxReq.setRequestHeader("Accept","text/xml");
    objAjaxReq.onreadystatechange = setXmlMap;
    objAjaxReq.send(parsMap);

	},

	makesUI : function(Mapxml){

		var totalCnt = 0;

		if(this.cateNm == "ALL"){
				for(i = 0; i < Mapxml.mapResult.length; i++){

					divNm = Mapxml.mapResult[i].CRID;
					TT = Mapxml.mapResult[i].TT;
					CRID = Mapxml.mapResult[i].CRID;
					description = Mapxml.mapResult[i].CMT;

					var base_vill_url = "/welchon_front/handler/tour-view?section_id=20090901000004&menu_id=20090901000079&vill_id=";
					var base_exp_url = "/welchon_front/handler/tour-view?section_id=20090901000004&menu_id=20090901000080&gb=0&vill_id=";
					var base_lodg_url = "/welchon_front/handler/tour-view?section_id=20090901000004&menu_id=20090901000081&lodg_id=";
					var base_img_url = "/welchon_front";
					var	tblEl = "<div class=\"search_box\">";

					var post_id = Mapxml.mapResult[i].DRID.replace(CRID+":", "");
					var titleLink = "";

					var vill_title = Mapxml.mapResult[i].TT;
					var vill_memo = Mapxml.mapResult[i].FD6;
					var vill_addr = Mapxml.mapResult[i].FD7;
					var vill_type = Mapxml.mapResult[i].FD8;
					var vill_exp = Mapxml.mapResult[i].FD15;

					/*
					if(Mapxml.mapResult[i].FD15 != ""){
						vill_exp = vill_exp + Mapxml.mapResult[i].FD15+", ";
					}
					if(Mapxml.mapResult[i].FD17 != ""){
						vill_exp = vill_exp + Mapxml.mapResult[i].FD17+", ";
					}
					if(Mapxml.mapResult[i].FD19 != ""){
						vill_exp = vill_exp + Mapxml.mapResult[i].FD19+", ";
					}
					if(Mapxml.mapResult[i].FD21 != ""){
						vill_exp = vill_exp + Mapxml.mapResult[i].FD21;
					}
					*/

					var vill_home = Mapxml.mapResult[i].FD39;

					var vill_desc = Mapxml.mapResult[i].TT;

					var vill_href = base_vill_url + Mapxml.mapResult[i].DRID.replace(CRID+":", "");
					var vill_img_top = base_img_url + Mapxml.mapResult[i].FD11;
					var vill_img1 = base_img_url + Mapxml.mapResult[i].FD11;
					var vill_img2 = base_img_url + Mapxml.mapResult[i].FD12;
					var vill_img3 = base_img_url + Mapxml.mapResult[i].FD13;
					var vill_img4 = base_img_url + Mapxml.mapResult[i].FD14;

					var vill_exp_count = Mapxml.mapResult[i].FD9;
					var vill_lodg_count = Mapxml.mapResult[i].FD10;

					var vill_exp_href1 = base_exp_url + Mapxml.mapResult[i].DRID.replace(CRID+":", "") + "&exper_event_id=" + Mapxml.mapResult[i].FD17;
					var vill_exp_href2 = base_exp_url + Mapxml.mapResult[i].DRID.replace(CRID+":", "") + "&exper_event_id=" + Mapxml.mapResult[i].FD20;
					var vill_exp_href3 = base_exp_url + Mapxml.mapResult[i].DRID.replace(CRID+":", "") + "&exper_event_id=" + Mapxml.mapResult[i].FD23;
					var vill_exp_href4 = base_exp_url + Mapxml.mapResult[i].DRID.replace(CRID+":", "") + "&exper_event_id=" + Mapxml.mapResult[i].FD26;

					var vill_lodg_href1 = base_lodg_url + Mapxml.mapResult[i].FD29;
					var vill_lodg_href2 = base_lodg_url + Mapxml.mapResult[i].FD32;
					var vill_lodg_href3 = base_lodg_url + Mapxml.mapResult[i].FD35;
					var vill_lodg_href4 = base_lodg_url + Mapxml.mapResult[i].FD38;

					var vill_exp_img1 = base_img_url + Mapxml.mapResult[i].FD16;
					var vill_exp_img2 = base_img_url + Mapxml.mapResult[i].FD19;
					var vill_exp_img3 = base_img_url + Mapxml.mapResult[i].FD22;
					var vill_exp_img4 = base_img_url + Mapxml.mapResult[i].FD25;

					var vill_lodg_img1 = base_img_url + Mapxml.mapResult[i].FD28;
					var vill_lodg_img2 = base_img_url + Mapxml.mapResult[i].FD31;
					var vill_lodg_img3 = base_img_url + Mapxml.mapResult[i].FD34;
					var vill_lodg_img4 = base_img_url + Mapxml.mapResult[i].FD37;

					var vill_exp1 = checkNull(Mapxml.mapResult[i].FD15, 0, "");
					var vill_exp2 = checkNull(Mapxml.mapResult[i].FD18, 0, "");
					var vill_exp3 = checkNull(Mapxml.mapResult[i].FD21, 0, "");
					var vill_exp4 = checkNull(Mapxml.mapResult[i].FD24, 0, "");

					var vill_lodg1 = checkNull(Mapxml.mapResult[i].FD27, 0, "");
					var vill_lodg2 = checkNull(Mapxml.mapResult[i].FD30, 0, "");
					var vill_lodg3 = checkNull(Mapxml.mapResult[i].FD33, 0, "");
					var vill_lodg4 = checkNull(Mapxml.mapResult[i].FD36, 0, "");

					vill_exp_href1 = vill_exp1 == "" ? "#" : vill_exp_href1
					vill_exp_href2 = vill_exp2 == "" ? "#" : vill_exp_href2
					vill_exp_href3 = vill_exp3 == "" ? "#" : vill_exp_href3
					vill_exp_href4 = vill_exp4 == "" ? "#" : vill_exp_href4
					
					vill_lodg_href1 = vill_lodg1 == "" ? "#" : vill_lodg_href1
					vill_lodg_href2 = vill_lodg2 == "" ? "#" : vill_lodg_href2
					vill_lodg_href3 = vill_lodg3 == "" ? "#" : vill_lodg_href3
					vill_lodg_href4 = vill_lodg4 == "" ? "#" : vill_lodg_href4


					var template = this.createObj("MAPresult").innerHTML;

					for(var i=0;i<6;i++){
						template = template.replace("#vill_title", TT);
					}

					template = template.replace("#vill_memo", vill_memo);
					template = template.replace("#vill_addr", vill_addr);
					template = template.replace("#vill_type", vill_type);
					template = template.replace("#vill_exp", vill_exp);
					
					if(vill_home == ""){
						template = template.replace("#vill_home", "준비중입니다..");
					}else{
						template = template.replace("#vill_home", "<a href=\""+vill_home+"\" target=\"_blank\">"+vill_home+"</a>");
					}
					template = template.replace("#vill_desc", description);

					for(var i=0;i<2;i++){
						template = template.replace("#vill_href", vill_href);
					}

					template = template.replace("#vill_img_top", vill_img_top);

					if(vill_img1 == "" || vill_img1 == undefined || vill_img1 =="/welchon_front"){
						template = template.replace("#onmouseover1", "");
					}else{
						template = template.replace("#onmouseover1", "onmouseover=\"changeIt('main_img', '" + vill_img1 + "');\"");
					}

					if(vill_img2 == "" || vill_img2 == undefined || vill_img2 =="/welchon_front"){
						template = template.replace("#onmouseover2", "");
					}else{
						template = template.replace("#onmouseover2", "onmouseover=\"changeIt('main_img', '" + vill_img2 + "');\"");
					}

					if(vill_img3 == "" || vill_img3 == undefined || vill_img3 =="/welchon_front"){
						template = template.replace("#onmouseover3", "");
					}else{
						template = template.replace("#onmouseover3", "onmouseover=\"changeIt('main_img', '" + vill_img3 + "');\"");
					}

					if(vill_img4 == "" || vill_img4 == undefined || vill_img4 =="/welchon_front"){
						template = template.replace("#onmouseover4", "");
					}else{
						template = template.replace("#onmouseover4", "onmouseover=\"changeIt('main_img', '" + vill_img4 + "');\"");
					}

					template = template.replace("#vill_img1", vill_img1);
					template = template.replace("#vill_img2", vill_img2);
					template = template.replace("#vill_img3", vill_img3);
					template = template.replace("#vill_img4", vill_img4);

					template = template.replace("#vill_exp_count", vill_exp_count);
					template = template.replace("#vill_lodg_count", vill_lodg_count);

					template = template.replace("#vill_exp_href1", vill_exp_href1);
					template = template.replace("#vill_exp_href1", vill_exp_href1);
					template = template.replace("#vill_exp_href2", vill_exp_href2);
					template = template.replace("#vill_exp_href2", vill_exp_href2);
					template = template.replace("#vill_exp_href3", vill_exp_href3);
					template = template.replace("#vill_exp_href3", vill_exp_href3);
					template = template.replace("#vill_exp_href4", vill_exp_href4);
					template = template.replace("#vill_exp_href4", vill_exp_href4);

					template = template.replace("#vill_lodg_href1", vill_lodg_href1);
					template = template.replace("#vill_lodg_href1", vill_lodg_href1);
					template = template.replace("#vill_lodg_href2", vill_lodg_href2);
					template = template.replace("#vill_lodg_href2", vill_lodg_href2);
					template = template.replace("#vill_lodg_href3", vill_lodg_href3);
					template = template.replace("#vill_lodg_href3", vill_lodg_href3);
					template = template.replace("#vill_lodg_href4", vill_lodg_href4);
					template = template.replace("#vill_lodg_href4", vill_lodg_href4);

					template = template.replace("#vill_exp_img1", vill_exp_img1);
					template = template.replace("#vill_exp_img2", vill_exp_img2);
					template = template.replace("#vill_exp_img3", vill_exp_img3);
					template = template.replace("#vill_exp_img4", vill_exp_img4);

					template = template.replace("#vill_lodg_img1", vill_lodg_img1);
					template = template.replace("#vill_lodg_img2", vill_lodg_img2);
					template = template.replace("#vill_lodg_img3", vill_lodg_img3);
					template = template.replace("#vill_lodg_img4", vill_lodg_img4);

					template = template.replace("#vill_exp1", "'" + vill_exp1 + "'");
					template = template.replace("#vill_exp1", vill_exp1);
					template = template.replace("#vill_exp2", "'" + vill_exp2 + "'");
					template = template.replace("#vill_exp2", vill_exp2);
					template = template.replace("#vill_exp3", "'" + vill_exp3 + "'");
					template = template.replace("#vill_exp3", vill_exp3);
					template = template.replace("#vill_exp4", "'" + vill_exp4 + "'");
					template = template.replace("#vill_exp4", vill_exp4);

					template = template.replace("#vill_lodg1", "'" + vill_lodg1 + "'");
					template = template.replace("#vill_lodg1", vill_lodg1);
					template = template.replace("#vill_lodg2", "'" + vill_lodg2 + "'");
					template = template.replace("#vill_lodg2", vill_lodg2);
					template = template.replace("#vill_lodg3", "'" + vill_lodg3 + "'");
					template = template.replace("#vill_lodg3", vill_lodg3);
					template = template.replace("#vill_lodg4", "'" + vill_lodg4 + "'");
					template = template.replace("#vill_lodg4", vill_lodg4);

					for(var i=0;i<13;i++){
						template = template.replace("#onerror","onerror=\"this.src='/welchon_front/image/welchon/skin/ico_NoimgL.gif'\"");
					}

					tblEl = tblEl + template;
					document.getElementById("MAP").innerHTML += tblEl + "</div>";
				}
		}


		searchInit();
	},

	goTab:function(tabNm){
		if(this.search.formSendTF == true){
		 	this.form.query.value = this.keyword.value;
		 	this.form.action = tabNm;
		 	this.form.nhit.value = this.sort;
		 	this.form.page.value = 1;
		 	this.form.submit();
		}
	},

	setCategrory: function setCategrory(category){},
	setTopkeyword: function (){},
	setAutoComplete: function(){},
	setInResultSearchMap: function(){},
	setSortAccuracy: function(){},
	setSortDate: function(){},
	setSortHit: function(){},
	setTab: function(){},
	setAllTabCnt: function(totalCnt){}
};




/***************************************************************************************/
/*	Search Ver 1.1
/*	제 작 일 : 2009/10/01
/*	제	 작: 검색사업본부 프로젝트팀 김장호
/*
/***************************************************************************************/
var valSearchMap = new String();
valSearchMap.debug = true;//id개체 유무 판별(세팅시에 giude역할)



{

	valSearchMap.inputId = "qt"; //검색창
	valSearchMap.inputTxt = "qtTxt"; //검색어
	valSearchMap.searchBtn = "qtBtn"; //검색버튼
	valSearchMap.qtMsg = "qtMsg"; //검색어

}//search base attribute


{

	valSearchMap.cateNm = "";
	valSearchMap.menuNm = "menuNm";
	valSearchMap.noSearch = "noSearch";//결과값없음 메세지
	valSearchMap.resultArea = "resultArea"; //결과 뿌려주는 AREA wrap
	valSearchMap.tabTF = false;
	valSearchMap.ALLTabCNT = "ALLTabCNT"; //타이틀 템플릿
	//valSearchMap.notAllTitle = "notAllTitle";
	//valSearchMap.notAllTitleCnt = "notAllTitleCnt";
	//valSearchMap.titleSmpl = "TITLESMPL"; //타이틀 템플릿
	valSearchMap.MAPresult = "MAPresult";

}// ui attribute

{

	valSearchMap.formSendTF = true;
	valSearchMap.formId = "pageFrm";

}//


pagescale = 10;
var HIGHLIGHT_START = "<font color=\"#b85c2e\">";
var HIGHLIGHT_END   = "</font>";

var jsonMappingSrc = './toJsonMapping.jsp';
var tSearchMap = null;


var searchMapInit =  function(){
	valSearchMap.query = query // 파라메터로 받은 검색어
	valSearchMap.cateNm = cateNm.toUpperCase();
	valSearchMap.page = page;//인기검색 유무
	valSearchMap.nhit = nhit;

	tSearchMap = new SearchMap(valSearchMap);
}
addEvent(window, 'load', searchMapInit);

function tmaxRequest(tmax){
	if(document.getElementById(valSearchMap.inputId))
		document.getElementById(valSearchMap.inputId).instance.makeUI(tmax);
}

function setXmlMap(){

	  if(objAjaxReq.readyState == 4) {
			if(objAjaxReq.status == 200 || objAjaxReq.status == 0) {
				xml =  eval('(' + objAjaxReq.responseText + ')');
				if( objAjaxReq.responseText ){
					document.getElementById("MAPresult").instance.makesUI(xml);
				}else{
					//document.getElementById("resultArea").style.display = "none";
					//document.getElementById(valSearch.noSearch).style.display = "";
					//document.getElementById(valSearch.inputId).instance.setQtMsg(lastQuery, 0);
				}
			}else{
			alert(objAjaxReq.status);
			}
		}
	}
