 /*
 * sdNavColumns
 * This script is a plugin for jQuery
 *
 * To be used with drop down navigation menus where the user has a
 * large amount of links and wants to be able to display those links
 * in a column format.
 *
 ** Example Uses **
 *
 * With defaults:
 *
 *	$('ul').sdNavCol();
 *
 * Custom Options:
 *
 *  $('ul').sdNavCol({
 *		colCount: 5,
 *		liSize: 15
 *	});
 *
 * Copyright (c) 2010 Sliced Design
 * Author: Jeremy Hamel
 * Version: 1.0 (JUN 21, 2010)
 * jQuery Version: 1.3.2 (tested)
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html 
 *
 * http://www.sliceddesign.com
 */

(function($){ 
	
	$.fn.sdNavCol = function(opts) {  
            var defaults = {  
                colCount: 3, //number of columns to be displayed
				liSize: 10, // if number of li is greater than this number, split into multiple columns; otherwise, do nothing
				tableClass: 'tblNavCol', // ID given to the table holding the columns
				columnClass: 'col' // class given to each <td> within the table
            };  
            
            var opts = $.extend(defaults, opts);
			
			$(this).children().each(function() {
			
				var obj = $(this);
				
				// count the number of li in the ul
				var liCount = $('ul', obj).children().size();
	
				// if number of li is greater than the defined liSize, create columns
				if(liCount > opts.liSize){
					var i = 0;
					var count = parseInt(liCount / opts.colCount);
					var remainder = liCount - (count * opts.colCount);
					
					// array for storing columns and their size
					var col = new Array();
					
					// loop through and create array indexes for columns and add count values
					for (i=0;i<opts.colCount;i++) {
						col[i] = count;
						if(i > 0) 
						{
							col[i] += col[i-1];
						}
						if(remainder > i){
							col[i] += 1;
						}
					}
					
					// remove the <li> from around the links
					$('ul', obj).children().each(
						function(i){
							$(this).replaceWith($(this).html());
					 });
				
					// add column identifiers for each of the <a>
					$('ul', obj).children().each(
						function(j){
							if(j < col[0])
							{
								$(this).addClass('sdCol0');	
							}
							else 
							{
								for (i=1;i<opts.colCount;i++) {
									if(j >= col[i-1] && j < col[i])
									{
										$(this).addClass('sdCol'+ i);	
									}
									else if(j >= col[opts.colCount[i-1]])
									{
										$(this).addClass('sdCol'+ i);	
									}
								}
							}
					 });
					
					// wrap all links for each column in <td>
					for (i=0;i<opts.colCount;i++) {
						$('a.sdCol'+ i, obj).wrapAll('<td class="'+ opts.columnClass +'"></td>');
					}
					
					// remove added classes from <a>
					for (i=0;i<opts.colCount;i++) 
					{
						$('a', obj).removeClass('sdCol'+ i);
					}
					
					// add first & last classes to the correct columns
					$('td.col:first', obj).addClass('first');
					$('td.col:last', obj).addClass('last');
					
					// wrap all the created <td>s within a <li> and <table> for correct HTML  
					$('td.col', obj).wrapAll('<li><table class="'+ opts.tableClass +'"><tr></tr></table></li>');
					
					// remove nested ul as this causes layout issues
					$('ul', $('td.col', obj)).remove();
					
					
					
				}    
			});
	};	
})(jQuery);      
