/*
 * Using jQuery to split long lists
 * Copyright (c) 2007 Derek Ahmedzai (derek@proctors.co.uk)
 */

/*
jQuery 1.0.1 version
*/
$(document).ready(function(){

	$("ul.course-list").each(function(i){
	
		//get current list object
		var current_list = this;

		//get list items
		var list_items = $("li", this);
		var number_of_list_items = list_items.length;
		
		//ignore lists with less than 6 items
		if (list_items.length > 3)
		{
			//get mid-point to split list
			var split_at = Math.ceil(list_items.length / 2);
						
			//the order we wrap the divs is important
			//this one is first, even though it wraps the current list and all generated content.
			$(current_list).wrap("<div class=\"two-col\"></div>");
			
			//create new list
			$("<div class=\"col\"></div>").append('<ul></ul>').attr("id", "course-list-split-" + i).insertAfter(current_list);
			var new_course_list = $("#course-list-split-" + i + " ul").addClass("course-list");
			
			//split list
			//items_to_remove = $(list_items).slice(split_at);
			//jQuery 1.0.1 doesn't support .remove()
			//need to remove the items manually
			for (x = split_at; x < number_of_list_items; x ++)
			{
				new_course_list.append(list_items[x]);
			}

			//the order we wrap the divs is important
			//this one is last, even though it only wraps the current list
			$(current_list).wrap("<div class=\"col\"></div>");
		}
	});
});
