<? session_start();?>
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?
// Emulate register_globals on
if (!ini_get('register_globals')) {
   $superglobals = array($_SERVER, $_ENV,
      $_FILES, $_COOKIE, $_POST, $_GET);
   if (isset($_SESSION)) {
      array_unshift($superglobals, $_SESSION);
   }
   foreach ($superglobals as $superglobal) {
      extract($superglobal, EXTR_SKIP);
   }
   ini_set('register_globals', true);
   }

if($page == ""){
	$page=1;
}
?>
<?php
  require_once "/home/jsimonds/public_html/cmsAdmin/lib/viewer_functions.php";

  list($tile_imagesRecords, $tile_imagesMetaData) = getRecords(array(
    'tableName'   => 'tile_images',
    'perPage'     => '1',
	'debugSql' => true, 
  ));

  // show error message if no matching record is found
  if (!@$tile_imagesRecords[0]) {
    print "Record not found!";
    exit;
  }

  //Interactive Tools Help

  // get first record
  $tile_imagesRecord =& $tile_imagesRecords[0];

  // look up related fields from other section(s) and insert them into our record
  beta_lookupRelatedFields('tile_images', $tile_imagesRecords, array(
    // TODO: list all referencing fields here
    'floor_grout', 'wall_grout', 'wall_feature_grout', 'shower_wall_1_grout', 'shower_wall_2_grout', 'shower_feature_grout', 'bath_hob_and_face_grout', 'vanity_wall_grout', 'splashback_tile_grout', 'pool_tile_grout', 'facade_tile_grout', 'floor_tile', 'floor_feature_1', 'floor_feature_2', 'wall_tile', 'wall_feature_1', 'wall_feature_2', 'shower_wall_1', 'shower_wall_2', 'shower_feature', 'bath_hob_and_face', 'vanity_wall_tile', 'splashback_tile', 'pool_tile', 'facade_tile'
  ));

function beta_lookupRelatedFields($tableName, &$recordList, $fieldNameList) {
  $schema = loadSchema($tableName);

  // look through supplied fields and records, compiling a list of tables and nums to lookup
  $lookupsToDo = array();
  foreach ( $fieldNameList as $fieldName ) {
    $field =& $schema[$fieldName];
    if ( !$field ) { die("lookupRelatedFields: field '$fieldName' not found"); }
    if ( @$field['optionsType'] != 'table' ) { die("lookupRelatedFields: field '$fieldName' is not set to 'Get options from database (advanced)'"); }
    if ( !@$field['optionsTablename'] ) { die("lookupRelatedFields: field '$fieldName' does not reference another table"); }
    if ( @$field['optionsValueField'] != 'num' ) { die("lookupRelatedFields: field '$fieldName' is not set to use 'num' for option values"); }

    // add an array for this table if we don't have one already
    $refTableName = $field['optionsTablename'];
    if (!@$lookupsToDo[$refTableName]) { $lookupsToDo[$refTableName] = array(); }

    // add nums to be looked up in the referenced table
    foreach ( $recordList as $record ) {
      if (@$record[$fieldName]) {
        $refNums = split("\t", trim($record[$fieldName]));
        foreach ( $refNums as $refNum ) {
          $lookupsToDo[$refTableName][ $refNum ] = true;
        }
      }
    }
  }

  // get referenced records
  $referencedRecords = array();
  foreach ( $lookupsToDo as $refTable => $refNums ) {
    $refNumsCommaSep = join(', ', array_keys($refNums));
    list($refRecords, $refMeta) = getRecords(array(
      'tableName'   => $refTable,
      'where'       => "num in ($refNumsCommaSep)",
    ));
    $refRecordsByNum = array_combine(beta_array_collect($refRecords, 'num'), $refRecords);
    $referencedRecords[ $refTable ] = $refRecordsByNum;
  }

  // inject referenced records into original records' fields
  foreach ( array_keys($recordList) as $recordKey ) {
    $record =& $recordList[$recordKey];
    foreach ( $fieldNameList as $fieldName ) {
      $field =& $schema[$fieldName];

      if ($field['listType'] == 'checkboxes' || $field['listType'] == 'pulldownMulti') {
        $newValues = array();
        if (@$record[$fieldName]) {
          $refNums = split("\t", trim($record[$fieldName]));
          foreach ( $refNums as $refNum ) {
            $newValues[] =& $referencedRecords[ $field['optionsTablename'] ][ $refNum ];
          }
        }
        $record[$fieldName] = $newValues;
      }
      else {
        $record[$fieldName] =& $referencedRecords[ $field['optionsTablename'] ][ $record[$fieldName] ];
      }
    }
  }
}

