-
As pointed in the other answer, you should not have prettyPhoto part
-
The link should be changed to utilize the function get_attachment_link
Basically change this line in your code
$link = '<a href="'.get_attachment_link($id).'">'.wp_get_attachment_image($id, $size, false).'</a>';