<?php 
 
// include "sthcommongamefunctions.inc";     *already included
// CREATE PDF FILE
	// print "<div> Creating PDF TK1</div>";
	
	//require_once('tcpdf/config/lang/eng.php');
	require_once('tcpdf/tcpdf.php');
	
	
function print_error($newerrormsg)
{
	array_push($GLOBALS['printerrorlist'], $newerrormsg);
	$GLOBALS['printerrorcount']++;
	print "<div>New Print Error Msg: $newerrormsg</div>";
	return 1;
}

//<cfparam name = "form.order_number" default = "none">
$GLOBALS['printerrorlist']  = array();
$GLOBALS['printerrorcount'] = 0;

$sth_game_number = $session->sth_game_num;


$load_game_query = $db->query("SELECT * FROM `aa_sth_games` WHERE `sth_game_number` = '$sth_game_number' LIMIT 1");
$num_rows = mysqli_num_rows($load_game_query);  // for testing
if ($num_rows == 0) {
	print_error("Database returns 0 rows for this sth_game_number: $sth_game_number");
}
else {

	while($row = $load_game_query->fetch_array()) {
		$db_game_id =  $row['id'];	
		$db_game_name = $row['game_name'];
		$glyph_list = explode("|", $row['glyph_list']);
		$glype_level = explode("|", $row['glyph_level']);
		$glyph_location = explode("|", $row['glyph_location']);
		$clue_list = explode("|", $row['clue_list']);
		$clue_location = explode("|", $row['clue_location']);
		$clue_level = explode("|", $row['clue_level']);
		$key_location = explode("|", $row['key_location']);
		$glyph_jpg_list = explode("|", $row['glyph_jpg_list']);
		$sth_game_state = $row['game_state'];
		$pdf_filename = $row['pdf_filename'];
	}

	
	
}


$GLOBAL['GlyphNumbersForThisClue'] = array();
/*
 * DB Table
 * game_state,
	glyph_list,
	glyph_level,
	glyph_location,
	clue_list,
	clue_level,
	clue_location,
	glyph_jpg_list,
	key_location,
	sth_game_number,
	ticket_number,
	ticket_id,
	date_created
 * 
 */

?>

<?php
// ***********************************************************
// ***********************************************************
// ***********************************************************
// create new PDF document
	// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Tenzing Kernan');
$pdf->SetTitle('Super Treasure Hunt Game');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 

//set some language-dependent strings
// $pdf->setLanguageArray($l); 
// was from old version of tcpdf library
	
	// ---------------------------------------------------------
	
	// set font
	$pdf->SetFont('helvetica', '', 8);
	
	// add a page
	$pdf->AddPage();	
	
	// PDF File is READY for the Game Content ***************************************
	//*******************************************************************************
	// $pdf->writeHTML($htmlcontent, true, 0, true, 0); 
	// print a line using Cell()
	//$pdf->Cell(0, 12, 'Game Number:'.$sth_game_number, 1, 1, 'C'); 
	//$pdf->Cell(0, 12, 'Filename:'. $_SESSION['sth_pdf_filename'], 1, 1, 'C'); 


	
	//header image
	//<img src="content/sth/gameart/treasure_chest5.gif" width="171" height="141" align="left">
    
	
	$htmlcontent = '<span style="font-size: x-large;"><div> Super Treasure Hunt Instructions </div></span>
	<span style="font-size: large;"><ol>
        <li>Print out these 24 pages.</li>
        <li>Each printed pages contains all the Clues to hide in one location.</li>
        <li>Just cut out the clues from one page at a time and place them together into the specified location.</li> 
        <li>For parties with more than 7 children, Split the group into teams (4-6 kids per team) and hide a copy of the clues for each team.  So, with 2 teams, print the Treasure Hunt twice.</li>
			
		<li>The last printed pages have the first Clue the kids get to start the game (written in plain english) and the final Congratulations Message which goes into the Treasure Chest.  Lock the Treasure Chestwith the combination lock after you put in the treasure and also the Congratulations Message.</li>
                
                <li>You can <strong>print from any internet computer</strong> for the next 30 days You just need your order number, which was emailed to you.</li>
                </ol></span>';   


$pdf->writeHTML($htmlcontent, true, 0, true, 0); 


// background image test
$img_file = K_PATH_IMAGES.'sthgameart/stonebackground2.png';
//$pdf->Image($img_file, $x=43, $y=23, $w=131, $h=75, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0); 




// Multi-Cell Setup:
$pdf->SetCellPadding(.3);
 //Colors, line width and bold font for the header
$pdf->SetFillColor(255); //background color of next Cell
$pdf->SetTextColor(0); //font color of next cell
 
