$(document).ready(function(){

	//how much items per character to show
	var show_per_character = 1;
	//getting the amount of elements inside content div
	var number_of_items = $('#characters').children().size();
	//calculate the number of characters we are going to have
	var number_of_characters = Math.ceil(number_of_items/show_per_character);

	//set the value of our hidden input fields
	$('#current_character').val(0);
	$('#show_per_character').val(show_per_character);

	//now when we got all we need for the navigation let's make it '

	/*
	what are we going to have in the navigation?
		- character to previous character
		- characters to specific characters
		- character to next character
	*/
	var navigation_html = '<a class="previous_character" href="javascript:previouscharacter();">Prev</a>';
	var current_character = 0;
	while(number_of_characters > current_character){
		navigation_html += '<a class="character_character" href="javascript:go_to_character(' + current_character +')" longdesc="' + current_character +'">'+ (current_character + 1) +'</a>';
		current_character++;
	}
	navigation_html += '<a class="next_character" href="javascript:nextcharacter();">Next</a>';

	$('#character_navigation').html(navigation_html);

	//add active_character class to the first character character
	$('#character_navigation .character_character:first').addClass('active_character');

	//hide all the elements inside content div
	$('#characters').children().css('display', 'none');

	//and show the first n (show_per_character) elements
	$('#characters').children().slice(0, show_per_character).css('display', 'block');

});

function previouscharacter(){

	new_character = parseInt($('#current_character').val()) - 1;
	//if there is an item before the current active character run the function
	if($('.active_character').prev('.character_character').length==true){
		go_to_character(new_character);
	}

}

function nextcharacter(){
	new_character = parseInt($('#current_character').val()) + 1;
	//if there is an item after the current active character run the function
	if($('.active_character').next('.character_character').length==true){
		go_to_character(new_character);
	}

}
function go_to_character(character_num){
	//get the number of items shown per character
	var show_per_character = parseInt($('#show_per_character').val());

	//get the element number where to start the slice from
	start_from = character_num * show_per_character;

	//get the element number where to end the slice
	end_on = start_from + show_per_character;

	//hide all children elements of content div, get specific items and show them
	$('#characters').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');

	/*get the character character that has longdesc attribute of the current character and add active_character class to it
	and remove that class from previously active character character*/
	$('.character_character[longdesc=' + character_num +']').addClass('active_character').siblings('.active_character').removeClass('active_character');

	//update the current character input field
	$('#current_character').val(character_num);
}

