
// Listify is a jQuery plugin that turns big lists into easy
// to manage expandable smaller lists.
(function($){
	
	$.fn.listify = function( count ) {
		
		// default count is 3
		count = count || 3;
	
		$(this).each(function(){
	
			var $this = $(this);
			
			var lis = $this.children("li");
			var lisCount = lis.size();
			var moreMessage = $this.attr("mr:listifymoremessage") || "Show more...";
			var thisCount = $this.attr("mr:listifycount") || count;
			
			if (lisCount > thisCount) {
				
				// hide the extra items
				lis.each(function(itemIndex){
					
					if (itemIndex > thisCount-1) {
						$(this).hide().addClass("listify-excess");
					}
					
				});
				
				// add a 'more' list item
				$this.append(
					$("<li/>")
						.addClass("listify-more-items")
						.append(
							$("<a/>")
							
								// this link doesn't go anywhere
								.attr("href", "javascript:void(0);")
								
								// set the text
								.text(moreMessage)
								
								.toggle(function(){
									
									// show
									$(this)
										.text("Hide some")
										.parents(".listify").eq(0).children("li").slideDown();
									
									
								}, function(){
									
									// hide
									$(this)
										.text(moreMessage)
										.parents(".listify").eq(0).children("li.listify-excess").slideUp();
									
								})
								
						)
				)
				
				// add a print-only 'more available online' list item
				$this.append(
					$("<div/>").addClass("more-online").text("more available online...")						
				)
				
			}
		
		});
	
	};
})(jQuery);

// get going when the page is ready...
$(function(){
	
	// show javascript only elements
	$(".javascript-only").show();

	// listify the lists!
	$(".listify").listify(3);
	
});

