var pageNum = 0;
var onPage=0;

//make a namespace
var newmind = window.newmind || {};
newmind.wtc = window.newmind.wtc || {};

newmind.wtc.pageinit = function(){
	//create a div after pageContentTitle
	$("#pageContent h1.pageContentTitle").after('<div class="page" id="page'+pageNum+'"></div>');
	
	var TestNum=0;
	//select all items inside pagecontent not including the title, script or div.page
	//h1 ~ * (selects all the siblings (grouped with their children rather than every element alone) below.
	$("#pageContent h1 ~ *:not(h1.pageContentTitle, script, div.page)").each(function(){
		//if there is an attribute style
		if($(this).attr("style") != null && $(this).attr("style") != ""){
			//if this style includes the page break (how fck editor does page breaks)
			if($(this).attr("style").toLowerCase().match("page-break-after: always")){
				//increment the page
				pageNum = pageNum + 1;
				//make a new page
				$("#pageContent h1.pageContentTitle").after('<div class="page" id="page'+pageNum+'"></div>');		
				$(this).remove();
			}
			else{
				$('#page'+pageNum).append(this); 		
			}
		}
		else{
			$('#page'+pageNum).append(this); 	
		}
	});
};

newmind.wtc.pagechoice = function(){
	
	//if we have multiple pages, then show the page selector div
	if(pageNum>0)
	{
		$(".pageChoice").show();
		
		//build the page list
		var strLink;
		var iTmpNum;
		
		newmind.wtc.buildLink(0,"Previous Page","jump");
		
		for(iTmpNum=0;iTmpNum<=pageNum;iTmpNum++)
		{
			newmind.wtc.buildLink(iTmpNum,iTmpNum+1,"page");
		}
		
		newmind.wtc.buildLink(1,"Next Page","jump");
		
		//hide everything apart from the last loaded page (loads the first page last)
		$("div.page:not(:last)").hide();
	}
};

$(newmind.wtc.pageinit);
$(newmind.wtc.pagechoice);

var iLinkCount=0;

newmind.wtc.buildLink=function (ipPageNum, strpPageTitle, strpPageClass) {
	//the class is to let the js know what the id is.
	var strpDivClass=strpPageClass + ipPageNum
	strLink='<a href="#' + ipPageNum + '" class="' + strpPageClass + ipPageNum+'">' + strpPageTitle + '</a>';
									
	$(strLink).click(function (e){
		iLinkCount++;
		//get the class from the link
		var choosePage=$(e.target).attr("class");
		//remove the "page" and get the page number the user wants to load
		
		//alert(choosePage + "=" + iLinkCount + " is the info");
		
		if(iLinkCount<3)
		{
			//alert(choosePage + "=" + iLinkCount + " is the info");
		}
		newmind.wtc.changePage(choosePage.replace("page",""));
		return false;
	}).appendTo('div.pageChoice');
	
	$(".jump0").css("visibility", "hidden");
}

newmind.wtc.nextPage=function () {
	//hide all the visible pages
	//var thisID=$("div.page:visible").id();
	onPage=onPage+1;
	
	$("div.page:visible").hide();
	$('div#page'+onPage).show();
	$('div.page'+onPage).disabled=true;
	
	newmind.wtc.showLinks(onPage);
}

newmind.wtc.previousPage=function () {
	//hide all the visible pages
	onPage=onPage-1;					
	$("div.page:visible").hide();
	$('div#page'+onPage).show();
	$(".jump"+onPage).css("visibility", "hidden");
	newmind.wtc.showLinks(onPage);						
}

newmind.wtc.showLinks=function(ipTmpNum) {
	if(ipTmpNum==pageNum)
	{
		$(".jump1").css("visibility", "hidden");
	}
	else
	{
		$(".jump1").css("visibility", "visible");
	}
	
	if(ipTmpNum==0)
	{
		$(".jump0").css("visibility", "hidden");
	}
	else
	{
		$(".jump0").css("visibility", "visible");
		//$(".jump0:visible").show();
	}
}

newmind.wtc.changePage=function (ipTmpNum) {
	//hide all the visible pages
	
	if(ipTmpNum=="jump1")
	{
		if(onPage!=pageNum)
		{
			newmind.wtc.nextPage();
		}
	}
	else if(ipTmpNum=="jump0")
	{
		if(onPage!=0)
		{
			newmind.wtc.previousPage();
		}
	}
	else
	{
		//$("div.page:visible").hide();
		//show the page selected
		$('div#page'+ipTmpNum).show();
		newmind.wtc.showLinks(ipTmpNum);	
	}
}