<?
require_once('../connect/connect.php');
include_once('../lib/lib.php');

$bas="../";
$arr_f=array();

if($_GET['viget']) $viget=anti($_GET['viget']);
$dir_viget=dir_top();

$vd = "../informer/".$viget.".js";
$vk = $dir_viget."informer/".$viget.".js";

if($viget && (!$vd || (@filemtime($vd)+1200) < time()))
	{
	$ex_viget=explode("_", $viget);
	$q='';

	if($ex_viget[0])
		{
		$r=mysql_query("SELECT * FROM region WHERE id_region='".$ex_viget[0]."'");
		$rr=mysql_fetch_array($r);
		$arr_reg=explode(' ', $rr['path']);
		$count_reg=count($arr_reg)-1;
		$q .= ' region_'.$count_reg.' = '.$ex_viget[0].' ';
		}

	if($ex_viget[1])
		{
		$r=mysql_query("SELECT * FROM catalog WHERE id_catalog='".$ex_viget[1]."'");
		$rr=mysql_fetch_array($r);
		$arr_cat=explode(' ', $rr['path']);
		$count_cat=count($arr_cat)-1;
		if($q)
		$q .= ' AND ';
		$q .= ' razdel_'.$count_cat.' = '.$ex_viget[1].' ';
		}
	
	if($q)
	$q .= ' AND ';
	$q .= " small !='' ";
	
	$arr_sort=array('id','id DESC','zag','zag DESC','small DESC','small');
	shuffle ($arr_sort);

	$rez=30;
	$rz = mysql_query ("SELECT * FROM board WHERE ".$q." ORDER BY ".$arr_sort[0]." LIMIT ".$rez."") or die(mysql_error());
	$nrz = mysql_num_rows($rz);
	if($nrz)
		{
		ob_start();
		$i=0;
		while($ams=mysql_fetch_array($rz))
			{
			if($ex_viget[3]==1)
			$arr_f[$i] = '<tr><td valign=top width=100>';
			else
			$arr_f[$i] = '<td valign=top width=100>';
			if(file_exists($bas.$ams['small']) && is_file($bas.$ams['small']))
			$arr_f[$i] .= '<a href="'.$dir_viget.'items/'.$ams['zag_url'].'_'.$ams['id'].'"><img border=0 src="'.$dir_viget.$ams['small'].'" ></a><br>';
			$arr_f[$i] .= '<a href="'.$dir_viget.'items/'.$ams['zag_url'].'_'.$ams['id'].'">'.trim($ams['zag']).'</a><br><br>';
			$arr_f[$i] .= '</td>';
			$i++;
			}

		if($nrz < $rez) $rez=$nrz;
		if($ex_viget[2]>=$rez) {$next_blok=0; $ex_viget[2]=$rez;} else $next_blok=1;
		
		?>
var script_ks_c = <?=$ex_viget[2]?>; 
var script_ks_next_blok = <?=$next_blok?>; 
var script_ks_blok = new Array();
		
		<?
		foreach ($arr_f as $i=>$ar)
			{
    		echo("\nscript_ks_blok[".$i."] = '".$ar."';");
			}
		?>


var script_ks_len=script_ks_blok.length;

if(script_ks_next_blok==1)
	{
	Array.prototype.shuffle = function( b ){ var i = this.length, j, t; while( i )  {  j = Math.floor( ( i-- ) * Math.random() );  t = b && typeof this[i].shuffle!=='undefined' ? this[i].shuffle() : this[i];  this[i] = this[j];  this[j] = t; } return this;};
	script_ks_blok.shuffle();
	}

var script_ks_begin = script_ks_c;
var script_ks_end = 0;
var script_ks_a = '';
var script_ks_end_2=0;

var script_ks_b="<table><tr>";
for(var j=0; j<script_ks_c; j++)
	{
	script_ks_b=script_ks_b+script_ks_blok[j];
	}
script_ks_b=script_ks_b+"</tr></table>";
			
function toNext() {
if(script_ks_begin == script_ks_len) 
	{
	script_ks_begin=0;
	}
		
script_ks_end = script_ks_begin+script_ks_c;
if(script_ks_end > script_ks_len)
	{
	script_ks_end_2 = script_ks_end - script_ks_len;
	script_ks_end = script_ks_len;
	}
				
script_ks_a = "<table ><tr>";
for(i = script_ks_begin; i < script_ks_end; i++)
	{
	script_ks_a=script_ks_a+script_ks_blok[i];
	}
script_ks_begin=script_ks_end;

if(script_ks_end_2>0)
	{
	for(var i = 0; i < script_ks_end_2; i++)
		{
		script_ks_a=script_ks_a+script_ks_blok[i];
		}
	script_ks_begin=script_ks_end_2;
	script_ks_end_2=0;
	}
				
script_ks_a=script_ks_a+"</tr></table>";
document.getElementById("script_ks_text").innerHTML=script_ks_a; 
}

document.write('<div id="script_ks_text" >');
document.write(script_ks_b);
document.write('</div>');

function start_viget()
	{
   	setInterval('toNext()',6000);
	}
	
if (script_ks_next_blok==1)
	{
	start_viget();
	}



		<?
		$buf = ob_get_contents();
		ob_end_clean();


		$file1 = fopen ($vd, "w");
		flock($file1, LOCK_EX);
		fputs($file1, $buf);
		fclose($file1);
		}
	}
?>
