<?php
$optres['css'][] = $module_css_path . 'amodules/filter_adverts_list/css/filter_adverts_list.css';
$optres['js'][] = $path . 'amodules/filter_adverts_list/js/filter_adverts_list_default.js';
$filter_content_show = 0;
ob_start();

$filter_title = (isset($module_params['filter_title']) && trim($module_params['filter_title']) != "") ? trim($module_params['filter_title']) : '';
$filter_description = (isset($module_params['filter_description']) && trim($module_params['filter_description']) != "") ? trim($module_params['filter_description']) : '';
$filter_id = (isset($module_params['filter_id']) && intval($module_params['filter_id']) > 0) ? intval($module_params['filter_id']) : 0;
$filter_pagination = (isset($module_params['filter_pagination']) && intval($module_params['filter_pagination']) > 0) ? intval($module_params['filter_pagination']) : 0;
$filter_range = (isset($module_params['filter_range']) && intval($module_params['filter_range']) > 0) ? intval($module_params['filter_range']) : 10;


$filter_locations = (isset($module_params['filter_locations']) && count($module_params['filter_locations'])>0 ) ? $module_params['filter_locations'] : array();
$filter_locations_tmp = array_keys($locations_map);

if($filter_id > 0) {

    $filter_friendly_url = '';
    $p_url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $p_f = parse_url($p_url);
    $b_f = parse_url($base_url);

    $filter_friendly_url = ltrim(str_replace($b_f['path'], '', $p_f['path']), '/');

    $canonical_link = $protocol . $p_f['host'] . $p_f['path'];
    $def_canonical_link = $canonical_link;

    $current_page = 1;
    if(isset($_GET['p']) && intval($_GET['p']) > 0) {
        $current_page = intval($_GET['p']);
        if($filter_pagination == 0) {
            $send301 = array("send301" => 1, "url" => $def_canonical_link);
        }
    }

    // Reset the current page if we do not care about pagination
    if($filter_pagination == 0) $current_page = 1;

    $filter_str = sprintf("%'.04d", $filter_id);
    $filter_adverts_key = "filter_adverts_" . $middle_catdom_module_id;
    //$pfcache->delete($filter_adverts_key);
    $filter_adverts = $pfcache->get($filter_adverts_key);
    if($filter_adverts == null) {

        $catdoms = array();
        $catdoms [] = $domain_id;
        $result_sub = mysql_query ("SELECT * FROM  domaincategories WHERE parent_id='$domain_id' AND active='yes';");
        if (mysql_num_rows($result_sub) <> 0){
            while($line_sub = mysql_fetch_array($result_sub)){
                $catdoms [] = intval($line_sub["catdom_id"]);
            }
        }

        $results = array();
        $q = "SELECT
                      a.*,
                      d.catdom_id as bound_catdom_id,
                      d.active as bound_active,
                      d.category_top as bound_top,
                      d.params as bound_params
                      FROM
                      advertisment a
                      LEFT JOIN domain_advertisment d
                        ON d.advertisment_id=a.advertisment_id AND d.catdom_id IN (" . (implode(",", $catdoms)) . ")
                      WHERE
                      a.active='yes'
                      AND a.filter_id LIKE ('%$filter_str%')
                      ";

        if (count($filter_locations) > 0) {

            foreach ($filter_locations as $i => $f_loc) {
                $filter_locations[$i] = "'" . $f_loc . "'";
            }

            $filter_locations_str = implode(",", $filter_locations);
            $q = "SELECT
                      a.*,
                      d.catdom_id as bound_catdom_id,
                      d.active as bound_active,
                      d.category_top as bound_top,
                      d.params as bound_params
                      FROM
                      (advertisment a,
                      advertisment_locations l)
                      LEFT JOIN domain_advertisment d ON d.advertisment_id=a.advertisment_id AND d.catdom_id IN (" . (implode(",", $catdoms)) . ")
                      WHERE
                      a.advertisment_id=l.advertisment_id
                      AND l.district IN ($filter_locations_str)
                      AND a.active='yes'
                      AND a.filter_id LIKE ('%$filter_str%')
                      ";
        }

        if (isset($_REQUEST['debug_filters'])) echo $q;


        //$wb_search_params['wb_adverts'] = array();

        //$qq [] = $q;

        $r = mysql_query($q) or die(mysql_error());
        if ($r && mysql_num_rows($r) > 0) {
            $count = 0;
            while ($line = mysql_fetch_assoc($r)) {
                $adv_id = $line['advertisment_id'];
                if (!isset($results[$adv_id])) {
                    $count++;
                    $results[$adv_id] = array();
                    $results[$adv_id]["order"] = 0;
                    $results[$adv_id]["categ_order"] = 0;
                    $results[$adv_id]["title_en"] = stripslashes($line["short_title_en"]);
                    $results[$adv_id]["advertisment_id"] = $line["advertisment_id"];
                    $results[$adv_id]["use_town_dir"] = false;
                    $results[$adv_id]["town_locations"] = array();
                    $results[$adv_id]["paying"] = false;
                    $results[$adv_id]["data"] = array_map('stripslashes', $line);
                }

                if ($line['bound_top'] == "yes") {
                    $results[$adv_id]["paying"] = true;
                    if ($line['bound_catdom_id'] == $domain_id) {
                        if ($results[$adv_id]["order"] < 10) {
                            $results[$adv_id]["order"] = 10;
                            $results[$adv_id]["categ_order"] = 10;
                        }

                    } elseif ($results[$adv_id]["order"] < 6) {
                        $results[$adv_id]["order"] = 6;
                        $results[$adv_id]["categ_order"] = 6;
                    }
                }

                if ($line['bound_active'] == "yes") {
                    $results[$adv_id]["paying"] = true;
                    if ($line['bound_catdom_id'] == $domain_id) {
                        if ($results[$adv_id]["order"] < 8) {
                            $results[$adv_id]["order"] = 8;
                            $results[$adv_id]["categ_order"] = 8;
                        }

                    } elseif ($results[$adv_id]["order"] < 6) {
                        $results[$adv_id]["order"] = 6;
                        $results[$adv_id]["categ_order"] = 6;
                    }
                }

                $params = $line['bound_params'];
                if (!empty($params) && $params != "") {
                    $params = unserialize(base64_decode($params));
                    if (is_array($params) && count($params) > 0) {
                        $results[$adv_id]["town_locations"] = $params;
                    }
                }


                //$wb_search_params['wb_adverts'][] = $line['adv_code'];


            }
            $filter_adverts = $results;
            unset($results);
            $pfcache->set($filter_adverts_key, $filter_adverts, 600);
        }
    }

    if(is_array($filter_adverts) && count($filter_adverts) > 0) {
        $results = $filter_adverts;
        $num_results = count($results);
        $filter_content_show = 1;

        $range = $filter_range;
        $num_pages = intval($num_results/$range);
        if($num_results % $range !== 0) $num_pages += 1;

        if($current_page > $num_pages) {
            $send301 = array("send301" => 1, "url" => $def_canonical_link);
        }

        if($filter_pagination == 1) {
            if($current_page > 1) {

                if ($current_page < $num_pages) {
                    $rel_next_page = $def_canonical_link . "?p=" . ($current_page + 1);
                    $rel_prev_page = $def_canonical_link . "?p=" . ($current_page - 1);
                    $canonical_link .= "?p=" . $current_page;
                } else {
                    $rel_prev_page = $canonical_link . "?p=" . ($current_page - 1);
                    $canonical_link .= "?p=" . $current_page;
                }
                if($current_page == 2) {
                    $rel_prev_page = $def_canonical_link;
                }
            }
            elseif($current_page == 1 && $num_pages > 1) {
                $rel_next_page = $canonical_link . "?p=2";
            }
        }

        $start = ($current_page - 1) * $range;

        if($num_results > $start + $range){
            $end = $start + $range;
        }
        else{
            $end = $num_results;
        }

        if(!isset($send404) || $send404 === false) {
            ?>
            <section class="filter-adverts-list">
                <?php
                if ($filter_title != '') {
                    echo '<header><h2>' . $filter_title . '</h2></header>';
                }

                if ($filter_description != '') {
                    echo '<p class="filter-desc">' . $filter_description . '</p>';
                }


                $order = array();
                $categ_order = array();
                $title_en = array();
                $advertisment_id = array();
                $town_locations = array();
                foreach ($results as $key => $row) {
                    $order [$key] = $row['order'];
                    $categ_order [$key] = $row['categ_order'];
                    $title_en [$key] = $row['title_en'];
                    $advertisment_id [$key] = $row['advertisment_id'];
                }

                array_multisort($categ_order, SORT_NUMERIC, SORT_DESC, $title_en, SORT_ASC, SORT_STRING, $results);

                //echo '<pre>', var_export($results, true), '</pre>';

                $i = 0;
                foreach ($results as $key => $row) {
                    $order[$i] = $row['order'];
                    $categ_order[$i] = $row['categ_order'];
                    $title_en[$i] = $row['title_en'];
                    $advertisment_id[$i] = $row['advertisment_id'];
                    $town_locations[$i] = $row['town_locations'];
                    $i++;
                }


                //echo "<hr>";


                $click_count_paths = array();

                if ($filter_pagination == 0) {
                    $start = 0;
                    $end = $num_results;
                }

                for ($i = $start; $i < $end; $i++) {
                    $paying_advert = false;


                    if (isset($results[$i])) {
                        $line = $results[$i]['data'];
                        $use_town_dir = false;
                        $current_advertisment_id = intval($line['advertisment_id']);
                        $adv_code = $line['adv_code'];

                        // Check if it is a paying advert
                        $paying_advert = $results[$i]['paying'];


                        $website = fix_url(trim($line['website']));
                        $show_website = intval($line['show_website']);


                        $advert_active = ($line['active'] == "yes") ? true : false;

                        $shortNameURL = $line["title_tr"];
                        $shortNameALT = str_replace('"', '', $line["short_title_en"]);


                        $box_class = "freelisting";
                        if ($categ_order[$i] == 10) $box_class = "freelisting featured_1";
                        if ($categ_order[$i] == 8) $box_class = "freelisting featured_2";
                        if ($categ_order[$i] == 6) $box_class = "freelisting featured_3";

                        $click_link_id = 'adv_' . $line['advertisment_id'];
                        if ($is_hotel) {
                            if ($paying_advert) {
                                if ($advert_active) {
                                    $click_count_paths [$click_link_id] = 'click_count.php?link=' . $line["website"] . '&amp;type=www&amp;id=' . $line['advertisment_id'] . '&amp;domain=' . $line_domain["domainname"] . '&amp;domain_id=' . $domain_id . '&amp;click_from=s';
                                } else {
                                    $click_count_paths [$click_link_id] = 'click_count.php?link=' . $line["website"] . '&amp;type=www&amp;id=' . $line['advertisment_id'] . '&amp;domain=' . $line_domain["domainname"] . '&amp;domain_id=' . $domain_id . '&amp;click_from=s';
                                }
                            } else {
                                $click_count_paths [$click_link_id] = 'click_count.php?link=' . $line["website"] . '&amp;type=www&amp;id=' . $line['advertisment_id'] . '&amp;domain=' . $line_domain["domainname"] . '&amp;domain_id=' . $domain_id . '&amp;click_from=s';
                            }
                        } else {
                            $click_count_paths [$click_link_id] = 'click_count.php?link=' . $line["website"] . '&amp;type=www&amp;id=' . $line['advertisment_id'] . '&amp;domain=' . $line_domain["domainname"] . '&amp;domain_id=' . $domain_id . '&amp;click_from=s';
                        }

                        ?>
                        <div class="box box-primary box-compact <?php echo $box_class; ?>">
                            <?php
                            if (!isset($_SESSION['user_id']) && $paying_advert) {
                                mysql_query("UPDATE advertisment SET views=views+1 WHERE advertisment_id='{$line['advertisment_id']}'") or die (mysql_error());
                                count_views('adverts_stats_views', 'advertisment_id', $line['advertisment_id'], $domain_id, '', 's');
                            }


                            if ($is_hotel) {
                                echo '<div class="box-header with-border"><h2>';
                                if ($use_town_dir) {
                                    $shortNameURL = $town_friendly_url . $shortNameURL;
                                }

                                if ($paying_advert && $advert_active) {
                                    ?>
                                    <a href="<?php echo $protocol . $microdata_host . '/' . $shortNameURL; ?>"
                                       target="_blank"
                                       class="h2-result-link"><?php echo stripslashes($line["short_title_en"]); ?></a>
                                    <?php
                                } else {
                                    ?>
                                    <a href="<?php echo $protocol . $microdata_host . '/' . $shortNameURL; ?>"
                                       target="_blank"
                                       class="h2-result-link"><?php echo stripslashes($line["short_title_en"]); ?></a>
                                    <?php
                                }
                                echo '</h2></div>';
                            } else {
                                ?>
                                <div class="box-header with-border">
                                    <h2 class=""><?php echo trim(stripslashes($line["short_title_en"])); ?></h2>
                                </div>

                                <?php
                            }
                            if (isset($_REQUEST['debug1'])) {
                                echo "<strong>" . $categ_order[$i] . "</strong>";
                            }
                            $rec_locations = array();
                            $location = array();
                            $q = "select * from advertisment_locations where advertisment_id='{$advertisment_id[$i]}' and active='1' order by main_location asc;";
                            $qq [] = $q;
                            $r = @mysql_query($q);
                            if ($r && mysql_num_rows($r) > 0) {
                                while ($row = mysql_fetch_assoc($r)) {
                                    if (in_array($row['district'], $filter_locations_tmp)) {
                                        $rec_locations [] = $row;

                                        if (isset($_REQUEST['debugx'])) {
                                            echo '<br/>' . $row['district'] . ' is in ' . var_export($wb_locations, true);
                                        }

                                    } else {
                                        if (isset($_REQUEST['debugx'])) {
                                            echo '<br/>' . $row['district'] . ' not in ' . var_export($wb_locations, true);
                                        }
                                    }
                                    if ($row['main_location'] == 1 && count($location) == 0) $location = $row;
                                }
                            }

                            if (count($rec_locations) > 0) {
                                if (count($rec_locations) > 1 && count($location) == 0) $location = $rec_locations[0];
                                if (count($rec_locations) == 1) $location = $rec_locations[0];
                                if (count($town_locations[$i]) > 0) {
                                    //Decided not to use - htaccess issues
                                    //$use_town_dir = true;
                                }
                            }

                            if (isset($_REQUEST['debugx'])) {
                                echo '<pre>', var_export($rec_locations, true), '</pre>';
                                echo '<pre>', var_export($wb_locations, true), '</pre>';
                            }

                            ?>
                            <div class="box-body">
                                <div class="row no-margin">
                                    <div class="col-md-3 text-center search-result-logo">
                                        <?php
                                        if ($website != "" && $show_website == 1) {
                                            if ($advert_active) {
                                                if ($paying_advert) {
                                                    if ($is_hotel) {
                                                        ?>
                                                        <span class="click-link flimg"
                                                              rel="<?php echo $click_link_id; ?>" extra_class="flimg">
                                                                <img
                                                                    src="<?=$screenshot_url;?>w=120&amp;h=90&amp;url=<?php echo urlencode($website); ?>"
                                                                    width="120" height="90" border="0"
                                                                    alt="<?php echo stripslashes($line['short_title_en']); ?>"/>
                                                            </span>
                                                        <?php
                                                    } else {
                                                        ?>
                                                        <a class="flimg"
                                                           href="click_count.php?link=<?php echo $line["website"]; ?>&amp;type=www&amp;id=<?php echo $line['advertisment_id']; ?>&amp;domain=<?php echo $line_domain["domainname"]; ?>&amp;domain_id=<?php echo $domain_id; ?>&amp;click_from=s"
                                                           target="_blank">
                                                            <img
                                                                src="<?=$screenshot_url;?>w=120&amp;h=90&amp;url=<?php echo urlencode($website); ?>"
                                                                width="120" height="90" border="0"
                                                                alt="<?php echo stripslashes($line['short_title_en']); ?>"/>
                                                        </a>
                                                        <?php
                                                    }
                                                }
                                                else {
                                                    if ($is_hotel) {
                                                        ?>
                                                        <span class="click-link flimg"
                                                              rel="<?php echo $click_link_id; ?>" extra_class="flimg">
                                                            <img
                                                                src="<?=$screenshot_url;?>w=120&amp;h=90&amp;url=<?php echo urlencode($website); ?>"
                                                                width="120" height="90" border="0"
                                                                alt="<?php echo stripslashes($line['short_title_en']); ?>"/>
                                                        </span>
                                                        <?php
                                                    }
                                                    else {
                                                        ?>
                                                        <span class="flimg">
                                                            <img
                                                            src="<?=$screenshot_url;?>w=120&amp;h=90&amp;url=<?php echo urlencode($website); ?>"
                                                            width="120" height="90" border="0"
                                                            alt="<?php echo stripslashes($line['short_title_en']); ?>"/>
                                                        </span>
                                                        <?php
                                                    }
                                                }
                                            } else {
                                                if ($is_hotel) {
                                                    ?>
                                                    <span class="click-link flimg" rel="<?php echo $click_link_id; ?>"
                                                          extra_class="flimg">
                                                        <img
                                                        src="<?=$screenshot_url;?>w=120&amp;h=90&amp;url=<?php echo urlencode($website); ?>"
                                                        width="120" height="90" border="0"
                                                        alt="<?php echo stripslashes($line['short_title_en']); ?>"/>
                                                    </span>
                                                    <?php
                                                } else {
                                                    ?>
                                                    <span class="flimg">
                                                        <img
                                                        src="<?=$screenshot_url;?>w=120&amp;h=90&amp;url=<?php echo urlencode($website); ?>"
                                                        width="120" height="90" border="0"
                                                        alt="<?php echo stripslashes($line['short_title_en']); ?>"/>
                                                    </span>
                                                    <?php
                                                }
                                            }
                                        }
                                        ?>
                                    </div>
                                    <div class="col-md-7 search-result-data">

                                        <table class="table">
                                            <tr>
                                                <td class="th col-sm-3">phone:</td>
                                                <td class="col-sm-7">
                                                    <?php
                                                    if (isset($location['tel1']) && trim($location['tel1']) != "") {
                                                        $tmp_tel = preg_replace("/[^0-9]/", "", $location['tel1']);
                                                        $tmp_tel = substr($tmp_tel, -8);
                                                        echo '<a href="tel:+357' . $tmp_tel . '">(+357) ' . number_format($tmp_tel, null, null, ' ') . '</a>';
                                                    } else {
                                                        echo 'n/a';
                                                    }
                                                    ?>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="th col-sm-3">email:</td>
                                                <td class="col-sm-7">
                                                    <?php
                                                    if (isset($location['email']) && trim($location['email']) != "") {
                                                        ?>
                                                        <a href="javascript:;" class="mailto"
                                                           rel="<?php echo str_replace('@', '[ at ]', trim($location['email'])); ?>"><?php echo str_replace('@', ' [ at ] ', trim($location['email'])); ?></a>
                                                        <?php
                                                    } else {
                                                        echo 'n/a';
                                                    }
                                                    ?>
                                                </td>
                                            </tr>
                                            <?php
                                            if ($show_website == 1) {
                                                ?>
                                                <tr>
                                                    <td class="th col-sm-3">website:</td>
                                                    <td class="col-sm-7">
                                                        <?php
                                                        if ($website != "") {
                                                            if ($advert_active) {
                                                                if ($paying_advert) {
                                                                    if ($is_hotel) {
                                                                        ?>
                                                                        <span class="click-link"
                                                                              data-rel="<?php echo $click_link_id; ?>"><?php echo $website; ?></span>
                                                                        <?php
                                                                    } else {
                                                                        ?>
                                                                        <a href="<?php echo $protocol . $microdata_host . '/' . $shortNameURL; ?>"
                                                                           data-link="<?php echo $protocol . $microdata_host . '/'; ?>click_count.php?link=<?php echo $line["website"]; ?>&amp;type=www&amp;id=<?php echo $line['advertisment_id']; ?>&amp;domain=<?php echo $line_domain["domainname"]; ?>&amp;domain_id=<?php echo $domain_id; ?>&amp;catdom_id=<?php echo $catdom_id; ?>&amp;click_from=s"
                                                                           target="_blank"
                                                                           class="adv_linkage"><?php echo $website; ?></a>
                                                                        <?php
                                                                    }
                                                                } else {
                                                                    if ($is_hotel) {
                                                                        //echo '<a href="' . 'click_count.php?link=' .$line["website"] . '&amp;type=www&amp;id=' . $line['advertisment_id'] . '&amp;domain=' . $line_domain["domainname"] .'&amp;domain_id=' . $domain_id. '&amp;click_from=s' . '" target="_blank">'.$website.'</a>';
                                                                        ?>
                                                                        <span class="click-link"
                                                                              data-rel="<?php echo $click_link_id; ?>"><?php echo $website; ?></span>
                                                                        <?php
                                                                    } else {
                                                                        echo $website;
                                                                    }
                                                                }
                                                            } else {
                                                                if ($is_hotel) {
                                                                    ?>
                                                                    <span class="click-link"
                                                                          data-rel="<?php echo $click_link_id; ?>"><?php echo $website; ?></span>
                                                                    <?php
                                                                    //echo '<a href="' . 'click_count.php?link=' .$line["website"] . '&amp;type=www&amp;id=' . $line['advertisment_id'] . '&amp;domain=' . $line_domain["domainname"] .'&amp;domain_id=' . $domain_id. '&amp;click_from=s' . '" target="_blank">'.$website.'</a>';
                                                                } else {
                                                                    echo $website;
                                                                }
                                                            }
                                                        } else {
                                                            echo 'n/a';
                                                        }
                                                        ?>
                                                    </td>
                                                </tr>
                                                <?php
                                            }
                                            ?>
                                        </table>
                                    </div>
                                </div>
                            </div>
                            <div class="box-footer">
                                <?php
                                if ($is_hotel) {
                                    echo '';
                                    if (isset($_SESSION['user_id'])) {
                                        echo '<a href="../admin/advert_manager.php?advert_action=advert_edit_main&advertisment_id=', $line['advertisment_id'], '" target="_blank">Edit</a> | ';
                                    }
                                } else {


                                    if ($paying_advert && $advert_active) {
                                        ?>
                                        More about <a
                                            href="<?php echo $protocol . $microdata_host . '/' . $shortNameURL; ?>"
                                            target="_blank"><strong><?php echo stripslashes($line["short_title_en"]); ?></strong>
                                            <i class="fa fa-arrow-circle-right fa-lg pull-right"></i></a>
                                        <?php
                                    } else {
                                        ?>
                                        More about <a
                                            href="<?php echo $protocol . $microdata_host . '/' . $shortNameURL; ?>"
                                            target="_blank"
                                            class=""><strong><?php echo stripslashes($line["short_title_en"]); ?></strong>
                                            <i class="fa fa-arrow-circle-right fa-lg pull-right"></i></a>
                                        <?php
                                    }
                                }
                                ?>
                            </div>
                        </div>
                        <?php
                    }
                }

                if ($filter_pagination == 1 && $num_pages > 1) {
                    ?>
                    <div class="search-results-pagination text-left">
                        <div class="search-results-num">
                            <?php
                            if ($num_results > $start + $range) {
                                $end = $start + $range;
                            } else {
                                $end = $num_results;
                            }
                            echo "Results <b>", ($start + 1), " - ", $end, "</b> out of <b>", $num_results, "</b>";
                            ?>
                        </div>
                        <nav>
                            <ul class="pagination">
                                <?php
                                $pagesrange = 2;
                                $pages = (($num_results / $range) - intval($num_results / $range) == 0) ? intval($num_results / $range) : (intval($num_results / $range) + 1);
                                $startmsg = '';
                                if (($start / $range) <= $pagesrange) {
                                    $startrange = 0;
                                } else {
                                    $startrange = $start / $range - $pagesrange;
                                    $prev = $current_page - 1;
                                    $startmsg = '<li><a href="' . $filter_friendly_url . '?p=' . $prev . '" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>';
                                    $startmsg .= '<li><a href="' . $filter_friendly_url . '?p=1">1</a></li>';
                                    $startmsg .= '<li><a href="#">...</a></li>';
                                    //$startmsg = "<a href='{$_SERVER['PHP_SELF']}?start=$prev&search=$search&amp;catdom_ids=$catdom_ids&filter_ids=$filter_ids&seri=111&module_page=search_adv' class=\"small10\">Previous</a>  <a href='{$_SERVER['PHP_SELF']}?start=&search=$search&amp;catdom_ids=$catdom_ids&filter_ids=$filter_ids&seri=111&module_page=search_adv' class=\"small10\">1</a> . . .  ";
                                }

                                if (($startrange + (2 * $pagesrange) + 1) >= $pages) {
                                    $endrange = $pages;
                                    //$endmsg = '<li><a href="#" aria-label="Next" class="disabled"><span aria-hidden="true">&raquo;</span></a></li>';
                                    $endmsg = '';
                                } else {
                                    $endrange = $startrange + (2 * $pagesrange) + 1;
                                    //$nex=$start+$range;
                                    $nex = $current_page + 1;
                                    $endpage = ceil($pages);
                                    $end = floor($pages) * $range;
                                    $endmsg = '<li><a href="#">...</a></li>';
                                    $endmsg .= '<li><a href="' . $filter_friendly_url . '?p=' . ($endpage) . '">' . $endpage . '</a></li>';
                                    $endmsg .= '<li><a href="' . $filter_friendly_url . '?p=' . ($nex) . '" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>';
                                    //$endmsg="  . . .  <a href='{$_SERVER['PHP_SELF']}?start=$end&search=$search&amp;catdom_ids=$catdom_ids&filter_ids=$filter_ids&seri=111&module_page=search_adv' class=\"small10\">$endpage</a> <a href='{$_SERVER['PHP_SELF']}?start=$nex&search=$search&amp;catdom_ids=$catdom_ids&filter_ids=$filter_ids&seri=111&module_page=search_adv' class=\"small10\">Next</a>";
                                }

                                echo $startmsg;

                                for ($noofpage = $startrange; $noofpage < $endrange; $noofpage++) {
                                    if ($start != $noofpage * $range) {
                                        $active_link = '';
                                        if ($noofpage + 1 == $current_page) $active_link = 'active';
                                        ?>
                                        <li>
                                            <a href="<?php echo $filter_friendly_url . '?p=' . ($noofpage + 1) . ''; ?>"
                                               class=""><?php echo($noofpage + 1); ?></a></li>
                                        <?php
                                    } else {
                                        echo '<li class="active"><a href="#">' . ($noofpage + 1) . '</a></li>';
                                    }
                                }
                                echo $endmsg;
                                ?>
                            </ul>
                        </nav>
                    </div>
                    <?php
                }
                ?>
                <script type="text/javascript">
                    var filter_click_count_paths = <?php echo (is_array($click_count_paths)) ? json_encode($click_count_paths) : '{}';?>;
                </script>
            </section>
            <?php
        }
    }

}



$filter_content = ob_get_contents();
ob_end_clean();



if($filter_content_show == 1) {
	echo $filter_content;
}
if( (isset($send404) && $send404 === true) ) {
    ob_clean();
    $send404 = true;
    require_once(dirname(__FILE__) . "/../404/404.php");
}