///////////////////////////////////////////////////////////////////////////////////////////////////////////			
//                                   Globals
///////////////////////////////////////////////////////////////////////////////////////////////////////////

var currentPage;			
var chapterBack = false;	
var arrowclick = false;		
var pageInitialized = false;
var inMotion = false;
					


///////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                   Render Stuff
///////////////////////////////////////////////////////////////////////////////////////////////////////////
				
///////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////
function FocusPage()
{	
	if(!Array.indexOf){
	    Array.prototype.indexOf = function(obj){
	        for(var i=0; i<this.length; i++){
	            if(this[i]==obj){
	                return i;
	            }
	        }
	        return -1;
	    }
	}


	if ((lastPage > 1) && (currentPage > lastPage))
		currentPage = lastPage;
		
	if(currentPage < 0)
		currentPage = lastPage;
	
	if( !pageInitialized)
	{		
		for( var i = 1; i <= lastPage; i++)
		{
			if( i == currentPage)
			{
				$('#page' + i).show();
				$('#page' + i).css('width', '231');
				break;
			}
		}
		$('#loader').hide();
		pageInitialized = true;
	}
	else if( !arrowclick)
	{	
		for( var i = 1; i < lastPage; i++)
		{
			if( i < currentPage)
			{
				$('#page' + i).hide();
				$('#page' + i).css('width', '0');
			}
			else if ( i > currentPage)
			{
				$('#page' + i).hide();
			}
			else
			{
				$('#page' + i).show();
				$('#page' + i).css('width', '231');
			}			
		}
	}
	else
	{		
		arrowclick = false;	
	}
	
	if( illustrationList.indexOf(currentPage) >= 0 )
	{
		$('#addillustration').html("Delete Illustration");		
	}
	else
	{
		$('#addillustration').html("Insert Illustration");	
	}
	
	if( adList.indexOf(currentPage) >= 0 )
	{
		$('#addad').html("Delete Ad");		
	}
	else
	{
		$('#addad').html("Insert Ad");	
	}
	
	
	$('.waiter').hide();
	if(lastChapter == 1)
		$('#chapterpage').html("Page " + currentPage);	
	else
		$('#chapterpage').html("Chapter " + currentChapter + ", Page " + currentPage);	
}
			
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                   Nav Stuff
///////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////
function GetNavParam( paramName )
{
	var splat = $.address.parameterNames();
	var result = $.address.parameter(paramName);
	if(result) 
		result = parseInt(result);
	else
		result = 1;
	
	return result;
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////			
function handleAddressChange()
{
	currentPage = GetNavParam("page");
	FocusPage();												
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////
function PageForward()
{					
	$('#page' + currentPage).hide();
	currentPage++;
	$('#page' + currentPage).show();	
	$('#page' + currentPage).animate({ width : 231}, 100, 'swing');
	arrowclick = true;	
	$.address.value("?page=" + currentPage);	
	inMotion = false;	
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////
function PageBackward()
{
	$('#page' + currentPage).hide();
	currentPage--;
	$('#page' + currentPage).show();
	$('#page' + currentPage).animate({ width : 231}, 500, 'swing');			
	arrowclick = true;		
	$.address.value("?page=" + currentPage);								
}

function StartTransition( target )
{
	$('.contentdiv').animate({ width : 0}, 300);
	setTimeout( Transition(target), 300);
}

function Transition( target )
{
	$('.waiter').show();
	window.location = target;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                   Init Stuff
///////////////////////////////////////////////////////////////////////////////////////////////////////////


$(document).ready(function() {

	$('#loader').show();
	$('.waiter').show();
	$('.buttons').show();
	$('#chapterpage').show();
	
	currentPage = GetNavParam("page");		
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////				
	$('#nextpage').click( function(){
		if( currentPage < lastPage )
		{
			if( !inMotion )
			{
				inMotion = true;
				$('#page' + currentPage).animate({ width : 0}, 500, 'swing', PageForward );					
			}
		}
		else
		{
			if (currentChapter == lastChapter)
			{
				alert("You have reached the end of this thumb novel.");
				StartTransition('page_library.php');
			}
			else
			{	
				$('.contentdiv').animate({ width : 0}, 300);	
				setTimeout( "$('.waiter').show();window.location = 'page_read.php?novel=" + novelID + "&chapter=" + (currentChapter + 1) + "'", 300);				
			}
		}
	});
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	$('#prevpage').click( function(){
		if( currentPage > 1)
		{
			PageBackward();						
		}
		else
		{
			if (currentChapter == 0)
			{
				$('.contentdiv').animate({ width : 0}, 300);
				setTimeout( "$('.waiter').show();window.location = 'page_titlepage.php?novel=" + novelID + "'", 300);
			}
			else if ((currentChapter == 1) && (lastChapter == 1))
			{
				$('.contentdiv').animate({ width : 0}, 300);
				setTimeout( "$('.waiter').show();window.location = 'page_titlepage.php?novel=" + novelID + "'", 300);
			}
			else
			{
				chapterBack = true;
				
				$('.contentdiv').animate({ width : 0}, 300);	
				setTimeout( "$('.waiter').show();window.location = 'page_read.php?novel=" + novelID + "&chapter=" + (currentChapter - 1) + "#/?page=-1'", 300);				
			}
		}
	});		
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	$('#togglemenu').click( function(){
		$('#menu').show(300);		
	});
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	$('#closemenu').click( function(){
		$('#menu').hide(300);		
	});
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	$('#toc').click( function(){
		$('#menu').hide(300);	
		$('.contentdiv').animate({ width : 0}, 300);	
		setTimeout( "$('.waiter').show();window.location = 'page_read.php?novel=" + novelID + "&chapter=0'", 300);		
	});
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	$('#closenovel').click( function(){
		$('#menu').hide(300);	
		$('.contentdiv').animate({ width : 0}, 300);	
		setTimeout( "$('.waiter').show();window.location = 'index.php'", 300);		
	});
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	$('#writenovel').click( function(){
		$('#menu').hide(300);	
		$('.contentdiv').animate({ width : 0}, 300);	
		setTimeout( "$('.waiter').show();window.location = 'page_novels.php'", 300);		
	});
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	$('#contactauthor').click( function(){	
		$('#menu').hide(300);	
		if( authorContact == "" )
			alert('The author has not left a contact address');
		else
		{
			$('.contentdiv').animate({ width : 0}, 300);	
			setTimeout( "$('.waiter').show();window.location = 'page_contactauthor.php?nid=" + novelID + "&page=" + currentPage + "'", 300);		
		}
	});
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	$('#reportviolation').click( function(){
		$('#menu').hide(300);	
		$('.contentdiv').animate({ width : 0}, 300);	
		setTimeout( "$('.waiter').show();window.location = 'page_violation.php?nid=" + novelID + "&chapter=" + currentChapter + "&page=" + currentPage + "'", 300);		
	});	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	$('#comment').click( function(){
		$('#menu').hide(300);	
		$('.contentdiv').animate({ width : 0}, 300);	
		setTimeout( "$('.waiter').show();window.location = 'page_comment.php?nid=" + novelID + "&chapter=" + currentChapter + "&page=" + currentPage + "'", 300);		
	});	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	$('#addillustration').click( function(){
		$('#menu').hide(300);	
		$('.contentdiv').animate({ width : 0}, 300);	
		setTimeout( "$('.waiter').show();window.location = 'page_illustrate.php?nid=" + novelID + "&chapter=" + currentChapter + "&page=" + currentPage + "'", 300);		
	});	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	$('#addad').click( function(){
		$('#menu').hide(300);	
		$('.contentdiv').animate({ width : 0}, 300);	
		setTimeout( "$('.waiter').show();window.location = 'page_ad.php?nid=" + novelID + "&chapter=" + currentChapter + "&page=" + currentPage + "'", 300);		
	});	
	
	pageInitialized = false;
	$('div.pagecontent').hide();
	$.address.change(handleAddressChange);		

});

