keywordSid = "0101_18030000_";
productSid = "0101_19070000_01_";

unitStart = 44032;
unitEnd = 55203;
units1st = new Array("ぁ", "あ", "い", "ぇ", "え", "ぉ", "け", "げ", "こ", "さ", "ざ", "し", "じ", "す", "ず", "せ", "ぜ", "そ", "ぞ");
units2nd = new Array("た", "だ", "ち", "ぢ", "っ", "つ", "づ", "て", "で", "と", "ど", "な", "に", "ぬ", "ね", "の", "は", "ば", "ぱ", "ひ", "び");
units3th = new Array("", "ぁ", "あ", "ぃ", "い", "ぅ", "う", "ぇ", "ぉ", "お", "か", "が", "き", "ぎ", "く", "ぐ", "け", "げ", "ご", "さ", "ざ", "し", "じ", "ず", "せ", "ぜ", "そ", "ぞ");
doubleUnits = new Array("", "", "", "", "", "", "", "", "", units2nd[8] + units2nd[0], units2nd[8] + units2nd[1], units2nd[8] + units2nd[20], "", "", units2nd[13] + units2nd[4], units2nd[13] + units2nd[5], units2nd[13] + units2nd[20], "", "", units2nd[18] + units2nd[20], "");
engKorMap = {"a":"け", "b":"ば", "c":"ず", "d":"し", "e":"ぇ", "E":"え", "f":"ぉ", "g":"ぞ", "h":"で", "i":"ち", "j":"っ", "k":"た", "l":"び", "m":"ぱ", "n":"ぬ", "o":"だ", "O":"ぢ", "p":"つ", "P":"て", "q":"げ", "Q":"こ", "r":"ぁ", "R":"あ", "s":"い", "t":"さ", "T":"ざ", "u":"づ", "v":"そ", "w":"じ", "x":"ぜ", "y":"に", "z":"せ"};
products = new Array();
productObjs = new Array();

doubleUnits[units1st[4]] = units1st[3] + units1st[3];
doubleUnits[units1st[8]] = units1st[7] + units1st[7];
doubleUnits[units1st[13]] = units1st[12] + units1st[12];

doubleUnits[units2nd[9]] = units2nd[8] + units2nd[0];
doubleUnits[units2nd[10]] = units2nd[8] + units2nd[1];
doubleUnits[units2nd[11]] = units2nd[8] + units2nd[20];
doubleUnits[units2nd[14]] = units2nd[13] + units2nd[4];
doubleUnits[units2nd[15]] = units2nd[13] + units2nd[5];
doubleUnits[units2nd[16]] = units2nd[13] + units2nd[20];
doubleUnits[units2nd[19]] = units2nd[18] + units2nd[20];

doubleUnits[units3th[2]] = units1st[0] + units1st[0];
doubleUnits[units3th[3]] = units1st[0] + units1st[9];
doubleUnits[units3th[5]] = units1st[2] + units1st[12];
doubleUnits[units3th[6]] = units1st[2] + units1st[18];
doubleUnits[units3th[9]] = units1st[5] + units1st[0];
doubleUnits[units3th[10]] = units1st[5] + units1st[6];
doubleUnits[units3th[11]] = units1st[5] + units1st[7];
doubleUnits[units3th[12]] = units1st[5] + units1st[9];
doubleUnits[units3th[13]] = units1st[5] + units1st[16];
doubleUnits[units3th[14]] = units1st[5] + units1st[17];
doubleUnits[units3th[15]] = units1st[5] + units1st[18];
doubleUnits[units3th[18]] = units1st[7] + units1st[9];
doubleUnits[units3th[20]] = units1st[9] + units1st[9];

enableSuggestKeyEvent = suggestFocus = false;
suggestBoxMaxHeight = 304;
defaultSuggestCount = 10;
currentSuggestResult = totalSuggestCount = 0;
prevFocusObj = null;

if (document.domain.indexOf("dnshop.com") > -1) {
	baseDomain = "dnshop.com";
} else {
	baseDomain = "dnshop.daum.net";
}

