ah, yeah - I misread the class bit... and had to do the big dancearound as a result
as for where the new divs end up... well, that's the sneaky little hack I mentioned... in the code shown, one gets put at the start of the body, one gets put at the end, leaving ID3 in the middle

. Being that you are basically restructuring the page and moving everything around (but leaving some things where they are, like #id3) you have to decide where the newly created divs should go.
and then I guess you should give those divs an id and append to them that way, rather than playing around with the
$('div[class*=" y"]:last').prepend($(this)); routine, but that's up to you.