// ==================================
// 즐겨찾기 부분
// ==================================
var req_FAV;
var isIE_FAV = false;
var favlist_FAV = new Array;
var mycmtlist_FAV = new Array;
var loc_FAV = "top";
var read_top_FAV = false;
var read_menu_FAV = false;
var read_mycmt = false;
var divhtml = "";
var layer_max_height = 500;
if (screen.height < 1024) layer_max_height = 400;

function loadXMLDoc(url) {
	if (window.XMLHttpRequest) {
		req_FAV = new XMLHttpRequest();
		req_FAV.onreadystatechange = processReqChange;
		req_FAV.open("GET", url, true);
		req_FAV.send(null);
	} else if (window.ActiveXObject) {
		isIE_FAV = true;
		req_FAV = new ActiveXObject("Microsoft.XMLHTTP");
		if (req_FAV) {
			req_FAV.onreadystatechange = processReqChange;
			req_FAV.open("GET", url, true);
			req_FAV.send();
		}
	}
}

function processReqChange() {
	if (req_FAV.readyState == 4) {
		result = false;
		if (req_FAV.status == 200) {
			if (loc_FAV == "mycmt") {
				result = get_mycmt_info();
			} else {
				result = get_fav_info();
			}
		}

		if (!result) {
			alert("자료를 읽는데 실패하였습니다.\n잠시후 다시 시도해 보세요.");

			if (loc_FAV == "mycmt") {
				read_mycmt = false;
			} else {
				if (loc_FAV == "top") {
					read_top_FAV = false;
				} else {
					read_menu_FAV = false;
					divfavmenu.innerHTML = "";
				}
			}
		}
	}
}

function get_mycmt_info()
{
	error = req_FAV.responseXML.getElementsByTagName("error");

	if (error.length != 1) return false;
	if (error[0].firstChild.nodeValue != "0") return false;

	var items = req_FAV.responseXML.getElementsByTagName("item");

	mycmtlist_FAV = new Array;
	mycmtlist_cnt = 0;
	for(i=0;i<items.length;i++) {
		fi = items[i];

		if (
				getElementTextNS("", "bid", fi, 0)        	&& getElementTextNS("", "bid", fi, 0)        != 0
			&& 	getElementTextNS("", "folder_id", fi, 0)  	&& getElementTextNS("", "folder_id", fi, 0)  != 0
			&& 	getElementTextNS("", "blog_title", fi, 0) 	&& getElementTextNS("", "blog_title", fi, 0) != 0
			&& 	getElementTextNS("", "parent_id", fi, 0)  	&& getElementTextNS("", "parent_id", fi, 0)  != 0
			&& 	getElementTextNS("", "cmt_id", fi, 0)     	&& getElementTextNS("", "cmt_id", fi, 0)     != 0
			&& 	getElementTextNS("", "content", fi, 0)    	&& getElementTextNS("", "content", fi, 0)    != 0
			&& 	getElementTextNS("", "timestamp", fi, 0)  	&& getElementTextNS("", "timestamp", fi, 0)  != 0
			&& 	getElementTextNS("", "folder_case", fi, 0)  && getElementTextNS("", "folder_case", fi, 0)  != 0
		) {
			mycmtlist_FAV[mycmtlist_cnt] = new Array;
			mycmtlist_FAV[mycmtlist_cnt][0] = getElementTextNS("", "bid", fi, 0);
			mycmtlist_FAV[mycmtlist_cnt][1] = getElementTextNS("", "folder_id", fi, 0);
			mycmtlist_FAV[mycmtlist_cnt][2] = getElementTextNS("", "blog_title", fi, 0);
			mycmtlist_FAV[mycmtlist_cnt][3] = getElementTextNS("", "parent_id", fi, 0);
			mycmtlist_FAV[mycmtlist_cnt][4] = getElementTextNS("", "cmt_id", fi, 0);
			mycmtlist_FAV[mycmtlist_cnt][5] = getElementTextNS("", "content", fi, 0);
			mycmtlist_FAV[mycmtlist_cnt][6] = getElementTextNS("", "timestamp", fi, 0);
			mycmtlist_FAV[mycmtlist_cnt][7] = getElementTextNS("", "folder_case", fi, 0);
			mycmtlist_cnt++;
		}
	}

	view_my_cmt();

	return true;
}

