var nMyTongId = 0;
var nMyBoxId = 0;
var nMyPostId = 0;
var nMyGuestbookId = 0;
var strViewMode = 'mytong';
var strSearchKeyword = '';
var strOwnerNickName = '';
var strVisitorNateId = '';
var nBoxCount = 0;
var nMyFirstBoxId = 0;
var nTickerBoxId = 0;
var bOwner = false;

function PageInit()
{
	//frameLeftVisitorList.location.href = '/user/main.do?myTongID=' + nMyTongId;
	frameLeftBoxList.location.href = '/mytong/box.do?action=menu&myTongID=' + nMyTongId;
	
	if (strViewMode == 'mytong')
		GoToTong(false, nMyTongId);
	else if (strViewMode == 'box')
		GoToBox(false, nMyTongId, nMyBoxId);
	else if (strViewMode == 'post')
		GoToPost(false, nMyTongId, nMyBoxId, nMyPostId);
	else if (strViewMode == 'puzzle')
		GoToAdmin(3);
	else if (strViewMode == 'recommand')
		GoToBox(false, nMyTongId, nMyBoxId);
	else if (strViewMode == 'linkmap')
		GoToLinkmap(nMyTongId, nMyBoxId);
	else if (strViewMode == 'comment')
		GoToComment(nMyTongId, nMyBoxId, nMyPostId);
	else if (strViewMode == 'weave')
		GoToWeave(nMyTongId, nMyBoxId, nMyPostId);
	else if (strViewMode == 'sympathy')
		GoToSympathy(nMyTongId, nMyBoxId, nMyPostId);
	else if (strViewMode == 'rss')
		GoToBox(false, nMyTongId, nMyBoxId);
	else if (strViewMode == 'search')
		GoToSearch();
	else if (strViewMode == 'guest')
		GoToGuestbook(nMyGuestbookId);
	else if (strViewMode == 'list' || strViewMode == 'clist' || strViewMode == 'plist')
		GoToBox(false, nMyTongId, nMyBoxId, strViewMode);
	else if (strViewMode == 'admin_invite')
		GoToAdmin(4);
	else if (strViewMode == 'write_guest')
		GoToWrite();
	else
		GoToTong(false, nMyTongId);
}
function RefreshBoxList()
{
	var objInterestList = document.getElementById('myInterestList')

	var InterestList = objInterestList.childNodes;
	var nRowCount = 	InterestList.length;
	
	for (var i = 0; i < nRowCount; i++)
	{
		this_node = InterestList[0];
		removed_node = objInterestList.removeChild(this_node);	
	}			
	nBoxCount = 0;
	nMyFirstBoxId = 0;
	
	frameLeftBoxList.location.href = '/mytong/box.do?action=menu&myTongID=' + nMyTongId;
}
function RefreshVisitorList()
{
	var objVisitor = document.getElementById("ulvisiotr");
	var visitorList = objVisitor.childNodes;
	var nRowCount = 	visitorList.length;
	
	for (var i = 0; i < nRowCount; i++)
	{
		this_node = visitorList[0];
		removed_node = objVisitor.removeChild(this_node);	
	}		
	
	document.getElementById('visitor').style.display = "";	
	document.getElementById('visitormore').style.display = "none";
	document.getElementById('visitor_nothing').style.display = "";		
		
/*	if (nMyBoxId)
		frameLeftVisitorList.location.href = '/user/visitList.do?action=box&myTongID=' + nMyTongId + '&boxID=' + nMyBoxId;
	else */
		frameLeftVisitorList.location.href = '/user/main.do?myTongID=' + nMyTongId;
		
}
function GoToWrite()
{
	strViewMode = 'box';
	ShowTicker(true);
	RefreshVisitorList();
	frameMain.location.href = '/boxitem/jini.do?action=boxDesc&_tongID=' + nMyTongId + '&_boxID=' + nMyBoxId + '&viewMode=write';
}
function GoToAdmin(nType)
{
	nMyBoxId = (nType == 1 ? nMyBoxId : 0);
	ShowTicker(nType == 1);
	RefreshVisitorList();

	switch (nType)
	{
		case 0:
    		frameMain.location.href = '/mytong/mytong.do?action=form&myTongID=' + nMyTongId;
    		break;
		case 1:
			frameMain.location.href = '/mytong/box.do?action=manageform&myTongID=' + nMyTongId + '&boxID=' + nMyBoxId;
			break;
		case 2:
			frameMain.location.href = '/mytong/box.do?action=manageform&myTongID=' + nMyTongId;
			break;
		case 3:
			frameMain.location.href = '/mytong/puzzle.do?action=form&myTongID=' + nMyTongId;
			break;
		case 4:
			frameMain.location.href = '/friend/inviteList.do?myTongID=' + nMyTongId;
			break;
		case 5:
			frameMain.location.href = '/mytong/statistics.do?action=default_statistics&myTongID=' + nMyTongId;
			break;	
		default:
			break;
	}
}
function GoToTong(bTopNavigate, nTongId)
{
	if (bTopNavigate)
	{
		self.location.href = '/mytong/mytong.do?action=main&myTongID=' + nTongId;
		return;
	}
	
	nMyTongId = nTongId;
	strViewMode = 'mytong';
	nMyBoxId = 0;
	ShowTicker(false);
	RefreshVisitorList();
	frameMain.location.href = '/mytong/mytong.do?action=body&myTongID=' + nTongId;
}
function GoToBox(bTopNavigate, nTongId, nBoxId, strMode)
{
	if (!strMode || strMode == '')
		strMode = 'box';
	
	if (bTopNavigate)
	{
		self.location.href = '/mytong/mytong.do?action=' + (strMode == 'box' ? 'main' : 'redirect') + '&myTongID=' + nTongId + '&boxID=' + nBoxId + '&viewMode=' + strMode;
		return;
	}
	
	nMyTongId = nTongId;
	nMyBoxId = nBoxId;
	strViewMode = 'box';
	nMyBoxId = nBoxId;
	ShowTicker(true);
	RefreshVisitorList();
	frameMain.location.href = '/boxitem/jini.do?action=boxDesc&_tongID=' + nTongId + '&_boxID=' + nBoxId + '&viewMode=' + strMode;
}
function GoToPost(bTopNavigate, nTongId, nBoxId, nPostId)
{
	if (bTopNavigate)
	{
		self.location.href = '/mytong/mytong.do?action=main&myTongID=' + nTongId + '&boxID=' + nBoxId + '&postID=' + nPostId + '&viewMode=post';
		return;
	}
	
	nMyTongId = nTongId;
	nMyBoxId = nBoxId;
	strViewMode = 'post';
	nMyBoxId = nBoxId;
	ShowTicker(true);
	RefreshVisitorList();
	frameMain.location.href = '/boxitem/jini.do?action=boxDesc&_tongID=' + nTongId + '&_boxID=' + nBoxId + '&_boxItemID=' + nPostId + '&viewMode=post';
}
function GoToSearch()
{
	strViewMode = 'mytong';
	nMyBoxId = 0;
	ShowTicker(false);
	RefreshVisitorList();
	frameMain.location.href = '/search/search.do?action=searchintong&myTongID=' + nMyTongId + '&keyword=' + strSearchKeyword;
}
function GoToLinkmap(nTongId, nBoxId, bPostAll)
{
	strViewMode = 'box';
	nMyBoxId = nBoxId;
	ShowTicker(true);
	RefreshVisitorList();
	frameMain.location.href = '/boxitem/jini.do?action=boxDesc&_tongID=' + nTongId + '&_boxID=' + nBoxId + '&viewMode=' + (bPostAll ? 'linkmap_postall' : 'linkmap');
}
function GoToComment(nTongId, nBoxId, nPostId)
{
	strViewMode = 'post';
	nMyBoxId = nBoxId;
	ShowTicker(true);
	RefreshVisitorList();
	frameMain.location.href = '/boxitem/jini.do?action=boxDesc&_tongID=' + nTongId + '&_boxID=' + nBoxId + '&_boxItemID=' + nPostId + '&viewMode=comment';
}
function GoToWeave(nTongId, nBoxId, nPostId)
{
	strViewMode = 'post';
	nMyBoxId = nBoxId;
	ShowTicker(true);
	RefreshVisitorList();
	frameMain.location.href = '/boxitem/jini.do?action=boxDesc&_tongID=' + nTongId + '&_boxID=' + nBoxId + '&_boxItemID=' + nPostId + '&viewMode=weave';
}
function GoToSympathy(nTongId, nBoxId, nPostId)
{
	strViewMode = 'post';
	nMyBoxId = nBoxId;
	ShowTicker(true);
	RefreshVisitorList();
	frameMain.location.href = '/boxitem/jini.do?action=boxDesc&_tongID=' + nTongId + '&_boxID=' + nBoxId + '&_boxItemID=' + nPostId + '&viewMode=sympathy';
}
function GoToGuestbook(nGuestbookId)
{
	strViewMode = 'mytong';
	nMyBoxId = 0;
	ShowTicker(false);
	RefreshVisitorList();
	frameMain.location.href = '/mytong/mytong.do?action=body&myTongID=' + nMyTongId + '&guestbookid=' + nGuestbookId + '&viewMode=guestbook';
}
function TickerLoaded()
{
	if (document.getElementById('tickerContent').style.display == "")
		ShowTicker(true);
}
function ShowTicker(bShow)
{
	//document.getElementById('tdTitle').width = (bShow ? 529 : 882);
	//document.getElementById('tdTickerMargin').style.display = (bShow ? "" : "none");
	
	document.getElementById('tickerContent').style.display = (bShow ? "" : "none");
	document.getElementById('count').style.display = (bShow ? "none" : "");

	
	if (nMyBoxId && nTickerBoxId != nMyBoxId)
	{
		var nOldTickerBoxId = nTickerBoxId;
		nTickerBoxId = nMyBoxId;
		
		try
		{
			var objTicker = document.getElementById((bIE ? 'object' : 'embed') + '_ticker_interface_new');
			objTicker.SetVariable("/:tID", nMyTongId);
			objTicker.SetVariable("/:bID", nTickerBoxId);
			objTicker.TCallFrame("/", "3");
		}
		catch (e)
		{
			bShow = false;
			nTickerBoxId = nOldTickerBoxId;
		}
	}
}
function embed_ticker_interface_DoFSCommand(command, args)
{
	// ok we may just return safely
	return;
}

