<?php
ob_start();
$optres['css'][] = $module_css_path . 'amodules/wb_categories/css/wb_categories.css';

$qq = array();
$st = microtime(true);
?>
<section class="wb_categories">
    <?php

    $wb_show_content = 1;
    if(!isset($catdom_id) || intval($catdom_id) == 0) $catdom_id = $domain_id;
    if(isset($_REQUEST['debug3'])) {
        echo "<br/><strong>CATDOM_ID=</strong>" . $catdom_id;
    }

    $wb_search_params = array();


    if(isset($_POST['wb_search'])) {
        $wb_search_params['filter'] = array();
        foreach ($_POST as $wb_fld=>$wb_selection) {
            $wb_search_params['filter'][$wb_fld] = $wb_selection;
        }
        //echo '<pre>', var_export($_POST, true), '</pre>';
    }

    // Get town friendly url
    $town_friendly_url = "";
    if($catdom_id != $domain_id) {
        $q = "select * from domaincategories where catdom_id='$catdom_id';";
        $qq [] = $q;
        $r = @mysql_query($q);
        if($r && mysql_num_rows($r) > 0) {
            $row = mysql_fetch_assoc($r);
            $town_friendly_url = trim($row['friendly_url']) . "/";
        }
    }

    $microdata_domain = str_replace("www.", "", $ultra_domainname);
    $microdata_host = $_SERVER['HTTP_HOST'];
    if(str_replace('www.', '', $microdata_host) != str_replace('www.', '', $ultra_domainname)) $microdata_host .= "/" . $ultra_addonname;
    $have_microdata = (isset($microdata_domains) && in_array($microdata_domain, $microdata_domains));

    if(isset($wb_page_num)) $start = $wb_page_num;
    $wb_domainname = str_replace("www.", "", $ultra_domainname);

    $is_hotel = false;
    if($wb_domainname == "cyprus-hotel.com") {
        $is_hotel = true;
    }

    if(isset($module_params)) {
        if(isset($module_params['module_show_results']) && intval($module_params['module_show_results']) == 0) {
            $wb_show_content = 0;
        }
    }


    $wb_locations = array();
    $wb_locations_tmp = array();
    if(isset($module_params) && isset($module_wb_locations)) {
        $wb_locations = $module_wb_locations;
        $wb_locations_tmp = $wb_locations;
    }

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

    $current_page = 1;
    if(isset($_REQUEST['current_page'])) {
        $wb_show_content = 1;
        $wb_show_with_title = true;
        $current_page = intval($_REQUEST['current_page']);

        if(isset($module_params) && isset($module_wb_title) && trim($module_wb_title != "")) {
            $webpage_title_en = $module_wb_title . ' - Page ' . $current_page;
            echo '<header><h1>'. $webpage_title_en . '</h1><!--[/SocialShareButtons/]--></header>';

        }
        else {
            $module_wb_title = $webpage_title_en;
            $webpage_title_en = $webpage_title_en . ' - Page ' . $current_page;
            echo '<header><h1>'. $webpage_title_en . '</h1><!--[/SocialShareButtons/]--></header>';
        }
    }
    else {
        if(isset($module_params) && isset($module_wb_title) && trim($module_wb_title != "")) {
            //
        }
        else {
            $module_wb_title = $webpage_title_en;
        }
    }

    $wb_friendly_url = "";
    $q = "select * from friendly_url where catdom_id='$domain_id' and module_table='domaincategories' and module_id_name='catdom_id' and module_id_value='$catdom_id'" ;
    $qq [] = $q;
    $r = @mysql_query($q);
    if($r && mysql_num_rows($r) == 1) {
        $row = mysql_fetch_assoc($r);
        $wb_friendly_url = trim($row['friendly_url']);
    }

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

    if($wb_show_content == 1) {
        // Get domain catdoms
        $catdoms = array();
        $catdoms [] = $domain_id;
        $qq [] = "SELECT * FROM  domaincategories WHERE parent_id='$domain_id';";
        $result_sub = mysql_query ("SELECT * FROM  domaincategories WHERE parent_id='$domain_id';");
        if (mysql_num_rows($result_sub) <> 0){
            while($line_sub = mysql_fetch_array($result_sub)){
                $catdoms [] = intval($line_sub["catdom_id"]);
            }
        }


        $q = "select * from wb_categories_domains where wb_domainname='$wb_domainname';";
        $qq [] = $q;
        $r = @mysql_query($q);
        if($r && mysql_num_rows($r) > 0) {
            $row = mysql_fetch_assoc($r);
            $wb_uid = $row['wb_uid'];

            //$results = $pfcache->get('wb_categories_adverts_' . $wb_domainname);
            /* Made the change below to try to have different cache for towns*/
            $results = $pfcache->get('wb_categories_adverts_' . $catdom_id);

            if($results == null) {
                $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,
                      wb_categories_adverts c)
                      LEFT JOIN domain_advertisment d ON d.advertisment_id=a.advertisment_id AND d.catdom_id IN (" . (implode(",", $catdoms)) . ")
                      WHERE
                      a.adv_code=c.wb_advert_uid
                      AND c.wb_uid='$wb_uid'
                      AND c.wb_status='1'
                      ";

                if (count($wb_locations) > 0) {

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

                    $wb_locations_str = implode(",", $wb_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,
                      wb_categories_adverts c)
                      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 ($wb_locations_str)
                      AND a.adv_code=c.wb_advert_uid
                      AND c.wb_uid='$wb_uid'
                      AND c.wb_status='1'
                      ";
                }

                if (isset($_REQUEST['debug4'])) 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'];


                    }

                    $pfcache->set('wb_categories_adverts_' . $catdom_id, $results, 600);
                }
            }
            $num_results = count($results);

            if($num_results > 0) {
                $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>";
                if($num_results > $start + $range){
                    $end = $start + $range;
                }
                else{
                    $end = $num_results;
                }

                $click_count_paths = array();

                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"]);

                        $thumbshot = trim($line['image']);
                        $thumbshot_file = dirname(__FILE__) . "/../../images/advertisments/advert_images/" . trim($line['image']);
                        $thumbshot_url = $protocol . $_SERVER['HTTP_HOST'] . "/images/advertisments/advert_images/" . trim($line['image']);
                        $has_thumbshot = ($thumbshot != "" && file_exists($thumbshot_file));

                        if(isset($_REQUEST['current_page'])) {
                            $meta_keywords [] = $line["short_title_en"];

                            if($i == $start) {
                                $meta['description'] = $line["short_title_en"];
                            }
                            else {
                                $meta['description'] .= ', ' . $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=' .'' . '&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=' .'' . '&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=' .'' . '&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=' .'' . '&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(isset($_REQUEST['debug1'])) {
                                echo "<strong>".$categ_order[$i]."</strong>";
                            }
                            $rec_locations = array();
                            $location = array();
                            $phones_array = array();
                            $q = "select * from advertisment_locations where advertisment_id='{$advertisment_id[$i]}' and active='1' order by main_location desc;";
                            $qq [] = $q;
                            $r = @mysql_query($q);
                            if($r && mysql_num_rows($r)>0) {
                                while($row = mysql_fetch_assoc($r)) {

                                    if(trim($row['tel1']) != "") {
                                        $phones_array [] = trim($row['tel1']);
                                    }

                                    if(in_array($row['district'], $wb_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;
                                }
                            }

                            // Get the Phone
                            $adv_tel = "";
                            if(count($phones_array) > 0) {
                                $tels = array();
                                $phones  = explode(',', trim($phones_array[0]));
                                if(is_array($phones)) {
                                    foreach($phones as $phone) {
                                        $phone = trim($phone);
                                        if($phone != "") {
                                            $tmp_tel = preg_replace("/[^0-9]/", "", $phone);
                                            $tmp_tel = substr($tmp_tel, -8);
                                            $tels [] = '<a href="tel:+357' . $tmp_tel . '">(+357) ' . number_format($tmp_tel, null, null, ' ') . '</a>';
                                        }
                                    }
                                }

                                if(count($tels) > 0) {
                                    ob_start();

                                    ?>
                                    <div class="btn-group wb-tel-btn pull-right">
                                        <button type="button" class="btn btn-danger btn-circle dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                            <i class="fa fa-phone fa-lg"></i>
                                        </button>
                                        <ul class="dropdown-menu">
                                            <?php
                                            foreach($tels as $tel_lnk) {
                                                echo '<li>'.$tel_lnk.'</li>';
                                            }
                                            ?>
                                        </ul>
                                    </div>
                                    <?php


                                    $adv_tel = ob_get_clean();
                                }

                            }





                            if($is_hotel) {
                                echo '<div class="box-header with-border"><h2 class="wb-title">';
                                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>';
                                echo $adv_tel;
                                echo '</div>';
                            }
                            else {
                                ?>
                                <div class="box-header with-border">
                                    <h2 class="wb-title">
                                        <a href="<?php echo $protocol . $microdata_host . '/' .  $shortNameURL;?>" target="_blank"><?php echo trim(stripslashes($line["short_title_en"]));?></a>
                                    </h2>
                                    <?=$adv_tel;?>
                                </div>

                                <?php
                            }


                            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-xs-10 col-sm-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">
                                                            <?php
                                                            if($has_thumbshot){
                                                                ?>
                                                                <img
                                                                    src="<?php echo $thumbshot_url; ?>"
                                                                    width="120" height="90" border="1"
                                                                    alt="<?php echo $line['short_title_en'] . ' Website Screenshot'; ?>"
                                                                    class="img-responsive img-thumbnail"/>
                                                                <?php
                                                            }
                                                            else {
                                                                ?>
                                                                <img
                                                                    src="<?=$screenshot_url;?>w=120&h=90&url=<?php echo urlencode($website); ?>"
                                                                    sizes="(max-width: 639px) 640px,
                                                                            120px"
                                                                    srcset="<?=$screenshot_url;?>w=120&h=90&url=<?php echo urlencode($website); ?> 120w,
                                                                    <?=$screenshot_url;?>w=640&h=480&url=<?php echo urlencode($website); ?> 640w"
                                                                    alt="<?php echo $line['short_title_en'] . ' Website Screenshot'; ?>"
                                                                    class="img-responsive img-thumbnail"/>
                                                                <?php
                                                            }
                                                            ?>
                                                        </span>
                                                        <?php
                                                    }
                                                    else {
                                                        ?>
                                                        <a class="flimg" href="click_count.php?link=<?php echo '';?>&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">
                                                            <?php
                                                            if($has_thumbshot){
                                                                ?>
                                                                <img
                                                                    src="<?php echo $thumbshot_url; ?>"
                                                                    width="120" height="90" border="1"
                                                                    alt="<?php echo $line['short_title_en'] . ' Website Screenshot'; ?>"
                                                                    class="img-responsive img-thumbnail"/>
                                                                <?php
                                                            }
                                                            else {
                                                                ?>
                                                                <img
                                                                    src="<?=$screenshot_url;?>w=120&h=90&url=<?php echo urlencode($website); ?>"

                                                                    sizes="(max-width: 639px) 640px,
                                                                            120px"
                                                                    srcset="<?=$screenshot_url;?>w=120&h=90&url=<?php echo urlencode($website); ?> 120w,
                                                                    <?=$screenshot_url;?>w=640&h=480&url=<?php echo urlencode($website); ?> 640w"

                                                                    alt="<?php echo $line['short_title_en'] . ' Website Screenshot'; ?>"
                                                                    class="img-responsive img-thumbnail"/>
                                                                <?php
                                                            }
                                                            ?>
                                                        </a>
                                                        <?php
                                                    }
                                                }
                                                else {
                                                    if($is_hotel) {
                                                        ?>
                                                        <span class="click-link flimg" rel="<?php echo $click_link_id;?>" extra_class="flimg">
                                                            <?php
                                                            if($has_thumbshot){
                                                                ?>
                                                                <img
                                                                    src="<?php echo $thumbshot_url; ?>"
                                                                    width="120" height="90" border="1"
                                                                    alt="<?php echo $line['short_title_en'] . ' Website Screenshot'; ?>"
                                                                    class="img-responsive img-thumbnail"/>
                                                                <?php
                                                            }
                                                            else {
                                                                ?>
                                                                <img
                                                                    src="<?=$screenshot_url;?>w=120&h=90&url=<?php echo urlencode($website); ?>"
                                                                    sizes="(max-width: 639px) 640px,
                                                                            120px"
                                                                    srcset="<?=$screenshot_url;?>w=120&h=90&url=<?php echo urlencode($website); ?> 120w,
                                                                    <?=$screenshot_url;?>w=640&h=480&url=<?php echo urlencode($website); ?> 640w"
                                                                    alt="<?php echo $line['short_title_en'] . ' Website Screenshot'; ?>"
                                                                    class="img-responsive img-thumbnail"/>
                                                                <?php
                                                            }
                                                            ?>
                                                        </span>
                                                        <?php
                                                    }
                                                    else {
                                                        ?>
                                                        <span class="flimg">
                                                            <?php
                                                            if($has_thumbshot){
                                                                ?>
                                                                <img
                                                                    src="<?php echo $thumbshot_url; ?>"
                                                                    width="120" height="90" border="1"
                                                                    alt="<?php echo $line['short_title_en'] . ' Website Screenshot'; ?>"
                                                                    class="img-responsive img-thumbnail"/>
                                                                <?php
                                                            }
                                                            else {
                                                                ?>
                                                                <img
                                                                    src="<?=$screenshot_url;?>w=120&h=90&url=<?php echo urlencode($website); ?>"
                                                                    sizes="(max-width: 639px) 640px,
                                                                            120px"
                                                                    srcset="<?=$screenshot_url;?>w=120&h=90&url=<?php echo urlencode($website); ?> 120w,
                                                                    <?=$screenshot_url;?>w=640&h=480&url=<?php echo urlencode($website); ?> 640w"
                                                                    alt="<?php echo $line['short_title_en'] . ' Website Screenshot'; ?>"
                                                                    class="img-responsive img-thumbnail"/>
                                                                <?php
                                                            }
                                                            ?>
                                                        </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); ?>"
                                                        sizes="(max-width: 639px) 640px,
                                                                            120px"
                                                        srcset="<?=$screenshot_url;?>w=120&h=90&url=<?php echo urlencode($website); ?> 120w,
                                                                    <?=$screenshot_url;?>w=640&h=480&url=<?php echo urlencode($website); ?> 640w"
                                                        alt="<?php echo stripslashes($line['short_title_en']); ?>"
                                                        class="img-responsive img-thumbnail"/>
                                                    </span>
                                                    <?php
                                                }
                                                else {
                                                    ?>
                                                    <span class="flimg">
                                                    <img
                                                        src="<?=$screenshot_url;?>w=120&amp;h=90&amp;url=<?php echo urlencode($website); ?>"
                                                        sizes="(max-width: 639px) 640px, 120px"
                                                        srcset="<?=$screenshot_url;?>w=120&h=90&url=<?php echo urlencode($website); ?> 120w,
                                                                    <?=$screenshot_url;?>w=640&h=480&url=<?php echo urlencode($website); ?> 640w"
                                                        alt="<?php echo stripslashes($line['short_title_en']); ?>"
                                                        class="img-responsive img-thumbnail"/>
                                                    </span>
                                                    <?php
                                                }
                                            }
                                        }
                                        ?>
                                    </div>
                                    <div class="col-xs-10 col-sm-7 search-result-data">

                                        <table class="table">

                                            <tr>
                                                <td class="col-sm-10">
                                                    <i class="fa fa-at fa-lg pull-left"></i>
                                                    <?php
                                                    if(isset($location['email']) && trim($location['email']) != ""){
                                                        $loc_emails = array();
                                                        $loc_email = explode("|", $location['email']);
                                                        if(is_array($loc_email) && count($loc_email) > 0) {
                                                            $loc_email = array_map('trim', $loc_email);
                                                            foreach($loc_email as $l_email) {
                                                                if($l_email != "") $loc_emails [] = '<a href="javascript:;" class="mailto" rel="' . str_replace('@', '[ at ]', trim($l_email)) . '">' . str_replace('@', ' [ at ] ', trim($l_email)) . '</a>';
                                                            }
                                                        }
                                                        else {
                                                            $l_email = trim($location['email']);
                                                            $loc_emails [] = $loc_emails [] = '<a href="javascript:;" class="mailto" rel="' . str_replace('@', '[ at ]', trim($l_email)) . '">' . str_replace('@', ' [ at ] ', trim($l_email)) . '</a>';
                                                        }

                                                        echo implode("; ", $loc_emails);
                                                        ?>

                                                        <?php
                                                    }
                                                    else {
                                                        echo 'n/a';
                                                    }
                                                    ?>
                                                </td>
                                            </tr>
                                            <?php
                                            if($show_website == 1) {
                                            ?>
                                            <tr>
                                                <td class="col-sm-10">
                                                    <i class="fa fa-globe fa-lg pull-left"></i>
                                                    <?php
                                                    if($website != ""){
                                                        if($advert_active) {
                                                            if($paying_advert) {
                                                                if($is_hotel) {
                                                                    ?>
                                                                    <span class="click-link" rel="<?php echo $click_link_id;?>"><?php echo strlen($website) > 43 ? substr($website, 0, 36) . '..' : $website;?></span>
                                                                    <?php
                                                                }
                                                                else {
                                                                    ?>
                                                                    <a href="<?php echo $protocol . $microdata_host . '/' .  $shortNameURL;?>" link="<?php echo $protocol . $microdata_host . '/';?>click_count.php?link=<?php echo '';?>&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 strlen($website) > 43 ? substr($website, 0, 36) . '..' : $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" rel="<?php echo $click_link_id;?>"><?php echo strlen($website) > 43 ? substr($website, 0, 36) . '..' : $website;?></span>
                                                                    <?php
                                                                }
                                                                else {
                                                                    echo $website;
                                                                }
                                                            }
                                                        }
                                                        else {
                                                            if($is_hotel) {
                                                                ?>
                                                                <span class="click-link" rel="<?php echo $click_link_id;?>"><?php echo strlen($website) > 43 ? substr($website, 0, 36) . '..' : $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 strlen($website) > 43 ? substr($website, 0, 36) . '..' : $website;
                                                            }
                                                        }
                                                    }
                                                    else {
                                                        echo 'n/a';
                                                    }
                                                    ?>
                                                </td>
                                            </tr>
                                            <?php
                                            }
                                            ?>
                                        </table>
                                    </div>
                                </div>
                            </div>

                        </div>
                        <?php
                    }
                }

                ?>
                <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="'.$protocol.$microdata_host.'/'.$wb_friendly_url.'/page-'.$prev.'.html" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>';
                                $startmsg .= '<li><a href="'.$protocol.$microdata_host.'/'.$wb_friendly_url.'/page-1.html">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="'.$protocol.$microdata_host.'/'.$wb_friendly_url.'/page-'.($endpage).'.html">'.$endpage.'</a></li>';
                                $endmsg .= '<li><a href="'.$protocol.$microdata_host.'/'.$wb_friendly_url.'/page-'.($nex).'.html" 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 $protocol . $microdata_host . '/' . $wb_friendly_url . '/page-' . ($noofpage + 1) . '.html'; ?>"
                                           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">
        //<![CDATA[
        var click_count_paths = <?php echo json_encode($click_count_paths);?>;

        //]]>
        </script>
        <?php
        $optres['js'][] = $path . "amodules/wb_categories/js/wb_categories_default.js";
        if(isset($wb_show_with_title) && $wb_show_with_title) {
            //echo '</div>';
        }
    }
    ?>
</section>
<?php

if(isset($pages) && intval($pages) > 0 && $current_page > $pages) {
    ob_clean();
    $send404 = true;
    require_once(dirname(__FILE__) . "/../404/404.php");
}

$wb_content = ob_get_contents();
ob_end_clean();



if($wb_show_content == 1) {
	echo $wb_content;
}
else {
	?>

	<div class="callout callout-info">
		<?php	
		echo '<p class="text-white"><i class="fa fa-info-circle"></i> Please click the following link to find more <a href="'.$protocol.$microdata_host.'/'.$wb_friendly_url.'/page-1.html" title="'.$module_wb_title.'" class="text-bold">'.$module_wb_title.'</a>.</p>';
		?>
	</div>
	<?php	
}

$nd = microtime(true);
//echo "<br>Finished in " . ($nd-$st);