var view_url = '/view/'+manga_id+'/'+manga_name_formatted;
var viewer_url = '/viewer/'+manga_id+'/'+manga_name_formatted;

function previous_chapter()
{
	if(current_page<=1&&current_chapter_index+1==total_chapters)
	{
		document.location=view_url+'/';
	}
	else
	{
		document.location=viewer_url+'/'+(document.getElementById('top_chapter_list').options[current_chapter_index+1].value)+'/'+format_page(trim_number(previous_chapter_pages),1)+'/';
	}
	
	return false;
}

function next_chapter()
{
	if(current_page>=total_pages&&current_chapter_index===0)
	{
		document.location=view_url+'/';
	}
	else
	{
		document.location=viewer_url+'/'+(document.getElementById('top_chapter_list').options[current_chapter_index-1].value)+'/01/';
	}
	
	return false;
}

function previous_page()
{
	if(current_page<=1)
	{
		previous_chapter();
	}
	else
	{
		document.location=viewer_url+'/'+current_chapter+'/'+format_page(current_page-1,0)+'/';
	}
	
	return false;
}

function next_page()
{
	if(current_page>=total_pages)
	{
		next_chapter();
	}
	else
	{
		document.location=viewer_url+'/'+current_chapter+'/'+format_page(current_page+1,0)+'/';
	}

	return false;
}

function trim_number(s)
{
	return s.replace(/^0+/, '');
} 

function format_page(n,k) 
{ 
	if(k === 0)
	{
		n = n.toString();
	}
	
	var pd = ''; 
	if (n.length < 2) 
	{ 
		for (i = 0; i < (2-n.length); i++) 
		{ 
			pd += '0'; 
		} 
	}
	return pd + n; 
} 