<?php

$db = mysql_connect("localhost","abuguet","CambiamE!!");
mysql_set_charset ( 'utf8', $db );
mysql_select_db("abuguet");

//Prepare random Query
mysql_query('SELECT @count := COUNT(*) FROM `gab_ascii`');
mysql_query('SET @offset = CONVERT(FLOOR(RAND() * @count), SIGNED)');
mysql_query('PREPARE `randquery` FROM "SELECT * FROM `gab_ascii` LIMIT ?, 1"');

//get 20
$rows = array();
for($i = 0; $i < 20; $i++){
	mysql_query('SET @offset = CONVERT(FLOOR(RAND() * @count), SIGNED)');
	$res = mysql_query('EXECUTE `randquery` USING @offset');
	$rows[$i] = mysql_fetch_assoc($res);
}

$area = array();
$chars = array();
$counter = 0;
//empty array.
for($i = 0; $i < 25; $i++){
	for ($j = 0; $j < 80; $j++){
		$area[$i][$j] = 0;
	}
}

function getCharArray ($jstring)
{
  if (mb_strlen ($jstring, 'UTF-8') == 0)
    return array();
 
  $ret  = array ();
  $alen = strlen ($jstring);
  $char = '';
  for ($i = 0; $i < $alen; $i++) {
    $char .= $jstring[$i];
    if (mb_check_encoding ($char, 'UTF-8')) {
      array_push ($ret, $char);
      $char = '';
    }
  }
 
  return $ret;
}

function can_place($w,$h,$x,$y){
	global $area;
	for($i = $y; $i < $y+$h; $i++){
		for ($j = $x; $j < $x+$w; $j++){
			if($area[$i][$j] != 0){
				return false;
			}
		}
	}
	return true;
}

function do_place($row,$x,$y){
	global $area, $chars, $counter;
	for($i = $y,$ch = 0; $i < $y+$row['height']; $i++,$ch++){
		for ($j = $x,$cw = 0; $j < $x+$row['width']; $j++,$cw++){
			if($ch == 0 && $cw == 0){ $area[$i][$j] = 2; }else{ $area[$i][$j] = 1;}
			$text = getCharArray($row['text']);
			$chars[$i][$j] = $text[($row['width']*$ch)+$cw];
			$counter++;
		}
	}
}

foreach($rows as $row){
	if($counter < 660){								//try to keep it at 33% fill.
		$tries = 0;									//lets do only 10 tries.
		$placed = false;
		while(!$placed && $tries < 10){				//while it isn't placed and we still have tries
			$x = rand (0, 80-$row['width'] );		//get a valid root point
			$y = rand (0, 25-$row['height'] );
			
			if(can_place($row['width'],$row['height'],$x,$y)){
				do_place($row,$x,$y);
				$placed = true;
			}else{
				$tries++;
			}
		}
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
	<head>
		<title>nyaw nyaw nyaw :3</title>
		<style type="text/css">
		
			@font-face {
    			font-family: 'Mona';
			    src: url('mona.ttf') format('truetype');
   				font-weight: normal;
    			font-style: normal;

			}
		
			*{
				margin: 0;
				padding: 0;
			}
			
			body{
				text-align: center;
				font-size: 0.625em;
				background-color: #fff;
			}
			
			#wrapper{
				margin: 0 auto;
				width: 960px;
				margin-top: 100px;
				text-align: left;
			}
			
			#wrapper #term{
				font-size: 1.2em;
				font-family: Mona,'MS PGothic', monospace;
				margin-top: 100px;
			}
			
			.letter{
				color: #999;
				display: block;
				float: left;
				width: 12px;
				height: 16px;
			}
			
			#clear{
				clear: both;
				height: 0;
				width: 0;
			}
			
			.letter.test{
				background-color: #c00;
				color: #fff;
			}
			
		</style>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<script src="http://www.google.com/jsapi" type="text/javascript"></script> 
		<script type="text/javascript"> 
		  // Load jQuery
		  google.load("jquery", "1.4.0");
		</script> 
	</head>
	<body>
		<div id="wrapper">
			<div id="term"><?php
				for($i = 0; $i < 25; $i++){
					for ($j = 0; $j < 80; $j++){
						if($area[$i][$j] != 0){
							echo "<span class=\"letter test\" id=\"letter-".((80*$i)+$j)."\">".htmlspecialchars($chars[$i][$j])."</span>";
						}else{
							echo "<span class=\"letter\" id=\"letter-".((80*$i)+$j)."\">".htmlspecialchars(chr(rand(33,126)))."</span>";
						}
					}
					echo "<div id=\"clear\"/>&nbsp;</div>\n";
				}
			?></div>
		</div>
	</body>
</html>