<tr>
  <td valign="top">
    <?php
    if ($lewa_strona == "true") {
      $szerok_l = BOX_WIDTH_LEFT_IS - 42; } else { $szerok_l = BOX_WIDTH_RIGHT_IS - 42;
    }
    ?>
    <div class="box_left"></div>
    <div class="infoBoxHeading" style="width:<?php echo $szerok_l; ?>px"><?php echo $boxHeading; ?></div>
      <?php if ($boxLink != '') { ?>
        <div style="float:left;"><?php echo $boxLink ?></div>
        <?php } else { ?>
        <div class="box_right"></div>
      <?php } ?>
  </td>
</tr>
<tr>
  <td>
    <table border="0" width="100%" cellspacing="0" cellpadding="1" class="Box_border">
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>
      <tr>
        <td align="center" valign="top">
          <table border="0" width="92%" cellspacing="0" cellpadding="0">
            <tr>
              <td class="boxText"<?php echo $boxContent_attributes; ?>>

                <div id="treemenu"><noscript>
                <?php
                $categories = array();
                $expanded = array();
                // This is the time to live of the tree_items.js file in seconds.
                $time_to_live = 300;

                $categories_string = '';
                $tree_items_string = '';
                $current_category_name = '';

                if (tep_not_null($cPath)) {
                  $expanded = explode("_", $cPath);
                }

                $expanded = array_reverse($expanded);
                $expanded[] = 0;
                $expanded =	array_reverse($expanded);

                loadTree();
                loadBranch(0, 0);
                cache_tree_items();

                $info_box_contents = array();
                $info_box_contents[] = array('text' => $categories_string);
                //new infoBox($info_box_contents);

                function loadBranch($parent_id, $depth = 0, $draw_category = true, $first_element = true ) {
                  global $categories, $categories_string, $expanded, $tree_items_string, $current_category_name;

                  foreach ($categories[$parent_id]['child_list'] as $cat) {

                    if ($draw_category) {
                      $categories_string .= '<a href="' . tep_href_link(FILENAME_DEFAULT, 'cPath=' . $categories[$cat]['path']) . '">';

                      if ((count($expanded) > $depth + 1) && ($categories[$cat]['categories_id'] == $expanded[$depth + 1])){
                        $categories_string .= '<b>';
                      }

                      $categories_string .= str_repeat('&nbsp;&nbsp;', $depth);

                      $categories_string .= $categories[$cat]['categories_name'];

                      if ($categories[$cat]['child_count'] > 0) {
                        $categories_string .= '->';
                      }

                      if ((count($expanded) > $depth + 1) && ($categories[$cat]['categories_id'] == $expanded[$depth + 1])) {
                        $categories_string .= '</b>';
                      }

                      $categories_string .= '</a>';

                      if (SHOW_COUNTS == 'true') {
                        if ((CATEGORIES_COUNT_ZERO == '1' and $categories[$cat]['count_with_sub'] == 0) or $categories[$cat]['count_with_sub'] >= 1) {
                          $categories_string .= ' (' . $categories[$cat]['count_with_sub'] . ')';
                        }
                      }

                      $categories_string .= '<br />';
                    }

                    $tree_items_string .= ",\n"
                                       . str_repeat("  ", $depth + 2)
                                       . "['" . addslashes($categories[$cat]['categories_name']);

                    if (SHOW_COUNTS == 'true') {
                      if ((CATEGORIES_COUNT_ZERO == '1' and $categories[$cat]['count_with_sub'] == 0) or $categories[$cat]['count_with_sub'] >= 1) {
                        $tree_items_string .= ' (' . $categories[$cat]['count_with_sub'] . ')';
                      }
                    }
                    $tree_items_string .= "', '"
                           . tep_href_link(FILENAME_DEFAULT, 'cPath=' . $categories[$cat]['path']) . "', "
                             . (($first_element && $parent_id == 0 && $depth == 0) ? 'true' : 'false');
                    $first_element = false;

                    if ((count($expanded) > $depth + 1) && ($categories[$cat]['categories_id'] == $expanded[$depth + 1])) {
                      $current_category_name = $categories[$cat]['categories_name'];
                      if (SHOW_COUNTS == 'true') {
                        if ((CATEGORIES_COUNT_ZERO == '1' and $categories[$cat]['count_with_sub'] == 0) or $categories[$cat]['count_with_sub'] >= 1) {
                          $current_category_name .= ' (' . $categories[$cat]['count_with_sub'] . ')';
                        }
                      }
                      loadBranch($categories[$cat]['categories_id'], $depth + 1);
                    } else {
                      loadBranch($categories[$cat]['categories_id'], $depth + 1, false);
                    }

                    $tree_items_string .= ']';
//                    $tree_items_string .= "\n" . str_repeat("  ", $depth + 2) . ']';
                  }
                }

                function loadTree() {
                  global $categories, $languages_id, $cPath, $expanded;

                  // Get the categories data and put it into the tree
                  $categories[0] = array('categories_id' => 0,
                                         'parent_id' => -1,
                                         'categories_name' => 'root',
                                         'child_count' => 0,
                                         'child_list' => array(),
                                         'count' => 0,
                                         'count_with_sub' => 0,
                                         'path' => '0');

                  $categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = cd.categories_id and cd.language_id='" . (int)$languages_id ."' and c.categories_status = '1' order by sort_order, cd.categories_name");

                  while ($groups_cat = tep_db_fetch_array($categories_query))  {
                    $categories[$groups_cat['categories_id']] = array('categories_id' => $groups_cat['categories_id'],
                                                                      'parent_id' => $groups_cat['parent_id'],
                                                                      'categories_name' => $groups_cat['categories_name'],
                                                                      'child_count' => 0,
                                                                      'child_list' => array(),
                                                                      'count' => 0,
                                                                      'count_with_sub' => 0,
                                                                      'path' => $groups_cat['categories_id']);
                  }


                  //Count child for each category
                  foreach($categories as $cat) {
                    if (array_key_exists($cat['parent_id'], $categories)) {
                      $categories[$cat['parent_id']]['child_count']++;
                      array_push($categories[$cat['parent_id']]['child_list'], $cat['categories_id']);
                    }
                  }

                  if (SHOW_COUNTS == 'true') {
                    // Populate the product counts array
                    $products_query = tep_db_query("select count(p2c.products_id) as count, p2c.categories_id
                                                   from " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " .
                                                   TABLE_PRODUCTS . " p
                                                   where p.products_id=p2c.products_id
                                                   and p.products_status='1'
                                                   group by categories_id");

                    while ($productsCount = tep_db_fetch_array($products_query)) {
                      if (array_key_exists($productsCount['categories_id'], $categories)) {
                        $categories[$productsCount['categories_id']]['count'] = $productsCount['count'];
                        $categories[$productsCount['categories_id']]['count_with_sub'] = $productsCount['count'];
                      }
                    }
                  }
                  foreach($categories as $cat) {

                    $parentId = $cat['parent_id'];

                    // walkup to root and update counters and cPath. don`t count for root
                    while($parentId > 0 and array_key_exists($parentId, $categories)) {
                      $categories[$parentId]['count_with_sub'] += $cat['count'];
                      $categories[$cat['categories_id']]['path'] = $parentId . '_' . $categories[$cat['categories_id']]['path'];
                      $parentId = $categories[$parentId]['parent_id'];
                    }
                  }
                return;
              }

              function cache_tree_items() {
                global $tree_items_string, $language, $time_to_live;
                $filename = DIR_FS_CATALOG . 'tmp/tree_items_' . $language . '.js';

                $difference = 0;

                if (file_exists($filename)) {
                  $now = time();
                  $filetime = filemtime($filename);
                  $difference = $now - $filetime;
                }

                if (!file_exists($filename) || ($difference > $time_to_live)) {
                  if ($fp = @fopen($filename, 'w')) {
                    flock($fp, 2);
                    fputs($fp, "var TREE_ITEMS = [\n" . "  ['', 0, false" . $tree_items_string . "\n  ]\n];");
                    flock($fp, 3);
                    fclose($fp);
                  }
                }
              }
              ?>
              </noscript></div>

              <!-- tigra tree menu  //-->
              <script type="text/javascript" language="JavaScript">
                load_tree_menu = 0;
                var tree_icons_dir = "includes/javascript/tigramenu/images/icons/treemenu";
              </script>
              <script type="text/javascript" language="JavaScript" src="includes/javascript/tigramenu/jscript_tree.js"></script>
              <script type="text/javascript" language="JavaScript" src="includes/javascript/tigramenu/jscript_tree_tpl.js"></script>
              <script type="text/javascript" language="JavaScript" src="tmp/tree_items_<?php echo $language ?>.js"></script>
              <table border="0" width="100%" cellspacing="0" cellpadding="3"><tr><td>
                <div id="treemenuContent">
                <script type="text/javascript" language="JavaScript">
                  function getElem (idName) { return document.all ? document.all[idName] : document.getElementById(idName);};
                  var treemenu = getElem('treemenuContent');
                  if (getElem('treemenu') && load_tree_menu) {
                    getElem('treemenu').innerHTML = '';
                    new tree (TREE_ITEMS, TREE_TPL, '<?php echo ( ($current_category_id > 0) ? addslashes(tep_href_link(FILENAME_DEFAULT, 'cPath=' . $cPath)) : 'null') ?>');
                  }
                </script>
                </div>
              </td></tr></table>
              <!-- tigra tree menu eof //-->
            </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
    </tr>
  </table>
</td>
</tr>
<tr>
 <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '3'); ?></td>
</tr>
