/***************************************************************
* comment : Á¾¸ñ¸í/ÄÚµå¸¦ ÀÔ·ÂÇÏ¿´À»¶§ ¹Ì¸®º¸±â ±â´É
* prototype.js¸¦ °°ÀÌ include ÇÏ¿© »ç¿ëÇØ¾ßÇÔ.
* 2008/03/05 javascript ¸®ÅÏ ¹æ½Ä Ãß°¡ - ¼­Á¤È¯, È£Ãâ ÆäÀÌÁö : /common/itemSearchJSON.jsp
***************************************************************/

var autocomplete = Class.create();
autocomplete.prototype = {
	initialize:function(options){
		//³Ñ¾î¿Ã °ª : *_inputBox, _hiddenBox,  _layerID, _tableID, _viewSize, *_function
		//*´Â ²À ¼³Á¤ÇØ¾ßÇÔ.
		if($(options._inputBox) == null || options._inputBox == ''){/*alert("         - ÀÚµ¿°Ë»ö ¼³Á¤ ½ÇÆÐ!! -         \n\n_inputBox ´Â ²À ¼³Á¤ÇØ ÁÖ¼Å¾ß ÇÕ´Ï´Ù.");*/ return;}
		if(options._function == null || options._function == ''){/*alert("         - ÀÚµ¿°Ë»ö ¼³Á¤ ½ÇÆÐ!! -         \n\n_function ´Â ²À ¼³Á¤ÇØ ÁÖ¼Å¾ß ÇÕ´Ï´Ù.");*/ return;}
		this._nansu = parseInt(Math.random() * 1000);
		this._inputBox = options._inputBox;
		this._hiddenBox = (options._hiddenBox) ? options._hiddenBox : null;
		this._hiddenBoxName = (options._hiddenBoxName) ? options._hiddenBoxName : null;
		this._layerID = (options._layerID) ? options._layerID : "_preview_"+this._nansu;
		this._layerFlag = (options._layerID) ? true : false;
		this._layerIDS = options._layerID+"S";
		this._tableID = (options._tableID) ? options._tableID : "_table_"+this._nansu;
		this._function = options._function;
		this._viewSize = (options._viewSize) ? options._viewSize : 6;
		this._retType = (options._retType) ? options._retType : "xml";
		this._width = (options._width) ? options._width : 260;

		this._chosung = new Array('¤¡', '¤¢', '¤¤', '¤§', '¤¨','¤©', '¤±', '¤²', '¤³', '¤µ', '¤¶','¤·', '¤¸', '¤¹', '¤º', '¤»', '¤¼', '¤½', '¤¾' );
		this._jungsung = new Array('¤¿','¤À','¤Á','¤Â','¤Ã','¤Ä','¤Å','¤Æ','¤Ç','¤È','¤É','¤Ê','¤Ë','¤Ì','¤Í','¤Î','¤Ï','¤Ð','¤Ñ','¤Ò','¤Ó');//Áß¼º 21°³
		this._jongsung = new Array('','¤¡','¤¢','¤£','¤¤','¤¥','¤¦','¤§','¤©','¤ª','¤«','¤¬','¤­','¤®','¤¯','¤°','¤±','¤²','¤´','¤µ','¤¶','¤·','¤¸','¤º','¤»','¤¼','¤½','¤¾');//Á¾¼º 28°³
		this._not = new Array('¤¢','¤£','¤¥','¤¦','¤ª','¤«','¤¬','¤­','¤®','¤¯','¤°','¤´','¤¶');//Á¾¼ºÁß¿¡ ¹ÞÄ§À¸·Î »ç¿ëÇÒ¼ö ¾ø´Â°Í
		this._han = new Array('°¡', '±î', '³ª', '´Ù', 'µû', '¶ó', '¸¶', '¹Ù', 'ºü', '»ç', '½Î', '¾Æ', 'ÀÚ', 'Â¥', 'Ã»', 'Ä«', 'Æ¼', 'ÆÄ', 'ÇÏ');
		this._itemCode = new Array();
		this._itemName = new Array();
		this._inputBox.autocomplete = "off";
		this._flag = false;
		this._o = true;
		this._listSize = 0;
		this._idx = 0;
		this._keyWord = "";
		this._height = 0;
		this._scroll = 0;
		this._cnt = 0;
		this._x = 0;
		this._y = 0;
		this.createElementDiv();
		this.createElementTables();
		this.position();
		this._script = null;

		Event.observe(this._inputBox, 'mousedown', this.display.bind(this));
		Event.observe(this._inputBox, 'keydown', this.KeyListener.bind(this));
		Event.observe(this._inputBox, 'blur', this.mouseOut.bind(this));
		Event.observe(this._layerID, 'mouseover', this.mouseOver.bind(this));
		Event.observe(this._layerID, 'mouseout', this.mouseOut.bind(this));
		Event.observe(document, 'click', this.eventCheck.bind(this));
	},

	include_script:function(url)
	{
	    	if(this._script != null)
	    	{
	    		document.getElementsByTagName('head')[0].removeChild(this._script) ;
	    		this._script = null;
	    	}
	    	
		this._script = document.createElement('script');
		this._script.src = url;
		this._script.type = 'text/javascript';
		this._script.defer = true ;
		this._script.charset = 'euc-kr';
		this._script = document.getElementsByTagName('head')[0].appendChild(this._script);
	},

	getJamoCodes:function(txt)//¾ÅÅ©Ç® ÀÌ¶ó¸é ¤¶ ¤Ó ¤· ¤» ¤Ñ ¤½ ¤Ì ¤© ÀÌ·±½ÄÀ¸·Î Àß¶ó³¿
	{
		this._c = txt.charCodeAt(0), this._c = this._c<0x3130?0:this._c<0x3164?this._c-0x3130:this._c<0xac00?0:this._c<0xd7a5?this._c+68:0;
		if (this._c>51) return Array((this._c-this._c%588)/588-74,((this._c-this._c%28)/28)%21+1,this._c%28);
		return Array(this._c<3?this._c:this._c<4?0:this._c<5?this._c-1:this._c<7?0:this._c<10?this._c-3:this._c<17?0:this._c<20?this._c-10:this._c<21?0:this._c<31?this._c-11:0,this._c<31?0:this._c-30,0);
	},

	position:function()//inputboxÀÇ À§Ä¡¸¦ Ã£¾Æ¼­ ·¹ÀÌ¾î À§Ä¡¸¦ ¼³Á¤
	{
		this._positions = Position.positionedOffset($(this._inputBox));
		//alert($(this._inputBox));
		this._x = this._positions[0];
		this._y = (this._positions[1] + $(this._inputBox).offsetHeight);
	},

	mouseOver:function(){ this._o = true; },//¸¶¿ì½º°¡ ·¹ÀÌ¾î À§¿¡ ÀÖ´ÂÁö Ã¼Å©
	mouseOut:function(){ this._o = false; },//¸¶¿ì½º°¡ ·¹ÀÌ¾î¿¡¼­ ³ª°¬´ÂÁö Ã¼Å©
	eventCheck:function(){	if(!this._o) this.hide();},//¸¶¿ì½º°¡ ·¹ÀÌ¾î ¹ÛÀ¸·Î ³ª°£»óÅÂ¸é ·¹ÀÌ¾î¸¦ ¼û±ä´Ù.

	createElementDiv:function()//¹Ì¸®º¸±â ·¹ÀÌ¾î¸¦ »ý¼ºÇÑ´Ù.
	{
		this.idiv = document.createElement('div');//½ºÅ©·Ñ ¸®½ºÆ®¸¦ º¸¿©ÁÙ ·¹ÀÌ¾î
		this.idiv.id = this._layerIDS;
		this.idiv.className = "layer_scroll";
		
		if(!this._layerFlag)
		{
			this.div = document.createElement('div');//º¸ÀÓ, ¼û±è ±â´ÉÀ» ÇÒ ·¹ÀÌ¾î
			this.div.id = this._layerID;
			this.div.style.cssText = "display:none;";
	
			$(this._inputBox).parentNode.appendChild(this.div);
		}
		else
		{
			this.div = $(this._layerID);
		}
	},

	createElementTable:function()//Å×ÀÌºíÀ» »ý¼ºÇÑ´Ù.
	{
		this._table = document.createElement('table');//¸®½ºÆ² Ãâ·ÂÇÒ Å×ÀÌºí
		this._table.id = this._tableID;
		this._table.border = 0;
		this._table.cellSpacing = 0;
		this._table.cellPadding = 0;
		this._table.style.cssText = "width:145px;background:#FFFFFF;";
	},

	createElementTables:function()
	{
		this._otable = document.createElement('table');//¿Ü°¢ Å×ÀÌºí
		this._otable.border = 0;
		this._otable.cellSpacing = 1;
		this._otable.cellPadding = 2;
		this._otable.style.cssText = "width:145px;background:#8FADCF;";
		this._otr = this._otable.insertRow(0);
		this._otd = this._otr.insertCell(0);
		this._otd.bgColor = "#FFFFFF";
		this.div.appendChild(this._otable);

		this._itable = document.createElement('table');//¾È Å×ÀÌºí
		this._itable.border = 0;
		this._itable.cellSpacing = 1;
		this._itable.cellPadding = 0;
		this._itable.style.cssText = "width:145px;background:#C3CEE0;";
		this._itr = this._itable.insertRow(0);
		this._itd = this._itr.insertCell(0);
		this._otd.appendChild(this._itable);
	},

	KeyListener:function(event)//Å°¸¦ ´­¸®¸é È£Ãâ
	{
		if(event.keyCode == Event.KEY_BACKSPACE || event.keyCode == Event.KEY_DELETE){//backspace key or delete key
			($(this._inputBox).value.replace(/(^\s*)|(\s*$)/g,"") != "") ? setTimeout(this.preview.bind(this), 300) : this._flag = false;
			if($(this._inputBox).value == "" && $(this._hiddenBox) != null) $(this._hiddenBox).value = "";
			if($(this._inputBox).value == "" && $(this._hiddenBoxName) != null) $(this._hiddenBoxName).value = "";
		}
		else if(event.keyCode == Event.KEY_RETURN)
		{//Enter key
			if($(this._inputBox).value.replace(/(^\s*)|(\s*$)/g, "") != "")
			{
				setTimeout(this._function, 250);
				this.hide();
			}
		}
		else if(event.keyCode == Event.KEY_SPACE)
		{//space key
			return event.returnValue=false;
		}
		else if(event.keyCode == Event.KEY_UP || event.keyCode == Event.KEY_DOWN)
		{//arrow key top/bottom
			if(!this._flag) return;
			this.move(event.keyCode);
		}
		else
		{
			//setTimeout(this.preview.bind(this), 300);
			setTimeout(this.preview.bind(this), 500);
		}
	},

	move:function(keyCode){//¸¶¿ì½º up, downÀ» ´©¸£¸é È£Ãâ
		switch (keyCode){
			case Event.KEY_DOWN: //¡é
			if(this._listSize > this._idx && this._flag)
			{
				if(Element.visible(this._layerID) == false) Element.show(this._layerID);
				if(this._idx % this._viewSize == 0 && this._idx != 0)
				{
					this._cnt++;
					if(parseInt(this._listSize / this._viewSize) == this._cnt)
					{
						this._scroll = (this._listSize % this._viewSize) * 25;
					}
					else
					{
						this._scroll = 25 * this._viewSize;
					}
					$(this._layerIDS).scrollTop += this._scroll;
				}
				if(this._idx > 0) $(this._tableID).rows[this._idx-1].style.backgroundColor = '#FFFFFF';
				$(this._tableID).rows[this._idx].style.backgroundColor = '#E6EBF2';
				$(this._inputBox).value = this._itemName[this._idx];
				if($(this._hiddenBox) != null) $(this._hiddenBox).value = this._itemCode[this._idx];
				if($(this._hiddenBoxName) != null) $(this._hiddenBoxName).value = this._itemName[this._idx];
				this._idx++;
			}
			else
			{
				return;
			}
			break;
			case Event.KEY_UP: //¡è
			if(this._idx > 1 && this._flag)
			{
				if(Element.visible(this._layerID) == false) Element.show(this._layerID);
				this._idx--;
				if(this._idx % this._viewSize == 0&& this._idx != 0)
				{
					(parseInt(this._listSize / this._viewSize) == this._cnt) ? this._scroll = (this._listSize % this._viewSize) * 25 : this._scroll = 25 * this._viewSize;
					$(this._layerIDS).scrollTop -= this._scroll;
					this._cnt--;
				}
				$(this._tableID).rows[this._idx].style.backgroundColor= '#FFFFFF';
				$(this._tableID).rows[this._idx-1].style.backgroundColor='#E6EBF2';
				$(this._inputBox).value = this._itemName[this._idx-1];
				if($(this._hiddenBox) != null) $(this._hiddenBox).value = this._itemCode[this._idx-1];
				if($(this._hiddenBoxName) != null) $(this._hiddenBoxName).value = this._itemName[this._idx-1];
			}
			else
			{
				this._idx = 0;
				Element.hide(this._layerID);
				$(this._inputBox).value = this._keyWord;
				//if($(this._hiddenBox) != null) $(this._hiddenBox).value = "";
			}
			break;
			default:
			break;
		}
	},

	preview:function(){
		this._keyWord = $(this._inputBox).value;
		this._param = $(this._inputBox).value;
		//if(this._retType == 'xml')
		//{
			if(encodeURI($(this._inputBox).value).indexOf("%E3%") != -1)//°Ë»ö¾î Ã¹¹øÂ°¿¡ ÃÊ¼º¸¸ ÀÔ·Â¸¸ µÈ°æ¿ì..
			{
				if(this._param.length == 1)
				{
					for(var i = 0; i < this._chosung.length ; i++)
					{
						if(this._chosung[i] == $(this._inputBox).value)//ÃÊ¼º¿¡¼­ °Ë»öÀ» ÇÏ¿©
						{
							this._param = this._han[i];//ÆÄ¶ó¸ÞÅÍ º¯¼ö¿¡ ¼ÂÆÃ
						}
					}
				}
				
				//alert(this._param);
				/*else
				{
					return;
				}*/
			}

			this._tmp = this._keyWord.split("");
			if(this._tmp.length >  0)
			{
				this._arr = this.getJamoCodes(this._tmp[this._tmp.length-1]);
				if(this._jongsung[this._arr[2]] != "" && this._jongsung[this._arr[2]] != undefined)
				{
					for(var i = 0; i < this._not.length; i++)
					{
						if(this._not[i] == this._jongsung[this._arr[2]]) return;//Á¾¼º¿¡ »ï¼º ˆ ÀÌ·±½ÄÀ¸·Î ¤¥ÀÌ µîµîÀÌ µé¾î¿À¸é °Ë»öÀ» ÇÏÁö ´õÀÌ»ó ¼öÇàÇÏÁö ¾Ê´Â´Ù.
					}
				}
			}
		//}

		if(this._retType == 'xml')
		{
			var pars = $H({p_code:this._param.toUpperCase(), nansu:Math.random()}).toQueryString();
			new Ajax.Request('/common/itemSearch.jsp', {method: 'get', parameters: pars, onComplete: this.parseXML.bind(this)});
		}
		else if(this._retType == 'javascript')
		{
			//new Ajax.Request('http://www.thinkpool.com/common/itemSearchJSON.jsp', {method: 'get', parameters: pars, onComplete: this.parseJSON.bind(this)});
			this.include_script('http://www.thinkpool.com/common/itemSearchJSON.jsp?p_code='+encodeURIComponent(this._param.toUpperCase())+'&ran='+Math.random());
			//½ºÅ©¸³Æ® Çü½ÄÀ¸·Î ÀÎÅ¬·çµå ÇÑ´Ù.
			setTimeout(this.parseJSON.bind(this), 150);
		}
	},
	
	parseJSON:function()
	{
		/*
		*	µ¥ÀÌÅÍ°¡ ·Îµå µÆ´ÂÁö¸¦ Ã¼Å©ÇÑ´Ù.
		*	µ¥ÀÌÅÍ¸¦ ·ÎµåµÇÁö ¾ÊÀº »óÅÂÀÌ¸é ´Ù½Ã È£Ãâ.
		*/
		if(!_loaded)
		{
			setTimeout(this.parseJSON.bind(this), 250);
			return;
		}
		
		_loaded = false;
		var data = _jsonData;//.evalJSON();
		
		var _this = this;
		this._idx = 0;
		this._cnt = 0;


		this._listSize = data.ResultSet.Result.length;
		

		(this._listSize > this._viewSize) ? this._height = 25 * this._viewSize : this._height = 25 * this._listSize;
		if(this._layerFlag)
		{
			this.div.style.cssText = "position:absolute;width:"+this._width+"px;display:none;z-index:99;height:150px;";
		}
		else
		{
			this.div.style.cssText = "position:absolute;width:"+this._width+"px;display:none;z-index:99;height:"+this._height+"px;left:"+this._x+"px;top:"+this._y+"px;";
		}

		this.idiv.style.cssText = "width:"+this._width+"px;height:"+this._height+"px;background:#FFFFFF;overflow-x:hidden;overflow-y:scroll;";
		if($(this._tableID) != null) $(this._tableID).remove();
		this.createElementTable();
		if(this._listSize == 1 && data.ResultSet.Result[0].code == "0"){ this._listSize = 0;if($(this._hiddenBox) != null) $(this._hiddenBox).value = "";}
		if(this._listSize == 1 && data.ResultSet.Result[0].code == "0"){ this._listSize = 0;if($(this._hiddenBoxName) != null) $(this._hiddenBoxName).value = "";}
		for(var i = 0; i < this._listSize; i++)
		{
			this._itemName[i] = data.ResultSet.Result[i].name;
			this._itemCode[i] = data.ResultSet.Result[i].code;

			this.tr = this._table.insertRow(i);
			this.tr.style.cssText = "height:25px;";
			this.tr.setAttribute("name", data.ResultSet.Result[i].name);
			this.tr.setAttribute("code", data.ResultSet.Result[i].code);
			this.tr.onmouseover = function(){this.style.backgroundColor = '#E6EBF2';this.style.cursor = 'hand';};
			this.tr.onmouseout = function(){this.style.backgroundColor = '';};
			this.tr.onclick = function(){$(_this._inputBox).value = this.getAttribute("name");if($(_this._hiddenBox) != null) $(_this._hiddenBox).value = this.getAttribute("code");if($(_this._hiddenBoxName) != null) $(_this._hiddenBoxName).value = this.getAttribute("name");_this.hide(); _this._function.call();};

			for(var j = 0; j< 2; j++)
			{
				this.td = this.tr.insertCell(j);
				this.txt = document.createElement('p');
				if(j == 0)
				{
					this.txt.innerHTML = "&nbsp;"+data.ResultSet.Result[i].view_name;
					this.txt.style.cssText = 'width:'+(this._width - 65)+';';
				}
				else
				{
					this.txt.innerHTML = data.ResultSet.Result[i].view_code;
					this.txt.style.cssText = 'width:45px;';
				}
				this.td.appendChild(this.txt);
				this.td.valign = "bottom";
			}
		}
		this._itd.appendChild(this.idiv);
		this.idiv.appendChild(this._table);

		if(this._listSize > 0)
		{
			this._flag = true;
			if($(this._hiddenBox) != null) $(this._hiddenBox).value = data.ResultSet.Result[0].code;
			if($(this._hiddenBoxName) != null) $(this._hiddenBoxName).value = data.ResultSet.Result[0].name;
			Element.show(this._layerID);
		}
		else
		{
			this._flag = false;
		}
	},

	parseXML:function(req)// itemSearch.jsp¿¡¼­ ÀÐ¾î¿Â xml ÄÚµå¸¦ ÆÄ½ÌÇÑ´Ù.
	{
		var _this = this;
		var xml = req.responseXML;
		this._idx = 0;
		this._cnt = 0;

		var nodes = xml.getElementsByTagName("item");
		var code = xml.getElementsByTagName("code");
		var name = xml.getElementsByTagName("name");
		var view_code = xml.getElementsByTagName("view_code");
		var view_name = xml.getElementsByTagName("view_name");

		this._listSize = nodes.length;

		(this._listSize > this._viewSize) ? this._height = 25 * this._viewSize : this._height = 25 * this._listSize;
		if(this._layerFlag)
		{
			this.div.style.cssText = "position:absolute;width:"+this._width+"px;display:none;z-index:99;height:"+this._height+"px;";
		}
		else
		{
			this.div.style.cssText = "position:absolute;width:"+this._width+"px;display:none;z-index:99;height:"+this._height+"px;left:"+this._x+"px;top:"+this._y+"px;";
		}
		this.idiv.style.cssText = "width:"+this._width+"px;height:"+this._height+"px;background:#FFFFFF;overflow-x:hidden;overflow-y:scroll;z-index:100;";
		if($(this._tableID) != null) $(this._tableID).remove();
		this.createElementTable();
		if(this._listSize == 1 && code[0].firstChild.nodeValue == "0"){ this._listSize = 0;if($(this._hiddenBox) != null) $(this._hiddenBox).value = "";}
		if(this._listSize == 1 && code[0].firstChild.nodeValue == "0"){ this._listSize = 0;if($(this._hiddenBoxName) != null) $(this._hiddenBoxName).value = "";}
		for(var i = 0; i < this._listSize; i++)
		{
			this._itemName[i] = name[i].firstChild.nodeValue;
			this._itemCode[i] = code[i].firstChild.nodeValue;

			this.tr = this._table.insertRow(i);
			this.tr.style.cssText = "height:25px;";
			this.tr.setAttribute("name", name[i].firstChild.nodeValue);
			this.tr.setAttribute("code", code[i].firstChild.nodeValue);
			this.tr.onmouseover = function(){this.style.backgroundColor = '#E6EBF2';this.style.cursor = 'hand';};
			this.tr.onmouseout = function(){this.style.backgroundColor = '';};
			this.tr.onclick = function(){$(_this._inputBox).value = this.getAttribute("name");if($(_this._hiddenBox) != null) $(_this._hiddenBox).value = this.getAttribute("code");if($(_this._hiddenBoxName) != null) $(_this._hiddenBoxName).value = this.getAttribute("name");_this.hide(); _this._function.call();};

			for(var j = 0; j< 2; j++)
			{
				this.td = this.tr.insertCell(j);
				this.txt = document.createElement('p');
				if(j == 0)
				{
					this.txt.innerHTML = "&nbsp;"+view_name[i].firstChild.nodeValue;
					this.txt.style.cssText = 'width:'+(this._width - 65)+'px;';
				}
				else
				{
					this.txt.innerHTML = view_code[i].firstChild.nodeValue;
					this.txt.style.cssText = 'width:45px;';
				}
				this.td.appendChild(this.txt);
				this.td.valign = "bottom";
			}
		}
		this._itd.appendChild(this.idiv);
		this.idiv.appendChild(this._table);

		if(this._listSize > 0)
		{
			this._flag = true;
			if($(this._hiddenBox) != null) $(this._hiddenBox).value = code[0].firstChild.nodeValue;
			if($(this._hiddenBoxName) != null) $(this._hiddenBoxName).value = name[0].firstChild.nodeValue;
			Element.show(this._layerID);
		}
		else
		{
			this._flag = false;
		}
	},

	hide:function()//·¹ÀÌ¾î¸¦ ¼û±è
	{
		if(Element.visible(this._layerID)) Element.hide(this._layerID);
	},

	display:function(){//inputbox¿¡ ¸¶¿ì½º¸¦ Å¬¸¯ÇßÀ»µ¥ È£Ãâ
		if($(this._inputBox).getValue() == 'Á¾¸ñ°Ë»ö' || $(this._inputBox).getValue() == 'Á¾¸ñ¸í/ÄÚµå') $(this._inputBox).clear();
		this._o = true;
		if($(this._inputBox).value != "" && this._flag)
		{
			if(Element.visible(this._layerID)){
				Element.hide(this._layerID);
			}else{
				if(this._idx==0){
					Element.show(this._layerID);
				}else{
					this.preview();
				}
			}
		}
	}
};

var _jsonData = null;
var _loaded = false;

function toJS(data)
{
	_jsonData = data;
	_loaded = true;
}