/*
 * @descrition: Fix columns where fewer than desired columns exist
 * @author: David Sole
 * @copyright: Mobile Electronics Australia. All rights reserved
 */
;(function($){
	$.fn.fixcols = function(op){
		this.each( function(i,e) {
			e = $(e);
			var cols = e.children("div.col");
			var numCols = cols.length;
			numCols = numCols < 2 ? 2 : numCols;
			
			if( !e.hasClass("cols-"+numCols) )
			{
				e.removeClass("cols-1 cols-2 cols-3 cols-4 cols-5 cols-6").addClass("cols-"+numCols);
			}
			
			// fix up the heights of any rows.
			var minRows = 0;
			cols.each( function( c, ce ) {
					var numRows = $(ce).find('div.rows div.row').length;
					if( numRows < minRows || minRows == 0)
					{
						minRows = numRows;
					}
				}
			);
			
			if( minRows > 0 )
			{
				cols.find('div.rows div.row:gt('+(minRows-1)+')').remove();
			}
			for( var ri = 1; ri <= minRows; ri++ )
			{
				var maxHeight = 0;
				cols.find('div.rows div.row:nth-child('+ri+')').each(
					function( sri, el ) {
						var height = $(el).height();
						if( height > maxHeight)
						{
							maxHeight = height;
						}
					}
				);
				
				cols.find('div.rows div.row:nth-child('+ri+')').each(
						function( sri, el ) {
							$(el).height(maxHeight);
						}
					);
			}
			
			
		} );
	};
	
	var fc = $.fn.fixcols;
})(jQuery);

