<?php
/*
	Copyright © Alexander Sunvas
	ICQ: 854984, 122255566
	URL: http://sunvas.info
	E-mail: sunvas@mail.ru; sunvas@vingrad.ru

	Powered by Sunvas. Resale is forbidden.
*/
global $conf, $db, $prefix, $index;
$content='';
$temp_parent=0;
$temp_text='';
$div='<div id="cats-'.strtolower($conf['name']).'-{id}" style="display: {disp}; padding-left: 20px;">categ{id}|</div>';
$image='<img src="images/{type}.gif" border="0" id="image-'.strtolower($conf['name']).'-{id}" onclick="SwitchCategory(\'{id}\',\''.strtolower($conf['name']).'\');" style="cursor: pointer;" alt="Вложенных - {cnt}" />';
$result = $db->sql_query('SELECT f.`id`, f.`title`, f.`description`, f.`parentid`, COUNT(s.`id`) FROM `'.$prefix.'_categories` AS f LEFT JOIN `'.$prefix.'_categories` AS s ON f.`id`=s.`parentid` WHERE f.`modul`=\''.mysql_escape_string(strtolower($conf['name'])).'\' GROUP BY f.`id` ORDER BY f.`parentid`, LEFT(f.`title`,3)');
while (list($id, $title, $descr, $parentid, $cnt) = $db->sql_fetchrow($result))
{
	if ($parentid!=$temp_parent)
	{
		if (strpos($content,'categ'.$temp_parent.'|')>0)
			$content=str_replace('categ'.$temp_parent.'|',$temp_text,$content);
		else
			$content.=$temp_text;
		$temp_text="";
		$temp_parent=$parentid;
	}
	$cnt=intval($cnt);
	if ($cnt>0)
	{
		if ($_COOKIE['cats-'.strtolower($conf['name']).'-'.$id]=='true')
		{
			$disp='block';
			$type='minus';
		}
		else
		{
			$disp='none';
			$type='plus';
		}
		$this_image=str_replace(array('{id}','{cnt}','{type}'),array($id,$cnt,$type),$image)." ";
		$this_div=str_replace(array('{id}','{disp}'),array($id,$disp),$div);
	}
	else
	{
		$this_div='';
		$this_image='<img border="0" src="images/misc/navi.png" alt="" /> ';
	}
	$temp_text.=$this_image.'<a href="index.php?name='.$conf['name'].'&amp;cat='.$id.'" title="'.strip_tags($descr).'">'.$title.'</a><br />'.$this_div."\r\n";
}
if (!empty($temp_text))
{
	if (strpos($content,'categ'.$temp_parent.'|')>0)
		$content=str_replace('categ'.$temp_parent.'|',$temp_text,$content);
	else
		$content.=$temp_text;
	$temp_text='';
	$temp_parent=$parentid;
}
$content='<script src="ajax/BlockCategories.js" type="text/javascript"></script>'.((empty($content) or strpos($index,'<a href="http://sunvas.info" title="Sunvas"')===false) ? 'Нет категорий' : '<div align="left" style="padding-left: 15px;">'.$content.'</div>');
?>