var post_count = 0;
var post_defaultLoadingText = '페이지 로딩중입니다.';
var post_loadingText = post_defaultLoadingText;
var post_offset = '';

var post_init = true;

function update_PostList(id, count, url)
{
	var url = 'http://creaty.net/'+url+'.php?id='+encodeURIComponent(id)+'&idx='+encodeURIComponent(post_count)+'&cnt='+encodeURIComponent(count);

	if (post_loadingText != post_defaultLoadingText)
		document.getElementById('PostList').innerHTML = '';

	document.getElementById("WritedPost").innerHTML = '';
	document.getElementById("WritedReply").innerHTML = '';

	var div = document.createElement('div');
	div.align='center';
	div.width='100%;';
	document.getElementById('PostList').appendChild(div);
	PageLoad(url, document.getElementById('PostList'), div, post_loadingText);
	
	post_loadingText = post_defaultLoadingText;

	post_count += count;


	if (document.getElementById('update_btn') != null)
		document.getElementById('update_btn').innerHTML = 'More +5 Posts';
}
function update_SubPostList(id, parent, isOpen)
{
	var url = 'http://creaty.net/post_history.php?id='+id+'&parent='+parent;

	document.getElementById("WritedPost").innerHTML = '';
	document.getElementById("WritedReply").innerHTML = '';

	if (isOpen)
	{
		var scrollTop = '0px';
		document.getElementById('PostSubList').innerHTML = '';
		if (document.documentElement)
		{
			post_offset = document.documentElement.scrollTop;
			document.documentElement.scrollTop = scrollTop;
		}
		else
		{
			post_offset = document.body.scrollTop;
			document.body.scrollTop = scrollTop;
		}
	}
	else
	{
		if (document.documentElement)
			document.documentElement.scrollTop = post_offset;
		else
			document.body.scrollTop = post_offset;
	}

	var div = document.createElement('div');
	div.align='center';
	div.width='100%;';
	document.getElementById('PostSubList').appendChild(div);
	PageLoad(url, document.getElementById('PostSubList'), div, post_loadingText);

	post_loadingText = post_defaultLoadingText;


	if (document.getElementById('update_btn') != null)
		document.getElementById('update_btn').innerHTML = 'More +5 Posts';
}


function Reply_Checker()
{
	Edit_Checker('Reply_Editor', 'ReplyLength');
	return EditKeyDown(event, 'Reply_Editor', 'ReplyLength', ajaxReplySend);
}

function callback_TransferFinish(Content)
{
	if (post_init == true && Content.innerHTML == '')
	{
		post_count = 0;
		document.getElementById('PostList').innerHTML = 
			'<div class="NoPost" style="margin:100px;text-align:center;">'+
			'<div class="NoPostSubject" style="font-size:20px;font-weight:bold;color:#666666;margin-bottom:20px;">Have not any Cast!</div>'+
			'<div class="NoPostDescription" style="color:#AAAAAA;">Cast your Mind, Technique or anything,<br/>and then invite your friend</div>'+
			'</div>';

		document.getElementById('update_btn').innerHTML = 'Refresh';
	}
	post_init = false;

	FooterSetting();

	if (document.getElementById('Reply_Editor') != null)
	{
		document.getElementById('Reply_Editor').onkeydown = Reply_Checker;
		document.getElementById('Reply_Editor').onkeypress = Reply_Checker;
		document.getElementById('Reply_Editor').onkeyup = Reply_Checker;
	}
}



var isPageLoading = false;
var m_PageCount = 4;
var DivList = new Array();	//제어할 Div를 목록으로 가진다. 히든페이지까지 총 4페이지로 구성된다.


function PageLoad(PageLink, List, Content, InitLoading){
	Content.innerHTML = InitLoading;
	FooterSetting();
	if (!isPageLoading)
	{
		if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		} else {// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}

		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {//전송끝
				isPageLoading = false;
				if (xmlhttp.status == 200)
				{
					Content.innerHTML = xmlhttp.responseText;
					Content.align = 'left';
					if (Content != null && Content.Num != null)
					{
						//alert(Content.Element.innerHTML.split("<!--")[0]);
						//eval(Content.Element.innerHTML.split("<!--")[1]);
					}
					//다음 로딩할 페이지 지정
					//for(var i = 0; i < m_PageCount ; i++)
					//{
					//	if (DivList[i].LoadingURL != null)
					//	{
					//		var url = DivList[i].LoadingURL;
					//		DivList[i].LoadingURL = null;
					//		PageLoad(url, DivList[i]);
					//		break;
					//	}
					//}

					if (post_loadingText != post_defaultLoadingText)
						List.innerHTML = '';
					callback_TransferFinish(Content);
				}
				else {
					PageLoad(PageLink, List, Content, InitLoading);
				}
			}
		}
		isPageLoading = true;
		xmlhttp.open("GET" , PageLink, true);
		xmlhttp.send();

	}else //페이지가 로딩중이면..
	{
		Content.LoadingURL = PageLink;
	}
}

function SetDiv(div0, div1, div2, div3){ // 북의 div를 가져온다.
	for (var index=0; index < m_PageCount; index++ )
	{
		DivList[index] = new function() {
			this.Num = 0;
			this.LoadingURL = null;
		}
	}
	DivList[0].Element = document.getElementById(div0);
	DivList[1].Element = document.getElementById(div1);
	DivList[2].Element = document.getElementById(div2);
	DivList[3].Element = document.getElementById(div3);
}
