jQuery(document).ready(function() {
	
	// only display 3 items
	$('div#latest-blogs ul.list-entries li:gt(2)').hide();
	
	// onely display 4 items
	//$('div#featured-books ul.list-entries li:gt(3)').hide();
	
	// onely display 5 items
	//$('ul#list-authors ul li:gt(4)').hide();
	
/*---------------------------------------------------------------------------*/
	
	var pagination = function(selector, carousel, position) {
		var options = carousel.options, self = $(selector);
		var current = Math.ceil(position / options.scroll);
		var length = Math.ceil(options.size / options.scroll);
		
	/*-------------------------------------------------------------------------
		Find Range:
		
		This code makes sure that the range of pages is always five long,
		unless there are less than five pages.
	-------------------------------------------------------------------------*/
		
		var range = {
			start: current - 2,
			stop: current + 2,
			page: 0
		};
		
		if (range.start < 1) {
			range.stop -= range.start - 1;
			range.start = 1;
		}
		
		if (range.stop > length) {
			range.start += length - range.stop;
			range.stop = length;
		}
		
		if (range.start < 1) range.start = 1;
		if (range.stop > length) range.stop = length;
		
		range.page = range.start;
		
	/*-------------------------------------------------------------------------
		Update:
	-------------------------------------------------------------------------*/
		
		// Clear:
		self.find('li.page, li.spacer, li.position').remove();
		
		// Show current:
		self.append('<li class="position">Page ' + current + ' of ' + length + '</li>');
		
		// Append ... prefix:
		if (range.start > 1) {
			self.append('<li class="spacer">...</li>');
		}
		
		// Draw pages:
		while (range.page <= range.stop) {
			var type = 'page', page = range.page;
			
			if (range.page == current) type += ' current';
			
			// Append page:
			self.append('<li class="' + type + '"><a href="#">' + page + '</a></li>');
			
			// Add behaviour:
			self.find('li:last a').click(function() {
				carousel.scroll(Math.floor(
					jQuery.jcarousel.intval($(this).text())
					* options.scroll
				) - (options.scroll - 1));
				
				return false;
			});
			
			range.page++;
		}
		
		// Append ... suffix:
		if (range.stop < length) {
			self.append('<li class="spacer">...</li>');
		}
		
	/*-----------------------------------------------------------------------*/
	};
	
/*---------------------------------------------------------------------------*/
	
	$('#authors .options').prepend('<ul class="pagination"></ul>');
	
	$('#authors #list-authors').jcarousel({
		scroll: 5,
		itemVisibleInCallback: function(carousel, item, position) {
			pagination('#authors .pagination', carousel, position);
		}
	});
	
/*---------------------------------------------------------------------------*/
	
	$('#featured').append('<ul class="pagination"></ul>');
	
	$('#featured ul:first').jcarousel({
		scroll: 1,
		itemVisibleInCallback: function(carousel, item, position) {
			pagination('#featured .pagination', carousel, position);
		}
	});
	
/*---------------------------------------------------------------------------*/
	
	$('div#featured-books ul').jcarousel({
		scroll : 2
	});
	
	$('ul#list-authors li h3').each(function(n){
		var date = $(this).attr("title");
		if (date == "NaN") {
			$(this).attr("title", "0");
		}
	});
	
	$('div.options li.sort-az a').click(function() {
	
		if($(this).hasClass('active')) return false;
		
		sort_authors(sort_alpha_desc);
		
		$('div.options a').removeClass('active');
		$(this).addClass('active');
		
		return false;
	
	});
	
	$('div.options li.sort-za a').click(function() {

		if($(this).hasClass('active')) return false;

		sort_authors(sort_alpha_asc);
		
		$('div.options a').removeClass('active');
		$(this).addClass('active');
		
		return false;
	
	});
	
	$('div.options li.sort-active a').click(function() {

		if($(this).hasClass('active')) return false;

		sort_authors(sort_activity);
		
		$('div.options a').removeClass('active');
		$(this).addClass('active');
		
		return false;
	
	});
	
	// set initial sort order
	$('div.options li.sort-active a').click();
	
	// remove search query text on click
	$("input#q")
	.each(function(){ if(this.value == '') this.value = this.title; })
	.focus(function(){ if(this.value == this.title) this.value = ''; })
	.blur(function(){ if(this.value == '') this.value = this.title; })
	.click(function(){ if(this.value == this.title && this.title != '') this.value=''; });
	
});


function sort_alpha_desc(a,b){
	return (a.name < b.name) ? -1 : 1;	
}

function sort_alpha_asc(a,b){
	return (b.name < a.name) ? -1 : 1;	
}

function sort_activity(a,b){
	return (a.date > b.date) ? -1 : 1;	
}

function sort_authors(filter) {

	$('ul#list-authors li').each(function(n) {	
		$(this).attr('id', ('author-'+n))	
	});

	var tmp = []; 
	
	$('ul#list-authors li').find('h3').each(function(n){		
		tmp.push({		
			index: n,
			name: $(this).text(),
			date: parseInt($(this).attr("title"))
		});
	}); 
		
	tmp.sort(filter);
	
	var data = '';
	
	for(i=0; i<tmp.length; i++){ 	
		$("ul#list-authors").append($("ul#list-authors li#author-"+tmp[i].index));
	} 
	
}

