$(document).ready(function(){
	$('li.hidden').remove();
	$(".menuebottom.first .listmenue").append('<div id="paginationone" class="pagination"><input type="hidden" id="current_page" /><input type="hidden" id="show_per_page" /><div id="page_navigation"></div></div>');
	
	//wieviele einträge pro seite
	var show_per_page = 15; 
	//zählen der childrens inerhalb und der seiten...
	var number_of_items = $('.pagin1').children().size();
	var number_of_pages = Math.ceil(number_of_items/show_per_page);
	
	$('#current_page').val(0);
	$('#show_per_page').val(show_per_page);
	
	var navigation_html = '<div id="prev"><a class="previous_link" href="javascript:previous();">« zurück</a></div><div id="pageinator">';
	var current_link = 0;
	while(number_of_pages > current_link){
		navigation_html += '<a class="page_link" href="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'"></a>';
		current_link++;
	}
	navigation_html += '</div><div id="next"><a class="next_link" href="javascript:next();">vor »</a></div>';
	
	$('#page_navigation').html(navigation_html);
	
	//active auf das erste element
	$('#pageinator a:first').addClass('active');
	
	//erst verstecken aller elemente / Childrens
	$('.pagin2').children().css('display', 'none');
	//und dann die zeigen die da sein müssen
	$('.pagin2').children().slice(0, show_per_page).css('display', 'block').addClass('first');

});
function previous(){
	new_page = parseInt($('#current_page').val()) - 1;
	if($('.active').prev('.page_link').length==true){
		go_to_page(new_page);
	} 
}
function next(){
	new_page = parseInt($('#current_page').val()) + 1;
	if($('.active').next('.page_link').length==true){
		go_to_page(new_page);
	}	
}
function go_to_page(page_num){
	var show_per_page = parseInt($('#show_per_page').val());
	start_from = page_num * show_per_page;
	end_on = start_from + show_per_page;
	$('.pagin1').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');
	/*get the page link that has longdesc attribute of the current page and add active class to it
	and remove that class from previously active page link*/
	$('.page_link[longdesc=' + page_num +']').addClass('active').removeClass('page_link').siblings('.active').removeClass('active').addClass('page_link');
	//update von der seitennr
	$('#current_page').val(page_num);
}