function get_fav_info()
{
	error = req_FAV.responseXML.getElementsByTagName("error");

	if (error.length != 1) return false;
	if (error[0].firstChild.nodeValue != "0") return false;

	var items = req_FAV.responseXML.getElementsByTagName("folder");

	favlist_FAV = new Array;
	for(i=0;i<items.length;i++) {
		fi = items[i];
		//alert(getElementTextNS("", "folder_name", fi.getElementsByTagName("folder_name"), 0));

		fn = getElementTextNS("", "folder_name", fi, 0);
		favlist_FAV[i] = new Array;
		favlist_FAV[i][0] = fn;		//folder_name
		favlist_FAV[i][1] = new Array;

		len = fi.getElementsByTagName("item").length;
		favlist_FAV_cnt = 0;
		for(j=0;j<len;j++) {
			if (getElementTextNS("", "id", fi, j)) {
				favlist_FAV[i][1][favlist_FAV_cnt] = new Array;
				favlist_FAV[i][1][favlist_FAV_cnt][0] = getElementTextNS("", "id", fi, j);
				favlist_FAV[i][1][favlist_FAV_cnt][1] = getElementTextNS("", "url", fi, j);
				favlist_FAV[i][1][favlist_FAV_cnt][2] = getElementTextNS("", "name", fi, j);
				favlist_FAV[i][1][favlist_FAV_cnt][3] = getElementTextNS("", "update", fi, j);		// 0:no, 1:new, N/A:no YKblog
				favlist_FAV[i][1][favlist_FAV_cnt][4] = getElementTextNS("", "mycmt_url", fi, j);
				favlist_FAV[i][1][favlist_FAV_cnt][5] = getElementTextNS("", "mycmt_cmt_id", fi, j);
				favlist_FAV[i][1][favlist_FAV_cnt][6] = getElementTextNS("", "mycmt_content", fi, j);
				favlist_FAV_cnt ++;
			}
		}
	}

	if (loc_FAV == "top") {
		view_top_module();
	} else {
		view_menu_module();
	}

	return true;
}

function getElementTextNS(prefix, local, parentElem, index) {
	var result = "";
	if (prefix && isIE_FAV) {
		result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
	} else {
		result = parentElem.getElementsByTagName(local)[index];
	}
	if (result) {
		if (result.childNodes.length > 1) {
			return result.childNodes[1].nodeValue;
		} else {
			return result.firstChild.nodeValue;
		}
	} else {
		return "";
	}
}

function clear_fav(loc)
{
	loc_FAV = loc;

	if (loc_FAV == "mycmt") {
		getObj("mycmtlist").innerHTML = "내가 쓴 답글 목록을 읽는 중입니다.<br>잠시 기다려주세요~";
		getObj('tblmycmt').style.display = 'block';
		getObj('divmycmt').style.visibility = 'visible';
		getObj('divmyfav').style.visibility = 'hidden';
		getObj('tblmyfav').style.display = 'none';
	} else {
		if (loc_FAV == "top") {
			// 읽는중....
			//alert(fav_top_select.options.length);
			getObj("myfavlist").innerHTML = "즐겨찾기 정보를 읽는 중입니다.<br>잠시 기다려주세요~";
			getObj('tblmyfav').style.display = 'block';
			getObj('divmyfav').style.visibility = 'visible';
			getObj('divmycmt').style.visibility = 'hidden';
			getObj('tblmycmt').style.display = 'none';
		} else {
			getObj('divfavmenu').innerHTML = "즐겨찾기 정보를 읽는 중입니다.";
		}
	}
}

function AddFavOptions(text1, value1)
{
	if (text1 == "" && value1 == "") {
		divhtml += "<tr><td height=7></td></tr>";
	}else {
		if (value1 == "") {
			divhtml += "<tr><td height=17><font class='global'>" + text1 + "</font></td></tr>";
		} else {
			divhtml += "<tr><td height=17><a href='" + value1 + "' class='global' style='text-decoration:none;'>" + text1 + "</a></td></tr>";
		}
	}
}

