/**
 * jQuery readMoreLess plugins
 * @author Jonathan Dextraze <jonathan.dextraze@isiconseil.com>
 */
jQuery.fn.readMoreLess = function(opts) {
	// Options
	opts = jQuery.extend({
		useHeight: false,		// Use current height or line-height * line
		speed: 0,				// Speed of the animation
		line: 7,				// Number of line to show at start
		readMore: 'READ MORE',	// Read more text
		readLess: 'READ LESS',	// Read less text
		btnClass: ''			// Additional class for the button
	},opts||{});
	
	return this.each(function() {
		var self = this,
			collapsed = true,
			expandedHeight = this.scrollHeight,
			lineHeight = parseInt($(this).css('line-height')),
			collapsedHeight = opts.useHeight ? $(this).height() : (lineHeight * opts.line),
			jBtn = null,
			speed = opts.speed == 0 ? (100 + expandedHeight - collapsedHeight) : opts.speed;
		if (expandedHeight > collapsedHeight) init();
		/**
		 * Method called to initialise
		 */
		function init() {
			$(self).css({height:collapsedHeight,overflow:'hidden'}).
				after('<a href="#" class="readMoreButton ' + opts.btnClass + '">' + opts.readMore + '</a>');
			setTimeout(function() {
				jBtn = $(self).next();
				jBtn.click(function() { self.readToggle(); return false; });
			}, 13);
		}
		/****** External methods ******/
		/**
		 * Expand the zone to read more
		 */
		this.readMore = function() {
			$(this).animate({height:expandedHeight},speed,function() {
				collapsed = false;
				jBtn.html(opts.readLess);
			});
		};
		/**
		 * Collapse the zone to read less
		 */
		this.readLess = function() {
			$(this).animate({height:collapsedHeight},speed,function() {
				collapsed = true;
				jBtn.html(opts.readMore);
			});
		};
		/**
		 * Toggle zone between more and less
		 */
		this.readToggle = function() {
			if (collapsed) this.readMore();
			else this.readLess();
		};
	});
};

