/*
 *	jQuery ttexpander
 *	copyright 2009
 *
 *	@author 	Boy van Amstel
 *	@version 	1.0
 *
 *	Changes 1.0:
 *	[06/01/2010]	- First release 
 *
 */
 
jQuery.ttexpander = 
{
	build : function(options) {

		try {
		 
			// Default settings
			var defaults = {
				target				: 	''
				,display			:	3
				,per_click			:	3
				,list				: 	'li'
				,reached_class		:	"reached"
			}
	
			// Move to options
			var options = jQuery.extend(defaults, options);
			
			var object = this;			

			var total = jQuery(options.target).find(options.list).length;

			jQuery(options.target).find(options.list).each(function(i, item) {
				if(i >= options.display) jQuery(item).hide();
			});
			
			// Add custom functions
			var extensions = {
				expand		: 	function() { 
				
					var last = jQuery(options.target).find(options.list).index(jQuery(options.target).find(options.list +':hidden')[0]);

					jQuery(options.target).find(options.list).each(function(i, item) {
						var index = jQuery(options.target).find(options.list).index(item);
						if(jQuery(item).is(':hidden') && index < (last + options.per_click)) {
							jQuery(item).fadeIn(800);
						}
						if(last + options.per_click >= total) {
							jQuery(object).addClass(options.reached_class);
						}
					});

				}
			}
			jQuery.extend(object, extensions);
			
			jQuery(object).click(function() {
				extensions.expand();	
				return false;		
			});
			
			return this;
		}
		catch(err) {
			return false;
		}
	}
};

jQuery.fn.extend({
		ttexpander: jQuery.ttexpander.build
});	