if (!window.insertComma) {
	scriptObj = document.createElement("SCRIPT");

	scriptObj.setAttribute("id", "jsMathFunc");
	scriptObj.setAttribute("type", "text/javascript");
	scriptObj.setAttribute("src", "http://uishopimage.hanmail.net/woongs/search_math.js");

	document.getElementsByTagName("HEAD").item(0).appendChild(scriptObj);
}

if (!window.addEvent) {
	function addEvent(object, eventName, functionName) {
		method = option = "";

		if (navigator.appVersion.toUpperCase().indexOf("MSIE") > 0) {
			method = "attachEvent";
		} else {
			method = "addEventListener";
			eventName = eventName.replace("on", "");
			option = ", false";
		}

		eval((object ? "object." : "") + method + "(\"" + eventName + "\", functionName " + option + ");");
	}
}

if (!window.getOffsetLeft) {
	function getOffsetLeft(object) {
		return object ? object.offsetLeft + getOffsetLeft(object.offsetParent) : 0;
	}
}

if (!window.getOffsetTop) {
	function getOffsetTop(object) {
		return object ? object.offsetTop + getOffsetTop(object.offsetParent) : 0;
	}
}

function Json() {

	this.head = document.getElementsByTagName("HEAD").item(0);
	this.scriptID = "JsonSuggestScript_" + (new Date()).getTime() + "_" + Math.round(Math.random() * 1000000);
	
	this.submit = function (url) {
		var url = (url.indexOf("?") < 0 ? url + "?" : url) + "&uniqueKey=" + this.scriptID;

		if (this.scriptObj = document.getElementById(this.scriptID)) {
			this.scriptObj.setAttribute("src", url);
		} else {
			this.scriptObj = document.createElement("SCRIPT");
			
			this.scriptObj.setAttribute("id", this.scriptID);
			this.scriptObj.setAttribute("type", "text/javascript");
			this.scriptObj.setAttribute("src", url);
			
			this.head.appendChild(this.scriptObj);
		}
	}
	
	this.remove = function () {
		try { if (this.scriptObj) this.head.removeChild(this.scriptObj); } catch (e) {}
	}
}

function setSuggestBox(inputBox) {
	if (!inputBox) return;

	var keywordBox, productBox, retentionWrap;
	var newLeft = inputBox.getAttribute("leftOffset"), newTop = inputBox.getAttribute("topOffset");
	var browserLeft = browserTop = 0;

	if (!newLeft && !newTop) return;

	if (navigator.appName.indexOf("Explorer") < 0) {
		browserLeft = 0;
		browserTop = 16;
	}

//	if (retentionWrap = document.getElementById("Retention_Wrap")) browserTop = browserTop - retentionWrap.offsetTop;

	if (keywordBox = document.getElementById("suggest_200901")) {
		keywordBox.style.left = (getOffsetLeft(inputBox) + (newLeft ? parseInt(newLeft.replace(/[^0-9\-]/g, "")) : -6) + browserLeft) + "px";
		keywordBox.style.top = (getOffsetTop(inputBox) +  (newTop ? parseInt(newTop.replace(/[^0-9\-]/g, "")) : -6) + browserTop) + "px";
	}

	if (productBox = document.getElementById("suggest_200901_product")) {
		productBox.style.left = (parseInt(keywordBox.style.left.replace(/[^0-9]/g, "")) + 225) + "px";
		productBox.style.top = (parseInt(keywordBox.style.top.replace(/[^0-9]/g, "")) + 17) + "px";
	}
}

function setSuggestInput(inputBox) {
	if (inputBox) {
		window.suggestInput = inputBox;

		if (window.suggestInput && !window.subSuggestInput) {
			window.subSuggestInput = document.createElement("INPUT");

			subSuggestInput.id = "autoSuggestWord";
			subSuggestInput.name = "autoSuggestWord";
			subSuggestInput.type = "text";
			subSuggestInput.readOnly = true;
			subSuggestInput.style.display = "none";

			suggestInput.offsetParent.appendChild(subSuggestInput);
		}

//		setSuggestBox(inputBox);

		if (inputBox.getAttribute("setSuggest") != "yes") {
			inputBox.setAttribute("autocomplete", "off");
			inputBox.setAttribute("defaultKeyword", inputBox.value);
			inputBox.setAttribute("setSuggest", "yes");

			inputBox.onkeyup = getSuggest;
			inputBox.onmouseover = function () { suggestFocus = true; }
			inputBox.onfocus = function () { suggestFocus = true; clearSuggestField(this); }
			inputBox.onblur = function () { suggestFocus = false; }
			inputBox.onmousedown = function () { suggestFocus = true; clearSuggestField(this); }

			inputBox.onclick = function () {
				setSuggestInput(this);
				
				if (this.value.replace(/[\s\t\r\n]+/g, "").length <= 0) { hideSuggestBox(true); return; }
				if (window.previousSuggestWord) if (previousSuggestWord == this.value) { viewSuggestBox(); return; }

				hideSuggestBox(true);
				getSuggest(event);
			}
		}
	}
}