function AddFavListItem(url, text1, isnew, cmturl, cmt)
{
	divhtml += "<tr><td height=17 style='padding-left:4px;'><img src=\"http://" + IMG_HOST + SKIN_PATH + "folder_bullet.gif\" width=\"2\" height=\"2\" style=\"margin-bottom:3px\"> ";
	divhtml += "<a href='" + url + "' class='global' style='text-decoration:none;'>" + text1 + " ";
	if (isnew) divhtml += "<img src='http://img.yahoo.co.kr/blog/2005/07/images/icon_u.gif' align=absmiddle alt='새로 작성된 글이 있습니다' title='새로 작성된 글이 있습니다.'>";
	divhtml += "</a> ";
	if (cmturl) divhtml += "<a href='" + cmturl + "' class='global' style='text-decoration:none;'><img align=absmiddle src='http://img.yahoo.co.kr/blog/2005/07/images/icon_my.gif' border=0 alt='내가 쓴 답글이 있습니다' title='내가 쓴 답글이 있습니다'></a>";
	divhtml += "</td></tr>";
}

function AddMyCmtListItem(bid, fid, blog_title, parent_id, cmt_id, comment, timestamp, folder_case)
{
	short_title = fncom_chk_strlength_cut(32, blog_title);
	if (short_title != blog_title) blog_title = short_title + "...";

	if (folder_case == 'poll') {
		divhtml += "<tr><td height=17 style='padding-left:3px;'><img src=\"http://" + IMG_HOST + SKIN_PATH + "folder_bullet.gif\" width=\"2\" height=\"2\" style=\"margin-bottom:3px\"> ";
		divhtml += "<a href='http://" + HTTP_HOST + "/" + bid + "/POLL/yblog_poll_result.html?fid=" + fid + "&no=" + parent_id + "' class='global' style='text-decoration:none;'><b>" + blog_title + "</b></a><br>";
		divhtml += "&nbsp;&nbsp;&nbsp;";
		divhtml += "<a href='http://" + HTTP_HOST + "/" + bid + "/POLL/yblog_poll_result.html?fid=" + fid + "&no=" + parent_id + "#" + cmt_id + "' class='global' style='text-decoration:none;'>" + comment + "</a>";
		divhtml += "</td></tr>";
	} else {
		if (folder_case == 'guestbook') {
			divhtml += "<tr><td height=17 style='padding-left:3px;'><img src=\"http://" + IMG_HOST + SKIN_PATH + "folder_bullet.gif\" width=\"2\" height=\"2\" style=\"margin-bottom:3px\"> ";
			divhtml += "<a href='http://" + HTTP_HOST + "/" + bid + "/MYBLOG/guest.html?expend=" + parent_id + "' class='global' style='text-decoration:none;'><b>" + blog_title + "</b></a><br>";
			divhtml += "&nbsp;&nbsp;&nbsp;";
			if (cmt_id == "-1") {
				divhtml += "<a href='http://" + HTTP_HOST + "/" + bid + "/MYBLOG/guest.html?expend=" + parent_id + "' class='global' style='text-decoration:none;'>[방명록] " + comment + "</a>";
			} else {
				divhtml += "<a href='http://" + HTTP_HOST + "/" + bid + "/MYBLOG/guest.html?expend=" + parent_id + "#" + cmt_id + "' class='global' style='text-decoration:none;'>" + comment + "</a>";
			}
			divhtml += "</td></tr>";
		} else {
			divhtml += "<tr><td height=17 style='padding-left:3px;'><img src=\"http://" + IMG_HOST + SKIN_PATH + "folder_bullet.gif\" width=\"2\" height=\"2\" style=\"margin-bottom:3px\"> ";
			divhtml += "<a href='http://" + HTTP_HOST + "/" + bid + "/" + parent_id + ".html' class='global' style='text-decoration:none;'><b>" + blog_title + "</b></a><br>";
			divhtml += "&nbsp;&nbsp;&nbsp;";
			divhtml += "<a href='http://" + HTTP_HOST + "/" + bid + "/" + parent_id + ".html#" + cmt_id + "' class='global' style='text-decoration:none;'>" + comment + "</a>";
			divhtml += "</td></tr>";
		}
	}
}

function AddFavLine()
{
	divhtml += "<tr><td height=1 class='global'><font style='font-size:1px;'>----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------</font></td></tr>";
}

