if(!(/\/admin\//.test(window.location.pathname)))
{
	jQuery(function(){
		//var navWidth = jQuery('#nav').width();
		// [CPOD] Hardcoded this due to IE bugs
		var navWidth = 891;
		var navItems = jQuery('#nav>li');
		
		// Function to help get the width
		
		var getWidthOf = function(el) {
			var w = 0;
			try
			{
				w = el.getWidth();
				if(w==0) throw "bleh";
				return w;
			} catch(ex) {}
			
			try
			{
				w = jQuery(el).width();
				if(w==0) throw "bleh";
				return w;
			} catch(ex) {};
			
			try
			{
				w = el.offsetWidth;
				if(w==0) throw "bleh";
				return w;
			} catch(ex) {};
			
			return 20;
		};
		
		
		// Some CSS Hacks
		
		jQuery('li.level0>a>span, li.level0>span>a', '#nav').addClass('topLevelNavItem');
		jQuery('li.level0>a, li.level0>span', '#nav').addClass('topLevelNavWrapper');
		
		
		// Identify the rows
		var navRows = [];
		var lastTopOffset = 0;
		var row = -1;
		navItems.each(function(){
			var tmp = jQuery(this).offset();
			try
			{
				var theTop = tmp.top;
			}
			catch(ex)
			{
				try
				{
					var theTop = tmp['top'];
				}
				catch(ex)
				{
					var theTop = tmp[0];
				}
			}
			if(theTop > lastTopOffset)
			{
				lastTopOffset = theTop;
				row++;
				col = -1;
				navRows[row] = [];
				navRows[row].colSum = navRows[row].totalCols = 0;
			}

			col++;
			navRows[row].totalCols = col + 1;

			navRows[row][col] = jQuery(this);
			// [CPOD] This doesn't work properly in IE:
			//navRows[row].colSum += jQuery(this).width();
			//navRows[row].colSum += this.getWidth();
			navRows[row].colSum += getWidthOf(this);
		});
		//console.log(navRows);

		for(var curRow=0; curRow < navRows.length; curRow++)
		{
			if(navRows[curRow].colSum < navWidth)
			{
				diffToMakeUp = navWidth - navRows[curRow].colSum;
				totalCols = navRows[curRow].totalCols;
				howMuchToAddPerCol = Math.floor(diffToMakeUp / totalCols);
				remainder = diffToMakeUp - (howMuchToAddPerCol * totalCols);
						
				for(var curCol=0; curCol < totalCols; curCol++)
				{
					//var oldWidth = jQuery(navRows[curRow][curCol]).width();
					//var oldWidth = navRows[curRow][curCol][0].getWidth();
					var oldWidth = getWidthOf(navRows[curRow][curCol][0]);
					var newWidth = oldWidth + howMuchToAddPerCol + ((curCol<remainder)?1:0);
					
					//navRows[curRow][curCol].width(newWidth);
					navRows[curRow][curCol].css('width',newWidth);
				}
			}
		}
	});
}
