(function($) {
	
	var $skizzlist = $.fn.skizzlist = function(maxLines, options) {

		options = $.extend({}, $skizzlist.defaults, options);
		maxLines = parseInt(maxLines);
		
		this.each(initialize);
		
		function initialize() {
			
			var self = $(this);
			
			var paragraph = self.children('p:first-child');
			var lineHeight = parseInt(paragraph.css('line-height'));
			var lines = Math.ceil(paragraph.height() / lineHeight);
			var hasScoll = lines > maxLines;
			var scrollStep = maxLines - 1;
			var currentLine = 1;
			
			function animateTo(line) {
				currentLine = line;
				updateElements();
				paragraph.animate({marginTop: (currentLine - 1) * -lineHeight}, 500, 'easeOutBack');
			}
			function onPreviousClick() {
				if (!this.disabled) {
					animateTo(currentLine - scrollStep);
				}
			}
			function onNextClick() {
				if (!this.disabled) {
					animateTo(currentLine + scrollStep);
				}
			}
			function updateElements() {
				
				var previousDisabled 	= currentLine <= 1;
				var nextDisabled 		= currentLine > lines - (maxLines - 1);
				
				elements.previous.get(0).disabled 	= previousDisabled;
				elements.next.get(0).disabled 		= nextDisabled;
				
				elements.previous.css({
					color: previousDisabled ? '#CCC' : '#000',
					cursor: previousDisabled ? 'default' : 'pointer'
				});
				elements.next.css({
					color: nextDisabled ? '#CCC' : '#000',
					cursor: nextDisabled ? 'default' : 'pointer'
				});
			}
			
			var elements = {
				
				previous: $('<span />')
						.click(onPreviousClick)
						.text('< '),
						
				separator: $('<span />')
						.text('/'),
				
				next: $('<span />')
						.click(onNextClick)
						.text(' >')
			};
			
			self.css({
				overflow: 'hidden',
				position: 'relative'
			});
			
			if (hasScoll) {
				self
					.css('height', (((maxLines+1)*lineHeight)+2)+'px')
					.append(
						$('<div />')
							.attr('class', 'bottombar')
							.css({
								height: (lineHeight+2)+'px',
								width: '100%',
								textAlign: 'right',
								position: 'absolute',
								bottom: 0,
								left: 0,
								backgroundColor: '#fff'
							})
							.append(elements.previous)
							.append(elements.separator)
							.append(elements.next)
					);
				updateElements();
			}
		}
		
		return this;
	};
	
})(jQuery);