function view_top_module()
{
	getObj('tblmycmt').style.display = 'none';
	getObj('divmycmt').style.visibility = 'hidden';
	getObj('divmyfav').style.visibility = 'visible';

	//getObj('fav_top_select').options.length = 0;
	divhtml = "<table border=0 cellspacing=0 cellpadding=0>";

	//AddFavOptions("블로그 즐겨찾기",	"");
	AddFavOptions("<b>내가 쓴 답글 보기 <img src='http://img.yahoo.co.kr/blog/2005/07/images/icon_change.gif' border=0 alt='블로그 즐겨찾기' title='블로그 즐겨찾기'></b>", 	"javascript:set_mycmt();");
	AddFavOptions("마이 블로그", 				"/" + YID);
	AddFavOptions("즐겨찾기 글모음", 			"/" + YID + "/RSSREADER/reader.html");
	AddFavOptions("랜덤 블로그 가기", 			"/RANDOM");

	divheight = 75;
	if (favlist_FAV.length <= 0) {
		//getObj('tblmyfav').style.width = 170;
		//getObj('myfavlist').style.width = 170;
		AddFavLine();
		divheight += 15;

		AddFavOptions("블로그 즐겨찾기 정보가 없습니다.", "");
		AddFavOptions("블로그 즐겨찾기를 이용하여 편리한 블로깅을~ ^^", "");
		divheight += 40;
	} else {
		AddFavLine();
		divheight += 15;

		for(i=0;i<favlist_FAV.length;i++) {
			folder_name = favlist_FAV[i][0];
			folder_list = favlist_FAV[i][1];

			AddFavOptions("<b>" + folder_name + "</b>", "");

			for(j=0;j<folder_list.length;j++) {
				isnew 	= false;
				cmturl 	= "";
				cmt 	= "";

				if (folder_list[j][3] == "1") isnew = true;
				if (folder_list[j][4] != "-" && folder_list[j][4] != "") {
					cmturl 	= folder_list[j][4] + "#" + folder_list[j][5];
					cmt 	= folder_list[j][6];
				}

				AddFavListItem(folder_list[j][1], folder_list[j][2], isnew, cmturl, cmt);
				if (divheight < layer_max_height) divheight += 20;
			}

			AddFavOptions("", "");
		}
	}
	divhtml += "</table>";
	getObj('myfavlist').style.height = divheight;
	getObj("myfavlist").innerHTML = divhtml;
	getObj('tblmyfav').style.display = 'block';

	read_top_FAV = true;
}

function view_my_cmt()
{
	getObj('tblmyfav').style.display = 'none';
	getObj('divmyfav').style.visibility = 'hidden';
	getObj('divmycmt').style.visibility = 'visible';

	divhtml = "<table border=0 cellspacing=0 cellpadding=0>";

	//AddFavOptions("블로그 즐겨찾기",	"");
	AddFavOptions("<b>블로그 즐겨찾기 <img src='http://img.yahoo.co.kr/blog/2005/07/images/icon_change.gif' border=0 alt='내가 쓴 답글 보기' title='내가 쓴 답글 보기'></b>", 	"javascript:set_fav_top();");
	AddFavOptions("마이 블로그", 				"/" + YID);
	AddFavOptions("즐겨찾기 글모음", 			"/" + YID + "/RSSREADER/reader.html");
	AddFavOptions("랜덤 블로그 가기", 			"/RANDOM");

	divheight = 75;

	if (mycmtlist_FAV.length <= 0) {
		//getObj('tblmycmt').style.width = 220;
		//getObj('mycmtlist').style.width = 220;
		AddFavLine();
		divheight += 15;

		AddFavOptions("내가 쓴 답글 정보가 없습니다.", "");
		AddFavOptions("다른 사람의 블로그에 내가 쓴 답글을 이곳에서<br>확인 할 수 있습니다. ^^", "");
		divheight += 55;
	} else {
		AddFavLine();
		divheight += 15;

		for(i=0;i<mycmtlist_FAV.length;i++) {
			//alert(mycmtlist_FAV[i][0]);
			AddMyCmtListItem(mycmtlist_FAV[i][0], mycmtlist_FAV[i][1], mycmtlist_FAV[i][2], mycmtlist_FAV[i][3], mycmtlist_FAV[i][4], mycmtlist_FAV[i][5], mycmtlist_FAV[i][6], mycmtlist_FAV[i][7]);
			if (divheight < layer_max_height) divheight += 35;
		}
	}
	divhtml += "</table>";
	getObj('mycmtlist').style.height = divheight;
	getObj("mycmtlist").innerHTML = divhtml;
	getObj('tblmycmt').style.display = 'block';
	read_mycmt = true;
}