function clearSuggestField(inputBox) {
	if (!inputBox) return;

	var defaultValue = (inputBox ? inputBox.getAttribute("defaultKeyword") : "");

	inputBox.style.backgroundImage = "";

	if (inputBox.value == defaultValue) inputBox.value = "";
}

function getKorUnits(str) {
	var result = "", charCode, unicodeNo;

	for (var a=0 ; a<str.length ; a++) {
		charCode = str.charAt(a);
		unicodeNo = escape(charCode);
	 
		if (unicodeNo.length >= 4) {
			unicodeNo = parseInt(unicodeNo.replace(/\%u/,''), 16);
			
			if (unicodeNo >= unitStart && unicodeNo <= unitEnd) {
				unicodeNo2 = unicodeNo - unitStart;
				firstUnit = Math.floor(unicodeNo2 / 588);
				unicodeNo2 = unicodeNo2 % 588;
				secondUnit = (Math.floor(unicodeNo2 / 28));
				unicodeNo2 = unicodeNo2 % 28;
				thirdUnit = unicodeNo2;
				
				if (doubleUnits[secondUnit]) units2nd[secondUnit] = doubleUnits[secondUnit];

				charCode = units1st[firstUnit] + units2nd[secondUnit] + units3th[thirdUnit];
			}
		}

		result = result + charCode;
	}

	return result;
}

function getEngKor(str) {
	var result = "", korChar, charCode;

	for (var a=0 ; a<str.length ; a++) {
		if (korChar = engKorMap[charCode = str.charAt(a)]) {
			result = result + korChar;
		} else {
			result = result + charCode;
		}
	}

	return result;
}

function viewSuggestBox() {
	var tempObj;

	if (totalSuggestCount > 0) if (tempObj = document.getElementById("suggest_200901")) tempObj.style.display = 'block';
}

function hideSuggestBox(initialize) {
	var tempObj;

	if (initialize) {
		currentSuggestResult = 0;
		
		if (window.suggestList) suggestList.innerHTML = "";
		if (tempObj = document.getElementById("suggest_200901_product")) tempObj.innerHTML = "";
	}

	if (tempObj = document.getElementById("suggest_200901_product")) tempObj.style.display = 'none';
	if (tempObj = document.getElementById("suggest_200901")) tempObj.style.display = 'none';
}

function highlightCheck(str) {
	var returnStr = "", replaceTarget = "", matched, charCode, unicodeNo;

	if (matched = (returnStr = str.replace(revertQuery, "<strong>" + revertQuery + "</strong>")).match(new RegExp(revertQuery, "gi"))) {
		if (typeof matched == "object") {
			for (var a=0 ; a<matched.length ; a++) returnStr = addHighlightTag(str, matched[a]);
		} else {
			returnStr = addHighlightTag(str, matched);
		}
	}

	for (var a=0 ; a<revertQuery.length ; a++) {
		charCode = revertQuery.charAt(a);
		unicodeNo = escape(charCode);
	 
		if (unicodeNo.length >= 4) {
			unicodeNo = parseInt(unicodeNo.replace(/\%u/, ""), 16);
			
			if (unicodeNo >= unitStart && unicodeNo <= unitEnd) replaceTarget = replaceTarget + charCode;
		}
	}

	if (replaceTarget) {
		returnStr = returnStr.replace(replaceTarget, "<strong>" + replaceTarget + "</strong>");
		replaceTarget = replaceTarget.substring(0, replaceTarget.length - 1);
		returnStr = returnStr.replace(replaceTarget, "<strong>" + replaceTarget + "</strong>");
	}

	return returnStr;
}

