<?php
session_start();

include("admin/config.php");

include("admin/settings.inc.php");

include("shoppingcart.php");

include("makethumb.php");


// warning if install.php has not been deleted
if ((file_exists("$site_dir/install.php"))OR(file_exists("$site_dir/upgrade.php")))
        {
                print "<b><font color='red'>$la_store_security</b>";
                exit;
        }

include("header.inc.php");

//echo"
//<div id=\"dot0\" style=\"position: absolute; visibility: hidden; height: 11; width: 11;\"><img src=\"images/bullet.gif\" height=11 width=11></div>
//<div id=\"dot1\" style=\"position: absolute; height: 15; width: 15;\"><img src=\"images/bullet.gif\" height=15 width=15></div>
//<div id=\"dot2\" style=\"position: absolute; height: 15; width: 15;\"><img src=\"images/bullet.gif\" height=15 width=15></div>
//<div id=\"dot3\" style=\"position: absolute; height: 15; width: 15;\"><img src=\"images/bullet.gif\" height=15 width=15></div>
//<div id=\"dot4\" style=\"position: absolute; height: 15; width: 15;\"><img src=\"images/bullet.gif\" height=15 width=15></div>
//<div id=\"dot5\" style=\"position: absolute; height: 15; width: 15;\"><img src=\"images/bullet.gif\" height=15 width=15></div>
//<div id=\"dot6\" style=\"position: absolute; height: 15; width: 15;\"><img src=\"images/bullet.gif\" height=15 width=15></div>
//<SCRIPT LANGUAGE=\"JavaScript\" src=scripts.js>
//</script>";



$cart = new Cart;

include("subheader.inc.php");

include_once("menu.inc.php");
//include_once("menu02.inc.php");

// start border
sb("100%","",$colour_1,$bg_colour);

if($cat_format=="b")
        {
                $cat_span=$cat_rows*2;
        }

else
        {
                $cat_span=$cat_rows;
        }

$col_width= 100/$cat_rows;
// if no category is selected display Top_Level categories
  //      <td colspan=\"$cat_span\" align=center class=\"title1\"><strong>$la_store_select_cat</strong></td>
$najnowszy=0;
If (empty($cat_id) and(cat_id)<>5) {$cat_id=1;}
//if (!empty($catname))
//{
If ($cat_id==4){$cat_id=217;}

If ($cat_id==5){$cat_id=0;}
If ($cat_id==7){$cat_id=1;}
If ($cat_id==8){$cat_id=1;}
If ($cat_id==9){$cat_id=1;}
If (($cat_id==5)&&($catname=='   ')) {$cat_id=0;}

// }




If (empty($cat_id))
        {
                // $cat_id=2;
                // welcome note
                echo"<table border=\"0\" width=\"100%\" cellspacing=\"2\" cellpadding=\"5\" align=\"center\">
                                <tr>

                                </tr>
                </table>";

                 
               // ze 106            <td align=\"left\"><strong>$welcome_note</strong></td>
               // ze 116  <td colspan=\"$cat_span\" align=center class=\"title1\"><strong>Latest Pictures</strong></td>
                 
                echo"<table border=\"0\" width=\"95%\" cellspacing=\"2\" cellpadding=\"5\" align=\"center\">
                                <tr>


                                </tr>
                                <tr align=\"center\">";







                                $sql_select = mysql_query("SELECT * FROM ".$prefix."store_category where cat_father_id = '$najnowszy' order by $cat_order");
                                $cat_count = 0;
                                while ($row = mysql_fetch_array($sql_select))
                                        {
                                                $cat_count = $cat_count+1;
                                                $cat_image = $row["cat_image"];
                                                $cat_id_list = $row["cat_id"];
                                                $category = $row["category"];

                                                if($cat_format=="i")
                                                        {
                                                                // image
                                                                echo"<td align=\"center\" width=\"$col_width%\"><a href=\"index.php?cat_id=$cat_id_list&catname='$category'\"><img src='images/$cat_image' alt='$category' border='0'></a>";

                                                        if($cat_count!==$cat_rows)
                                                                {
                                                                        echo"</td>";
                                                                }

                                                        if($cat_count==$cat_rows)
                                                                {
                                                                        echo"</td>
                                                                        </tr>
                                                                        <tr>";
                                                                        $cat_count = 0;
                                                                }
                                                        }

                                                if($cat_format=="t")
                                                        {
                                                                // text
                                                                echo"<td align=\"center\" width=\"$col_width%\"><a href=\"index.php?cat_id=$cat_id_list&catname='$category'\">$category</a>";

                                                                if($cat_count!==$cat_rows)
                                                                {
                                                                        echo"</td>";
                                                                }

                                                        if($cat_count==$cat_rows)
                                                                {
                                                                        echo"</td></tr><tr align=\"center\">";
                                                                        $cat_count = 0;
                                                                }
                                                        }

                                                if($cat_format=="b")
                                                        {
                                                                // text
                                                                echo"<td align=\"center\" width=\"$col_width_b%\" valign=\"bottom\"><a href=\"index.php?cat_id=$cat_id_list&catname='$category'\"><img src='images/$cat_image' alt='$category'  border='0'></a><br><a href=\"index.php?cat_id=$cat_id_list&catname='$category'\">$category</a>";

                                                                if($cat_count!==$cat_rows)
                                                                        {
                                                                                echo"</td>";
                                                                        }

                                                                if($cat_count==$cat_rows)
                                                                        {
                                                                                echo"</td></tr><tr align=\"center\">";
                                                                                $cat_count = 0;
                                                                        }
                                                        }

                                        }
        echo"</tr></table>";
        }

