﻿var ctnJson = null;
var fvtJson = null;
var cartoon = null;
var newsJson = null;

Event.observe(window, "load", function()
{
	Init();
});

function Init()
{
	cartoon = new CartoonLib(top.window.frames["mainFrame"], "/Cartoon/go.ashx?c=");
	cartoon.ShowNavigator = ShowNavigator;
	cartoon.HideNavigator = HideNavigator;
	cartoon.Logging = Logging;
	cartoon.AddFavorite = AddFavorite;

	LoadCategory();
	LoadFavorites();
	LoadNews();
}

function PrintCategory()
{
	if (ctnJson == null) return;
	$("categoryMsg").hide();
	$("categoryBox").show();

	opt = document.createElement("option");
	opt.text = "=사이트 선택=";
	opt.value = -1;
	$("pubList").options.add(opt);

	ctnJson.each(function(s)
	{
		opt = document.createElement("option");
		opt.value = s.pid;
		opt.text = s.name;
		$("pubList").options.add(opt);
	});

	//BindCartoonList(0);
}

function BindCartoonList(index)
{
	if (ctnJson == null) return;
	var oSelect = $("ctnList");
	oSelect.update("");

	opt = document.createElement("option");
	opt.text = "=카툰 선택=";
	opt.value = 0;
	oSelect.options.add(opt);

	ctnJson[index].cartoons.each(function(s)
	{
		var opt = document.createElement("option");
		opt.value = s.id;
		opt.text = s.shortName;
		opt.title = s.fullName;
		oSelect.options.add(opt);
	});
}

function LoadFavorites()
{
	new Ajax.Request("/Lib/myCartoons.ashx", {
		on200: function(xhttp)
		{
			data = xhttp.responseText;
			fvtJson = data.evalJSON();
			PrintFavorites();
		},
		on500: function(xhttp)
		{
			alert("My카툰 목록을 읽어오는데 실패했습니다.\n잠시후 다시 이용해주세요.");
		}
	});
}

function PrintFavorites()
{
	var divTxt = "";
	fvtJson.each(function(s)
	{
		divTxt += "<span class='favoriteItem' title='" + s.fullName + "' onclick='_goCartoon(\"" + s.id + "\")'>* " + s.shortName.truncate(10) + "</span>";
		myCtnObj = $("chkCtn_" + s.id);
		if (myCtnObj)
			myCtnObj.checked = true;
	});
	if (!divTxt)
		divTxt = "<div align='center'><a href='#' onclick='return viewAll();' align='center'>여기를 클릭해서 자주 보는 카툰을 추가하세요.</a> <a href='#' onclick='return viewPart()'>[닫기]</a></div>";
	$("favoriteList").update(divTxt);
}

function PrintNews()
{
	var divTxt = "";
	newsJson.each(function(s)
	{
		divTxt += "<span class='favoriteItem' title='" + s.name + "' onclick='_goNews(\"" + s.url + "\")'>* " + s.name.truncate(10) + "</span>";
	});
	$("newsList").update(divTxt);
}
function LoadCategory()
{
	new Ajax.Request("/Lib/ctnList.ashx", 	{
		on200: function(xhttp) 
		{
			data = xhttp.responseText;
			ctnJson = data.evalJSON(); 
			PrintCategory();
			PrintCategoryAll();
		},
		on500: function(xhttp) 
		{
			$('categoryMsg').update("Failed");
			alert("카툰 목록을 읽어오는데 실패했습니다.\n잠시후 다시 이용해주세요.");
		}
	});
}
function LoadNews()
{
	new Ajax.Request("/Lib/newsList.ashx", {
		on200: function(xhttp)
		{
			data = xhttp.responseText;
			newsJson = data.evalJSON();
			PrintNews();
		},
		on500: function(xhttp)
		{
			$('categoryMsg').update("Failed");
			alert("뉴스 목록을 읽어오는데 실패했습니다.\n잠시후 다시 이용해주세요.");
		}
	});
}
function _goCartoon(cid)
{
	cartoon.HideNavigator();
	if (arguments.length == 0)
	{
		cid = $("ctnList").getValue();
	}
	if (cid > 0)
	{
		cartoon.Id = cid;
		cartoon.Go("");
		return true;
	}
	else
		return false;
}

function _goNews(url)
{
	top.window.frames["mainFrame"].location.href = url;
}

function ViewNavigator(prevData, nextData)
{
	cartoon.NextData = nextData;
	cartoon.PrevData = prevData;
	//cartoon.Windows.Navigator.ViewNavigator();
	cartoon.ShowNavigator();
}

function Next()
{
	cartoon.Next();
}

function Prev()
{
	cartoon.Prev();
}

function Logging()
{
	new Ajax.Request("/Lib/Clog.ashx", {
		method: "post",
		parameters: "c=" + cartoon.Id
	});
}

function AddFavorite(cid)
{
//	if (confirm("추가합니까?"))
//	{
		if (arguments.length == 0)
			cid = cartoon.Id;
			
		if (cid != "" && cid != null)
		{
			new Ajax.Request("/Lib/AddFavorite.ashx", {
				method: "post",
				parameters: "c=" + cid,
				on200: function(res)
				{
					//alert("추가되었습니다.");
					LoadFavorites();
				}
			});
			return true;
		}
		else
			return false;
//	}
//	else
//		return false;
}

