var selectedPage = 1;
var commentsPopup = null;

function PreviousPage(chapterId, totalPages){
	GoToPage(chapterId, selectedPage - 1, totalPages)
}

function NextPage(chapterId, totalPages){
	GoToPage(chapterId, selectedPage + 1, totalPages)
}

function UnselectPage(){
	object = document.getElementById("page" + selectedPage + "-top");
	object.className = "work-pagination";
	object.style.color = "#000000";

	object = document.getElementById("page" + selectedPage + "-bottom");
	object.className = "work-pagination";
	object.style.color = "#000000";
}

function ShowNovelSummary(){
	$('#novelsummarylink').slideUp();
	$('#novelsummary').slideDown();
}

function SelectPage(page, totalPages){
	try{
		object = document.getElementById("page" + page + "-top");
		object.className = "work-pagination-highlight";
		object.style.color = "#BB0000";

		object = document.getElementById("page" + page + "-bottom");
		object.className = "work-pagination-highlight";
		object.style.color = "#BB0000";

		if(page == totalPages){
			$('#nextpage-top').hide();
			$('#nextpage-bottom').hide();
		}
		else{
			$('#nextpage-top').css("display", "inline");
			$('#nextpage-bottom').css("display", "inline");
		}

		if(page == 1){
			$('#previouspage-top').hide();
			$('#previouspage-bottom').hide();
		}
		else{
			$('#previouspage-top').css("display", "inline");
			$('#previouspage-bottom').css("display", "inline");
		}

	}
	catch(ex){
		return false;
	}

	if(totalPages > 1){
		UnselectPage();
		selectedPage = page;
	}

	return true;
}

function GoToPage(chapterId, page, totalPages){
	if(page == selectedPage){
		return;
	}

	if(SelectPage(page, totalPages)){
		$('#page-wait-bottom').text('Just a sec...');
		$('#page-wait-top').text('Just a sec...');

		var ac = new AjaxConnection();

		var params = new Object();
		params["chapterid"] = chapterId;
		params["page"] = page;

		ac.Send("/webservices/getchapterpage.php", params, "GoToPageComplete");
	}
}

function GoToPageComplete(response){
	$('#page-wait-bottom').text("");
	$('#page-wait-top').text("");

	xp = new XmlParser();
	xml = xp.GetXmlDocument(response);

	root = xml.documentElement;
	try{
		$('#work-body').html(root.getElementsByTagName('Body')[0].firstChild.nodeValue);
		ScrollTo('work-block');
	}
	catch(ex){
	}
}

function AddChapterSubscription(chapterId){
	var params = new Object();
	params["chapterid"] = chapterId;

	ac = new AjaxConnection();
	ac.Send("/webservices/addchaptersubscription.php", params, "AddChapterSubscriptionComplete");
}

function AddChapterSubscriptionComplete(){
	$("#addchaptersubscriptiontoolbar").slideUp();
}

function DeleteChapterSubscription(chapterId){
	var params = new Object();
	params["chapterid"] = chapterId;

	ac = new AjaxConnection();
	ac.Send("/webservices/deletechaptersubscription.php", params, "DeleteChapterSubscriptionComplete");
}

function DeleteChapterSubscriptionComplete(){
	$("#deletechaptersubscriptiontoolbar").slideUp();
}

function AcceptCode(type){
	$("#error").hide();
	//validate the checkboxes
	if(!$('#rule1').attr("checked") || !$('#rule2').attr("checked") || !$('#rule3').attr("checked") || !$('#rule4').attr("checked") || !$('#rule5').attr("checked")){
		$("#error").slideDown();
		return;
	}

	$('#acceptbutton').attr("disabled", true);
	$('#acceptbutton').val('Just a sec...');

	var ac = new AjaxConnection();
	var params = new Object();
	params["codetype"] = type;

	ac.Send("/webservices/updatecodeofconductstatus.php", params, "AcceptCodeComplete");
}

function AcceptCodeComplete(response){
	CloseLightbox();
}

function CloseLightbox(){
	$.fn.colorbox.close()
}

function RedirectToDashboard(){
	window.location = "/dashboard/";
}

function InitializeLightbox(){
	$("#authorcodelink").colorbox({overlayClose:true, resize:false, height:"500px", width:"730px"});
	$("#critiquercodelink").colorbox({overlayClose:true, resize:false, height:"500px", width:"730px"});
}
