document.domain = "nownuri.net";
var _nowict_dialogs = new Array();

document.write("<style type='text/css'>");
document.write(".ictmenu_on  { background-Color:#E5E5E5; padding-top:2px; padding-left:4px; cursor:pointer; }");
document.write(".ictmenu_off { background-Color:#FFFFFF; padding-top:2px; padding-left:4px; }");
document.write("</style>");

function ICTViewRow(idx, name, text, onclickEvent)
{
	this.idx = idx;
	this.name = name;
	this.text = text;
	this.onclickEvent = onclickEvent;
	this.renderRow = renderRow;
	
	this.isVisible = true;
}

function renderRow()
{
	if (!this.isVisible) {
		return "";
	}
	
	var str  = "<tr height='18'><td id='ictViewRow_"+this.name+"'";
		str += " class='ictmenu_off' onmouseover=this.className='ictmenu_on' onmouseout=this.className='ictmenu_off'";
		str += " onclick=\"hideICTView();clickAreaCheck=true;"+this.onclickEvent+";\">";
		str += this.text + "</td></tr>";

	return str;
}

function ICTView(curObj, userid)
{
	this.targetObj  = "ictmenu";
	this.curObj     = curObj;
	this.userid     = userid;
	this.showLayer  = showLayer;
	this.makeICTMenu = makeICTMenu;
	this.tails      = new Array();
	this.insertTail = insertTail;
	this.getRow     = getRow;
	this.hideRow    = hideRow;		

	this.insertTail("pf",      "ÇÁ·ÎÇÊ º¸±â", "_nowict_Pf('"+this.userid+"')");
	this.insertTail("onechat", "1:1 ´ëÈ­",	  "_nowict_OneChat('"+this.userid+"')");
	this.insertTail("to",      "ÂÊÁö º¸³»±â", "_nowict_To('"+this.userid+"')");
	this.insertTail("wmail",   "ÆíÁö º¸³»±â", "_nowict_WMail('"+this.userid+"')");
}

function showLayer()
{
	var oICTViewLayer = document.getElementById(this.targetObj);
	var oBody = document.body;

	clickAreaCheck = true;
	
	if (oICTViewLayer == null) {
		oICTViewLayer = document.createElement("DIV");
		oICTViewLayer.id = this.targetObj;
		oICTViewLayer.style.position = 'absolute';
		oICTViewLayer.style.zIndex   = '99999999';
		oBody.appendChild(oICTViewLayer);
	}

	oICTViewLayer.innerHTML = this.makeICTMenu();
	
	if (getAbsoluteTop(this.curObj) + this.curObj.offsetHeight + oICTViewLayer.scrollHeight + 100 > oBody.scrollHeight) {
		oICTViewLayer.style.top = getAbsoluteTop(this.curObj) - oICTViewLayer.scrollHeight;
	} else {
		oICTViewLayer.style.top = getAbsoluteTop(this.curObj) + this.curObj.offsetHeight;
	}

	oICTViewLayer.style.left = getAbsoluteLeft(this.curObj) - this.curObj.offsetWidth;

	divDisplay(this.targetObj, 'block');

	// ÀÌ¿ëÀÚ »óÅÂ Ãâ·Â
	// Å©·Î½º µµ¸ÞÀÎ/Æ÷Æ®´Â Á¢±ÙÇÒ ¼ö ¾ø´Ù.
	// sendRequest(displayStatus, {id:this.userid}, 'GET', 'http://client.nownuri.net:8080/app/checkUserStatus2.cgi', true, true);
}

function showICT(curObj, userid)
{
	var ictView = new ICTView(curObj, userid);
	ictView.showLayer();
}

function displayStatus(oj)
{
	var tmp, tmpstatus;
	tmpstatus = parseInt(oj.responseText);

	switch (tmpstatus)
	{
		case 0:
		case 1:
		case 3:
			tmp = "<font color=#CC0055><b>On</b></font>";
			break;
		default:
			tmp = "<font color=#006688><b>Off</b></font>";
			break;
	}
	
	document.getElementById("USERMODE").innerHTML = tmp;
}

function makeICTMenu()
{
	var str = "<table width='100' cellpadding='0' cellspacing='0' style='border:2px solid #D0D0D0' bgcolor='#FFFFFF'>";

	// ÀÌ¿ëÀÚ »óÅÂ
	str += "<tr height='18'><td style='padding-top:2px; padding-left:4px;'><b>"+this.userid+"</b>&nbsp;<span id=USERMODE></td></tr>";

	var j = 0;
	for (j=0; j < this.tails.length; j++) {
		str += this.tails[j].renderRow();
	}
	
	str += "</table>";
	return str;
}

