var balloon = {};  //net.onlinenote.ajax 패키지정의

function cutString(str, limitBytes, tail)
{
	var byteLen = 0;
	for(i=0; i<str.length; i++)
	{
		if(str.charCodeAt(i) > 255) byteLen += 2;
		else byteLen ++;
		if(byteLen > limitBytes) return str.substring(0, i)+tail;
	}
	return str;
}

function parseXML(text)
{
    var doc=null;
    // code for IE
    if (window.ActiveXObject)
    {
        doc=new ActiveXObject("Microsoft.XMLDOM");
        doc.async="false";
        doc.loadXML(text);
    }
    // code for Mozilla, Firefox, Opera, etc.
    else
    {
        var parser=new DOMParser();
        doc=parser.parseFromString(text,"text/xml");
    }
    return doc;
}

// 문자열에 trim함수 추가하기
String.prototype.trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/gi, "");
}

balloon.Request = function(url, params, callback, method, obj) { //Request 클래스의 생성자, 객체 생성과 동시에 Send함수 호출
    this.url = url;
    this.params = params;
    this.callback = callback;
    this.method = method;
    this.obj = obj;
    this.send();
}
balloon.Request.prototype = {
    getXMLHttpRequest: function() {
        if (window.ActiveXObject) {
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                return new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e1) { return null; }
            }
        } else if (window.XMLHttpRequest) {
            return new XMLHttpRequest();
        } else {
        return null;
        }
    },
    send: function() {
        this.req = this.getXMLHttpRequest();   // req 프로퍼티에 XMLHttpRequest 객체를 저장

        var httpMethod = this.method ? this.method : 'GET';
        if (httpMethod != 'GET' && httpMethod != 'POST') {
            httpMethod = 'GET';
        }
        var httpParams = (this.params == null || this.params == '') ? null : this.params;
        var httpUrl = this.url;
        if (httpMethod == 'GET' && httpParams != null) {
            httpUrl = httpUrl + "?" + httpParams;
        }
        this.req.open(httpMethod, httpUrl, true);
        this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        var request = this;
        this.req.onreadystatechange = function() {
            request.onStateChange.call(request);     // XMLHttpRequest 객체의 readyState 값이 바뀔때 Request 객체
        }                                                         //의 onStateChange 함수 호출
        this.req.send(httpMethod == 'POST' ? httpParams : null);
    },
    onStateChange: function() {
        if(this.obj==null) this.callback(this.req);
        else this.callback(this.req, this.obj);
    }
}


/**
 * Modified by 이상과현실사이
 * Homepage	: http://www.onlinenote.net
 * Last update : 2008/04/23
 * 
 * Created by 행복한고니
 * Homepage	: http://mygony.com
 * Last update : 2005/04/10
 * 2005-04-10 
 *  - 첫번째 릴리즈
 */


var clickAreaCheck = false;
document.onclick = function()
{
	if (!clickAreaCheck) {
		if(balloon.Hint.layer) balloon.Hint.layer.hide();
	} else {
		clickAreaCheck = false;
	}
}

balloon.Hint = function(Id)
{
	balloon.Hint.layerId = Id;
	balloon.Hint.eventX = 0;
	balloon.Hint.eventY = 0;

	document.addEventListener('mouseover', balloon.Hint.Show, false);
	//document.addEventListener('mouseout', balloon.Hint.Hide, false);
}

balloon.Hint.layerId = null;

balloon.Hint.Show = function (evt)
{
	if (typeof evt == "undefined" || typeof evt.target == "undefined") {
		(evt=event).target = event.srcElement;
	}
	balloon.Hint.eventX = evt.clientX;
	balloon.Hint.eventY = evt.clientY;

	var keyword = evt.target.getAttribute("keyword");
	if (keyword == null || keyword.length == 0) return;
	if (balloon.Hint.layer == null) balloon.Hint.makeLayer();

	// load desc from server using AJAX
	//var param = "keyword="+keyword;
	var param = "keyword="+encodeURIComponent(keyword);
	new balloon.Request("/dictionary/keyword.hint.asp", param, balloon.Hint.keywrodResult, 'POST', this);

	//keyword=keyword;
}

