var g_currentURL = "";
var g_aryTag = new Array();
// ºÏ¸¶Å© ÅÂ±× Ãß°¡
function fnAddTag(objTag, bAlert)
{
	iCurrentNum = objTag.curNum;
	curBookmarkID = objTag.curBookmarkID;
		
	var strNewTag = fnTrimSpace(objTag.value);
	if(strNewTag == "")
	{
		if(bAlert) 
		{
			alert("ÅÂ±×¸¦ ÀÔ·ÂÇÏ¼¼¿ä.");
			objTag.focus();
		}
		return;
	}
	
	// ÀÌ¹Ì Á¸ÀçÇÏ´ÂÁö Ã¼Å©
	if(!ValidateTagName(g_aryTag[iCurrentNum],strNewTag))
	{
		objTag.value = "";
		objTag.focus();
		return;
	 }

	Request.sendPOST("/bookmark.do",  "action=addTag&_themeID=" + sThemeID + "&_bookmarkID=" + curBookmarkID + "&_tag=" + TongEncodeURIComponent(strNewTag) ,cbBookmarkTag, iCurrentNum);	
	HideFrame();
}

// ºÏ¸¶Å© ÅÂ±× »èÁ¦
function fnDelBookmarkTag(sTagName, iBookmarkID, iCurrentNum)
{ 
	if(!fnChkLogin()) return false;
	alert(iBookmarkID + "/" + sTagName + "/" + iCurrentNum);
	g_objTextSearch = document.getElementById("idTextSearch" + iCurrentNum);
	Request.sendPOST("/bookmark.do", "action=removeTag&_themeID=" + sThemeID + "&_bookmarkID=" + iBookmarkID + "&_tag=" + TongEncodeURIComponent(sTagName), cbBookmarkTag, iCurrentNum);
}

// Aajx CallBack of ºÏ¸¶Å© ÅÂ±× - CONTENT °øÅë
function cbBookmarkTag(httpRequest, iCurrentNum)
{
	eval("var JSONObjectData=" + httpRequest.responseText);

	try {
		if (JSONObjectData.code == 'success') 
		{
			g_aryTag[iCurrentNum]= null;
			g_aryTag[iCurrentNum] = new Array();
			
			var sHTML ='';
			for (var i = 0 ; i < JSONObjectData.tags.length ; i++) 
			{	
				Add_aryTagList(g_aryTag[iCurrentNum],JSONObjectData.tags[i].tagName);
				var sTagTemp = JSONObjectData.tags[i].tagName;			
				sTagTemp = sTagTemp.split("\"").join("&quot;").split("<").join("&lt;");			
				sTagTemp = sTagTemp.split("\\").join("\\\\").split("\'").join("\\\'").split("&lt;").join("<").split("&#39;").join("'");
					
				if(i != 0) sHTML += " , ";
				sHTML += "<a href='#' class='t_style3'>" + sTagTemp + "</a>";
				if(sVisitorID == JSONObjectData.tags[i].ownerID) sHTML += " <img src='/images/themetong/btn_tagdelete.gif' onclick=\"fnDelBookmarkTag('" + sTagTemp + "'," + Number(JSONObjectData.bookmarkID) + "," + iCurrentNum + ")\" align='' border='0' />"
			}

			document.getElementById("taglist" + iCurrentNum ).innerHTML = sHTML + " ";
			g_objTextSearch.value = "";
			g_objTextSearch.focus();
			HideFrame();

		}
		else {
			alert("Fail");
		}
	} 
	catch (e) {
		alert(e);
	}
}

// ³ªµµ¿¬°áÇÏ±â Å¬¸¯ - CONTENT °øÅë 
function fnViewJoinMenu(sType)
{
	if(!fnChkLogin()) return false;
	
	var oDiv = document.getElementsByTagName("div")
	
	for(var i=0; i < oDiv.length; i++)
	{
		if(oDiv[i].getAttribute("name") == "JoinMenuDiv" && oDiv[i].id != (sType + "JoinMenuDiv"))
		{
			oDiv[i].style.display = "none";
		}
	} 

	chgViewMode(sType + "JoinMenuDiv");
}

