//<![CDATA[
/*----------------------------------------------------------------------------
 *  ÆÄÀÏ¸í : keyword.js
 *  ¼³¸í : ¿¬°ü°Ë»ö¾î °ü·ÃÆÄÀÏ(JS)
 *  ÀÛ¼ºÀÚ : ºí·Î±×¿¡¼­ ÆÛ¿Â °Í ÀÓ
 *  ÃÖÃÊÀÛ¼ºÀÏ : 2010-07-26
 *--------------------------------------------------------------------------*/

//=================== ÀÌº¥Æ® µî·Ï/»èÁ¦ ==================================================
var aEvent = {};
aEvent.bind = function(func, obj){
	return function(){
		return func.apply(obj, arguments);
	}
}
//ÀÌº¥Æ® µî·ÏÇÔ¼ö
aEvent.addEvent = function(element, name, observer, useCapture){
	useCapture = useCapture || false;
	if (element.addEventListener){
		element.addEventListener(name, observer, useCapture);
	}
	else if (element.attachEvent){
		element.attachEvent('on'+name, observer);
	}
}
//ÀÌº¥Æ® »èÁ¦ÇÔ¼ö
aEvent.removeEvent = function(element, name, observer, useCapture){
	useCapture = useCapture || false;
	if (element.removeEventListener){
		element.removeEventListener(name, observer, useCapture);
	}
	else if (element.detachEvent){
		element.detachEvent('on'+name, observer);
	}
}