$pdf->SetDrawColor(0); //cell borders - similiar to border color
$pdf->SetLineWidth(.4); //similiar to cellspacing
 
// $cols=array('Clue','Translation');//Column titles
// $width=array(80,120); //amount of elements must correspond with $header array above
 
// for($i = 0; $i < count($cols); $i++)
//{
     //void Cell( float $w, [float $h = 0], [string $txt = ''], [mixed $border = 0],
     //[int $ln = 0], [string $align = ''], [int $fill = 0], [mixed $link = ''], [int $stretch = 0])
      
     // $pdf->Cell($width[$i],7,$cols[$i],0,0,'C',1);
 //}
 //$pdf->Cell(80,7,'Clue',0,0,'R',1);
 //$pdf->Cell(120,7,'Translation',0,0,'L',1);
 //$pdf->Ln(); //new row



$pdf->SetFont('helvetica', '', 8);

$pdf->Cell(0, 12, 'Super Treasure Hunt Cheat Sheet - For Parents Eyes Only', 0, 1, 'C'); 



	// <cfloop from="2" to="13" index="i">
	// <cfset Ctext = ListGetAt(Session.clue_list,i)>
	for ($i = 1; $i < 13; $i++)
	{
		$Ctext = $clue_list[$i];
		$result = GetGlyphNumbersFromClue($Ctext);
		
		$images_string = "";
		foreach($GLOBAL['GlyphNumbersForThisClue'] as $GnumX)
		{
			$GlyphArtSuffix =$glyph_jpg_list[$GnumX-1]; // possible GnumX is -1
			$art_source = "http://treasure-hunt-game.com/images/sthgameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
			$images_string .= '<img src="'.$art_source.'" border="0" height="13" />';
		}

		$pdf->SetCellPadding(.3);
		$pdf->MultiCell(80,5.5,$images_string,0,'R',1,0,'','',1,0,1); 
		$pdf->SetCellPadding(1);
     	$pdf->MultiCell(120,5.5,$Ctext,0,'L',1,0,'','',1,0,1); 
		$pdf->Ln(); //new row
		$pdf->SetCellPadding(.3);
			
	}
	

		
//<h4 style="page-break-after: always"></h4>

// Game!
$pdf->AddPage();	

//<!--- fill up the locations --->

