Script Rasterpoints

alright guys heres the prob.

on my stage i want to load a bunch of MCs. I want them to position in a grid and i want that grid to adjust to the width of the stage.

for example i have 10 pix on my stage. If i slide the the browser window and there would only fit 5 pic in a row, i want to get the other 5 pics on the next row exactly under the 1st row. So if i have 3 pics in a row i want to put 3 on the next, 3 on THAT next and 1 on THAT next.

So i came up with this code:

function rasterImages() {

    for (var i = 0; i<nameArray.length; i++) {

        var itemsInRow:Number = Math.ceil((Stage.width-201)/201);
        var itemHeight:Number = 150;
        var itemWidth:Number = 200;
        var myPic = nameArray*;
        var myItem = _root["myPic"+myPic];

        // align the pics
        if (i<=itemsInRow-1) {
            // row 1
            myItem._y = 0;
            myItem._x = (itemWidth+1)*i;
        } else if (i>=itemsInRow-1 && i<itemsInRow*2) {
            //row 2
            myItem._y = (itemHeight+1)*1;
            myItem._x = (itemWidth+1)*(i-itemsInRow);
        } else if (i>=itemsInRow*2-1 && i<itemsInRow*3) {
            //row 3
            myItem._y = (itemHeight+1)*2;
            myItem._x = (itemWidth+1)*(i-itemsInRow*2);
        } else if (i>=itemsInRow*3-1 && i<itemsInRow*4) {
            //row 4
            myItem._y = (itemHeight+1)*3;
            myItem._x = (itemWidth+1)*(i-itemsInRow*3);
        } else if (i>=itemsInRow*4-1 && i<itemsInRow*5) {
            //row 5
            myItem._y = (itemHeight+1)*4;
            myItem._x = (itemWidth+1)*(i-itemsInRow*4);
        } else if (i>=itemsInRow*5-1 && i<itemsInRow*6) {
            //row 6
            myItem._y = (itemHeight+1)*5;
            myItem._x = (itemWidth+1)*(i-itemsInRow*5);
        } else if (i>=itemsInRow*6-1 && i<itemsInRow*7) {
            //row 7
            myItem._y = (itemHeight+1)*6;
            myItem._x = (itemWidth+1)*(i-itemsInRow*6);
        } else if (i>=itemsInRow*7-1 && i<itemsInRow*8) {
            //row 8
            myItem._y = (itemHeight+1)*7;
            myItem._x = (itemWidth+1)*(i-itemsInRow*7);
        }
    }
}

But what if i have like 50 rows. I dont really want to copy paste the code and change the code for the 42 rows remaining.

I hope someone knows a better way of accomplishing this if its possible.

If youre the one, please let me know