function addHighlightTag(str, match) {
	if (match) str = str.replace(new RegExp(match, "g"), "<strong>" + match + "</strong>");

	return str;
}

function changeMyColor(liObj, mode) {
	if (mode == "on") {
		if (prevFocusObj && prevFocusObj.id != liObj.id) changeMyColor(prevFocusObj, "off");

		liObj.style.backgroundColor = "#eaedf9";
		prevFocusObj = liObj;

		getSuggestProduct(liObj.title, parseInt(liObj.id.replace(/[^0-9]+/g, "")));
	} else {
		if (prevFocusObj && prevFocusObj.id != liObj.id) return;

		liObj.style.backgroundColor = "transparent";

		if (tempObj = document.getElementById("suggest_200901_product")) tempObj.style.display = 'none';
	}
}

function scrollByKey(thisEvent) {
	var myEvent = thisEvent? thisEvent : event, key = myEvent.keyCode, tempObj;

	if (!window.currentSuggestResult) window.currentSuggestResult = 0;

	if (key == "38" || key == "40") {
		if (key == "38") {
			if (!currentSuggestResult) currentSuggestResult = 2;

			if (document.getElementById("suggest_result_" + (currentSuggestResult - 1))) {
				if (tempObj = document.getElementById("suggest_result_" + currentSuggestResult)) changeMyColor(tempObj, 'off');
				if (tempObj = document.getElementById("suggest_result_" + (currentSuggestResult - 1))) changeMyColor(tempObj, 'on');

				currentSuggestResult = currentSuggestResult - 1;
			} else {
				changeMyColor(document.getElementById("suggest_result_1"), 'on');

				currentSuggestResult = 1;
			}
		} else if (key == "40") {
			if (!currentSuggestResult) currentSuggestResult = 0;

			if (document.getElementById("suggest_result_" + (currentSuggestResult + 1))) {
				if (tempObj = document.getElementById("suggest_result_" + currentSuggestResult)) {
					changeMyColor(tempObj, 'off');
				}

				if (tempObj = document.getElementById("suggest_result_" + (currentSuggestResult + 1))) {
					changeMyColor(tempObj, 'on');
				}

				currentSuggestResult = currentSuggestResult + 1;
			}
		}
		moveScrollBar(currentSuggestResult);
		selectWord(currentSuggestResult);
	}

	return;
}

function moveScrollBar(resultNo) {
	var unitHeight, scrollHeight, scrollTop, scrollUp;

	scrollHeight = suggestBox.scrollHeight;
	scrollTop = suggestBox.scrollTop;
	unitHeight = document.getElementById("suggest_result_" + resultNo).offsetHeight;
	scrollUp = Math.floor((resultNo-1) / defaultSuggestCount) * unitHeight * defaultSuggestCount;

	if (scrollTop != scrollUp) suggestBox.scrollTop = (scrollUp < scrollHeight ? scrollUp : scrollHeight);
}

function getSuggestProduct(keyWord, no) {
	keyWord = keyWord.replace(/[\s\t\r\n]+/g, "");

	if (products[keyWord]) {
		if (products[keyWord] != "none") {
			viewSuggestProduct(products[keyWord], no);
			return;
		}
	}

	if (!window.suggestorExt) window.suggestorExt = new Json();

	suggestorExt.submit("/front/search/KeywordProduct?keyword=" + keyWord + "&wordNo=" + no + "&function=viewSuggestProduct");
}