// display if category has been selected

if (!empty($cat_id))
        {

                // create navigation
                require ("link_navi.php");

$sql_select = mysql_query( "SELECT * FROM ".$prefix."store_category where cat_id='$cat_id' order by category");
        while ($row = mysql_fetch_array($sql_select))
                {
                        $cat_image = $row["cat_image"];
                        $category = $row["category"];
                }

  //PICTURE     // if (($cat_image!='catnophoto.gif')&&($cat_format!=="t"))
       //         {
       //                 echo"</p><img src='images/$cat_image'><br>";
       //         }


echo "<table border=\"0\" cellspacing=\"2\" width=\"95%\" cellpadding=\"10\" align=\"center\">
                <tr>";
                $myq="SELECT * FROM ".$prefix."store_category where cat_father_id = $cat_id order by category";
                $sql_select = mysql_query( "SELECT * FROM ".$prefix."store_category where cat_father_id = $cat_id order by category");
                $more_cat = MYSQL_NUM_ROWS($sql_select);
                $cat_count=0;
                while ($row = mysql_fetch_array($sql_select))
                        {
                                $cat_count = $cat_count+1;
                                $cat_image = $row["cat_image"];
                                $cat_id_list = $row["cat_id"];
                                $category = $row["category"];

                                $count_products = mysql_query( "select * from ".$prefix."store_inventory where cat_id='$cat_id_list'");
                                $totalprod = MYSQL_NUM_ROWS($count_products);
                                $cat_format="b";



                                if($cat_format=="b")
                                        {
                                                // both
                                                // ================================== wstawka search ================================
                                                if ($cat_id_list==4)
                                                {
                                                echo"<td align=\"center\"><a href=\"search.php?search=&kraj=&turn=US Open&rok=2011\"><img src='images/$cat_image' border='0' alt='$category'></a><br><a href=\"search.php?search=&kraj=&turn=Nasdaq 100 Open&rok=2006\">$category</a>";
                                                }
                                                else
                                               // if ($cat_id_list!==4)
                                                {
                                                echo"<td align=\"center\"><a href=\"index.php?cat_id=$cat_id_list&catname='$category'\"><img src='images/$cat_image' border='0' alt='$category'></a><br><a href=\"index.php?cat_id=$cat_id_list&catname='$category'\">$category</a>";
                                                }
                                                if ($totalprod!==0)
                                                {echo"&nbsp;($totalprod)";}

                                                if($cat_count!==$cat_rows)
                                                        {
                                                          //echo "$cat_count <> $cat_rows";
                                                                echo"</td>";
                                                        }

                                                if($cat_count==$cat_rows)
                                                        {

                                                                echo"</td>";
                                                               // echo "$cat_count == $cat_rows";
                                                                echo"</tr><tr align=\"center\">";
                                                                $cat_count = 0;
                                                        }
                                        }

                        }
        echo"</tr></table>";


// set limit value for number of records to be shown per page
// query database to find total number of records to display
$limit = $prod_limit;
$ile_column=5;
$query_count = " SELECT * FROM ".$prefix."store_inventory where cat_id='$cat_id' order by $prod_order";
$result_count = mysql_query($query_count);
$totalitem = mysql_num_rows($result_count);
$totalrows = ceil($result_count / $ile_column);
if(empty($page))

$page = 1;

$limitvalue = $page * $limit - ($limit);

//$query = "SELECT * FROM ".$prefix."store_inventory where cat_id='$cat_id' order by $prod_order LIMIT $limitvalue, $limit";
$query = "SELECT * FROM ".$prefix."store_inventory where cat_id='$cat_id' order by RIGHT(product,15) desc LIMIT $limitvalue, $limit";
$result = mysql_query($query) or die("Error: " . mysql_error());
$count_result = mysql_num_rows($result);

if((mysql_num_rows($result) == 0)and($more_cat==0))
echo("<p align=\"center\"><br>$la_store_no_products</p><br>");

// Display links at the top to indicate current page and number of pages displayed
$numofpages = ceil($totalitem / $limit );
//echo "order: $prod_order";
// echo "kolumn - $ile_column <-> items - $totalitem <-> count - $count_result <-> stron - $numofpages";
$from=$limit*$page-$limit+1;

$to=$from + $count_result-1;

echo "<table align=\"center\" width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
            <tr>
                                <td width=\"50%\" bgcolor=\"$colour_1\" align=\"left\">";if($numofpages>1){echo"$la_store_showing $from - $to</td><td width=\"50%\" bgcolor=\"$colour_1\" align=\"right\"><b>$la_store_go_to_page</b> ($numofpages) ";}

                                $upper_limit = $page + 3;
                                $lower_limit = $page - 2;

                                if($page != 1)
                                        {
                                                $pageprev = $page - 1;
                                                echo("<a href=\"$PHP_SELF?page=1&cat_id=$cat_id\">$la_store_prev</a>&nbsp;");
                                        }

                                $lower_dots = $page - $lowerlimit;

                                if($lower_dots > 3)
                                        {
                                                echo("...");
                                        }

                                for($i = 1; $i <= $numofpages; $i++)
                                        {
                                                if($numofpages>1)
                                                        {
                                                                if($i == $page)
                                                                        {
                                                                                echo("&nbsp;<b>[".$i."]</b>&nbsp;");
                                                                        }

                                                                if(($i != $page)&&($i < $upper_limit)&&($i >= $lower_limit))
                                                                        {
                                                                                echo("&nbsp;<a href=\"$PHP_SELF?page=$i&cat_id=$cat_id\">$i</a>&nbsp;");
                                                                        }
                                                        }
                                        }

                                $upper_dots = $numofpages - 2;

                                if($page < $upper_dots)
                                        {
                                                echo("...&nbsp;");
                                        }

                                if(($totalrows - ($limit * $page)) > 0)
                                        {
                                                $pagenext = $page + 1;
                                                echo("&nbsp;<a href=\"$PHP_SELF?page=$numofpages&cat_id=$cat_id\">$la_store_next</a>");
                                        }
                                echo"</tr>
                        </td>
                </table>";

// display products if there are any
$ix=1;
// echo "totr - $totalrows col- $ile_column it - $totalitem";
if ( $count_result <= $ile_column) { $ile_col=$to-$from+1; }
//else { $ile_col=$ile_column; }
if ( $count_result > $ile_column) { $ile_col=$ile_column; }

if($totalitem>0)
        {
                echo"<br><table align=\"center\" valign=\"top\" width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
                                        <tr bgcolor=\"$colour_2\">
                                                <td>
                                                        <table cellpadding=\"2\" border=\"0\" cellspacing=\"1\" valign=\"top\" width=\"100%\" align=\"center\">
                                                           <tr bgcolor=\"$colour_1\" height=\"20\" width=\"100%\">";
                                                           // echo "totr - $totalrows col- $ile_column it - $totalitem";



                                                           $ix=1;
                                                           while ($ix <= $ile_col)
                                                               {
                                                                     //  echo"<td width=\"200\"  align=\"center\">&nbsp</td>";
                                                                    //   echo"<td background=\"images/bevel_bg.gif\" align=\"center\">$totalrows col- $ile_column</td>";
                                                                  $ix++;
                                                               }

                                                               echo"</tr>";


        }
                                                        $bgcolour=$colour_4;

                                                        $zdjecie_numer=0;
                                                        while($row = mysql_fetch_array($result))
                                                                {
                                                                        $zdjecie_numer=$zdjecie_numer+1;
                                                                        $product=$row["product"];
                                                                        $cat_iid=$row["cat_id"];
                                                                        $description= $row["description"];
                                                                        $price=$row["price"];
                                                                        $sale_price=$row["sale_price"];
                                                                        $image=$row["image"];
                                                                        $title=$row["title"];
                                                                        $title=stripslashes($title);
                                                                        $description=stripslashes($description);
                                                                        $description=substr($description,0,100);

                                                                        if ($bgcolour ==$colour_3)
                                                                                {
                                                                                        $bgcolour =$colour_4;
                                                                                }
                                                                        elseif ($bgcolour ==$colour_4)
                                                                                {
                                                                                        $bgcolour =$colour_3;
                                                                                }
                                                                                
                                                                                

                                                                        if (!file_exists("$site_dir/images/" . "thumb_$image"))
                                                                                {

                                                                                        $thumb=new thumbnail("$site_dir/images/$image");
                                                                                        $thumb->size_auto(120);
                                                                                        $thumb->jpeg_quality(100);
                                                                                        $thumb->save("$site_dir/images/thumb_$image", $gd_version);
                                                                                }
                                                                        if ($zdjecie_numer==1)
                                                                                  {

                                                                                  $thumb_name="thumb_$image";
                                                                                  $thumb_no = "catnophoto.gif";
                                                                                  //echo $thumb_name;
                                                                                   $sql_cot = "select * from ".$prefix."store_category where cat_id = '$cat_iid'";

                                                                                    // echo $title;
	                                                                           $rez = mysql_query ($sql_cot);
	                                                                           $lin = mysql_fetch_array($rez);

	                                                                           $thumb_yes = $lin["cat_image"];
	                                                                          // echo "- $thumb_yes -";
	                                                                           if ($thumb_yes == $thumb_no)
                                                                                   {
                                                                               //   $sql_rep = "replace ".$prefix."store_category ( cat_image values ('$thumb_name'))";
			                                                      //    $rezu = mysql_query($sql_rep);
			                                                            $sql_upt = "update ".$prefix."store_category set cat_image='$thumb_name' where cat_id = '$cat_iid'";
	                                                                            $ret = mysql_query ($sql_upt);
			                                                           }
                                                                                  }

                                                                        if ($kol >= $ile_column)
                                                                          {
                                                            // http://forums.devshed.com/t50348/s.html

                                                             echo"<tr width=\"180\" bgcolor=\"$bgcolour\">";

                                                                           $kol=1;
                                                                          }
                                                                          else
                                                                          { $kol++; }

                     echo"<td width=\"180\" height=\"180\" bgcolor=\"$bgcolour\" align=\"center\" valign=\"middle\"><a href=\"view_product.php?product=$product\"><img src=\"images/thumb_$image\" border=\"0\"></a>
             <br><a href=\"view_product.php?product=$product\">$title</a>
             <br><a href=\"view_product.php?product=$product\"><font size=\"1\" color=\"#008080\">Calculate price</font></a>
             </td>";
             //echo  "$kol -=- $ile_column";
           
         
         
         //echo "els $kol - $ile_col";
      //   echo"<td width=\"180\" height=\"180\" bgcolor=\"$bgcolour\" align=\"center\"><a href=\"view_product.php?product=$product\"><img src=\"images/$image\" width=\"75\" height=\"75\" border=\"0\"></a>
   //        <br><a href=\"view_product.php?product=$product\">$product</a>
     //      </td>";
     // 

                                                                    }
                                                                 $kol=1;
                                                if($totalitem>0)
                                                        {

                                                                echo"</td>
                                                                </tr>
                                                        </table>
                                                </td>
                                        </tr>
                                </table><br>";
                        }


// Display links at the bottom to indicate current page and number of pages displayed
echo "<table align=\"center\" width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
                <tr>
                        <td bgcolor=\"$colour_1\" align=\"right\">";if($numofpages>1){echo"<b>$la_store_go_to_page</b> ($numofpages) ";}

                        $upper_limit = $page + 3;
                        $lower_limit = $page - 2;

                        if($page != 1)
                                {
                                        $pageprev = $page - 1;
                                        echo("<a href=\"$PHP_SELF?page=1&cat_id=$cat_id\">$la_store_prev</a>&nbsp;");
                                }

                        $lower_dots = $page - $lowerlimit;

                        if($lower_dots > 3)
                                {
                                        echo("...");
                                }

                        for($i = 1; $i <= $numofpages; $i++)
                                {
                                        if($numofpages>1)
                                                {
                                                        if($i == $page)
                                                                {
                                                                        echo("&nbsp;<b>[".$i."]</b>&nbsp;");
                                                                }

                                                        if(($i != $page)&&($i < $upper_limit)&&($i >= $lower_limit))
                                                                {
                                                                        echo("&nbsp;<a href=\"$PHP_SELF?page=$i&cat_id=$cat_id\">$i</a>&nbsp;");
                                                                }
                                                }
                                }

                        $upper_dots = $numofpages - 2;

                        if($page < $upper_dots)
                                {
                                        echo("...&nbsp;");
                                }

                        if(($totalrows - ($limit * $page)) > 0)
                                {
                                        $pagenext = $page + 1;
                                        echo("&nbsp;<a href=\"$PHP_SELF?page=$numofpages&cat_id=$cat_id\">$la_store_next</a>");
                                }
                        else

                        mysql_free_result($result);

                        echo"</td>
                </tr>
        </table><br><br>";
}
// end border

eb($bg_colour,$colour_1);
// include product spotlight is enabled

include("subfooter.inc.php");
//	echo"</tr>
//	</table>";
//echo"</td>
//</tr>";

include("footer.inc.php");

?>