function beta_array_collect(&$array, $key) {
  $result = array();
  foreach ( $array as $element ) {
    if(!in_array($element[$key], $result)) {
      array_push($result, $element[$key]);
    }
  }
  return $result;
}



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Simonds Tile Gallery</title>
	<link rel="stylesheet" href="css/all.css" type="text/css" />
	<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
	<!--[if IE ]><link rel="stylesheet" type="text/css" href="css/ie.css" /><![endif]-->
    <script type="text/javascript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->
    </script>
        <link rel="stylesheet" href="MagicZoom.css" type="text/css" media="screen" />
        <script src="mz-packed.js" type="text/javascript"></script>
</head>
<body>
	<div id="wrapper">
		<?php include("includes/header.php"); ?>
		<div id="main">
			<?php include("includes/sidenav.php"); ?>
	  </div>
			<div id="content">
				<div class="data">
					<span class="subheading">
					<?php foreach ($tile_imagesRecords as $record): ?>
                                    <strong>On display in:</strong> <?php echo join(', ', getListLabels('tile_images', 'display_village_s', $record['display_village_s'])); ?></a>
                                    <?php endforeach; ?></span>
                                    <?php foreach ($tile_imagesRecords as $record): ?>
									<?php
  list($housesRecords) = getRecords(array(
    'tableName'   => 'houses',
    'where'       => "num = '{$tile_imagesRecord['house']}'",
    'limit'       => '1',
  ));
  $housesRecord = @$housesRecords[0]; // get first record

  list($room_typesRecords) = getRecords(array(
    'tableName'   => 'room_types',
    'where'       => "num = '{$tile_imagesRecord['room_type']}'",
    'limit'       => '1',
  ));
  $room_typesRecord = @$room_typesRecords[0]; // get first record