function viewSuggestProduct(product, no) {
	var tempObj, productLink = "", productObj, prevProdObj, couponPrice;

	if (!no) no = 1;

	if (tempObj = document.getElementById("suggest_200901_product")) {
		if (!product || !product.prodid) {
			tempObj.style.display = "none";
			return;
		}

		if (product.keyword && !products[product.keyword]) products[product.keyword] = product;
		if (prevProdObj = document.getElementById("suggest_product")) prevProdObj.offsetParent.removeChild(prevProdObj);

		if (!(productObj = productObjs[product.prodid])) {
			var imgArea, imgLinkObj, prodImgObj, nameArea, nameLinkObj, couponImgObj, priceArea, priceObj;

			productObj = document.createElement("DIV");
			productObj.id = "suggest_product";
			productLink = "/front/product/ProductDetail?Sid=" + productSid + (no < 10 ? "0" + no : no) + "&PID=" + product.prodid;

			if (imgArea = document.createElement("P")) {
				imgLinkObj = document.createElement("A");
				prodImgObj = document.createElement("IMG");

				imgArea.className = "sgg_pic";
				imgLinkObj.href = productLink;
				prodImgObj.src = "http://image.dnshop.co.kr/m_productimages/" + product.imgurl;
				prodImgObj.alt = product.prodname;
				prodImgObj.style.width = "130px";
				prodImgObj.style.height = "130px";

				imgLinkObj.appendChild(prodImgObj);
				imgArea.appendChild(imgLinkObj);
				productObj.appendChild(imgArea);
			}

			if (nameArea = document.createElement("P")) {
				nameLinkObj = document.createElement("A");

				nameArea.className = "sgg_desc";
				nameLinkObj.href = productLink;
				nameLinkObj.innerHTML = product.prodname;

				if (product.couponid) {
					if (couponImgObj = document.createElement("IMG")) {
						couponImgObj.src = "http://uishopimage.hanmail.net/dnreah/07_newcate/default_img/coupon_" + product.coupon_discount + "dis.gif";
						couponImgObj.alt = product.salecost;

						nameArea.appendChild(couponImgObj);
					}
				}

				nameArea.appendChild(nameLinkObj);
				productObj.appendChild(nameArea);
			}

			if (priceArea = document.createElement("P")) {
				priceArea.className = "sgg_price";
				priceArea.innerHTML = insertComma(product.salecost) + "据";

				productObj.appendChild(priceArea);
			}

			if (product.couponid) {
				if (couponPriceArea = document.createElement("P")) {
					if (product.coupontype == "R") {
						couponPrice = floorK(product.salecost - (product.salecost * (product.coupon_discount / 100)));
					} else {
						couponPrice = product.salecost - product.coupon_discount;
					}

					couponPriceArea.className = "sgg_coupon";
					couponPriceArea.innerHTML = "庭肉亜 " + insertComma(couponPrice) + "据";

					productObj.appendChild(couponPriceArea);
				}
			}
			productObjs[product.prodid] = productObj;
		}

		if (window.suggestorExt) suggestorExt.remove();

		tempObj.appendChild(productObj);
		tempObj.style.display = 'block';
	}
}

function selectWord(resultNo) {
	if (!resultNo) return false;

	changeMyColor(document.getElementById("suggest_result_" + resultNo), 'on');

	suggestInput.value =  document.getElementById("suggest_word_" + resultNo).innerHTML.replace(/<\/?[^<>]{1,}>/gi, "");

	if (suggestInput.form) {
		if (!suggestInput.form.Sid) {
			sidInput = document.createElement("INPUT");

			sidInput.setAttribute("type", "hidden");
			sidInput.setAttribute("name", "Sid");

			suggestInput.form.appendChild(sidInput);
		}

		if (suggestInput.form.Sid) suggestInput.form.Sid.value = keywordSid + (parseInt(resultNo) < 10 ? "0" + resultNo : resultNo);
	}
}

function suggestFormSubmit() {
	if (window.suggestInput && suggestInput.form.name == "SEARCH") suggestInput.form.submit();
}

