document.observe('dom:loaded', function() {
	var remainingspace = 940;
	var levelone = $('mainNav').getElementsByClassName('level1');
	for (var count = 0; count<levelone.length; count++) {
		var secondnavlevel = levelone[count].getElementsByClassName('level2Container');
		if (secondnavlevel[0]) {
			var leveltwo = secondnavlevel[0].getElementsByTagName('li');
			var leveltwolinks = secondnavlevel[0].getElementsByTagName('a');
			if (leveltwo[0]) {
				
				secondnavlevel[0].setStyle({
					display: 'block'
				});
				
				var number = leveltwo.length;
				var cols = 0;
				
				if (number <= 6) {
					cols = 1;
				} else if (number <= 12) {
					cols = 2;
				} else if (number <= 18) {
					cols = 3;
				} else if (number <= 24) {
					cols = 4;
				}
				var height = 13;
				var maxheight = 0;
				for (var counter = 0; counter<leveltwo.length; counter++) {
					if ((counter / 6) < 1 ){
						leveltwo[counter].className = leveltwo[counter].className + ' firstcol';
					} else if ((counter / 6) < 2 ){
						leveltwo[counter].className = leveltwo[counter].className + ' secondcol';
					} else if ((counter / 6) < 3 ){
						leveltwo[counter].className = leveltwo[counter].className + ' thirdcol';
					} else if ((counter / 6) < 4 ){
						leveltwo[counter].className = leveltwo[counter].className + ' fourthcol';
					}
					
					if ((counter / 6) >= (cols - 1)) {
						leveltwo[counter].className = leveltwo[counter].className + ' last';
					}
					
					if (counter == 0 || counter == 6 || counter == 12 || counter == 18) {
						leveltwo[counter].className = leveltwo[counter].className + ' top';
					}
					
					if (counter == 5 || counter == 11 || counter == 17 || counter == 23) {
						leveltwo[counter].className = leveltwo[counter].className + ' bottom';
					}
					
					if ((counter + 1) == leveltwo.length && cols == 1) {
						leveltwo[counter].className = leveltwo[counter].className + ' bottom';
					}
					
					if ((counter % 6) == 0) {
						height = 7;
					}
					
					var newtop = height + 'px';
					
					height = height + leveltwolinks[counter].offsetHeight;
					
					
					leveltwo[counter].style.top = newtop;
					
					if (height > maxheight) {
						maxheight = height;
					}
				}
				
				maxheight = maxheight +'px';
				
				
				var navwidth = ((cols - 1) * 226) + 227;
				navwidth = navwidth + 'px';
				
				if (count == 0) {
					secondnavlevel[0].setStyle({
						width: navwidth,
						height: maxheight,
						display: 'none',
						left: '0px'
					});
				} else if ((((cols - 1) * 226) + 227) > remainingspace) {
					secondnavlevel[0].setStyle({
						width: navwidth,
						height: maxheight,
						display: 'none',
						right: '0px'
					});
				} else {
					secondnavlevel[0].setStyle({
						width: navwidth,
						height: maxheight,
						display: 'none',
						left: '-1px'
					});
				}
				
				remainingspace = remainingspace - levelone[count].offsetWidth;
			}
		}
	}
});