for ($Lnum = 1; $Lnum <=10; $Lnum++)
{

	
	$htmlcontent = '<div style = "font-size: x-large;">Location #'.$Lnum.' - Represented by this Clue:';
	$theclue = $clue_list[$Lnum-1];  // -1 was added for php port
	$htmlcontent .= '<strong>'.$theclue.'</strong></div>';
	$pdf->writeHTML($htmlcontent, true, 0, true, 0);  
	$htmlcontent = '<div>Cut out all of the Clues and Glyphs on this page (below) and hide them in 
            this location. </div>';
  	$pdf->writeHTML($htmlcontent, true, 0, true, 0); 
		
 	
  	
  	
  	
  	// Left Off Here         left off here
  	
  	
  	
  	// Calculate how big the background needs to be and draw it.
  	// count the number of rows of glyphs, and also if there is a clue row.
  	// stats for determining the size of the background that is behind the clue and glyphs 
    // (for no-cut version).
    //$box_height = 0;
    //$box_width = 0;
    //$box_has_clue = 0;
    //$box_rows_of_glyphs = 0;
  	
  	
    // <!--- put the clues into this location --->
    for ($i = 1; $i <= 14; $i++)
    {
    	
		    	
    	$Clocation = $clue_location[$i-1];
    	if ($Clocation == $Lnum)
    	{
    		$new_y = $pdf->getY() + 6;
			$pdf->setY($new_y);
				
    		$Ctext = $clue_list[$i-1];
    		$Clevel = $clue_level[$i-1];
    		if ($Clevel == 0)
    		{
    			// clue in plain english
    			$pdf->Cell(0, 12, $Ctext, 0, 1, 'C'); 	// where is this used?    			
    		}
    		else
    		{
    			// <!--- clue must be translated to glyphs --->
    			
    			$result = GetGlyphNumbersFromClue($Ctext);
    			
    			//  <!--- fills in Session.GlyphNumbersForThisClue --->
    			$images_string = "";
    			$image_string_width = 0;
    			$image_render_height = 36;
    			$box_render_offset = 14;
    			foreach ($GLOBALS['GlyphNumbersForThisClue'] as $GnumX)
    			{
    				$GlyphArtSuffix =$glyph_jpg_list[$GnumX-1]; // possible GnumX is -1
    				
					//$art_source = "http://treasure-hunt-game.com/images/sthgameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
					$art_source = "gamecode/sth3/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
					
					$art_source_for_size = "gamecode/sth3/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
					//$art_source_for_size = "tcpdf/images/sthgameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
					
					$images_string .= '<img src="'.$art_source.'" height="'.$image_render_height.'" />';
					
					$glyph_imagesize = getimagesize($art_source_for_size);
					$glyph_width = $glyph_imagesize[0];
					$glyph_height = $glyph_imagesize[1];
					$add_width = $glyph_width / $glyph_height * $box_render_offset;
					$image_string_width += $add_width;
					
					//print("<div>width: $glyph_width       height: $glyph_height    calcW: $add_width   Total: $image_string_width</div>");
				}
				
				$htmlcontent = '<span style="align:center;"><table border="1" align="center"><tr><td align="center">'.$images_string.'</td></tr></table></div>';
				// 
				//$pdf->writeHTML($htmlcontent, true, 0, true, 0); 
				
				$page_center = 104.5;
				
				$cell_x = $page_center - ($image_string_width / 2);
								
				$pdf->SetCellPadding(.3);
				$pdf->SetX ($cell_x);
				$pdf->MultiCell($image_string_width,0,$images_string,1,'C',1,0,'','',1,0,1); 
				$pdf->Ln(); //new row
				
				
				
		    				
    			    			
    		}
    	}    	//  - if there was a clue in this location, it is now printed.   
    		
    }
	$new_y = $pdf->getY() + 6;
	$pdf->setY($new_y);

	
    // <!--- Now put the glyphs into this location --->
	$pdf->SetCellPadding(1);
    
    $page_center = 104.5;
    $col_spread = 50;
    
    $col_centers = array();
    $col_center[0] = $page_center - $col_spread;
    $col_center[1] = $page_center;
    $col_center[2] = $page_center + $col_spread;
    
	$row_count = 0;   
	$column_count = 0;
	
	$size_of_list = count($glyph_list);	
	for($i = 1; $i <= $size_of_list; $i++)
	{
		//print"*";
		$this_glyph_location = $glyph_location[$i-1];
		if ($this_glyph_location == $Lnum)
		{
			//print"+";
			if ($column_count == 3)
			{
				$pdf->Ln(); //new row
				$new_y = $pdf->getY() + 3;
				$pdf->setY($new_y);
				
				$row_count++;
				$column_count = 0;
			}
			$this_glyph_word = $GLOBALS['glyph_list'][$i-1];
			$this_glyph_level = $GLOBALS['glyph_level'][$i-1];
       		$GlyphArtSuffix = $glyph_jpg_list[$i-1];
       		
       		
		  	if ( strlen($this_glyph_word) > 1 || (strlen($this_glyph_word) == 1 && ( 
		  		($this_glyph_word >= 'a' && $this_glyph_word <= 'z' ) ||
		  		($this_glyph_word >= 'A' && $this_glyph_word <= 'Z' ) ||
		  		($this_glyph_word >= '0' && $this_glyph_word <= '9' ) ) ) )
		  	{
		  		$glyphlink = "gamecode/sth3/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
		  		$glyphlink_pdf = "gamecode/sth3/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
				$word_size = "normal";		  				  										
		  	}
		  	else 
		  	{ // punctuation
		  		if ($this_glyph_word == "." || $this_glyph_word == ",")
		  		{
		  			$glyphlink = "gamecode/sth3/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
		  			$glyphlink_pdf = "gamecode/sth3/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
    				$word_size = "big";
		  			//print "<div><img src='$glyphlink'><span class='glyphText'><strong><font size = '6'> = $this_glyph_word</font></strong></span></div>";
		  		}
		  		else
		  		{   // some other punctuation
		  			$glyphlink = "gamecode/sth3/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
		  			$glyphlink_pdf = "gamecode/sth3/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
    				//print "<div><img src='$glyphlink'><span class='glyphText'><strong><font size = '4'> = $this_glyph_word</font></strong></span></div>";
		  			$word_size = "normal";		
		  		}
		  	}
		  	
		  	//find $render_glyph_width
	  		$box_render_offset = 14;
	  		$glyph_imagesize = getimagesize($glyphlink_pdf);
			$glyph_width = $glyph_imagesize[0];
			$glyph_height = $glyph_imagesize[1];
			$render_glyph_width = $glyph_width / $glyph_height * $box_render_offset + .1;

			//find $render_word_width
			if ($word_size == "normal") 
			{
				$pdf->SetFont('helvetica', '', 11);		
			} else
			{
				$pdf->SetFont('helvetica', '', 14);
			}
			
			$word_width_multiplier = 1.2;
			$word_width_add_on = 5;
			$word_width = $pdf->GetStringWidth ($this_glyph_word);
			$render_word_width = $word_width * $word_width_multiplier + $word_width_add_on;			
			
			$render_equal_width = 8;
			$render_cell_height = 13;
			
			$total_glyph_width = $render_glyph_width + $render_word_width + $render_equal_width; 	
			$cell_x = $col_center[$column_count] - ($total_glyph_width / 2);
							
			//$cell_x -= 120;
			$pdf->SetX ($cell_x);
			
			$pdf->SetCellPadding(.3);
			$glyph_image_html = '<img src="'.$glyphlink.'" height="32">';
			$pdf->MultiCell($render_glyph_width,$render_cell_height,$glyph_image_html,'LTB','C',1,0,'','',1,0,1);
			/*
			$pdf->MultiCell(
			 	$render_glyph_width,
				$render_cell_height,
				$glyph_image_html,
				$border = 'LTB',
				$align = 'C',
				$fill = false,
				$ln = 1,
				$x = '',
				$y = '',
				$reseth = true,
				$stretch = 0,
				$ishtml = true,
				$autopadding = true,
				$maxh = 0,
				$valign = 'T',
				$fitcell = false 
			);
			*/
			
			$pdf->SetCellPadding(3.2);
			$pdf->SetFont('helvetica', '', 14);
			$pdf->MultiCell($render_equal_width,$render_cell_height,'=','TB','C',1,0,'','',1,0,1);
			
			$pdf->SetCellPadding(3.6);
			$pdf->SetFont('helvetica', '', 12);
			$pdf->MultiCell($render_word_width,$render_cell_height,$this_glyph_word,'RTB','L',1,0,'','',1,0,1);
	  	
			$column_count++;       		
		}  // end of this glyph IF
	} // next glyph 
    
       	
     
			
	// print "</tr></table>";
	$pdf->Ln(); //new row
	
	
	
	
	
	// **** Place the Keys
	
	// count the keys:
	$keys_count = 0;
	for ($i = 1; $i<=9; $i++)
	{
		$this_key_location = $key_location[$i-1];
		if ($this_key_location == $Lnum)
		{
		$keys_count++;			
		}
	}
	
	// find the render locations:
	$pdf->SetCellPadding(1);
    $page_center = 104.5;
    $col_spread = 37;
    
    $col_centers = array();
    
    if ($keys_count == 1)
    {
    	 $col_center[0] = $page_center;
    }
    else if ($keys_count == 2)
    {
    	$col_center[0] = $page_center - $col_spread;
    	$col_center[1] = $page_center + $col_spread;    	
    }
    
    	
	
	$column_count = 0;
	for ($i = 1; $i<=9; $i++)
	{
		$this_key_location = $key_location[$i-1];
		if ($this_key_location == $Lnum)
		{
		
			$imglink = "gamecode/sth3/gameart/Keys/key".$i.".jpg";
			$imglink_for_size = "gamecode/sth3/gameart/Keys/key".$i.".jpg";
    		
			
			
				//find $render_glyph_width
	  		//$box_render_offset = 14;
	  		$key_imagesize = getimagesize($imglink_for_size);
			$key_width = $key_imagesize[0];
			$key_height = $key_imagesize[1];
			$render_size_multiplier = .38;
			$render_width = $key_width * $render_size_multiplier;
			$render_height = $key_height * $render_size_multiplier;
			
			//find $render_word_width
			$pdf->SetFont('helvetica', '', 12);
					
			$cell_x = $col_center[$column_count] - ($render_width / 2);
							
			//$cell_x -= 120;
			if ($column_count == 0)
			{
				$new_y = $pdf->getY() + 4;
				$pdf->setY($new_y);
			}
						
			$pdf->SetCellPadding(.3);
			$pdf->SetX ($cell_x);
			//$key_image_html = '<img src="'.$imglink.'" height="'.$render_width.'">';
			//$key_image_html = $cell_x;
			$key_image_html = '<img src="'.$imglink.'">';
			$pdf->MultiCell($render_width,$render_height,$key_image_html,'1','C',1,0,'','',1,0,1);
									    		
    		$column_count++;			
    		
		}
	}
	
	if ($column_count == 2)
	{
			$pdf->Ln(); //new row
	}
	

	// add a page
	$pdf->AddPage();	
}
// <!--- done filling Locations --->

