Horizontally center unknown width elements

HTML

<div id='container'>
    <div class='box'></div>
    <div class='box'></div>
    <div class='box'></div>
    <div class='box'></div>
    <div class='box'></div>
    <div class='box'></div>
    <div class='box'></div>
    <div class='box'></div>
    <div class='box'></div>
    <div class='box'></div>
    <div class='box'></div>
    <div class='box'></div>
    <div class='box'></div>
    <div class='box'></div>
</div>

CSS

body {
    text-align:center;
}
#container {
    background:blue;
    text-align: left;
    font-size:0;
    display: inline-block;
    padding-left:5px;
    padding-bottom:5px;
    /* for ie6/7: */
    *display: inline;
    zoom:1;
}
.box {
    display:inline-block;
    background:red;
    height:50px;
    margin-top:5px;
    margin-right:5px;
    width: 100px;
    height: 100px;
}

JavaScript

var boxAmount = 0;
var newWidth = 0;
setNewWidth();

window.onresize = function () {
    setNewWidth();
};

function setNewWidth() {
    var outerContainer = document.getElementsByTagName('body')[0];
    var outerWidth = outerContainer.offsetWidth;
    var box = document.getElementsByClassName('box');
    var boxWidth = box[0].offsetWidth;
    var innerContainer = document.getElementById('container');
    var containerPadding = parseInt(window.getComputedStyle(innerContainer, null).getPropertyValue('padding-left'), 10) 
    +  parseInt(window.getComputedStyle(innerContainer, null).getPropertyValue('padding-right'), 10);
    
    boxAmount = (outerWidth - containerPadding) / (boxWidth + containerPadding);
    boxAmount = Math.floor(boxAmount);
    if (boxAmount <= box.length) {
        newWidth = boxAmount * boxWidth + boxAmount * 5;
    }
    innerContainer.style.width = newWidth + 'px';
}