//////////////////////////////////////////////////////////////////////////////
//																			//
//		Author : Yuki														//
//		Position : Intergrated Search Platform team, Daum Communications	//
//		E-mail : yuki@daumcorp.com											//
//		Date : 04 June, 2007												//
//																			//
//////////////////////////////////////////////////////////////////////////////

var dic_TooltipBox;
var tooltipCg = 0; // (0:default, 1:오른쪽, 2:검색결과본문, 3:태그)
var tStyle;
var t_posX, t_posY;
var t_element;
var t_posX_limit, t_posY_limit;
var t_display = 0;

function startTooltip() {
	dic_TooltipBox = document.createElement("div");
	dic_TooltipBox.id = "dic_TooltipBox";
	document.body.appendChild(dic_TooltipBox);

	tStyle = document.getElementById("dic_TooltipBox").style;

	t_posX_limit = document.body.clientWidth;
	t_posY_limit = document.body.clientHeight;


	if (navigator.appName == "Microsoft Internet Explorer") {
		document.onmousemove = getMouseEvent;
	}
	else document.addEventListener('mousemove',saveMouseEvent,false);
}

function getMouseEvent() {
	t_posX = event.x;
	t_posY = event.y;
	t_element = event.relatedTarget;
	// IE에서 컨튼츠와 양측 사이의 공백만큼 재계산
	if ((parseInt(document.body.clientWidth) - 1004) / 2 > 0) {
		t_posX = t_posX + (parseInt((parseInt(document.body.clientWidth) - 1004) / 2));
	}
	
	// IE에서 브라우저 화면 크기에 따른 유동적 pixel 변화 처리
	if (tooltipCg == 0) {
		//if (parseInt(t_posX) < parseInt(t_posX_limit)) tStyle.left = parseInt((document.body.scrollWidth - 1259) / 2) + t_posX + 130 + "px";
		//if (parseInt(t_posY) < parseInt(t_posY_limit)) tStyle.top = t_posY + 163 + "px";
		tStyle.left = t_posX + 0 + "px";
		tStyle.top = t_posY + 155 + "px";
	}
	else if (tooltipCg == 1) {
		if (parseInt(t_posX) < parseInt(t_posX_limit)) tStyle.left = parseInt((document.body.scrollWidth - 1259) / 2) + t_posX - 50 + "px";
		if (parseInt(t_posY) < parseInt(t_posY_limit)) tStyle.top = t_posY + 153 + "px";
	}
	else if (tooltipCg == 2) {
		if (parseInt(t_posX) < parseInt(t_posX_limit)) tStyle.left = parseInt((document.body.scrollWidth - 1259) / 2) + t_posX + 130 + "px";
		if (parseInt(t_posY) < parseInt(t_posY_limit)) tStyle.top = t_posY + 153 + "px";
	}
	else if (tooltipCg == 3) {
		if(parseInt(t_posX)+247 > parseInt(t_posX_limit)) tStyle.left = t_posX - 123 + "px";		
		else tStyle.left = t_posX - 20 + "px";
		if (parseInt(t_posY) < parseInt(t_posY_limit)) tStyle.top = t_posY + 10 + "px";
	}	
	else return;
	
}

// FireFox 마우스 이벤트 처리 함수
function saveMouseEvent(e) {
	t_posX = e.pageX;
	t_posY = e.pageY;
	t_element = e.toElement;
	var wHeight = parseInt(document.body.scrollHeight);
	if (tooltipCg == 0) {
		//if (parseInt(t_posX) < parseInt(t_posX_limit)) tStyle.left = t_posX - 20 + "px";
		//if (parseInt(t_posY) < parseInt(wHeight) - 400) tStyle.top = t_posY + 15 + "px";
		//else 
		//tStyle.bottom = parseInt(wHeight) + parseInt(t_posY) - 600 + "px";
		tStyle.left = t_posX + "px";
		tStyle.top = t_posY + 10 + "px";
	}
	else if (tooltipCg == 1) {
		if (parseInt(t_posX) < parseInt(t_posX_limit)) tStyle.left = t_posX - 200 + "px";
		if (parseInt(t_posY) < parseInt(t_posY_limit)) tStyle.top = t_posY + 5 + "px";
	}
	else if (tooltipCg == 2) {
		if (parseInt(t_posX) < parseInt(t_posX_limit)) tStyle.left = t_posX - 20 + "px";
		if (parseInt(t_posY) < parseInt(t_posY_limit)) tStyle.top = t_posY + 5 + "px";
	}
	else if (tooltipCg == 3) {
		if(parseInt(t_posX)+247 > parseInt(t_posX_limit)) tStyle.left = t_posX - 123 + "px";
		else tStyle.left = t_posX - 20 + "px";
//		if (parseInt(t_posX) < parseInt(t_posX_limit)) tStyle.left = t_posX - 20 + "px";
		if (parseInt(t_posY) < parseInt(t_posY_limit)) tStyle.top = t_posY + 5 + "px";
	}	
	else return;
}