function AddBoxToLeftBoxList(nBoxId, nLevelIndex, bNeighbor, strName, bNew, bSameCategory, bHidden, sColor, bHasPermission, bAdultOnly)
{
	if (nBoxId == -9) // category line
	{
		AddCategoryToLeftBoxList(strName, sColor);
		return;
	}
	
	if (nMyFirstBoxId == 0)
		nMyFirstBoxId = nBoxId;
	
	// icon_new			12 ( 8px + space 4px)
	// level			19 ( 15px + space 4px)
	// hidden			14 ( 10px + space 4px)
	// adultonly		16 ( 12px + space 4px)

	var nAvailPixelWidth = 145; // 170 - level
	strName = strName.split("&quot;").join("\"").split("&lt;").join("<");

	if (bNew)
		nAvailPixelWidth = nAvailPixelWidth - 12;
	if (bHidden)
		nAvailPixelWidth = nAvailPixelWidth - 15;
	if (bAdultOnly)
		nAvailPixelWidth = nAvailPixelWidth - 16;
	
	var strDrawName = GetPixelSizedText(strName, nAvailPixelWidth, false);
	
	var strHtml = '<DIV class="list_sub"><img src=/images/mytong/tonglevel' + nLevelIndex + '.gif align=absmiddle class=img_bottom03>';
	strHtml = strHtml + ' <a href=javascript:GoToBox(false,' + nMyTongId + ',' + nBoxId + ')>';
	strHtml = strHtml + '<font color=' + sColor + '>' + strDrawName + '</font></a>';
	
	if (bHidden == 1)
		strHtml = strHtml + ' <img src=/images/mytong/icon_hidden.gif align=absmiddle class=img_bottom02>';
		
	if (bHidden == 2 && bHasPermission)
		strHtml = strHtml + ' <img src=/images/mytong/icon_key.gif align=absmiddle class=img_bottom02>';
		
	if (bNew)
		strHtml = strHtml + ' <img src=/images/mytong/icon_new.gif align=absmiddle class=img_bottom02>';
		
	if (bAdultOnly)
		strHtml = strHtml + ' <img src=/images/mytong/icon_19.gif align=absmiddle class=img_bottom02>';
			
	strHtml = strHtml + "</div>";
	
	MyInsertAdjacentHTML("BeforeEnd", strHtml, document.getElementById('myInterestList'));
	
	
	nBoxCount++;
}
function AddCategoryToLeftBoxList(strName, sColor)
{
	var nAvailPixelWidth = 170;
	strName = strName.split("&quot;").join("\"").split("&lt;").join("<");
  
	var strDrawName = GetPixelSizedText(strName, nAvailPixelWidth, true);	
	var strHtml = '<div class="list_tit"><span style=color:' + sColor + '>' + strDrawName + '</span></div>';
	
	MyInsertAdjacentHTML("BeforeEnd", strHtml, document.getElementById('myInterestList'));	
}

