Error with jQuery Dialog

Error with jQuery Dialog

I'm dynamically creating a dialog and filling it with local data, however when it gets created it always appears on the top left corner of the page(not the viewport) and when I try to resize Firebug gives the following message: "uncaught exception: Dimensions: jQuery collection is empty" and of course the resize doesn't work. Here is the code for reference:

   var id = Math.floor(Math.random()*1000);
   var $preview = $('<div></div>');
   $preview.attr('id',"research_preview"+id);
   $('body').append( $preview );
   $preview.attr("title",title);
    fill_preview($preview, markup);  //Fill preview just puts some data in the div before the dialog gets created
   $('#research_preview'+id).dialog( {
   show:'slide',
   maxWidth: '400' });