// to show a tooltip box
function tooltipOn(msg) {
	if (typeof dic_TooltipBox != "object") return false;
	tooltipCg = 0;
	dic_TooltipBox.innerHTML = "";
	t_display = 1;
	var msgHtml = "";
	msgHtml += "<div class='tooltipTop'></div>";
	msgHtml += "<div class='tooltipMid'>"
	msgHtml += "	<div id='dic_TooltipMsg'>" + msg + "</div>";
	msgHtml += "</div>";
	msgHtml += "<div class='tooltipBot'></div>";
	dic_TooltipBox.innerHTML = msgHtml;
	tStyle.display = "block";
	tStyle.visibility = "visible";
}

// to hide a tooltip box
function tooltipOff() {
	if (typeof dic_TooltipBox != "object") return false;
	t_display = 0;
	dic_TooltipBox.innerHTML = "";
	tStyle.display = "none";
	tStyle.visibility = "hidden";
}

// to show a right tooltip box
function right_tooltipOn(msg) {
	if (typeof dic_TooltipBox != "object") return false;
	tooltipCg = 1;
	dic_TooltipBox.innerHTML = "";
	t_display = 1;
	var msgHtml = "";
	msgHtml += "<div class='r_tooltipTop'></div>";
	msgHtml += "<div class='r_tooltipMid'>"
	msgHtml += "	<div id='dic_TooltipMsg'>" + msg + "</div>";
	msgHtml += "</div>";
	msgHtml += "<div class='r_tooltipBot'></div>";
	dic_TooltipBox.innerHTML = msgHtml;
	tStyle.display = "block";
	tStyle.visibility = "visible";
}

// to hide a right tooltip box
function right_tooltipOff() {
	if (typeof dic_TooltipBox != "object") return false;
	t_display = 0;
	dic_TooltipBox.innerHTML = "";
	tStyle.display = "none";
	tStyle.visibility = "hidden";
}

// to show a content list tooltip box
function cont_tooltipOn(msg) {
	if (typeof dic_TooltipBox != "object") return false;
	tooltipCg = 2;
	dic_TooltipBox.innerHTML = "";
	t_display = 1;
	var msgHtml = "";
	msgHtml += "<div class='cont_tooltipTop'></div>";
	msgHtml += "<div class='cont_tooltipMid'>"
	msgHtml += "	<div id='dic_TooltipMsg'>" + msg + "</div>";
	msgHtml += "</div>";
	msgHtml += "<div class='cont_tooltipBot'></div>";
	dic_TooltipBox.innerHTML = msgHtml;
	tStyle.display = "block";
	tStyle.visibility = "visible";
}

// to hide a content list tooltip box
function cont_tooltipOff() {
	if (typeof dic_TooltipBox != "object") return false;
	t_display = 0;
	dic_TooltipBox.innerHTML = "";
	tStyle.display = "none";
	tStyle.visibility = "hidden";
}

// to show a tag list tooltip box
function tag_tooltipOn(msg) {
	if (typeof dic_TooltipBox != "object") return false;
	tooltipCg = 3;
	dic_TooltipBox.innerHTML = "";
	t_display = 1;
	var msgHtml = "";
	msgHtml += "<div class='tag_tooltipTop'></div>";
	msgHtml += "<div id='dic_TooltipMsg' class='tag_tooltipMid'>";
	msgHtml += msg;
	msgHtml += "</div>";
	msgHtml += "<div class='tag_tooltipBot'></div>";
	dic_TooltipBox.innerHTML = msgHtml;
	tStyle.display = "block";
	tStyle.visibility = "visible";

	if (document.addEventListner) document.getElementById("dic_TooltipMsg").addEventListener('mouseout',tag_tooltipOff,false);
	else if (document.attachEvent) document.getElementById("dic_TooltipMsg").attachEvent('onmouseout',tag_tooltipOff);
	else document.getElementById("dic_TooltipMsg").onmouseout = tag_tooltipOff;

	if (navigator.appName == "Microsoft Internet Explorer") {
		if ((parseInt(document.body.clientWidth) - 1004) / 2 > 0) {
			t_posX = t_posX - (parseInt((parseInt(document.body.clientWidth) - 1004) / 4));
		}
		tStyle.left = t_posX - 30 + "px";
		tStyle.top = t_posY + 160 + "px";
	}
	else {
		tStyle.left = t_posX - 20 + "px";
		tStyle.top = t_posY + 12 + "px";
	}
}