?>
 <h3><?php echo $housesRecord['title'] ?>&nbsp;<?php echo $record['house_description'] ?>: <?php echo $room_typesRecord['title'] ?></strong>
 <? $house_descr_to_pass = $housesRecord['title']." ".$record['house_description'];?></h3>
 <?php endforeach; ?>
	
					<div class="main-gallery">
                     <?php if ($tile_imagesMetaData['prevPage']): ?>
								<a href="<?php echo $tile_imagesMetaData['prevPageLink'] ?>" class="link-prev">pre</a>

	<?php else: ?>
									<a href="<?php echo $tile_imagesMetaData['prevPageLink'] ?>" class="link-prev">pre</a>
    <?php endif ?>
    <?php foreach ($tile_imagesRecords as $record): ?>
             <?php foreach ($record['main_image'] as $upload): ?>
				      <div class="image-holder">
					    	<a href="<?php echo $upload['urlPath'] ?>" class="MagicZoom" rel="zoom-position:inner;zoom-fade:true;show-loading:true;loading-msg:Loading Zoom" style="margin: auto"/> <img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt=""></a>
					  </div>
                        <?php endforeach ?>
   											 <?php endforeach; ?>
					              <?php if ($tile_imagesMetaData['nextPage']): ?>
									<a href="<?php echo $tile_imagesMetaData['nextPageLink'] ?>" class="link-next">next</a>
    <?php else: ?>
									<a href="<?php echo $tile_imagesMetaData['nextPageLink'] ?>" class="link-next">next</a>
    <?php endif ?>
					</div>
					<div class="favourite">
                    <a href="favourites.php?image1=<?=urlencode($upload['urlPath'])?>&roomtype=<?=$room_typesRecord['num']?>&roomtitle=<?=urlencode($room_typesRecord['title'])?>&colour_tones=<?=$tile_imagesRecord['colour_tones']?>&house=<?=urlencode($house_descr_to_pass)?>&conceptno=<?=$record['concept_no']?>&page=<?=$page?>&village=<?php echo urlencode(join(', ', getListLabels('tile_images', 'display_village_s', $record['display_village_s']))); ?>"><span>Add To Favourites</span></a>
					  <a href="<?php echo $upload['urlPath'] ?>" class="view" target="_blank"><span>View Enlarged Image</span></a>
					</div>
				</div>
				<div class="aside">
					<div class="select-box type1">
					<h2>IN THIS IMAGE:</h2>
						<?php foreach ($tile_imagesRecords as $record): ?>
      <?php if ($record['concept_no']): ?>
      <p><em>Concept No.: </em><?php echo $record['concept_no'] ?></p>
      <?php endif ?>
	  <?php if ($record['floor_tile']): ?>
      <p><strong>Floor Tile: </strong>Similar to <?php echo $record['floor_tile']['tile_sku']?> <?php echo $record['floor_tile']['tile_dimensions']?> <span class="faint"><?php echo $record['floor_tile']['tile_category']?></span></p>
      <?php endif ?>
      <?php if ($record['floor_feature_1']): ?>
	  <p><strong>Floor Feature 1: </strong>Similar to <?php echo $record['floor_feature_1']['tile_sku']?> <?php echo $record['floor_feature_1']['tile_dimensions']?>      
      <span class="faint"><?php echo $record['floor_feature_1']['tile_category']?></span></p>      <?php endif ?>
      <?php if ($record['floor_feature_2']): ?>
      <p><strong>Floor Feature 2: </strong>Similar to <?php echo $record['floor_feature_2']['tile_sku']?> <?php echo $record['floor_feature_2']['tile_dimensions']?>		      
      <span class="faint"><?php echo $record['floor_feature_2']['tile_category']?></span></p>
      <?php endif ?>
      <?php if ($record['floor_grout']): ?>
 <p><strong>Floor Grout: </strong><?php echo $record['floor_grout']['title']?></p> <?php endif ?>
      <?php if ($record['wall_tile']): ?>
 <p><strong>Wall Tile: </strong>Similar to <?php echo $record['wall_tile']['tile_sku']?> <?php echo $record['wall_tile']['tile_dimensions']?> <span class="faint"><?php echo $record['wall_tile']['tile_category']?></span></p>      <?php endif ?>
      <?php if ($record['wall_grout']): ?>
