var MultiColumnList = function(element, node, columnMax, items){
	//our 'constructor'
	this.element = document.getElementById(element).getElementsByTagName(node);
	this.length = this.element.length;
	this.columnMax = columnMax;
	this.items = items;
	this.display();
}
MultiColumnList.prototype.getItemsPerColumn = function() {
	var items = this.items;
	var itemMax = this.columnMax * items;
	if (this.length > itemMax) {
		var remainingItems = this.length - itemMax;
		items = items + Math.ceil(remainingItems / this.columnMax);
	}
	else {
		items = this.length;
	}
	return items;
}
MultiColumnList.prototype.display = function() {
	this.setMaxColumnHeight();
	this.setColumnMargin();
}
MultiColumnList.prototype.setColumnList = function() {
	var k = 0;
	var itemsPerColumn = this.getItemsPerColumn();
	var columnMax = 1;
	columnMax = itemsPerColumn > this.items ? this.columnMax : columnMax;
	var a = [columnMax];
	for (i=0; i < columnMax; i++) {
		var l = 0;
		a[i] = [];
		for (j=k; j < itemsPerColumn + k; j++) {
			if (this.element[j]) {
				a[i][l] = this.element[j];
			}
			l = l + 1;
		}
		k = k + itemsPerColumn;
	}
	return a;
}

MultiColumnList.prototype.getColumnHeight = function() {
	var heights = [];
	var columnHeight = 0;
	var columnList = this.setColumnList();
	for (i=0; i < columnList.length; i++) {
		columnHeight = 0;
		for (j=0; j < columnList[i].length; j++) {
			columnList[i][j].className = "multi-column-" + (i + 1);
			columnList[i][j].style.height = columnList[i][j].offsetHeight + "px";
			columnHeight = columnHeight + columnList[i][j].offsetHeight;
		}
		heights[i] = columnHeight;
	}
	return heights;
}
MultiColumnList.prototype.setMaxColumnHeight = function() {
	var maxHeight = 0;
	var remainder = [];
	var element = this.getColumnHeight();
	for (i=0; i < element.length; i++) {
		maxHeight = element[i] > maxHeight ? element[i] : maxHeight;
		this.element[i].parentNode.style.height = maxHeight + 'px';
	}
}

MultiColumnList.prototype.setColumnMargin = function() {
	var element = this.setColumnList();
	var columnHeight = this.getColumnHeight();
	for (i=0; i < element.length; i++) {
		if (i > 0) {
			if (element[i][0]) {
				element[i][0].style.marginTop = "-" + columnHeight[i - 1] + "px";
			}
		}
	}
}


function displayMultiListColumns() {
	if (document.getElementById('concierge-features')) {
		var multiColumnList = new MultiColumnList('concierge-features', 'li', 2, 3);
	}
	if (document.getElementById('featured-cities')) {
		var multiColumnList = new MultiColumnList('featured-cities', 'li', 2, 10);
	}
	if (document.getElementById('property-list')) {
		var multiColumnList = new MultiColumnList('property-list', 'li', 3, 3);
	}
	if (document.getElementById('terms-list')) {
		var multiColumnList = new MultiColumnList('terms-list', 'li', 2, 2);
	}	
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
addLoadEvent(displayMultiListColumns);