// ³»Åë¿¡¼­ºÒ·¯¿À±â - CONTENT °øÅë 
function fnJoinMyTong(sType, sParam)
{
	if(!fnChkLogin()) return false;
	
	var sTempParam;
	if(typeof sParam == 'undefined')
	{
		sTempParam = "";
	} else {
		sTempParam = sParam;
	}
	
	switch(sType){
		case 'post':
			window.open(sWebServerURL + '/boxitem/post.do?action=postCallPopupThemeForm&_tongID=' + sVisitorID + '&_themeID=' + sThemeID + sTempParam,'callPost','scrollbars=no,status=yes,width=439,height=580,left=' + (window.screen.availWidth/2 - 340) + ',top=' + (window.screen.availHeight/2 - 385));
			break;
		case 'photo':
			window.open(sWebServerURL + '/library/photo.do?action=photoCallPopupThemeForm&_tongID=' + sVisitorID + '&_themeID=' + sThemeID + sTempParam,'callMmedia','scrollbars=no,status=yes,width=439,height=620,left=' + (window.screen.availWidth/2 - 340) + ',top=' + (window.screen.availHeight/2 - 385));
			break;
		case 'mmedia':
			window.open(sWebServerURL + '/library/mmedia.do?action=mmediaCallPopupThemeForm&_tongID=' + sVisitorID + '&_themeID=' + sThemeID + sTempParam,'callMmedia','scrollbars=no,status=yes,width=439,height=620,left=' + (window.screen.availWidth/2 - 340) + ',top=' + (window.screen.availHeight/2 - 385));
			break;
		case 'box':
			window.open(sWebServerURL + '/boxitem/jini.do?action=boxCallPopupThemeForm&_tongID=' + sVisitorID + '&_themeID=' + sThemeID + sTempParam,'callPost','scrollbars=no,status=yes,width=439,height=580,left=' + (window.screen.availWidth/2 - 340) + ',top=' + (window.screen.availHeight/2 - 385));
			break;
		case 'review':
			window.open(sWebServerURL + '/library/review.do?action=reviewCallPopupThemeForm&_tongID=' + sVisitorID + '&_themeID=' + sThemeID + sTempParam,'callMmedia','scrollbars=no,status=yes,width=439,height=615,left=' + (window.screen.availWidth/2 - 340) + ',top=' + (window.screen.availHeight/2 - 385));
			break;
	}
}

// Á÷Á¢¿Ã¸®±â - CONTENT °øÅë 
function fnRegMyTong(sType, sParam)
{
	if(!fnChkLogin()) return false;
	
	var _tag = TongEncodeURIComponent(sThemeTag);
	var sTempParam;
	if(typeof sParam == 'undefined')
	{
		sTempParam = "";
	} else {
		sTempParam = sParam;
	}
	
	switch(sType){
		case 'mmedia':
			window.open(sWebServerURL + '/themetong/mytheme/setMmedia.vm?_tongID=' + sVisitorID + '&_themeID=' + sThemeID + '&_themeTag=' + _tag + sTempParam,'addMmedia','status=yes,width=550, height=500,left=' + (window.screen.availWidth/2 - 340) + ',top=' + (window.screen.availHeight/2 - 385));
			break;
		case 'post':
			window.open(sWebServerURL + '/boxitem/post.do?action=createForm&_boxID=0&_tongID=' + sVisitorID + '&mode=theme&_themeID=' + sThemeID + '&_themeTag=' + _tag + sTempParam,'addPost','scrollbars=yes,status=yes,width=720,height=680,left=' + (window.screen.availWidth/2 - 340) + ',top=' + (window.screen.availHeight/2 - 385));
			break;
		case 'review':
			window.open(sWebServerURL + '/library/review.do?action=uploadSel&_tongID=' + sVisitorID + '&_themeID=' + sThemeID + '&_themeTag=' + _tag + sTempParam,'addReview','scrollbars=yes, status=yes,width=715, height=624,left=' + (window.screen.availWidth/2 - 340) + ',top=' + (window.screen.availHeight/2 - 385));
			break;
		case 'photo':
			window.open(sWebServerURL + '/library/photo.do?action=uploadForm&_tongID=' + sVisitorID + '&_themeID=' + sThemeID + '&_themeTag=' + _tag + '&_isThemeTong=y' + sTempParam,'addPhoto','scrollbars=yes, status=yes,width=705, height=700,left=' + (window.screen.availWidth/2 - 340) + ',top=' + (window.screen.availHeight/2 - 385));
			break;
		case 'bookmark':
			document.location.href = '/bookmark.do?action=form&_themeID=' + sThemeID;
			break;
	}	
}