function RemoveFavorite(cid)
{
//	if (confirm("제거합니까?"))
//	{
		if (arguments.length == 0)
			cid = cartoon.Id;

		if (cid != "" && cid != null)
		{
			new Ajax.Request("/Lib/DelFavorite.ashx", {
				method: "post",
				parameters: "c=" + cid,
				on200: function(res)
				{
					//alert("제거되었습니다.");
					LoadFavorites();
				}
			});
			return true;
		}
		else
			return false;
//	}
//	else
//		return false;
}

function ClearFavorite()
{
	if (confirm("My카툰을 모두 제거합니다.\n제거된 My카툰은 복구할수 없습니다.\n\n정말 삭제합니까?"))
	{
		new Ajax.Request("/Lib/DelFavorite.ashx", {
			method: "post",
			parameters: "ca=1",
			on200: function(res)
			{
				LoadFavorites();
			},
			on500:function()
			{
				alert("요청을 처리하는데 실패했습니다.\n잠시 후 다시 시도해주세요.");
			}
		});
		return true;
	}
	return false;
}


function CheckFavorite(o)
{
	if (o.checked)
		AddFavorite(o.value);
	else
		RemoveFavorite(o.value);
}

function PrintCategoryAll() {

	var oTable = $("tblCartoon");
	oTable.update("");

	var strHtml = "";

	ctnJson.each(function(s) {

		// 사이트 시작
		strHtml +=
		'<tr>' +
			'<td align="left" valign="top" style="padding-top:5px;"><table width="100%" border="0" cellspacing="0" cellpadding="0">' +
				'<tr>' +
				  '<td align="left" valign="top"><strong>' + s.name + '</strong></td>' +
				'</tr>';

		var nIndex = 0;
		s.cartoons.each(function(c) {

			// 카툰 한줄 시작
			if (nIndex % 7 == 0) {
				strHtml +=
				'<tr>' +
				  '<td align="left" valign="top" style="padding-top:3px;padding-bottom:5px;"><table width="100%" border="0" cellspacing="0" cellpadding="0">' +
					  '<tr>';
			}

			// 카툰 정보 td  생성
			strHtml += '<td width="135" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">' +
						'<tr>' +
						  '<td width="19" valign="top"><input type="checkbox" name="chkCartoonItem" id="chkCtn_' + c.id + '" value=' + 
						  c.id + ' onclick="CheckFavorite(this)" /></td>' +
						  '<td class="gray888" style="padding-top:1px;">' +
						  '<span class="cursor cartoonItem" title="' + c.fullName + '" onclick="_goCartoon(' + c.id + ')">' + 
						  c.shortName.truncate(10) + '</span></td>' +
						'</tr>' +
					  '</table></td>';

			
			// 사이트의 카툰 표시가 끝난경우 한줄 다 채우기 위한 여백 td 생성
			if (nIndex == s.cartoons.length - 1) {
				for (nCount = 0; nIndex % 7 < 6; nCount++) {
					strHtml += '<td width="135" align="left" valign="top">&nbsp;</td>';
					nIndex++;
				}
			}

			// 카툰 한줄 종료
			if (nIndex % 7 == 6) {
				strHtml +=
					'</tr>' +
					'</table></td>' +
				'</tr>'
			}

			nIndex++;
		})

		// 사이트 종료
		strHtml +=
			'</table></td>' +
		'</tr>' +
		'<tr>' +
		'<td height="1" align="left" valign="top" bgcolor="#ebebeb"><img src="/images/blank.gif" width="1" height="1" /></td>' +
		'</tr>';

	});

	oTable.update(strHtml);

	// favorite 처리
	fvtJson.each(function(s)
	{
		myCtnObj = $("chkCtn_" + s.id);
		if (myCtnObj)
			myCtnObj.checked = true;
	});
}


// 전체보기
function viewAll()
{
	$("btnsAll").show();
	$("trAll").show();
	$("btnsMin").hide();
	$("menuBox").setStyle({height: "280px"});

	parent.document.getElementById('topFrame').rows = "*,290";
}

// 큰창닫기
function viewPart() {

	$("btnsAll").hide();
	$("trAll").hide();
	$("btnsMin").show();
	$("menuBox").setStyle({ height: "80" });

	parent.document.getElementById('topFrame').rows = "*,90";
}

function ShowNavigator()
{
	if (cartoon.NextData != "")
		$("btnNext").show();
	else
		$("btnNext").hide();

	if (cartoon.PrevData != "")
		$("btnPrev").show();
	else
		$("btnPrev").hide();
		
	$("ctnNavi").show();
}

function HideNavigator()
{
	$("ctnNavi").hide();
}

function ChangeTarget(useNewWin)
{
	cartoon.UseNewWindow = useNewWin;
}

function ViewNewsMenu()
{
	$("newsBox").show();
	$("menuBox").hide();
	$("CtnMenuBtn").src = "/images/tabbott_cartoon_off.gif";
	$("NewsMenuBtn").src = "/images/tabbott_news_on.gif";
}


function ViewCartoonMenu()
{
	$("newsBox").hide();
	$("menuBox").show();
	$("CtnMenuBtn").src = "/images/tabbott_cartoon_on.gif";
	$("NewsMenuBtn").src = "/images/tabbott_news_off.gif";
}