/*******

	***	Make Columns ***
	*** http://manic.com.sg  by roland ***
	
	hope to get to a time where i dont have to use this anymore,
	but while the lack of support for multicolumns on browsers this will suffice
	
*****/
(function($){
	$.fn.makecolumns = function(options) {
		var defaults = {
			colwidth: 180,
			ulheight: 500,
			doneFunc : function(){},
			float : "left",
			hTotalMargin:40,
			firstMargin:20,
			lineHeight:20,
			checkClass:false,
			btmOffset:40
		};
		
		$listContainer = $(this);
		var options = $.extend(defaults, options);
		
		// toggle hold next col
		$holdCol = false;
		
		// clone original list
		$list = $listContainer.find('ul').clone();
		
		// remove original ul
		$listContainer.find('ul').remove();
		
		var curul = 1;
		
		// create temp div's to get the element height and to contain the ul's and lists
		$('<div id="temp" style="position:absolute; left:-9999px;"></div>').appendTo('body');
		// create temp div's to get the element height and to contain the ul's and lists
		$('<div id="temp2" style="position:absolute; left:-9999px;"></div>').appendTo('body');
		
		// add the current ul to the div temp
		$('<ul class="col'+curul+'"></ul>').appendTo($('div#temp'));
		
		// assign the number of elements before doing any appending
		var noElem = $list.children().size();
		
		var totalH = 0;
		// iterate through all children on the list
		$list.children().each(function(index) {
			
			var $curCol = $('div#temp').find('.col'+curul);
			
			var $toAdd = $(this);
			// add to temp div to have a virtual height
			$toAdd.appendTo($('div#temp2'));
			
			
			if($toAdd.hasClass("h3")){
				if(curul==1 && totalH < options.hTotalMargin){
					totalH += options.firstMargin;
				}else{
					totalH += options.hTotalMargin;
				}				
			}
			
			totalH += options.lineHeight;
			
			// check if checkclass then add additional margin
			if(options.checkClass==true && $toAdd.hasClass(options.className)){
				totalH += 8;				
			}
			
			// check height, if it does not exceed add the element
			if(totalH < options.ulheight-options.btmOffset){
				$toAdd.appendTo($curCol);
			}else{
			// else add the ul to the container, create a new ul
				$lastelem = $curCol.children().last();
				
				if(options.checkClass==true && $lastelem.hasClass('h4')){
					$holdCol = true;
					$toAdd.appendTo($curCol);
				}else if(options.checkClass==true && $toAdd.hasClass(options.className)){
					$holdCol = true;
					$toAdd.appendTo($curCol);
					//alert('tae' + $lastelem.text());
				}else{
					$holdCol = false;
					
					$curCol.appendTo($listContainer);
					
					totalH = 0;
					curul++;
									
					$curCol = $('<ul class="col'+curul+'"></ul>');
					$curCol.appendTo($('div#temp'));
					
					if($lastelem.hasClass('h3')){
						$lastelem.appendTo($curCol);
						totalH += options.firstMargin;
						totalH += options.lineHeight;
					}
					
					$toAdd.appendTo($curCol);
				}
			}
			
			// add the last column in
			if(index == noElem-1){
				$curCol.css('margin-right', '0');
				$curCol.appendTo($listContainer);
			}
			
		});
		
		$listContainer.width(((curul) * (options.colwidth)));
		
		// remove temp div
		$('div#temp').remove();
		$('div#temp2').remove();
		
		return this;
	};

})(jQuery);