// to hide a tag list tooltip box
function tag_tooltipOff(e) {

	var msgdiv = document.getElementById("dic_TooltipMsg").getElementsByTagName("SPAN");
	var msglink = document.getElementById("dic_TooltipMsg").getElementsByTagName("A");
	var msgnum = 0;

	for (var i=0; i<msgdiv.length; i++) {
		// When IE6, 7
		if (e.toElement) {
			if (e.toElement == msgdiv[i] || e.toElement.id == "dic_TooltipMsg") msgnum++;
		}
		else if (e.fromElement) {
			if (e.fromElement == msgdiv[i]) eknum++;
		}
		// When Mozilla or etc...
		else if (e.relatedTarget && e.target) {
			if (e.relatedTarget == msgdiv[i] || e.relatedTarget.id == "dic_TooltipMsg") msgnum++;
		}
	}
	for (i=0; i<msglink.length; i++) {
		// When IE6, 7
		if (e.toElement) {
			if (e.toElement == msglink[i] || e.toElement.id == "dic_TooltipMsg") msgnum++;
		}
		else if (e.fromElement) {
			if (e.fromElement == msglink[i]) eknum++;
		}
		// When Mozilla or etc...
		else if (e.relatedTarget && e.target) {
			if (e.relatedTarget == msglink[i] || e.relatedTarget.id == "dic_TooltipMsg") msgnum++;
		}
	}

	if (msgnum == 0) {
		t_display = 0;
		dic_TooltipBox.innerHTML = "";
		tStyle.display = "none";
		tStyle.visibility = "hidden";
	}
}

// to show a right tooltip box
function trans_tooltipOn(msg) {
	if (typeof dic_TooltipBox != "object") return false;
	tooltipCg = 1;
	dic_TooltipBox.innerHTML = "";
	t_display = 1;
	var msgHtml = "";
	msgHtml += "<div class='r_tooltipTop'></div>";
	msgHtml += "<div class='r_tooltipMid'>"
	msgHtml += "	<div id='dic_TooltipMsg'>" + msg.replace("dcLstCgTrans", "\'dcLstCgTrans\'") + "</div>";
	msgHtml += "</div>";
	msgHtml += "<div class='r_tooltipBot'></div>";
	dic_TooltipBox.innerHTML = msgHtml;
	tStyle.display = "block";
	tStyle.visibility = "visible";
}

// to hide a right tooltip box
function trans_tooltipOff() {
	if (typeof dic_TooltipBox != "object") return false;
	t_display = 0;
	dic_TooltipBox.innerHTML = "";
	tStyle.display = "none";
	tStyle.visibility = "hidden";
}

var tooltipSrc = "";
var tooltipDoc;
var tooltipReq;
var chkload = 0;

// to show a chinese tooltip box
function han_tooltipOn(kwd) {
	if (chkload == 0 && dic_TooltipBox) {
		tooltipReq = null;
		dic_TooltipBox.innerHTML = "";
		chkload = 1;
		var arrKwd = kwd.split("|");
		var numericKwd = "";
		for (var i=1; i<arrKwd.length; i++) {
			numericKwd += arrKwd[i];
		}
		
		var url = document.urlForm.HAN_TOOLTIP_URL.value;
		if (url || url != "") {
			tooltipSrc = url + "?chinese=" + kwd;
			
			loadTooltipReq();
			if (tooltipReq) {
				try {
					tooltipReq.onreadystatechange = function() {
						if(document.urlForm.LAYOUT_SMALL_WINDOW.value == "Y"){
							drawAjaxTooltip("3");							
						}else{
							drawAjaxTooltip("0");							
						}
					}
					tooltipReq.open("GET",tooltipSrc,true);
					tooltipReq.send(null);
				} catch(err) {
					drawAjaxTooltipError(err);
				}
			}
			else alert("Not available browser!");
		}
	}
}

function drawAjaxTooltipError(err) {
	alert(err);
}

function loadTooltipReq() {
	if (window.XMLHttpRequest) {
		tooltipReq = new XMLHttpRequest();
		if (tooltipReq.overrideMimeType) tooltipReq.overrideMimeType("text/xml");
	}
	else if (window.ActiveXObject) {
		tooltipReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function drawAjaxTooltip(_num) {
	var tgtObj;
	var schObj, schImg, schPage;
    if (tooltipReq.readyState == 4) {
        var httpStatus = tooltipReq.status;
        if (httpStatus == 200 && tooltipReq.responseXML != null) {
            tooltipDoc = tooltipReq.responseText;
	        if (typeof dic_TooltipBox != "object") return false;
			tooltipCg = _num;
			dic_TooltipBox.className = "hanja_tooltipbox";
			t_display = 1;
			var msgHtml = "";
			msgHtml += "<div class='tooltipTop'></div>";
			msgHtml += "<div class='tooltipMid'>"
			msgHtml += "	<div id='dic_TooltipMsg'>"  + tooltipDoc + "</div>";
			msgHtml += "</div>";
			msgHtml += "<div class='tooltipBot'></div>";
			dic_TooltipBox.innerHTML = msgHtml;
			tStyle.display = "block";
			tStyle.visibility = "visible";
			chkload = 0;
        }
    }
}

// to hide a chinese tooltip box
function han_tooltipOff() {
	chkload = 1;
	if (typeof dic_TooltipBox != "object") return false;
	t_display = 0;
	dic_TooltipBox.innerHTML = "";
	tStyle.display = "none";
	tStyle.visibility = "hidden";
	chkload = 0;
}
