Sorry. It's quite hard to check the result with the code you provided, as I get just the HTML output and thus don't see such obvious flaws.
Please try this:
<?php
require_once('include/config.inc.php');
header("Content-type: application/x-javascript");
$connect = mysql_connect($CONFIG['dbserver'],$CONFIG['dbuser'],$CONFIG['dbpass'])
or die('Error connecting to the server');
$connect_db = mysql_select_db($CONFIG['dbname'], $connect)
or die ('Error connecting to the database');
$sql = "SELECT *, a.title AS alb_title FROM `{$CONFIG['TABLE_PREFIX']}albums` AS a INNER JOIN `{$CONFIG['TABLE_PREFIX']}pictures` AS r ON a.aid = r.aid WHERE approved = 'YES' GROUP BY r.aid ORDER BY ctime DESC LIMIT 0 , 12";
$resultado = mysql_query($sql, $connect)
or die('No albums found with this query');
echo 'document.write(\'';
if(mysql_num_rows($resultado) == 0){
echo 'No albums created';
} else {
echo '<div class="wrapper"><ul>';
while($row = mysql_fetch_array($resultado)){
$album_img = "GALLERY URL/albums/".$row['filepath'].'thumb_'.$row['filename'].$row['datebrowse'];
echo '<li><div class="albimg"><img src="'.$album_img.'" alt="" /><div class="galtit"><a href="http://stanakaticbrasil.com./galteste/thumbnails.php?album='.$row['aid'].'">'.$row['alb_title'].'</a></div></div></li>';
}
}
echo '</ul></div>';
echo '\');';
?>