function AddTongToLeftVisitorList(bIsMytong, strVisitorId, strNateId, strTongTitle, nLevelIndex, strCategoryName, nBoxId)
{
	document.getElementById('visitor_nothing').style.display = "none";	
	document.getElementById('visitor_list').style.display = "";	

	strTongTitle = strTongTitle.split("&quot;").join("\"").split("&lt;").join("<");
	strTongTitle = GetPixelSizedText(strTongTitle, (strVisitorNateId == strNateId ? 148 : 164), false);

	var  strHtml = '<li><a href=/' + strNateId + ' class=nick>' + strTongTitle + '</a>';
	if (strVisitorNateId == strNateId || bIsMytong == "true")
		strHtml = strHtml + ' <a href="javascript:DeleteVisitorHistory(' + strVisitorId + ')"><img src=/images/mytong/icon_delete.gif align=absmiddle style=margin-bottom:2></a>';
	strHtml = strHtml+ '</li>'

	MyInsertAdjacentHTML("BeforeEnd", strHtml, document.getElementById('ulvisiotr'));
	
	var visitorList = document.getElementById("ulvisiotr").childNodes;
	var nRowCount = visitorList.length;
	var bMore = false;
	
	for (var i = 5; i < nRowCount; i++)
	{
		visitorList[i].style.display = "none";
		document.getElementById('imgVisitorListMore').src = '/images/library/button/btn_txt_more2.gif';
		document.getElementById('visitormore').style.display = "";
	}
}
function ToggleLeftVisitorList()
{
var visitorList = document.getElementById("ulvisiotr").childNodes;
	var nRowCount = 	visitorList.length;
	var bMore = false;

	// È®ÀåµÈ»óÅÂÀÌ¸é...
	if(visitorList[5].style.display == "")
	{
		bMore = true;
	}
			
	for (var i = 5; i < nRowCount; i++)
	{
		if(bMore == true) visitorList[i].style.display = "none";
		else visitorList[i].style.display = "";
	}
	document.getElementById('imgVisitorListMore').src = '/images/library/button/btn_txt_' + (bMore == "" ? "close" : "more2") + '.gif';

}
function DeleteVisitorHistory(strVisitorId)
{
	var objVisitor = document.getElementById("ulvisiotr");
	var visitorList = objVisitor.childNodes;
	var nRowCount = 	visitorList.length;
	
	for (var i = 0; i < nRowCount; i++)
	{
		this_node = visitorList[0];
		removed_node = objVisitor.removeChild(this_node);	
	}		
	document.getElementById('visitormore').style.display = "none";	
	frameLeftVisitorList.location.href = '/user/visitList.do?action=del&myTongID=' + nMyTongId + '&targetID=' + strVisitorId;	
}
function UpdateVisitorCount(nTodayVisitor, nTotalVisitor)
{
	document.getElementById('spnTodayVisitor').innerHTML = nTodayVisitor;
	document.getElementById('spnTotalVisitor').innerHTML = nTotalVisitor;
}
function ShowInfoPostList(strListName)
{
	var styleNeighborBox = document.getElementById('tblNeighborBox').style;
	var styleNeighborBlog = document.getElementById('tblNeighborBlog').style;
	var styleRecommendBox = document.getElementById('tblRecommendBox').style;
	
	if (strListName == "NeighborBox")
	{
		styleNeighborBox.display = "";
		styleNeighborBlog.display = "none";
		styleRecommendBox.display = "none";
		objDefaultImg = document.getElementById('imgNeighborBox');
	}
	else if (strListName == "NeighborBlog")
	{
		styleNeighborBox.display = "none";
		styleNeighborBlog.display = "";
		styleRecommendBox.display = "none";
		objDefaultImg = document.getElementById('imgNeighborBlog');
	}
	else if (strListName == "RecommendBox")
	{
		styleNeighborBox.display = "none";
		styleNeighborBlog.display = "none";
		styleRecommendBox.display = "";
		objDefaultImg = document.getElementById('imgRecommendBox');
	}
}
function NavigateBody(strPath)
{
	frameMainBody.location.href = strPath;
}
function PopupTongSummary(strType)
{
	if (strType == 'neighbor')
		window.open('/boxitem/jini.do?action=popupConcerned&value(sort)=0&_boxID=0&_tongID=' + nMyTongId, 'popupConcerned', 'scrollbars=yes,status=yes,width=440,height=476,left=' + (window.screen.availWidth/2 - 210) + ',top=' + (window.screen.availHeight/2 - 238));
	else if (strType == 'scrap')
		window.open('/boxitem/jini.do?action=popupScrapped&value(sort)=0&_boxID=0&_tongID=' + nMyTongId, 'popupScrapped', 'status=yes,width=500,height=454,left=' + (window.screen.availWidth/2 - 250) + ',top=' + (window.screen.availHeight/2 - 227));
}