// ´õº¸±â - CONTENT °øÅë
function fnViewList(sType)
{
	switch(sType){
		case 'post':
			document.location.href = "/mytheme.do?action=postList&_themeID=" + sThemeID;
			break;
		case 'photo':
			document.location.href = "/mytheme.do?action=photoList&_themeID=" + sThemeID;
			break;
		case 'mmedia':
			document.location.href = "/mytheme.do?action=mmediaList&_themeID=" + sThemeID;
			break;
		case 'box':
			document.location.href = "/mytheme.do?action=boxList&_themeID=" + sThemeID;
			break;
		case 'review':
			document.location.href = "/mytheme.do?action=reviewList&_themeID=" + sThemeID;
			break;
	}
}

// Å¸ÀÌÆ²¸ðµå º¯°æ - CONTENT °øÅë
function fnChgTitleMode(sType)
{
	// ex)	photoBeforeDiv - photoTitle
	//		photoAfterDiv - photoInputTitle
	
	var oStart = document.getElementById(sType+"BeforeDiv");
	var oEnd = document.getElementById(sType+"AfterDiv");
	
	if(oStart.style.display=="")	{
		chgViewMode(oStart);
		chgViewMode(oEnd);
		document.getElementById(sType + "InputTitle").value = oStart.getAttribute(sType + "Title");	
		
	} else {
		chgViewMode(oEnd);
		chgViewMode(oStart);
	}
	
	parent.ResizeFrame(self.name);
}

// Å¸ÀÌÆ² ¼öÁ¤ - CONTENT °øÅë
function fnChkTitle(sType,iThemeID)
{
	if(!fnChkLogin()) return false;
	
	var sTitle = document.getElementById(sType + "InputTitle").value;
	if( sTitle == "")
	{
		alert("Á¦¸ñÀ» ÀÔ·ÂÇÏ¼¼¿ä.");
		return;
	} else if(sTitle.length > 50) {
		alert("Á¦¸ñÀº 50ÀÚ±îÁö °¡´ÉÇÕ´Ï´Ù.");
		return;
	}
	
	Request.sendPOST("/mytheme.do", "action=ajaxTitleUpdate&_themeID=" + iThemeID + "&target=" + sType + "&title=" + TongEncodeURIComponent(sTitle), cbModifyTitle);
}

// Aajx CallBack of Å¸ÀÌÆ² ¼öÁ¤ - CONTENT °øÅë
function cbModifyTitle(httpRequest)
{
	var oStart,oEnd;
	
	try {
		eval("var JSONObjectData=" + httpRequest.responseText);
		
		var sType = JSONObjectData.target;
		oStart = document.getElementById(sType+"BeforeDiv");
		oEnd = document.getElementById(sType+"AfterDiv")
		
		if(JSONObjectData.result == true)
		{	
			setInnerText(oStart,JSONObjectData.title);
			oStart.setAttribute(sType + "Title", JSONObjectData.title);
		} else {
			alert("fail");
		}
		
		oStart.style.display = "";
		oEnd.style.display = "none";
	} catch (e) {
		alert(e.message);
	}	
}
var g_sThemeLinkHtml = "";
var g_sThemeSubSectionType = "";

function fnModifyRssLink(sType)
{
	if(typeof sType == 'undefined')
		g_sThemeSubSectionType  = "";
	else	
		g_sThemeSubSectionType = sType;
	document.getElementById("imgRSS").style.display = "";
	
}

function OpenXml()
{
	var strUrl = '/mytheme.do?action=rssxml&_themeID=' + sThemeID;
	if(g_sThemeSubSectionType != "")
		strUrl = '/mytheme.do?action=rssxml&_themeID=' + sThemeID + '&_rssType=' + g_sThemeSubSectionType;
		
	if (CopyToClipboard(sThemeWebServerURL + strUrl) && confirm('Å×¸¶Åë RSS ÁÖ¼Ò°¡ Å¬¸³º¸µå¿¡ º¹»çµÇ¾ú½À´Ï´Ù.\r\n¿øÇÏ½Ã´Â RSS ¸®´õ¿¡ ºÙ¿© ³ÖÀ¸¼¼¿ä.'))
		window.open(strUrl);
}