balloon.Hint.keywrodResult = function (req, hint)
{
	if(req.readyState==4)
	{
		if(req.status == 200)
		{
			//alert(req.responseText);
			//var xmlDoc = req.responseXML;
			var xmlDoc = parseXML(req.responseText);

			var ele = xmlDoc.getElementsByTagName("code");
			if(ele)
			{
				var code = ele.item(0).firstChild.nodeValue;
				if(code == 'success')
				{
					//var result = eval("(" + xmlDoc.getElementsByTagName('data').item(0).firstChild.nodeValue+")");
					// show hint
					var tagkeyword = xmlDoc.getElementsByTagName('keyword')[0];
					var tagcategory = xmlDoc.getElementsByTagName('category')[0];
					var tagfullname = xmlDoc.getElementsByTagName('fullname')[0];
					var tagdesc = xmlDoc.getElementsByTagName('desc')[0];

					var keyword = (tagkeyword.text)? tagkeyword.text: tagkeyword.textContent;
					var category = (tagcategory.text)? tagcategory.text: tagcategory.textContent;
					var fullname = (tagfullname.text)? tagfullname.text: tagfullname.textContent;
					var desc = (tagdesc.text)? tagdesc.text: tagdesc.textContent;
					if(keyword != undefined && keyword != "") keyword = keyword.trim();
					if(category != undefined && category != "") category = category.trim();
					if(fullname != undefined && fullname != "") fullname = fullname.trim();
					if(desc != undefined && desc != "") desc = desc.trim();

					with (balloon.Hint.layer) {
						innerHTML = sourceHTML;
						innerHTML = innerHTML.replace("--keyword--", keyword);
						if(fullname != undefined && fullname != ""){
							innerHTML = innerHTML.replace("--desc--", "<strong>"+fullname+"</strong><br>"+cutString(desc, 60, "..."));
						}
						else{
							innerHTML = innerHTML.replace("--desc--", desc);
						}
						//alert("suppert06.asp?keyword="+keyword);
						innerHTML = innerHTML.replace("--url--", "/support/support06.asp?key="+category+"#"+encodeURIComponent(keyword.replace(" ", "")));
						innerHTML = innerHTML.replace("#"+encodeURIComponent(keyword.replace(" ", ""))+"/", "#"+encodeURIComponent(keyword.replace(" ", "")));
//alert(innerHTML);
						show(balloon.Hint.eventX, balloon.Hint.eventY);
					}

				}
				else if(code=='failed')
				{
					var message = xmlDoc.getElementsByTagName('message').item(0).firstChild.nodeValue;
					//alert("Error : "+message);
				}
				else{
					//alert("xml format error");
				}
			}
			else{
				//alert("xml format error");
			}
		}
		else{
			//alert("Error : There is not a possibility of reading information.");
		}
	}
}

balloon.Hint.Hide = function (evt)
{
	if (typeof evt == "undefined" || typeof evt.target == "undefined") {
		(evt=event).target = event.srcElement;
	}

	var hint = evt.target.getAttribute("keyword");
	if (hint == null || hint.length == 0) return;

	balloon.Hint.layer.hide();
}

balloon.Hint.makeLayer = function()
{
	if (typeof document.body == "undefined") {
		document.body = document.getElementsByTagName("BODY")[0];
	}

	balloon.Hint.layer = document.getElementById(balloon.Hint.layerId);
	balloon.Hint.layer.sourceHTML = balloon.Hint.layer.innerHTML;
	balloon.Hint.layer.style.position = "absolute";

/*
	if (typeof window.createPopup == "undefined") {
*/
		balloon.Hint.layer.show = function(x, y) {
			balloon.Hint.layer.style.display = "block";
			balloon.Hint.layer.style.left = (x+document.body.scrollLeft) + "px";
			balloon.Hint.layer.style.top =  (y+document.body.scrollTop) + "px";
		}
		balloon.Hint.layer.hide = function() {
			balloon.Hint.layer.style.display = "none";
		}
/*
	}
	else {
		balloon.Hint.layer.popup = window.createPopup();
		balloon.Hint.layer.show = function(x, y) {
			with (balloon.Hint.layer) {
				style.display = "block";
				var w = offsetWidth, h = offsetHeight;
				style.display = "none";
				popup.document.body.innerHTML = innerHTML;
				popup.show(x, y, w, h, document.body);
			}
		}
		balloon.Hint.layer.hide = function() {
			balloon.Hint.layer.popup.hide();
		}
	}
*/
}

if (typeof document.addEventListener == "undefined") {
	if (typeof document.attachEvent != "undefined") {
		document.addEventListener = function (eventType, listener) {
			document.attachEvent("on"+eventType, listener);
		}
		document.removeEventListener = function (eventType, listener) {
			document.detachEvent("on"+eventType, listener);
		}
	}
}

document.write("<div id=\"balloon_hint\" style=\"display:none\">\n");
document.write("<table width=\"150\" height=\"120\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"#DDDDDD\">\n");
document.write("  <tr>\n");
document.write("	<td height=\"100\" valign=\"top\" bgcolor=\"#FFFFFF\" > <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");
document.write("		<tr>\n");
document.write("		  <td background=\"../imgs/dic_2.gif\"><img src=\"../imgs/dic_1.gif\" width=\"193\" height=\"25\"></td>\n");
document.write("		</tr>\n");
document.write("		<tr>\n");
document.write("		  <td style=\"font-size:11px;font-family:verdana;font-weight:bold;letter-spacing:-1px;text-decoration:none;color:#B78CAA;padding:7px 5 5px 8;\">--keyword--</td>\n");
document.write("		</tr>\n");
document.write("		<tr>\n");
document.write("		  <td style=\"font-size:9px;font-family:verdana;color:#575757;line-height:16px;padding:0 5 5px 8;\">--desc--</td>\n");
document.write("		</tr>\n");
document.write("	  </table></td>\n");
document.write("  </tr>\n");
document.write("  <tr>\n");
document.write("	<td height=\"15\" align=\"right\" bgcolor=\"#DDDDDD\"><a href=\"#\" onClick=\"location.href='--url--'; return false;\"><img src=\"../imgs/dic_3.gif\" width=\"193\" height=\"16\" border=\"0\"></a></td>\n");
document.write("  </tr>\n");
document.write("</table>\n");
document.write("</div>\n");


new balloon.Hint('balloon_hint');