/**
 * Adjust column widths of blurbs in the blurb module to optimize the display on the digital collections site.
 *
 * This script assumes that each blurb contains an unordered list of items - the width of each item should be consistent.
 *
 * @author Nathan White
 */
		
$(document).ready(function()
{
	adjustColumns();
	$(window).resize(adjustColumns);
	
	/**
	 * Figures out the best number of columns per blurb based on available screen space and the number of items per column and
	 * sets the width of each blurb accordingly.
	 */
	function adjustColumns()
	{
		$("div.blurbs").each(function()
		{
			var blurbs = new Array();
			var blurb_item_count = new Array();
			var columns_per_blurb = new Array();
			var total_item_count = 0;
			var max_list_item_width = 0;

			$("div.blurb", this).each(function( index )
			{
				blurbs[index] = $(this);
				$("li", this).each(function( index2 )
				{
					item_width = $(this).outerWidth(true ); // the true parameter includes margins in the calculation
					max_list_item_width = (item_width > max_list_item_width) ? item_width : max_list_item_width;
					blurb_item_count[index] = (index2 + 1);	
				});			
				total_item_count = (total_item_count + blurb_item_count[index]);
			});
			
			blurb_container_width = ($(this).innerWidth() - 1);
			max_num_columns = ( (max_list_item_width > 0) && (blurb_container_width > 0) ) ? Math.floor(blurb_container_width / max_list_item_width) : 0;
			total_columns = 0;
			
			if (max_num_columns > 0) // if we were able to find a reasonable max number of columns, lets apply it
			{
				column_width = Math.floor(blurb_container_width / max_num_columns);
				for (blurb_index in blurbs)
				{
					blurb = $(blurbs[blurb_index]);
					col_percent = (blurb_item_count[blurb_index] / total_item_count);
					columns_per_blurb[blurb_index] = Math.round(col_percent * max_num_columns);
					total_columns = total_columns + columns_per_blurb[blurb_index]
				}
				while (total_columns > max_num_columns) // trigger code to reduce columns if needed (should rarely be but could be due to rounding errors)
				{
					columns_per_blurb = removeColumns(columns_per_blurb, blurb_item_count);
					total_columns--;
				}
				for (blurb_index in blurbs)
				{
					new_width = column_width * columns_per_blurb[blurb_index];
					$("object", blurbs[blurb_index]).css("width", new_width); // this is basically only needed for IE6 but should not hurt
					$(blurbs[blurb_index]).width(new_width);
				}
			}
		});
	}
	
	/**
	 * Remove a column from the blurb with the most columns and fewest items
	 */
	function removeColumns(columns_per_blurb, blurb_item_count)
	{
		Array.max = function( array )
		{
			return Math.max.apply( Math, array );
		};
		var column_index_to_reduce = undefined;
		var target_columns = Array.max(columns_per_blurb);
		for (index in columns_per_blurb)
		{
			if (columns_per_blurb[index] == target_columns) // candidate column
			{
				column_index_to_reduce = (column_index_to_reduce == undefined) ? index : column_index_to_reduce;
				column_index_to_reduce = (blurb_item_count[index] < blurb_item_count[column_index_to_reduce]) ? index : column_index_to_reduce;
			}
		}
		columns_per_blurb[column_index_to_reduce]--;
		return columns_per_blurb;
	}
});
