<?php
  /**
   * Copyright 2011 Zazavi - http://www.zazavi.com
   * Zazavi is free software released under the terms of the GNU General Public License.
   * You should have received a copy of the license.  If not, see <http://www.gnu.org/licenses/>.
   */
  define("_VALID_PHP", true);
  
  require_once("../init.php");

?>
<?php
  if (isset($_POST['liveSearch']))
      : $string = sanitize($_POST['liveSearch'],15);

  if (strlen($string) > 3)
      : $sql = $db->query("SELECT pt.*, pt.id as id, pg.id as pageid, pg.title as pagetitle, pg.slug" 
	  . "\n FROM posts AS pt"
	  . "\n LEFT JOIN pages AS pg ON pg.id = pt.page_id"
	  . "\n WHERE MATCH (pt.title, pt.body) AGAINST ('" . $db->escape($string) . "*' IN BOOLEAN MODE)"
	  . "\n ORDER BY pg.id LIMIT 10");
  $display = '';	  
  $display .= '<div id="searchresults">';
  $i = 0; $color1 = "search-even"; $color2 = "search-odd";
  while ($row = $db->fetch($sql)): 
  $i++;
  
  if ($core->seo == 1) :
	$link = $core->site_url . '/' . intval($row['pageid']) . '/' . $row['slug'] . '.html';
  else:
	$link = $core->site_url . '/content.php?pageid=' . intval($row['pageid']);
  endif;
  
  $title = $row['title'];
  $body = cleanOut($row['body']);
  $content = sanitize($body, 100);
  if (strlen($title) > 65)
      $title = substr($title, 0, 50) . "...";
  $display .= '<div class="'.(($i % 2 == 0) ? $color1 : $color2).'"><a href="'.$link.'">'.$title.'<small>'.$content.'...</small></a></div>';
  endwhile;
  $display .= '</div>';
  print $display;
  endif;
  endif;
?>