$(document).ready(function(){
	
	//wieviele einträge pro seite
	var show_per_page = 12; 
	//zählen der childrens inerhalb und der seiten...
	var number_of_items = $('div.picgal div.images dl').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="prevgal"><a class="previous_link" href="javascript:previous();">« zurück</a></div><div id="pagegal">';
	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 +'">'+ (current_link + 1) +'</a>';
		current_link++;
	}
	navigation_html += '</div><div id="nextgal"><a class="next_link" href="javascript:next();">vor »</a></div>';
	
	$('#page_navigation').html(navigation_html);
	
	//active auf das erste element
	$('#pagegal a:first').addClass('active');
	
	//erst verstecken aller elemente / Childrens
	$('div.picgal div.images dl').children().css('display', 'none');
	//und dann die zeigen die da sein müssen
	$('div.picgal div.images dl').children().slice(0, show_per_page).css('display', 'block');
});

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;
	$('div.picgal div.images dl').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);
}




