/**
 * jQuery.autoshrinklist - Shrink display of listed itmes
 * @developer Angela Larson 
 * @Last Modification: 05/24/2010
 * @Notes: This plugin can be used to automatically shrink ul lists and provide a link for
 * expanding the link. The current default list of parameters are: 
 * maxLimit:  The manimum number of items to display     
 * expText: The text for the link to expand the displayed list
 * contText: The text to display to contract the displayed list
 * expClass: The class assigned to the link when displaying all items
 * contClass: The class assigned to the link when limiting the display
 * elmHideClass: The class assigned to the li element when hiding list elements
 * ***Currently this plugin only works with the ul list elements - Beware of trailing commas in definitions - 
 * trailing commas will break functionality in IE***
 * @examples:
 *	example1:
 *		<html>
 *			<head>
 *				<script type='text/javascript'>
 *					$(function() {
 *						$("#portlets ul").autoshrinklist();
 *					});
 *				</script>
 *			</head>
 *			<body>
 *				<div id='example' ></div>
 *			</body>
 *		</html>
 *
 *	example2:
 *		<html>
 *			<head>
 *				<script type='text/javascript'>
 *					$(function() {
 *						$.fn.autoshrinklist.defaults.maxLimit = 4
 *						$("#portlets ul").autoshrinklist();
 *					});
 *				</script>
 *			</head>
 *			<body>
 *				<div id='example' ></div>
 *			</body>
 *		</html>
 *
 *	example3:
 *		<html>
 *			<head>
 *				<script type='text/javascript'>
 *					$(function() {
 *						$("#portlets ul").autoshrinklist({maxLimit: 4 });
 *					});
 *				</script>
 *			</head>
 *			<body>
 *				<div id='example' ></div>
 *			</body>
 *		</html>
 */

(function($) {
   	$.fn.autoshrinklist = function(options) {
		//debug(this);

		var settings = $.extend({},$.fn.autoshrinklist.defaults,options);
		
		return this.each(function(){
								  
			var limit = settings['maxLimit'];
			var expandText = settings['expText'];
			var contractText = settings['contText'];
			var expandClass = settings['expClass'];
			var contractClass = settings['contClass'];
			var listElementHideClass = settings['elmHideClass'];
			var parentElement = settings['parListEl'];
			var childElement = settings['childListEl'];
			
			var totalLi = $(this).children('li');
			var childCount = totalLi.size();
			if(childCount <= limit){
				//Nothing to do
			}else{
				limit = limit-1;
				for(i=0; i<childCount; i++){
					if(i > limit){
						$(totalLi[i]).addClass(listElementHideClass);

						//if this is a dictionary list - add class to the dd sibling
						if(childElement == 'dt') {
							$(totalLi[i]).next('dd').addClass(listElementHideClass);
						}
					}
				}
				$(this).after('<a href="#" class="' + expandClass + '">' + expandText + '</a>');
			}
			
			var aLink = $(this).next('a.'+expandClass);
			aLink.each(function(){
				count = $(this).prev('ul').children().length;
				if(count <= (limit)){$(this).addClass(contractClass);}

				$(this).toggle(function(){
					$(this).addClass(contractClass);
					$(this).html(contractText);
					$(this).prev('ul').children('li.'+listElementHideClass).removeClass(listElementHideClass);
					//if dictionary list - remove class from dd element
					if(childElement == 'dt') {
						$(this).prev(parentElement).children(childElement).next('dd.'+listElementHideClass).removeClass(listElementHideClass);
					}
					return false;
				},function(){
					$(this).removeClass(contractClass);
					$(this).html(expandText);
					var totalLi = $(this).prev('ul').children('li');
					var childCount = totalLi.size();
					for(i=0; i<childCount; i++){
						if(i > limit){
							$(totalLi[i]).addClass(listElementHideClass);
							//if this is a dictionary list - add class to the dd sibling
							if(childElement == 'dt') {
								$(totalLi[i]).next('dd').addClass(listElementHideClass);
							}
						}
					}
					return false;
				});
			});
		});
	};
	
	// private function for debugging
	function debug($obj) {
		if (window.console && window.console.log)
	    	window.console.log('autoshrinklist selection count: ' + $obj.size());
	};
	
	$.fn.autoshrinklist.defaults = {
		maxLimit: 3,    
		expText: 'All Related Items',
		contText: 'Less Related Items',
		expClass: 'all',
		contClass: 'less',
		elmHideClass: 'hide',
		parListEl: 'ul',
		childListEl: 'li'
	};
	
})(jQuery);