//=================== Å°¿öµå ==================================================
//var KeywordClass = function(input, category, selValue, result){
var KeywordClass = function(input, result){
	this.input = input;			//ÀÎÇ²Ã¢
	//this.category = category;	//Ä«Å×°í¸®
	//this.selValue = selValue;	//
	this.result = result;

	this.isShow = false;	//º¸¿©ÁÖ´ÂÁö¿©ºÎ
	this.wordList = new ArrayList();	//Å°¿öµå ¸®½ºÆ®
	this.lastOver = null;				//¸¶Áö¸· ¸¶¿ì½º¿À¹ö


	//¼³Á¤
	this.delay = 500;				//Å°ÀÌº¥Æ® ÈÄ µô·¹ÀÌ½Ã°£
	this.overColor = '#EBEBEB';		//¼³Á¤Ã¢¹è°æ»öÄÃ·¯(¸¶¿ì½º¿À¹ö½Ã
	this.backColor = 'white';		//¼³Á¤Ã¢¹è°æ»öÄÃ·¯
	//this.url = 'http://www.insford.com/developer/common/inc_getKeywordXml.asp';	//AJAXÅ¸°ÙURL
	this.url = '/HiH/Home/i_src/inc/rsp_matching_data.php';	//AJAXÅ¸°ÙURL

	//¸¶¿ì½º ÀÌº¥Æ® µî·Ï
	aEvent.addEvent(this.input ,"click", aEvent.bind(this.getKeyword, this));
	//Å°º¸µå ÀÌº¥Æ® µî·Ï
	aEvent.addEvent(this.input ,"keydown", aEvent.bind(this.doTaiping, this));
	aEvent.addEvent(this.input ,"blur", aEvent.bind(this.doBlur, this));
}
KeywordClass.prototype = {
	//============================= ¼³Á¤ ÇÔ¼ö ====================================
	//ÀÚ±âÀÚ½Åµî·Ï
	addThis : function(This){
		this.self = This;
	},
	//Å°¿öµå µî·Ï
	addKeyword : function(word){
		var temp = new ALink(word, this);				//A¸µÅ© »ý¼º
		temp.setIndex(this.wordList.add(temp));			//ArrayList¿¡ µî·Ï, µî·Ï¹øÈ£ ¼ÂÆÃ
		this.result.appendChild(temp.getLink());		//DIVÅÂ±× Ãß°¡
		this.showKeyword();								//Å°¿öµå º¸ÀÌ±â
	},
	//ÀÌÀü¹øÈ£ ¼±ÅÃ
	prevSelLink : function(){
		if(this.wordList.isPrev()){
			if(this.lastOver){
				this.lastOver.setSelOut();	//»ö¾ø¾Ö±â
			}
			this.lastOver = this.wordList.getPrev();
			this.lastOver.setSelect();
		}else{
			this.closeKeyword();	//Å°¿öµå ´Ý±â
		}
	},
	//´ÙÀ½¹øÈ£ ¼±ÅÃ
	nextSelLink : function(){
		if(this.wordList.isNext()){
			if(this.lastOver){
				this.lastOver.setSelOut();	//»ö¾ø¾Ö±â
			}
			this.lastOver = this.wordList.getNext();
			this.lastOver.setSelect();
		}
	},
	//Æ¯Á¤¹øÈ£ ¼±ÅÃ
	selLink : function(index){
		if(this.lastOver){
			this.lastOver.setSelOut();	//»ö¾ø¾Ö±â
		}
		this.lastOver = this.wordList.getList(index);
		this.lastOver.setSelect();
	},
	//Å°¿öµå ´Ý°í °¨Ãß±â
	closeKeyword : function(){
		if(this.wordList.size() > 0){
			for (var i=0; i<this.wordList.size(); i++){
				this.wordList.getList(i).removeSelf();
			}
			this.wordList = new ArrayList();	//¿öµå¸®½ºÆ® ÃÊ±âÈ­
			this.hideKeyword();
		}
	},
	//º¸ÀÌ±â
	showKeyword : function(){
		if(!this.isShow){
			this.result.style.display = 'block';
			this.isShow = true;
		}
	},
	//°¨Ãß±â
	hideKeyword : function(){
		if(this.isShow){
			this.result.style.display = 'none';
			this.isShow = false;
		}
	},
	//inputÃ¢ °ª ÀÔ·ÂÇÏ±â
	setInput : function(str){
		this.input.value = str;
		this.closeKeyword();
	},
	//============================= ÀÌº¥Æ® Ã³¸® ÇÔ¼ö =============================
	//Å° Å¸ÀÌÇÎ
	doTaiping : function(e){
		var event = window.event || e;	//ÀÌº¥Æ® ±¸ÇÏ±â

		if(!this.isShow){
			if(event.keyCode==40){		//°á°úÃ¢ ¾Èº¸ÀÏ¶§ Å°º¸µå ´Ù¿î¹öÆ°½Ã ¹Ù·ÎÃ£±â
				this.getKeywordGo();
				return;
			}
			this.getKeyword();			//°á°úÃ¢ ¾Èº¸ÀÏ¶§ Å°º¸µå ¹öÆ° ´©¸£¸é Å°¿öµå Ã£±â
			return;
		}
		
		if(event.keyCode==13){
			if(this.wordList.getIndex()!=-1){
				var temp = this.wordList.getCur();
				temp.doClick();
			}
		}else if(event.keyCode==38){	//ÀÌÀü ¼±ÅÃ
			this.prevSelLink();
		}else if(event.keyCode==40){	//´ÙÀ½ ¼±ÅÃ
			this.nextSelLink();
		}else{
			this.getKeyword();
		}
	},
	//Å°¿öµåÃ£±â
	getKeyword : function(){
		setTimeout(this.self+".getKeywordGo();", this.delay);
	},
	getKeywordGo : function(){
		//´Ü¾î°¡ ¾Æ¹«°Íµµ ¾øÀ¸¸é
		if(this.input.value==''){
			this.result.style.display='none';
			return;
		}
		//if(this.selValue.value=="keyword" || this.selValue.value=="title"){
			//var parameter = 'keyword='+this.selValue.value+'&category='+this.category.value+'&word='+encodeURIComponent(this.input.value);
			var parameter = 'word='+encodeURIComponent(this.input.value);
			this.send(parameter);
		//}
	},
	doBlur : function(){
		setTimeout(this.self+".closeKeyword();", 300);
	},
	//============================== µ¥ÀÌÅÍ Àü¼Û Ã³¸® =============================
	//	XMLHTTP·òÄÉ½ºÆ® »ý¼º ÇÔ¼ö
	getXMLHttpRequest : function(){
		if (window.ActiveXObject) {
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					return new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e1) { return null; alert('5');}
			}
		} else if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		} else {
			return null;
		}
	},
	//Á¢¼ÓÁÖ¼Ò, ÆÄ¶ó¸ÞÅÍ, ÄÝ¹éÇÔ¼ö, ¸Þ¼ÒµåÁöÁ¤
	send : function(params){
		this.req = this.getXMLHttpRequest();
		var httpUrl = this.url;
		var httpParams = (params == null || params == '') ? null : params;
		httpUrl = httpUrl + "?" + httpParams;
		this.req.open('GET', httpUrl, true);
		this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		var request = this;

		this.req.onreadystatechange = function(){
			request.onStateChange.call(request);
		}
		this.req.send(null);

	},
	onStateChange : function(){
		this.callback(this.req);
	},
	// ÄÝ¹é ÇÔ¼ö
	callback : function(req){
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				try
				{
					var xmlDoc = req.responseXML.documentElement;

					var itemNode = xmlDoc.getElementsByTagName('item');

					if(itemNode.length > 0){
						this.closeKeyword();	//Å°¿öµå ÃÊ±âÈ­
						for (var i=0; i<itemNode.length; i++){
							var temp = itemNode[i].childNodes[0].data;
							this.addKeyword(itemNode[i].childNodes[0].data);
						}
					}					
				}
				catch (e)
				{
					this.closeKeyword();
				}


			}else{
				aalert('Á¢¼Ó ¿¡·¯ ÀÔ´Ï´Ù. \n\nÀá½Ã ÈÄ ´Ù½Ã »ç¿ëÇØ ÁÖ¼¼¿ä.');
			}
		}
	}
}


