// JavaScript Document

(function($){

	var methods = {
		init: function( options ){
			var settings = {
				columns: 3, // количество колонок
				minLines: 5, // мнимальное количество строк, после которого идет разбивка на колонки
				colBlockClass: 'columnBlock', // стиль блока колонки
				colClass: 'columnList', // стиль списка
				rowClass: 'columnListRow' // стиль строки
			};

			return this.each(function(){
				var list = $(this).html();
				if (options){
					$.extend(settings, options);
					options = settings;
				} else {options = settings}
				$(this).html(methods.columnize(options,list));
			});

		},
		columnize: function (options,list){
			var arrList = Array();
			arrList = list.split(/, /);
			var ll = arrList.length;
			var intRows = Math.ceil(ll/options.columns);
			var columnized = '';
			if (ll > 0 && intRows >= 1){
				var i = 1;
				do {
					var column = '';
					var limit = intRows * i;
					if (limit > ll){limit = ll};
					var blockOpen = '<div class="'+options.colBlockClass+'" id="col_'+i+'"><ul class="'+options.colClass+'" id="colList'+i+'">';
						for (var ii = (intRows*i)-intRows; ii < limit; ii++){column = column+('<li class="'+options.rowClass+'">'+arrList[ii]+'</li>')}
					var blockClose = '</ul></div>';
					columnized = columnized + (blockOpen + column + blockClose);

				} while(i++ < options.columns);
			}
			return columnized;
		}
	};

	$.fn.columnizer = function(method){
		if(methods[method]){
			return methods[method].apply( this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof method === 'object' || ! method){
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' + method + ' does not exists');
		}
	}

})(jQuery);