function view_menu_module()
{
	if (favlist_FAV.length <= 0) {
		var divhtml = "<table cellpadding=0 cellspacing=0 border=0 width=100%><tr><td style='padding-left:5px;'>즐겨찾기 정보가 없습니다.</td></tr></table>";
	} else {
		var divhtml = "<table cellpadding=0 cellspacing=0 border=0 width=100%>";
		for(i=0;i<favlist_FAV.length;i++) {
			folder_name = favlist_FAV[i][0];
			folder_list = favlist_FAV[i][1];
			divhtml += "<tr><td style=\"padding-left:5px;padding-top:3px;\" colspan=2 class=\"spread_text\"><b>" + folder_name + "</b></td></tr>";
			for(j=0;j<folder_list.length;j++) {
				if (WEBFONT_FONTTAG == "") {
					folder_title = fncom_chk_strlength_cut(19, folder_list[j][2]);
				} else {
					folder_title = fncom_chk_strlength_cut(17, folder_list[j][2]);
				}
				if (folder_title != folder_list[j][2]) folder_title += "...";

				sTAG = "<tr><td width=\"8\" align=\"center\">-</td><td><img src=\"http://" + IMG_HOST + SKIN_PATH + "folder_bullet.gif\" align=\"absmiddle\"> <a href=\"" + folder_list[j][1] + "\" class=\"spread_list\" title=\"" + folder_list[j][2] + "\" target=\"_blank\">" + folder_title + "</a></td></tr>";
				divhtml += sTAG;
			}
		}
		divhtml += "</table>";
	}

	divfavmenu.innerHTML = divhtml;
	read_menu_FAV = true;
}

// GNV - 즐겨찾기
function set_fav_top_menu()
{
	if (document.getElementById('tblplzlogin2').style.display != 'none')
		document.getElementById('tblplzlogin2').style.display = 'none'

	if (getObj('tblmycmt').style.display != 'none') {
		getObj('tblmycmt').style.display = 'none';
		getObj('divmycmt').style.visibility = 'hidden';
	} else {
		set_fav_top();
	}
}

// GNV - 즐겨찾기 실제 불러오기
function set_fav_top()
{
	if (!read_top_FAV) {
		clear_fav('top');
		loadXMLDoc("http://" + HTTP_HOST + "/" + YID + "/MYBLOG/favlist.html");
	} else {
		if (getObj('tblmyfav').style.display == 'none') {
			getObj('tblmyfav').style.display = 'block';
			getObj('divmyfav').style.visibility = 'visible';
			getObj('divmycmt').style.visibility = 'hidden';
			getObj('tblmycmt').style.display = 'none';
		} else {
			getObj('tblmyfav').style.display = 'none';
			getObj('divmyfav').style.visibility = 'hidden';
		}
	}
}

// GNV - 즐겨찾기 - 내가쓴 댓글
function set_mycmt()
{
	if (!read_mycmt) {
		clear_fav('mycmt');
		loadXMLDoc("http://" + HTTP_HOST + "/" + YID + "/MYBLOG/mycmtlist.html");
	} else {
		if (getObj('tblmycmt').style.display == 'none') {
			getObj('tblmycmt').style.display = 'block';
			getObj('divmycmt').style.visibility = 'visible';
			getObj('divmyfav').style.visibility = 'hidden';
			getObj('tblmyfav').style.display = 'none';
		} else {
			getObj('tblmycmt').style.display = 'none';
			getObj('divmycmt').style.visibility = 'hidden';
		}
	}
}

function set_fav_menu()
{
	if (!read_menu_FAV) {
		clear_fav('menu');
		loadXMLDoc('http://' + HTTP_HOST + '/' + BLOG_ID + '/MYBLOG/favlist.html');
	}
}

function fncom_chk_strlength_cut(vn_maxlength, vn_str)
{
	var vn_sumlength=0;
	var vn_restr='';
	for(var i= 0;i < vn_str.length; i++) {
		if( escape(vn_str.charAt(i)).length > 3 ) {
			vn_length = 2;
		} else if (vn_str.charAt(i) == '<' || vn_str.charAt(i) == '>') {
			vn_length = 4;
		} else {
			vn_length = 1 ;
		}
		if ( vn_maxlength < (vn_sumlength + vn_length) ) break;

		vn_sumlength += vn_length;
		vn_restr += vn_str.charAt(i);
	}
	return (vn_restr);
}
// 즐겨찾기 부분 끝