// A¸µÅ©Ã¢
var ALink = function(word, keywordArea){
	this.word = word;
	this.index = null;
	this.keywordArea = keywordArea;	//Å°¿öµå°´Ã¼

	this.div = document.createElement('DIV');
	this.div.style.cursor = 'pointer';
	this.div.innerHTML = word;

	//ÀÌº¥Æ® µî·Ï
	this.mouseOver = aEvent.bind(this.doOver, this);
	this.mouseClick = aEvent.bind(this.doClick, this);

	aEvent.addEvent(this.div ,"mouseover", this.mouseOver);
	aEvent.addEvent(this.div ,"click", this.mouseClick);
}
ALink.prototype = {
	//ÀÚ½ÅÀÇ ÀÎµ¦½º°ª
	setIndex : function(index){
		this.index = index;
	},
	// DIVÅÂ±× ¾ò±â
	getLink : function(){
		return this.div;
	},
	//ÇØ´ç ¼±ÅÃ ( »ö Ã¤¿ì±â )
	setSelect : function(){
		this.div.style.backgroundColor= this.keywordArea.overColor;
	},
	//ÇØ´ç ¼±ÅÃ ÇØÁ¦ ( »ö ¾ø¾Ö±â )
	setSelOut : function(){
		this.div.style.backgroundColor= this.keywordArea.backColor;
	},
	//ÀÚ½Å »èÁ¦
    removeSelf  : function(){
		aEvent.removeEvent(this.div ,"mouseover", this.mouseOver);
		aEvent.removeEvent(this.div ,"click", this.mouseClick);
		var temp = this.getLink();
		temp.parentNode.removeChild(temp);
	},
	//======================= ÀÌº¥Æ® °ü·Ã ÇÔ¼ö
	//¸¶¿ì½º ¿À¹ö
	doOver : function(){
		this.keywordArea.selLink(this.index);
		this.keywordArea.wordList.setIndex(this.index);
	},
	//¸¶¿ì½º Å¬¸¯
	doClick : function(){
		this.keywordArea.setInput(this.word);
	}
}

//]]>
