$('document').ready(function() {

	// Fix IE z-index

		/*var zIndexNumber = 1000;
		$('div').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});*/
		
	
		
	// Subtitle fix
	
		var subtitle = $('#content > h2');
		$('#header').append(subtitle);
		$('#content > h2').remove();
	
		
	// Alternating table rows
	
		$('tbody tr').removeClass("alt-row"); // Remove all .alt-row classes
		$('tbody tr:even').addClass("alt-row"); // Add .alt-row to even table rows
		
	
	// Collapse content boxes:
	
		$('.content-box-header h3').click(function() {
			if($(this).parent().parent().hasClass('closed')) {
				$(this).parent().parent().children('.content-box-content').show();
				$(this).parent().parent().removeClass('closed');
			} else {
				$(this).parent().parent().children('.content-box-content').hide();
				$(this).parent().parent().addClass('closed');
			}
		});

	// Awesome tables with dynamically generated pagination
	
	
	/*<div class="pagination">
											<a href="#" rel="first">First</a>
											<a href="#" rel="1" class="graybutton">1</a>
											<a href="#" rel="2" class="graybutton">2</a>
											<a href="#" rel="3" class="graybutton">3</a>
											<a href="#" rel="last">Last</a>
										</div>*/
	
	
	
	
		var perPage = 10; // Number of entries per page
	
		$('table.pagination').each(function() {
			$(this).children("tbody").children().hide(); // Hide all table-entries
			var childrenCount = $(this).children("tbody").children().size(); // Get total count of entries
			var pageCount = Math.ceil(childrenCount / perPage);
			
			$(this).find("tfoot tr td").append('<div class="pagination" />');
			$(this).find(".pagination").append('<a href="#first" rel="first">First</a>');
			
			for(var i = 0; i < pageCount; i++) {
				$(this).find(".pagination").append('<a href="#" class="graybutton" rel="' + (i + 1) + '">' + (i + 1) + '</a>');
			}
			
			$(this).find(".pagination").append('<a href="#last" rel="last">Last</a>');
			
			for(var i = 0; i < perPage; i++) { // Loop through entries
				$(this).children("tbody").children("tr:nth-child(" + (i + 1) + ")").show(); // Show requested entry
				$(this).find("tfoot .pagination a:nth-child(2)").addClass("active");
			}
			
			$(this).find('tfoot .pagination a[rel="first"]').click(function() {
				$(this).siblings().removeClass("active");
				$(this).siblings('a:nth-child(2)').addClass("active");
				
				$(this).parent().parent().parent().parent().parent().children("tbody").children().hide();
				
				for(var i = 0; i < perPage; i++) {
					$(this).parent().parent().parent().parent().parent().children("tbody").children("tr:nth-child(" + (i + 1) + ")").show();
				}
				return false;
			});
			
			$(this).find('tfoot .pagination a[rel="last"]').click(function() {
				$(this).siblings().removeClass("active");
				$(this).siblings('a:nth-child(' + (pageCount + 1) + ')').addClass("active");
				
				$(this).parent().parent().parent().parent().parent().children("tbody").children().hide();
				
				for(var i = 0; i < perPage; i++) {
					$(this).parent().parent().parent().parent().parent().children("tbody").children("tr:nth-child(" + (i + (pageCount - 1) * perPage + 1) + ")").show();
				}
				return false;
			});
			
			$(this).find('tfoot .pagination a[href="#"]').click(function() {
				$(this).siblings().removeClass("active"); // Remove all .active classes
				$(this).addClass("active"); // Add class .active
				
				var offset = perPage * ($(this).attr("rel") - 1); // Define offset
			
				$(this).parent().parent().parent().parent().parent().children("tbody").children().hide(); // Hide all other entries
			
				for(var i = 0; i < perPage; i++) { // Loop required entries
					$(this).parent().parent().parent().parent().parent().children("tbody").children("tr:nth-child(" + (i + 1 + offset) + ")").show(); // Show requested entry
				}
				return false;
			});
		});
	
});
