// TEMP: this didn't end up being used anywhere. deprecated?

/* Scrollable book box widget (daniel@mindworks.ee)
 *
 * Example usage:
 *
 *	<div><a class="bboxScroller" href="javascript:void(0);" onclick="jQuery.scrollBookBox('bbox', 'up', 500);">Previous</a></div>
 *	<div id="bboxContainer" class="sidebar_browse_items_scrollable"></div>
 *	<script type="text/javascript">  
 *	$().ready(function() {  
 *		$('#bboxContainer').renderScrollableBookBox({
 *			dataSource: 'http://mysite.com/booksJsonOutput.php',
 *			id: 'bbox'
 *		});  
 *	});  
 *	</script>
 *	<div><a class="bboxScroller" href="javascript:void(0);" onclick="jQuery.scrollBookBox('bbox', 'down', 500);">Next</a></div>
 *
 */
 
jQuery.extend({
	scrollBookBox: function(id, direction, duration) {
		// get dimensions of element
		var height = parseInt($('#' + id + 'Item0').outerHeight(true));

		var visibleHeight = $('#' + id + 'Canvas').parent().innerHeight();
		var totalHeight = (parseInt($('#' + id + 'Canvas').outerHeight(true)) - visibleHeight);
		
		var currentTop = parseInt($('#' + id + 'Canvas').css('top'));
		if (isNaN(currentTop)) currentTop = 0;
		
		
		var scrollTopTo = ((direction == 'down') ? (currentTop - height) : (currentTop + height));
		
		if ((scrollTopTo > 10) || (Math.abs(scrollTopTo) > totalHeight)) return false;
		
		$('.' + id + 'Scroller').hide();
		$('#' + id + 'Canvas').animate({ 
			top: scrollTopTo + 'px'
		}, duration, function(){ $('.' + id + 'Scroller').show(); });
	}
});

$.fn.renderScrollableBookBox = function(options) {
	var defaults = {
		id: 'bbox'
	};
	var options = $.extend(defaults, options);

	return this.each(function() {
		var obj = $(this);
		obj.html('Loading...');
		
		// Create box container
		var container = $('<div/>').attr('id', options.id + 'Canvas').attr('class', 'sidebar_browse_items_scrollable_canvas');
		
		$.getJSON(options.dataSource,
			function(data){
				$.each(data.items, function(i,item){
					// create new book div
					var book = $('<div/>').attr('class', 'sidebar_browse_item').attr('id', options.id + 'Item' + i);

					// create thumbnail div
					var thumb = $('<div/>').attr('class', 'sidebar_item_thumb');

					var author = ((item.author != null) ? ' | ' + item.author : '');
					var link = $('<a/>').attr('href', item.link).attr('title', item.title + author);

					$('<img/>').attr('src', item.image).attr('height', '93').attr('width', '65').appendTo(link);
					link.appendTo(thumb);
					thumb.appendTo(book);

					
					// create details div
					var details = $('<div/>').attr('class', 'sidebar_item_details');
					var title = $('<div/>').attr('class', 'sidebar_item_title');
					link = $('<a/>').attr('href', item.link).text(item.title);
					link.appendTo(title);
					title.appendTo(details);					
					details.append(item.author);
					details.appendTo(book);					

					// append book div to container
					$('<div/>').attr('class', 'c').appendTo(book);
					book.appendTo(container);
					$('<div/>').attr('class', 'c').appendTo(container);
				});

				
				obj.html(container);
			});
	});
};