function getSuggest(thisEvent) {
//	if (document.location.toString().indexOf("test-shop") < 0) return;		// 績獣稽 薦暗

	var myEvent, key, tempObj;
	var newSearchWord = "", subSearchWord = "";

	myEvent = thisEvent? thisEvent : event;
	key = myEvent.keyCode;
	if (!window.suggestBox) window.suggestBox = document.getElementById("suggest_box");
	if (!window.suggestList) window.suggestList = document.getElementById("suggest_list");
	if (!window.previousSuggestWord) window.previousSuggestWord = "";

	if (37 <= parseInt(key) && parseInt(key) <= 40) return scrollByKey(myEvent);

	if (window.suggestBox) {
		suggestBox.onmouseover = function () { suggestFocus = true; }
		suggestBox.onmouseout = function () { suggestFocus = false; }
	}

	if (!window.subSuggestInput) {
		window.subSuggestInput = document.createElement("INPUT");

		subSuggestInput.id = "autoSuggestWord";
		subSuggestInput.name = "autoSuggestWord";
		subSuggestInput.type = "text";
		subSuggestInput.readOnly = true;
		subSuggestInput.style.display = "none";

		suggestInput.offsetParent.appendChild(subSuggestInput);
	}

	if (suggestInput.value.replace(/[\s\t\r\n]+/g, "").length <= 0) hideSuggestBox();

	if (suggestInput.value.replace(/[\s\t\r\n]+/g, "").length > 0 && suggestInput.value != previousSuggestWord) {
		if (!window.suggestor) window.suggestor = new Json();
		
		window.breakSuggest = true;

		newSearchWord = getKorUnits(suggestInput.value);
		subSearchWord = getKorUnits(getEngKor(suggestInput.value));
		subSuggestInput.value = subSearchWord;

		suggestor.submit("http://suggest." + baseDomain + "/front/search/Suggest?SEARCH_KEYWORD=" + newSearchWord + "&subSearchWord=" + subSearchWord + "&returnType=json&callBackFunction=viewSuggest&limit=15");

		currentSuggestResult = 0;
		window.breakSuggest = false;
	}

	previousSuggestWord = suggestInput.value;
}

function viewSuggest(jsonObj) {
	var keyWord, rank, liObj;

	totalSuggestCount = (jsonObj && jsonObj.result ? jsonObj.result.length - 1 : 0);

	if (window.breakSuggest || totalSuggestCount < 1) {
		hideSuggestBox(true);
	} else {
		suggestList.innerHTML = "";
		//suggestBox.style.height = "auto";
		window.revertQuery = suggestInput.value;
		window.subRevertQuery = subSuggestInput.value;

		for (var a=0, b=0 ; a<jsonObj.result.length ; a++) {
			if (a > 14)		window.breakSuggest = true;
			if (window.breakSuggest) break;

			if (jsonObj.result[a]) {
				keyWord = jsonObj.result[a].item[0].keyword;
				rank = jsonObj.result[a].item[0].rank;
				count = jsonObj.result[a].item[0].count;
				result = jsonObj.result[a].item[0].result;

				if (keyWord) {
					liObj = document.createElement("LI");

					liObj.onmouseover = function () { changeMyColor(this, 'on'); }

					liObj.id = "suggest_result_" + (++b);
					liObj.title = keyWord;
					liObj.innerHTML = "<span id='suggest_word_" + b + "'>" + highlightCheck(keyWord) + "</span> <em>" + insertComma(count) + "噺</em>";

					eval("liObj.onclick = function (event) { selectWord(" + b + "); suggestFormSubmit(); }");
					suggestList.appendChild(liObj);

					totalSuggestCount = b;
				}
			}
		}

		viewSuggestBox();
	}

	if (totalSuggestCount < 1) hideSuggestBox(true);

	suggestor.remove();
}

function openF2Search(myEvent) {
	var thisEvent = myEvent ? myEvent : event;

	if (thisEvent.keyCode == 113) {
		if (tempObj = document.getElementById("f2search")) {
			if (tempObj.style.display == "none") {
				tempObj.style.position = "absolute";
				tempObj.style.left = (Math.round((document.body.clientWidth - tempObj.offsetWidth) / 2) + document.body.scrollLeft) + "px";
				tempObj.style.top = (Math.round((document.body.clientHeight - tempObj.offsetHeight) / 2) + document.body.scrollTop) + "px";
				tempObj.style.display = "block";

				if (window.setF2search) setF2search();
			} else {
				tempObj.style.display = "none";
			}
		}
	}

	if (thisEvent.keyCode == 27) if (tempObj = document.getElementById("f2search")) tempObj.style.display = "none";
}


if (!window.suggestInput) if (document.SEARCH) setSuggestInput(document.SEARCH.SEARCH_KEYWORD);

addEvent(document, "onclick", function () { if (!window.suggestFocus) hideSuggestBox(); });
addEvent(document, "onkeydown", function (event) { openF2Search(event); });

