/*

Used with the horizontal sliding comments.
In "appel a tous" for example

*/

$.fn.comments = function(settings) {
	settings = jQuery.extend({
				animationSpeed: 'normal', /* fast/slow/normal */
				itemsPerPage : 1
			}, settings);

	$(this).each(function(){
		var $listToSlide = $(this).find('ul:first');
		var currentPage = 1;
		
		// Find the highest comment
		maxHeight = 0;
		$listToSlide.find('li').each(function(){ maxHeight = ($(this).find('p:first').height() > maxHeight) ? $(this).find('p:first').height() : maxHeight; });
		$listToSlide.find('li').css({'overflow':'hidden','height':maxHeight + 15});
		$listToSlide.height(maxHeight + 30)
		
		var pageCount = $listToSlide.find('>li').size() / settings.itemsPerPage;
		var itemWidth = $listToSlide.find('li:first').width() + parseFloat($listToSlide.find('li:first').css('padding-left')) + parseFloat($listToSlide.find('li:first').css('padding-right')) + parseFloat($listToSlide.find('li:first').css('margin-right'));
		var pageWidth = itemWidth * settings.itemsPerPage;
			
		// Fix the list
		$listToSlide.find('>li').each(function(i){
			$(this).css({
				'position':'absolute',
				'left': i*itemWidth,
				'top':0
			});
		});
			
		// Find the paging nav
		var $paging = $listToSlide.nextAll('.paging:first');
		
		if($listToSlide.find('>li').size() > 1) {
			$paging.find('li:eq(1)').show();
		
		
			$paging.find('>li:not(.prev):not(.next) p').each(function(i){
				$(this).css({
					'position':'absolute',
					'left': (i*155) + 18,
					'top':0
				});
			});
		
			// Bind the functions
			$paging.find('li.next a').click(function(){
				_goToPage(this,'up');
				return false;
			});
		
			$paging.find('li.prev a').click(function(){
				_goToPage(this,'down');
				return false;
			});
		
			_selectItem(null,currentPage);
		}else{
			$paging.find('li:first,li:last').hide();
			$paging.find('li:eq(1)').show();
		}
		
		// Bind the remaining char function
		$listToSlide.nextAll('form').find('.characters').each(function(){
			$(this).data('maxChar',eval($(this).find('span.count').text()));

			$(this).prev().keyup(function(){
				var $maxCharContainer = $(this).next().find('span.count');
				charCount = $maxCharContainer.parent().data('maxChar') - $(this).val().length;

				if(charCount <= 1){
					charCount = 0;
					$maxCharContainer.parent().find('.plural').hide();
				}else{
					$maxCharContainer.parent().find('.plural').show();
				}
				
				$maxCharContainer.text(charCount);
			});
		});
		
		function _selectItem(caller,which) {
			if($(caller).parent().hasClass('disabled')) return;

			// Find the paging nav
			$paging = $listToSlide.nextAll('.paging:first');

			// Disable the next/previous if needed.
			$paging.find('li.disabled').removeClass('disabled');
			if(currentPage == 1){
				$paging.find('li.prev').addClass('disabled');
			}else if(currentPage == pageCount){
				$paging.find('li.next').addClass('disabled');
			};
		};
		
		function _goToPage(caller,which) {
			if($(caller).parent().hasClass('disabled')) return;

			// Define where and how to move.
			if(which == 'up'){
				currentPage++;
			}else if(which == 'down'){
				currentPage--;
			}else{
				currentPage=which;
			}

			// Slide the page
			$listToSlide.find('>li').each(function(i){
				$(this).stop().animate({'left': (i * itemWidth) + (pageWidth * -(currentPage-1))});
			});
			
			// Slide the commenter name
			$paging.find('>li:eq(1) p').each(function(i){
				$(this).stop().animate({'left': (i * 155) + (155 * -(currentPage-1)) + 18});
			});

			// Select the item
			_selectItem(caller,currentPage);
		};
	});

}