var strMapLayer = '<div id=divMapLayer style=position:absolute;top:193;left:916;>';
strMapLayer = strMapLayer + '<table border=0 cellspacing=0 cellpadding=0>';
strMapLayer = strMapLayer + '<tr id=trWaveLayer style=display:none><td>';
strMapLayer = strMapLayer + '<a href=javascript:PopupBoxLinkMap(nMyTongId,nMyBoxId,nMyTongId)><img src=/images/mytong/main/icon_wave.gif></a><br><br>';
strMapLayer = strMapLayer + '</td></tr>';
strMapLayer = strMapLayer + '<tr><td id=tdMapLayer>';
strMapLayer = strMapLayer + '</td></tr></table></div>';

var objMapLayer = null;
function UseMapLayer()
{
return;
	MyInsertAdjacentHTML("BeforeEnd", strMapLayer, document.body);
	objMapLayer = document.getElementById('divMapLayer');
}
function AddEventImgToMapLayer(strImg, strLink, strTarget)
{
//	if (!objMapLayer)
//		return;
	var strHtml = '';
	if (strLink != '')
		strHtml = '<br><a href="' + strLink + '" target="' + strTarget + '"><img src="' + strImg + '"></a><br>';
	else
		strHtml = '<br><img src="' + strImg + '"><br>';
	MyInsertAdjacentHTML("BeforeEnd", strHtml, document.getElementById('event_navi'));
}