function fnModifyThemeLink(aLinkList, iType)
{	
	document.getElementById('imgRSS').style.display = "none";
	if(aLinkList.length == 0) fnChangeIndexLayout("typeSubmain");
	else fnChangeIndexLayout("typeSubSection");
	
	var oDiv = document.getElementById("areaThemeLink");
	var sHTML = "";
	
	for(i=0; i < aLinkList.length; i++)
	{
		if(i == (aLinkList.length-1))
		{
			sHTML += " &gt; " + aLinkList[i][0];
		} else {
			sHTML += " &gt; <a href='" + aLinkList[i][1] + "' target='contentFrm' class='t_style2'>" + aLinkList[i][0] +"</a>";
		}
	}

	if(typeof iType == 'undefined')
	{
		oDiv.innerHTML = g_sThemeLinkHtml + sHTML;
	} else {
		if(aLinkList.length == 0)
		{
			oDiv.innerHTML = g_sThemeLinkHtml + " &gt; ³»ÀÚ·á¸¸ º¸±â" + sHTML;
		} else {
			oDiv.innerHTML = g_sThemeLinkHtml + " &gt; <a href='/mytheme.do?action=content&_themeID=" + sThemeID + "&_onlyMyData=y' target='contentFrm' class='t_style2'>³»ÀÚ·á¸¸ º¸±â</a>" + sHTML;
		}
		
	}

}

function fnChkLogin()
{
	if (_GetCookieValue('GUID')) {	
		if (_GetCookieValue('SYS_USER_ID'))
			return true;
		else {
			PopupJoinTong('&need_toss=true&redirect_url=' + g_currentURL);
			return false;
		}
	} else {
		PopupLogin('&need_mytong=true&need_toss=true&redirect_url=' + g_currentURL);
		return false;
	}
}

function fnLogin(sWebServer)
{
	if (_GetCookieValue('GUID'))
	{	
		if (_GetCookieValue('SYS_USER_ID'))
		{
			if(parent == window)
			{
				document.location.href = g_currentURL;
			} else {
				parent.location.href = g_currentURL;
			}
		} else {
			if (sWebServer) {
				PopupJoinTong('&need_toss=true&redirect_url=' + g_currentURL, sWebServer);
			} else {
				PopupJoinTong('&need_toss=true&redirect_url=' + g_currentURL);
			}
		}
	} else
		PopupLogin('&need_mytong=true&need_toss=true&redirect_url=' + g_currentURL);
}

function chgViewMode(node)
{
	if(typeof node != "undefined")
	{
		if(node.nodeType == 1)
		{
			node.style.display = (node.style.display == "") ? "none" : "";
		} else {
			document.getElementById(node).style.display = (document.getElementById(node).style.display == "") ? "none" : "";
		}
	}	
}

function setInnerText(oDiv, sTitle)
{	
	if (document.all)		//IE
	{
		oDiv.innerText = sTitle;
	}
	else
	{	
		oDiv.textContent = sTitle;
	}
}

// ½æ³×ÀÏ ÇÃ·¹ÀÌ¾î¿ë 
function goURL( szURL ) {
	document.location.href = szURL.replace(/=amp=/g,"&");
} 

var g_aryRightContentShow = [1,1,1,0,1];
var g_strLayoutType = ""

function fnChangeIndexLayout(sType)
{	
	if(g_strLayoutType == sType)	return;	
	var aryType = new Array();
	switch(sType)
	{
	case 'typeSubmain' :
		aryType = [1,1,1,0,1];
		break;
	case 'typeSubSection' :
		aryType = [1,0,1,1,1];
		break;
	}	
	g_strLayoutType = sType;
	
	for(var i = 0; i < g_aryRightContentShow.length ; i++)
	{
		if(g_aryRightContentShow[i] !=  aryType[i])
		{		
			document.getElementById("rightContent_" + i).style.display = (aryType[i] == 1)? "" : "none";  
			g_aryRightContentShow[i] =  aryType[i];
		}
	}	
}