<p><strong>Wall Grout: </strong><?php echo $record['wall_grout']['title']?></p>      <?php endif ?>
      <?php if ($record['wall_feature_1']): ?>
 <p><strong>Wall Feature 1: </strong>Similar to <?php echo $record['wall_feature_1']['tile_sku']?> <?php echo $record['wall_feature_1']['tile_dimensions']?> <span class="faint"><?php echo $record['wall_feature_1']['tile_category']?></span></p>      <?php endif ?>
      <?php if ($record['wall_feature_2']): ?>
 <p><strong>Wall Feature 2: </strong>Similar to <?php echo $record['wall_feature_2']['tile_sku']?> <?php echo $record['wall_feature_2']['tile_dimensions']?> <span class="faint"><?php echo $record['wall_feature_2']['tile_category']?></span></p>      <?php endif ?>
      <?php if ($record['wall_feature_grout']): ?>
 <p><strong>Wall Feature Grout: </strong><?php echo $record['wall_feature_grout']['title']?></p>   <?php endif ?>
      <?php if ($record['shower_wall_1']): ?>
 <p><strong>Shower Wall 1: </strong>Similar to <?php echo $record['shower_wall_1']['tile_sku']?> <?php echo $record['shower_wall_1']['tile_dimensions']?> <span class="faint"><?php echo $record['shower_wall_1']['tile_category']?></span></p>      <?php endif ?>
      <?php if ($record['shower_wall_1_grout']): ?>
 <p><strong>Shower Wall 1 Grout: </strong><?php echo $record['shower_wall_1_grout']['title']?></p>     <?php endif ?>
      <?php if ($record['shower_wall_2']): ?>
 <p><strong>Shower Wall 2: </strong>Similar to <?php echo $record['shower_wall_2']['tile_sku']?> <?php echo $record['shower_wall_2']['tile_dimensions']?> <span class="faint"><?php echo $record['shower_wall_2']['tile_category']?></span></p>      <?php endif ?>
      <?php if ($record['shower_wall_2_grout']): ?>
 <p><strong>Shower Wall 2 Grout: </strong><?php echo $record['shower_wall_2_grout']['title']?></p>  <?php endif ?>
      <?php if ($record['shower_feature']): ?>
 <p><strong>Shower Feature: </strong>Similar to <?php echo $record['shower_feature']['tile_sku']?> <?php echo $record['shower_feature']['tile_dimensions']?> <span class="faint"><?php echo $record['shower_feature']['tile_category']?></span></p>      <?php endif ?>
      <?php if ($record['shower_feature_grout']): ?>
 <p><strong>Shower Feature Grout: </strong><?php echo $record['shower_feature_grout']['title']?></p>      <?php endif ?>
      <?php if ($record['bath_hob_and_face']): ?>
 <p><strong>Bath Hob And Face: </strong>Similar to <?php echo $record['bath_hob_and_face']['tile_sku']?> <?php echo $record['bath_hob_and_face']['tile_dimensions']?> <span class="faint"><?php echo $record['bath_hob_and_face']['tile_category']?></span></p>      <?php endif ?>
      <?php if ($record['bath_hob_and_face_grout']): ?>
 <p><strong>Bath Hob And Face Grout: </strong><?php echo $record['bath_hob_and_face_grout']['title']?></p>      <?php endif ?>
      <?php if ($record['vanity_wall_tile']): ?>
 <p><strong>Vanity Wall: </strong>Similar to <?php echo $record['vanity_wall_tile']['tile_sku']?> <?php echo $record['vanity_wall_tile']['tile_dimensions']?> <span class="faint"><?php echo $record['vanity_wall_tile']['tile_category']?></span></p>      <?php endif ?>
      <?php if ($record['vanity_wall_grout']): ?>
 <p><strong>Vanity Wall Grout: </strong><?php echo $record['vanity_wall_grout']['title']?></p>      <?php endif ?>
      <?php if ($record['splashback_tile']): ?>
 <p><strong>Splashback Tile: </strong>Similar to <?php echo $record['splashback_tile']['tile_sku']?> <?php echo $record['splashback_tile']['tile_dimensions']?> <span class="faint"><?php echo $record['splashback_tile']['tile_category']?></span></p>      <?php endif ?>
      <?php if ($record['splashback_tile_grout']): ?>
 <p><strong>Splashback Tile Grout: </strong><?php echo $record['splashback_tile_grout']['title']?></p>      <?php endif ?>
      <?php if ($record['pool_tile']): ?>
 <p><strong>Pool Tile: </strong>Similar to <?php echo $record['pool_tile']['tile_sku']?> <?php echo $record['pool_tile']['tile_dimensions']?> <span class="faint"><?php echo $record['pool_tile']['tile_category']?></span></p>      <?php endif ?>
      <?php if ($record['pool_tile_grout']): ?>
 <p><strong>Pool Tile Grout: </strong><?php echo $record['pool_tile_grout']['title']?></p>      <?php endif ?>
      <?php if ($record['facade_tile']): ?>
 <p><strong>Facade Tile: </strong>Similar to <?php echo $record['facade_tile']['tile_sku']?> <?php echo $record['facade_tile']['tile_dimensions']?> <span class="faint"><?php echo $record['facade_tile']['tile_category']?></span></p>      <?php endif ?>
      <?php if ($record['facade_tile_grout']): ?>
 <p><strong>Facade Tile Grout: </strong><?php echo $record['facade_tile_grout']['title']?></p>      <?php endif ?>
      <?php if ($record['colour_tones']): ?>
 <p><em>Colour Tone: </em><?php echo $record['colour_tones'] ?></p>    <?php endif ?>
<?php endforeach; ?>
					</div>
					<span class="bottom"></span> 
					<div class="select-box type3">
					<h2>FIND MORE LIKE THIS</h2>
					<ul class="subnav type2">
						 <?php foreach ($tile_imagesRecords as $record): ?>
												<li><a href="detail.php?colour_tones=<?php echo $record['colour_tones'] ?>">Similar colour tone</a></li>
												<li><a href="detail.php?room_type=<?php echo $record['room_type'] ?>">Similar room type</a></li>
                                                <?php endforeach; ?>
					</ul>
				</div>
				</div>
			</div>
</div>
		<div class="wrapper-bottom"></div>
</body>
</html>