function getRow(name)
{
	var i = 0;
	var row = null;

	for (i=0; i<this.tails.length; ++i) {
		row = this.tails[i];
		if (row.name == name) return row;
	}

	return row;
}

function hideRow(name)
{
	var row = this.getRow(name);

	if (row != null) {
		row.isVisible = false;
	}
}

function insertTail(name, text, evt)
{
	var idx = this.tails.length;
	var row = new ICTViewRow(idx, name, text, evt);
	this.tails[idx] = row;
	return row;
}

function getAbsoluteTop(oNode)
{
	var oCurrentNode = oNode;
	var iTop = 0;

	while (oCurrentNode.tagName != "BODY") {
		iTop += oCurrentNode.offsetTop - oCurrentNode.scrollTop;
		oCurrentNode = oCurrentNode.offsetParent;
	}

	return iTop;
}

function getAbsoluteLeft(oNode)
{
	var oCurrentNode = oNode;
	var iLeft = oCurrentNode.offsetWidth;

	while (oCurrentNode.tagName != "BODY") {
		iLeft += oCurrentNode.offsetLeft;
		oCurrentNode = oCurrentNode.offsetParent;
	}

	return iLeft;
}

function divDisplay(id, act)
{
	document.getElementById(id).style.display = act;
}

function hideICTView()
{
	if (document.getElementById("ictmenu")) {
		divDisplay("ictmenu", "none");
	}
}

function _nowict_Pf(id)
{
    var url      = "http://client.nownuri.net:8088/profile2/profile.cgi?userid=" + id;
    var name     = "pf" + id;
    var pfTop    = 10;	//screen.availHeight /2 - 160;
    var pfLeft   = 10;	//screen.availWidth  /2 - 300;
    var features = "width=500, height=345, status=1, toolbar=0, top=" + pfTop + ", left=" + pfLeft;

    _nowict_openDialog(name, url, features);
    return;
}

function _nowict_To(id)
{
    var url = "http://client.nownuri.net:9033/app/sendtotpl.cgi?id=" + id;
    var name = "cmdto" + id;
    var features = "width=330, height=210, status=1, toolbar=0";

	_nowict_openDialog(name, url, features);
    return;
}

function _nowict_WMail(id)
{
    var url = "http://client.nownuri.net:9510/app/ict_wmail.cgi?To=" + id;
    var name = "wmail" + id.split(",").join("w");
    _nowict_openDialog(name, url, "left=10, top=10, width=650, height=600, resizable=1, scrollbars=1, status=1, toolbar=0");
}

function _nowict_OneChat(id)
{
    var url = "http://client.nownuri.net:9033/app/msg/oneto.cgi?uid=" + id;
    var name = "cmdchat" + id;

	_nowict_openDialog(name, url, "width=500, height=380, status=1, toolbar=0, resizable=1");
	_nowict_dialogs[name].focus();
    return;
}

function _nowict_openDialog(name, svcURL, features)
{
    if (_nowict_dialogs[name]) {
        // ±âÁ¸ÀÇ window°¡ ÀÖ´Ù. closeµÇ¾ú´ÂÁö °Ë»çÇÏ³®.
        if (_nowict_dialogs[name].closed) {
            // closeµÇ¾úÀ¸¸é »õ·ÎÀÌ »ý¼ºÇÑ´Ù.
            _nowict_dialogs[name] = _nowict_createWindow(svcURL, name, features);
        } else {
            // ÀÌ¹Ì ¿­·Á ÀÖ´Â °ÍÀÌ¶ó¸é URL·Î »õ·ÎÀÌ ¹Ù²Û´Ù.
            //dialogs[name].location.href = svcURL;
            _nowict_dialogs[name].focus();
        }
    } else {
        _nowict_dialogs[name] = _nowict_createWindow(svcURL, name, features);
    }

    if (_nowict_dialogs[name].opener == null) {
        _nowict_dialogs[name].opener = window;
	}
}

function _nowict_createWindow(url, name, features)
{
    // ÇöÀç browserÀÇ Áß¾Ó¿¡ À§Ä¡ ½ÃÅ²´Ù.
    //var features = arguments[2] == null ? "" : arguments[2];
    var left = (screen.width/2) - 200;
    var top = (screen.height/2) - 200;

    if (features != "" && features.indexOf("left") < 0) {
        features += ", left=" + left;
        features += ", top=" + top;
    }
    return window.open(url, name, features);
}

var clickAreaCheck = false;
document.onclick = function()
{
	if (event.srcElement.className == "nowict") {
		showICT(event.srcElement, event.srcElement.nowid);
	}

	if (!clickAreaCheck) {
		hideICTView();
	} else {
		clickAreaCheck = false;
	}
}