?>

<?php 
// <!--- fill up the l0 lockboxes --->
$BoxText = array("3","5","7","15","31","54","75","8","29","96");

for ($Bnum = 1; $Bnum <=10; $Bnum++)
{
		
	$boxtextnow = $BoxText[$Bnum-1];
    $boxtext = "Lock Box #".$boxtextnow;
    
    $pdf->SetFont('helvetica', '', 8);
	$pdf->Cell(0, 14, $boxtext, 0, 1, 'C'); 
    
    
    if (strlen($boxtextnow) == 2)
    {
    	$pdf->SetFont('helvetica', '', 8);
		$pdf->Cell(0, 12, "A Lockbox is a small paper bag or envelope 
              with these locks taped to it.", 0, 1, 'C');     	
		$lock_img_height = 180;
    }
    else 
    {
    	$pdf->SetFont('helvetica', '', 8);
		$pdf->Cell(0, 12, "A Lockbox is a small paper bag or envelope 
              with this lock taped to it.", 0, 1, 'C');  
		$lock_img_height = 200;
    }
    
    $htmlcontent = "<p><strong>Cut out all of the Clues and Glyphs on this page (below) and put them in 
            this lockbox.  Put all of the lockboxes on a shelf where the kids can see them. 
            When the kids show you the correct keys, they may open the lockbox. </strong></p>";
    
	$pdf->writeHTML($htmlcontent, true, 0, true, 0); 

	$imglink = "gamecode/sth3/gameart/LockBoxArt/LockBox".$Bnum.".jpg";
	//$imglink2 = "http://treasure-hunt-game.com/images/sthgameart/LockBoxArt/LockBox".$Bnum.".jpg";
	
	// WORKS  $pdf->Image($imglink, $x=43, $y=23, $w=0, $h=40, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0); 
	
	//$htmlcontent = '<img href = "'.$imglink2.'">';
	//$pdf->writeHTML($htmlcontent, true, 0, true, 0); 
	
	//$pdf->Cell(0, 12, $htmlcontent, 0, 1, 'C');  
	
	$lock_image_html = '<img src="'.$imglink.'" height="'.$lock_img_height.'">';
	$pdf->MultiCell(0,15,$lock_image_html,'LRTB','C',1,0,'','',1,0,1);
			
	$pdf->Ln(); //new row	
	$new_y = $pdf->getY() + 6;
	$pdf->setY($new_y);
	
	//<!--- put the clues into this lockbox --->

    for ($i = 1; $i <= 14; $i++)
    {
    	
		    	
    	$Clocation = $clue_location[$i-1];
    	if ($Clocation == $Bnum+100)	
    	{
    		$new_y = $pdf->getY() + 6;
			$pdf->setY($new_y);
				
    		$Ctext = $clue_list[$i-1];
    		$Clevel = $clue_level[$i-1];
    		if ($Clevel == 0)
    		{
    			// clue in plain english
    			$pdf->Cell(0, 12, $Ctext, 0, 1, 'C'); 	// where is this used?    			
    		}
    		else
    		{
    			// <!--- clue must be translated to glyphs --->
    			
    			$result = GetGlyphNumbersFromClue($Ctext);
    			
    			//  <!--- fills in Session.GlyphNumbersForThisClue --->
    			$images_string = "";
    			$image_string_width = 0;
    			$image_render_height = 36;
    			$box_render_offset = 14;
    			foreach ($GLOBAL['GlyphNumbersForThisClue'] as $GnumX)
    			{
    				$GlyphArtSuffix =$glyph_jpg_list[$GnumX-1]; // possible GnumX is -1
					// $art_source = "http://treasure-hunt-game.com/images/sthgameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
					$art_source = "gamecode/sth3/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
					//$art_source_for_size = "content/sth/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
					$art_source_for_size = "gamecode/sth3/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
					$images_string .= '<img src="'.$art_source.'" height="'.$image_render_height.'" />';
					
					$glyph_imagesize = getimagesize($art_source_for_size);
					$glyph_width = $glyph_imagesize[0];
					$glyph_height = $glyph_imagesize[1];
					$add_width = $glyph_width / $glyph_height * $box_render_offset;
					$image_string_width += $add_width;
					
					//print("<div>width: $glyph_width       height: $glyph_height    calcW: $add_width   Total: $image_string_width</div>");
				}
				
				$htmlcontent = '<span style="align:center;"><table border="1" align="center"><tr><td align="center">'.$images_string.'</td></tr></table></div>';
				// 
				//$pdf->writeHTML($htmlcontent, true, 0, true, 0); 
				
				$page_center = 104.5;
				
				$cell_x = $page_center - ($image_string_width / 2);
								
				$pdf->SetCellPadding(.3);
				$pdf->SetX ($cell_x);
				$pdf->MultiCell($image_string_width,0,$images_string,1,'C',1,0,'','',1,0,1); 
				$pdf->Ln(); //new row
				
				
				
		    				
    			    			
    		}
    	}    	//  - if there was a clue in this location, it is now printed.   
    		
    }
	$new_y = $pdf->getY() + 6;
	$pdf->setY($new_y);

	//*********************************
	
	
	
		
		
	// <!--- Now put the glyphs into this location/lockbox --->
	$pdf->SetCellPadding(1);
    
    $page_center = 104.5;
    $col_spread = 50;
    
    $col_centers = array();
    $col_center[0] = $page_center - $col_spread;
    $col_center[1] = $page_center;
    $col_center[2] = $page_center + $col_spread;
    
	$row_count = 0;   
	$column_count = 0;
	
	$size_of_list = count($glyph_list);	
	for($i = 1; $i <= $size_of_list; $i++)
	{
		//print"*";
		$this_glyph_location = $glyph_location[$i-1];
		if ($this_glyph_location == $Bnum+100)
		{
			//print"+";
			if ($column_count == 3)
			{
				$pdf->Ln(); //new row
				$new_y = $pdf->getY() + 3;
				$pdf->setY($new_y);
				
				$row_count++;
				$column_count = 0;
			}
			$this_glyph_word = $GLOBALS['glyph_list'][$i-1];
			$this_glyph_level = $GLOBALS['glyph_level'][$i-1];
       		$GlyphArtSuffix = $glyph_jpg_list[$i-1];
       		
       		
		  	if ( strlen($this_glyph_word) > 1 || (strlen($this_glyph_word) == 1 && ( 
		  		($this_glyph_word >= 'a' && $this_glyph_word <= 'z' ) ||
		  		($this_glyph_word >= 'A' && $this_glyph_word <= 'Z' ) ||
		  		($this_glyph_word >= '0' && $this_glyph_word <= '9' ) ) ) )
		  	{
		  		$glyphlink = "gamecode/sth3/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
		  		$glyphlink_pdf = "gamecode/sth3/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
		  				  										
		  	}
		  	else 
		  	{ // punctuation
		  		if ($this_glyph_word == "." || $this_glyph_word == ",")
		  		{
		  			$glyphlink = "gamecode/sth3/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
		  			$glyphlink_pdf = "gamecode/sth3/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
    				//print "<div><img src='$glyphlink'><span class='glyphText'><strong><font size = '6'> = $this_glyph_word</font></strong></span></div>";
		  		}
		  		else
		  		{   // some other punctuation
		  			$glyphlink = "gamecode/sth3/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
		  			$glyphlink_pdf = "gamecode/sth3/gameart/Glyphs/glyph".$GlyphArtSuffix.".jpg";
    				//print "<div><img src='$glyphlink'><span class='glyphText'><strong><font size = '4'> = $this_glyph_word</font></strong></span></div>";
		  		
		  		}
		  	}
		  	
		  	//find $render_glyph_width
	  		$box_render_offset = 14;
	  		$glyph_imagesize = getimagesize($glyphlink_pdf);
			$glyph_width = $glyph_imagesize[0];
			$glyph_height = $glyph_imagesize[1];
			$render_glyph_width = $glyph_width / $glyph_height * $box_render_offset + .1;

			//find $render_word_width
			$pdf->SetFont('helvetica', '', 11);
			$word_width_multiplier = 1.2;
			$word_width_add_on = 5;
			$word_width = $pdf->GetStringWidth ($this_glyph_word);
			$render_word_width = $word_width * $word_width_multiplier + $word_width_add_on;			
			
			$render_equal_width = 7;
			$render_cell_height = 13;
			
			$total_glyph_width = $render_glyph_width + $render_word_width + $render_equal_width; 	
			$cell_x = $col_center[$column_count] - ($total_glyph_width / 2);
							
			//$cell_x -= 120;
			$pdf->SetX ($cell_x);
			
			$pdf->SetCellPadding(.3);
			$glyph_image_html = '<img src="'.$glyphlink.'" height="32">';
			$pdf->MultiCell($render_glyph_width,$render_cell_height,$glyph_image_html,'LTB','C',1,0,'','',1,0,1);
			
			$pdf->SetCellPadding(3.2);
			$pdf->SetFont('helvetica', '', 14);
			$pdf->MultiCell($render_equal_width,$render_cell_height,'=','TB','C',1,0,'','',1,0,1);
			
			$pdf->SetCellPadding(3.6);
			$pdf->SetFont('helvetica', '', 12);
			$pdf->MultiCell($render_word_width,$render_cell_height,$this_glyph_word,'RTB','L',1,0,'','',1,0,1);
	  	
			$column_count++;       		
		}  // end of this glyph IF
	} // next glyph 
    
       	
     
			
	// print "</tr></table>";
	$pdf->Ln(); //new row
	
	
	
		
	
	//<!--- current version does not have keys in lockboxes --->

	
$pdf->AddPage();		
}  // end for Bnum   <!--- done filling Lock Boxes --->




// <!--- the stuff that you start the game with --->

$htmlcontent = "<p>Starting Clue and Message</p>";
$htmlcontent .= "<p><strong>Cut out and fold up this Greeing Card style Clue for to the kids to start the game with.  This is all that they will need to get going.</strong></p>"; 
$pdf->writeHTML($htmlcontent, true, 0, true, 0); 	
	
$card_width = 86;
$card_height = 96;			

$image_html = '<img src="gamecode/sth3/gameart/treasurehuntinvitation1.gif">';
$pdf->MultiCell($card_width,$card_height,$image_html,'RLTB','C',1,0,'','',1,0,1);

$image_html = '<img src="gamecode/sth3/gameart/card_back.jpg" width="120" height="98">';
$pdf->MultiCell($card_width,$card_height,$image_html,'RLTB','C',1,0,'','',1,0,1);

$pdf->Ln(); //new row

	
$Lnum = 0;

// <!--- write the Quest introduction --->
for ($i = 1; $i <= 14; $i++)
{
	$Clocation = $clue_location[$i-1];
	if ($Clocation == $Lnum)
	{
		//  <!--- there should only be one match --->
        $Ctext = $clue_list[$i-1];
        $Clevel = $clue_level[$i-1];
        
        if ($Clevel == 0)
        {
        	// <!--- clue in plain english --->
        	
        	$htmlcontent = '<p><strong>Hello Brave Adventurers</strong></p>';
        	$htmlcontent .='<div><strong>A magnificent treasure awaits you,</strong></div>';
        	$htmlcontent .= '<div><strong>if you can get the treasure chest open.</strong></div>';
            $htmlcontent .='<p><strong>To do that you will need to open the lock.  It will not be easy.</strong></p>';
            $htmlcontent .='<p><strong>To begin, here is your first clue</strong></p>';
            $htmlcontent .= '<p>';
            $htmlcontent .= $Ctext;
            $htmlcontent .= '</p>';
            $htmlcontent .= '<p><strong>Good Luck. You will need it!</strong></p>';
			
            $pdf->MultiCell($card_width,$card_height,'','RLTB','C',1,0,'','',1,0,1);
            $pdf->MultiCell($card_width,$card_height,$htmlcontent,'RLTB','C',1,0,'','',1,0,1);
			$pdf->Ln(); //new row
			
     	 	//$pdf->writeHTML($htmlcontent, true, 0, true, 0); 	     	 	
    
        }
          
	}
}     

		  
//	</h4>
	
//  <!--- print the final message / birthday card --->

$pdf->AddPage();
$htmlcontent = '<p align="center">Congratulations Greeting Card</p>';
$htmlcontent .= '<div align="center"><strong>Cut out this card and fold it in half twice, keeping the the chest artwork to the outsides.</strong></div>';
$htmlcontent .= '<div align="center"><strong>This creates the final Congratulations Message card to place into the Treasure Chest.</strong></div>'; 
$pdf->writeHTML($htmlcontent, true, 0, true, 0); 

$pdf->Ln(); //new row

$image_html = '<img src="gamecode/sth3/gameart/card_front.jpg"></td>';
$pdf->MultiCell($card_width,$card_height,$image_html,'RLTB','C',1,0,'','',1,0,1);
$image_html = '<img src="gamecode/sth3/gameart/card_back.jpg" width="120" height="98">';
$pdf->MultiCell($card_width,$card_height,$image_html,'RLTB','C',1,0,'','',1,0,1);

$pdf->Ln(); //new row
//	<!--- put glyphs inside card left --->
	  $Ctext = $clue_list[13];
      // print "* $Ctext *"; 
      // <!--- clue must be translated to glyphs --->
		$result = GetGlyphNumbersFromClue($Ctext);
		$totalGs = count($GLOBALS['GlyphNumbersForThisClue']);
		                  
        $counter1 = 0;
		$glyphs_html = "";
		for ($ZZdex = 1; $ZZdex <= $totalGs; $ZZdex++)
		{
			if ($counter1 == 0)
			{
				$glyphs_html .= '<div>';
			}
			
			$try_value = 0;
			while ($try_value == 0)
			{
				$rand_pick = rand(1,$totalGs);
				$try_value = $GLOBALS['GlyphNumbersForThisClue'][$rand_pick-1];								
			}
			$GnumX = $try_value;
			$GLOBALS['GlyphNumbersForThisClue'][$rand_pick-1] = 0;
			
			// 	<!--- now display the glyph for  each --->
			$GlyphArtSuffix = $glyph_jpg_list[$GnumX-1];
			$this_glyph_word = $GLOBALS['glyph_list'][$GnumX-1];
			$this_glyph_level = $GLOBALS['glyph_level'][$GnumX-1];
			
			$glyphlink = "gamecode/sth3/gameart/SmallGlyphs/glyph".$GlyphArtSuffix.".jpg";
    		$glyphs_html .= '<img src="'.$glyphlink.'" height="13">';

    		
			if ( strlen($this_glyph_word) > 1 || (strlen($this_glyph_word) == 1 && ( 
		  		($this_glyph_word >= 'a' && $this_glyph_word <= 'z' ) ||
		  		($this_glyph_word >= 'A' && $this_glyph_word <= 'Z' ) ||
		  		($this_glyph_word >= '0' && $this_glyph_word <= '9' ) ) ) )
		  	{
		  		$glyphs_html .= ' = ';
		  		$glyphs_html .= $this_glyph_word;
		  	}
		  	else 
		  	{ // punctuation
		  		if ($this_glyph_word == "." || $this_glyph_word == ",")
		  		{
					$glyphs_html .= '=';
		  			$glyphs_html .= $this_glyph_word;		  		}
		  		else
		  		{   // some other punctuation
		  			$glyphs_html .= '=';
		  			$glyphs_html .= $this_glyph_word;
		  		}
		  	}
    		    		
    		$counter1++;
			if ($counter1 == 2)
			{
				$glyphs_html .= '</div>';
				$counter1 = 0;
			}				
		}
		if ($counter1 == 1)
		{
			$glyphs_html .= '</div>';           
		}
		$pdf->MultiCell($card_width,$card_height,$glyphs_html,'RLTB','C',1,0,'','',1,0,1);
			
	
    $i = 14;
	    
    $Ctext = $clue_list[$i-1];
    $Clevel = $clue_level[$i-1];
    
    //  <!--- clue must be translated to glyphs --->
    
    $result = GetGlyphNumbersFromClue($Ctext);

    //    <!--- fills in Session.GlyphNumbersForThisClue --->

    $clue_html = '<div>';
    foreach ($GLOBALS['GlyphNumbersForThisClue'] as $GnumX)
    {
    	$GlyphArtSuffix = $glyph_jpg_list[$GnumX-1];
    	$glyphlink = "gamecode/sth3/gameart/SmallGlyphs/glyph".$GlyphArtSuffix.".jpg";
    	$clue_html .= '<img src="'.$glyphlink.'" height="13">';
    	
    }
    $clue_html .= '</div>';

    $pdf->MultiCell($card_width,$card_height,$clue_html,'RLTB','C',1,0,'','',1,0,1);

    
// </cfif> <!--- no real_errors  from above first table --->



if ($GLOBALS['printerrorcount'] > 0)
{
	
	$errorcount = $GLOBALS['printerrorcount'];
	print "<div>Errors Printing Game!!  Count: $errorcount </div>";
	foreach($GLOBALS['printerrorlist'] as $errortext)
	print "<div>ERROR: $errortext</div>";
	
	//$session->sth_game_state = "gameerror";		
}
else
{
	// print "<p>NO PRINT ERRORS !</p>";
	//$session->sth_game_state = "gameready";	
}
	

// END   Game!
	
	
	
	
	
	
	
	
	
	


	// ---------------------------------------------------------
	// ---------------------------------------------------------
	//Close and output PDF document  (starts below)
	// ---------------------------------------------------------
	// reset pointer to the last page
	$pdf->lastPage();	

	$pdf_file_path = "sthpdf/". $session->sth_pdf_filename;
	
	echo "1: {$pdf_filename} 2: {$session->sth_pdf_filename}";
	
	
	//$pdf->Output($pdf_file_path, 'F');
	
	$pdf->Output($_SERVER['DOCUMENT_ROOT'].'/'.$pdf_file_path, 'F');
	// the above (for localhost) puts the pdf into /opt/lampp/
	
	
	echo "File Saved to: ".$_SERVER['DOCUMENT_ROOT']."/".$pdf_file_path;
	
	
// ---------------------------------------------------------


//============================================================+
// END OF FILE                                                 
//=====================================		
// Done CREATE PDF FILE
// ***********************************************************
// ***********************************************************
// ***********************************************************	




?>




		

