<?php
if(!function_exists("is_nofollow")) {
    function is_nofollow($nofollow_domains)
    {
        global $domain_id;

        $formated_domain_id = sprintf("%04.0f", $domain_id);

        if (strpos($nofollow_domains, $formated_domain_id) !== false) return true;

        return false;
    }
}

$optres['js'][] = $path . 'amodules/search_adv/js/search_adv.js';
//$homepage_url = "/";

// JOSN-LD / JSONLD
if(!isset($jsonld['links'])) {
    $jsonld['links'] = [];
}


//echo '<pre>', var_export($_POST, true), '</pre>';
$catdom_id = $domain_id;
$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));

$range = 10;
$start = isset($search['start']) ? intval($search['start']) : 0;
$current_page = 1;
if ($start > 0) $current_page = $start / $range + 1;
/*if($filter != null && isset($filter['start'])) {
    $start = intval($filter['start']);
    if($start < 0 ) $start = 0;
}*/

$all_advs = $pfcache->get('all_bound_advers_ordered');
//$all_advs = null;
if($all_advs == null) {

    $all_advs = array();
    $all_catdoms = array($domain_id);
    $q = "SELECT * FROM advertisment WHERE active='yes' ";
    if($domain_id != 611) {
        $domain_idx2 = sprintf("%04.0f", $domain_id);
        $q .= " AND ( catdom_id LIKE '%$domain_idx2%'";
        $result_sub = mysql_query("SELECT * FROM  domaincategories WHERE parent_id='$domain_id'");
        if ($result_sub && mysql_num_rows($result_sub) > 0) {
            while ($line_sub = mysql_fetch_array($result_sub)) {
                $all_catdoms[] = $line_sub['catdom_id'];
                $domain_idx2 = sprintf("%04.0f", $line_sub["catdom_id"]);
                $q .= " OR catdom_id LIKE '%$domain_idx2%'";
            }
        }
        $q .= ")";
    }
    else {
        $q .= " ORDER BY short_title_en ASC";
    }
    //echo '<br>'. $q;
    $r = @mysql_query($q);
    if ($r && mysql_num_rows($r) > 0) {
        while ($row = mysql_fetch_assoc($r)) {
            $row['order'] = 0;
            $row['categ_order'] = 0;
            $row['paying'] = 0;
            $row['main_location'] = array();
            $all_advs[$row['advertisment_id']] = $row;
        }
        $all_catdoms_ids = implode(", ", $all_catdoms);
        $all_advs_ids = implode(", ", array_keys($all_advs));

        // Get Main Location
        $ql = "SELECT * FROM advertisment_locations WHERE advertisment_id IN ($all_advs_ids) AND main_location='1';";
        $rl = @mysql_query($ql);
        if($rl && mysql_num_rows($rl) > 0) {
            while($row = mysql_fetch_assoc($rl)) {
               $all_advs[$row['advertisment_id']]['main_location'] = $row;
            }
        }


        // Get Domain & Category level bindings
        if($domain_id != 611) {
            $qda = "SELECT * FROM domain_advertisment WHERE catdom_id IN ($all_catdoms_ids) AND advertisment_id IN ($all_advs_ids);";
        }
        else {
            $qda = "SELECT d.* FROM domain_advertisment d, advertisment a WHERE d.advertisment_id=a.advertisment_id AND a.active='yes' ORDER BY a.short_title_en ASC";
        }
        //echo '<br>'. $qda;
        $rda = @mysql_query($qda);
        if ($rda && mysql_num_rows($rda) > 0) {
            while ($row = mysql_fetch_assoc($rda)) {
                $bound_advertisment_id = intval($row['advertisment_id']);
                $bound_catdom_id = intval($row['catdom_id']);

                $category_top = $row['category_top'];
                $category_top_order_seq = intval($row['category_top_order_seq']);

                $active = $row['active'];
                $order_seq = intval($row['order_seq']);

                if ($bound_catdom_id == $domain_id && $domain_id != 611) {
                    // We are on homepage
                    $order = 0;
                    $categ_order = 0;
                    $paying = 0;
                    if ($category_top == "yes") {
                        $order = 10;
                        $categ_order = $category_top_order_seq;
                        $paying = 1;
                    } elseif ($active == "yes") {
                        $order = 8;
                        $categ_order = $order_seq;
                        $paying = 1;
                    }
                    if ($all_advs[$bound_advertisment_id]['order'] < $order) {
                        $all_advs[$bound_advertisment_id]['order'] = $order;
                        $all_advs[$bound_advertisment_id]['categ_order'] = $categ_order;
                    }

                    if($paying > 0) $all_advs[$bound_advertisment_id]['paying'] = $paying;

                } else {
                    // We are in a town/category page
                    $order = 0;
                    $categ_order = 0;
                    $paying = 0;
                    if ($category_top == "yes") {
                        $order = 6;
                        $categ_order = $category_top_order_seq;
                        $paying = 1;
                    } elseif ($active == "yes") {
                        $order = 4;
                        $categ_order = $order_seq;
                        $paying = 1;
                    }
                    if ($all_advs[$bound_advertisment_id]['order'] < $order) {
                        $all_advs[$bound_advertisment_id]['order'] = $order;
                        $all_advs[$bound_advertisment_id]['categ_order'] = $categ_order;
                    }

                    if($paying > 0) $all_advs[$bound_advertisment_id]['paying'] = $paying;
                }

            }
        }


        if($domain_id != 611) {
            uasort($all_advs, function ($a, $b) {
                if ($a['order'] == $b['order']) {
                    if ($a['categ_order'] < $b['categ_order']) return 1;
                    if ($a['categ_order'] == $b['categ_order']) return 0;
                    if ($a['categ_order'] > $b['categ_order']) return -1;
                }
                return $a['order'] - $b['order'];
            });

            $all_advs = array_reverse($all_advs, true);
        }

    }

    $pfcache->set('all_bound_advers_ordered', $all_advs, $pfcache_ttl);
}

/*foreach($all_advs as $adv_id=>$adv) {
    echo "<br>" . $adv['short_title_en'] . ' --------- ' . $adv['order'] . ' / ' . $adv['categ_order'];
}*/


if(isset($search['now']) && $search['now']=="yes"){
	
	$is_hotel = false;
	if(str_replace('www.', '', $ultra_domainname) == "cyprus-hotel.com") {
		$is_hotel = true;
	}
	
	// Get the searched catdom
	$catdom_id_adv = $domain_id;
	if(isset($search['catdom_id']) && intval($search['catdom_id']) > 0) {
		$catdom_id_adv = intval($search['catdom_id']);
	}

	if (isset($start) && strlen($start) < 1 ){
		$start = 0;
	}

	$catdom_idx = sprintf("%04.0f", $catdom_id);

    if(isset($filter) && $filter != null) {
        $query = "SELECT a.* 
                  FROM 
                    advertisment a
                    LEFT JOIN advertisment_versions v
                    ON (a.advertisment_id=v.advertisment_id AND v.domainname='$ultra_domainname')
                  WHERE 
                    a.active='yes' ";
        $search_keyword = '';
        if (isset($search['keyword'])) $search_keyword = mysql_real_escape_string(stripslashes(trim($search['keyword'])));
        if ($search_keyword != "") {
            $query .= " AND (
            a.short_title_en LIKE '%{$search_keyword}%' 
            OR a.website like '%{$search_keyword}%'
            OR a.MemberOf like '%{$search_keyword}%'
            OR a.Directors like '%{$search_keyword}%'
            OR a.title_el LIKE '%{$search_keyword}%' 
            OR a.title_tr LIKE '%{$search_keyword}%'
            OR ( CASE WHEN v.description_en IS NOT NULL THEN v.description_en like '%{$search_keyword}%' ELSE a.description_en like '%{$search_keyword}%' END ) 
            OR a.advertisment_id IN (SELECT l.advertisment_id FROM advertisment_locations l WHERE l.advertisment_id=a.advertisment_id AND l.active='1' AND (
                l.street like '%{$search_keyword}%'
                    OR l.postalcode like '%{$search_keyword}%'
                    OR l.city like '%{$search_keyword}%'
                    OR l.district like '%{$search_keyword}%'
                    OR l.contact_name like '%{$search_keyword}%'
                    OR l.tel1 like '%{$search_keyword}%'
                    OR l.tel2 like '%{$search_keyword}%'
                    OR l.mobile1 like '%{$search_keyword}%'
                    OR l.mobile2 like '%{$search_keyword}%'
                    OR l.fax like '%{$search_keyword}%'
                    OR l.email like '%{$search_keyword}%'
                )
                )   
            ) ";
        }
        if ($domain_id != 611) {
            if ($domain_id <> "" && $domain_id > 0) {
                // echo $domain_id;
                $query .= " AND";
                if (isset($search["catdom_id"]) && $search["catdom_id"] <> "") {
                    $catdom_idx2 = sprintf("%04.0f", $search["catdom_id"]);
                    $query .= " a.catdom_id LIKE '%$catdom_idx2%'";
                } elseif (isset($catdom_ids) && is_array($catdom_ids) && count($catdom_ids) > 0) {
                    $query .= " (";
                    while (list($key, $val) = each($catdom_ids)) {
                        if ($val > 0) {
                            $countval++;
                            if ($countval > 1 && isset($search['andor'])) {
                                $query .= " {$search['andor']} ";
                            }
                            $catdom_idx = sprintf("%04.0f", $val);
                            $query .= " a.catdom_id LIKE '%$catdom_idx%' ";
                        }
                    }
                    $query .= ") ";
                } else {
                    $domain_idx2 = sprintf("%04.0f", $domain_id);
                    $query .= " (a.catdom_id LIKE '%$domain_idx2%'";
                    $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)) {
                            $domain_idx2 = sprintf("%04.0f", $line_sub["catdom_id"]);
                            $query .= " OR a.catdom_id LIKE '%$domain_idx2%'";
                        }
                    }
                    $query .= ")";
                }
            }
        }
        if (isset($search['filter_ids']) && is_array($search['filter_ids']) && count($search['filter_ids']) > 0) {
            $where_filters = array();
            while (list($key, $val) = each($search['filter_ids'])) {
                if ($val > 0) {
                    $filter_idx = sprintf("%04.0f", $val);
                    $where_filters [] = "filter_id LIKE '%$filter_idx%'";
                }
            }
            if (count($where_filters) > 0) {
                $where_filters_str = implode(" AND ", $where_filters);
                $query .= " AND ($where_filters_str)";
            }
        }
        //			  if($catdom_idS<>"" && $catdom_idS=="0"){$query .=" AND (catdom_id='' OR catdom_id=0)";}
        if (isset($date_before) && $date_before <> "") {
            $query .= " AND a.added_on >= '$date_before'";
        }
        if (isset($date_after) && $date_after <> "") {
            $query .= " AND a.added_on <= '$date_after'";
        }

        $query .= " ORDER BY a.short_title_en ASC";

        // echo $query;
        $result = @mysql_query($query);
        $adverts = array();

        if ($result && mysql_num_rows($result) > 0) {
            $NumberOfFields = mysql_num_rows($result);
            while ($row = mysql_fetch_assoc($result)) {
                $adverts[] = $row['advertisment_id'];
            }
        }

        $results = array();
        $cnt = 0;
        foreach($all_advs as $adv_id=>$adv) {
            if(in_array($adv_id, $adverts)) {
                $results [$cnt] = $adv;
                $cnt++;
            }

        }


    }
    else {
        $results = array();
        $cnt = 0;
        foreach($all_advs as $adv_id=>$adv) {
            $results [$cnt] = $adv;
            $cnt++;
        }
    }

	?>
    <section class="search-results">
        <header>
            <?php
            //$search_title = stripslashes(trim($line_catdom_id["search_txt"]));
            $search_title = stripslashes(trim($line_catdom_id["title_en"])) . ' Search';
            if(isset($filter_ids) && is_array($filter_ids) && count($filter_ids) > 0) {
                $qf = "select * from filters where filter_id in (".implode(",", $filter_ids).");";
                $rf = @mysql_query($qf);
                if($rf && mysql_num_rows($rf) > 0) {
                    $rf_cnt = 0;
                    while ($rowf = mysql_fetch_assoc($rf)) {
                        if($rf_cnt == 0) {
                            $search_title .= " " . stripslashes($rowf['title_en']);
                        }
                        else {
                            $search_title .= ", " . stripslashes($rowf['title_en']);
                        }
                        $rf_cnt ++;
                    }
                }
            }

            $webpage_title_en = $search_title;

            if((in_array(str_replace('www.', '', $ultra_domainname), $special_domains))) {
                $meta['description'] = '';
            }
            else {
                $meta['description'] = $search_title. '. ';
            }


            ?>

            <h1><?php echo $search_title;?></h1>
            <!--[/SocialShareButtons/]-->
        </header>
        <div class="content-body">
            <?php
            require_once(dirname(__FILE__) . "/../advanced_search/advanced_search.php");


            if (isset($results) && count($results) > 0){
                $NumberOfFields = count($results);
                ?>

                <div>
                    <?php


                    $NumberOfFields = count($results);


                    //echo "<hr>";
                    if ($NumberOfFields > $start + $range) {
                        $end = $start + $range;
                    } else {
                        $end = $NumberOfFields;
                    }

                    $meta_keywords = array();
                    $meta_strip_array = array(
                        "cyprus", "limassol", "paphos", "larnaca", "famagusta", "ayia napa"
                    );
                    $meta_keywords [] = trim(str_ireplace($meta_strip_array, '', stripslashes(trim($line_catdom_id["title_en"]))));
                    //$_REQUEST['debugx'] = 1;

                    $click_count_paths = array();

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

                        $adv = $results[$i];
                        $website = fix_url(trim($adv['website']));
                        $nofollow_domains = $adv['nofollow_domains'];

                        $paying_advert = $adv['paying'] == 1;
                        $use_town_dir = false;

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

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

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

                        $meta_keywords [] = $adv["short_title_en"];

                        if((in_array(str_replace('www.', '', $ultra_domainname), $special_domains))) {
                            $meta['description'] = '';
                        }
                        else {
                            if ($i == $start) {
                                $meta['description'] .= $adv["short_title_en"];
                            } else {
                                $meta['description'] .= ', ' . $adv["short_title_en"];
                            }
                        }
                        $rec_locations = array();
                        $location = array();
                        $phones_array = array();
                        $q = "select * from advertisment_locations where advertisment_id='{$adv['advertisment_id']}' 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']);
                                }
                                $rec_locations [] = $row;

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

                        }

                        // 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();

                                ?>
                                <span class="wb-card-data">
                                        <i class="fa fa-phone fa-lg pull-left"></i>
                                    <?php
                                    echo implode(", ", $tels);
                                    ?>
                                    </span>
                                <?php


                                $adv_tel = ob_get_clean();
                            }

                        }

                        //print_r($app_conf);
                        $click_link_id = 'adv_' . $adv['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=' . $adv['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=' . $adv['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=' . $adv['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=' . $adv['advertisment_id'] . '&amp;domain=' . $line_domain["domainname"] .'&amp;domain_id=' . $domain_id. '&amp;click_from=s';
                        }

                        // Get the image code
                        $img_container_attr = array('tag' => 'div');
                        ob_start();

                        if($website != "" && $show_website == 1){
                            $nofollow_value = is_nofollow($nofollow_domains) ? 'nofollow' : '';
                            if($advert_active || (in_array(str_replace('www.', '', $ultra_domainname), $special_domains))) {
                                if($paying_advert || (in_array(str_replace('www.', '', $ultra_domainname), $special_domains))) {
                                    if($is_hotel) {
                                        $img_container_attr ['class'] = 'click-link flimg';
                                        $img_container_attr ['data-extra_class'] = 'flimg';
                                        $img_container_attr ['data-rel'] = $click_link_id;
                                        $img_container_attr ['data-nofollow'] = $nofollow_value;
                                        $img_container_attr ['rel'] = "noopener $nofollow_value";
                                        if($has_thumbshot){
                                            ?>
                                            <img
                                                data-src="<?php echo $thumbshot_url; ?>"
                                                width="160" height="120" border="1"
                                                alt="<?php echo $adv['short_title_en'] . ' Website Screenshot'; ?>"
                                                class="img-responsive"/>
                                            <?php
                                        }
                                        else {
                                            ?>
                                            <img
                                                data-src="<?=$screenshot_url;?>w=160&h=120&url=<?php echo urlencode($website); ?>"
                                                sizes="(max-width: 639px) 640px,
                                                                            160px"
                                                data-srcset="<?=$screenshot_url;?>w=160&h=120&url=<?php echo urlencode($website); ?> 160w,
                                                                    <?=$screenshot_url;?>w=640&h=480&url=<?php echo urlencode($website); ?> 640w"
                                                alt="<?php echo $adv['short_title_en'] . ' Website Screenshot'; ?>"
                                                class="img-responsive"/>
                                            <?php
                                        }

                                    }
                                    else {
                                        $img_container_attr ['tag'] = 'a';
                                        $img_container_attr ['class'] = 'flimg';
                                        $img_container_attr ['target'] = '_blank';
                                        $img_container_attr ['rel'] = "noopener $nofollow_value";
                                        $img_container_attr ['href'] = 'click_count.php?link=&amp;type=www&amp;id='.$adv['advertisment_id'].'&amp;domain='.$line_domain["domainname"].'&amp;domain_id='.$domain_id.'&amp;click_from=s';
                                        if($has_thumbshot){
                                            ?>
                                            <img
                                                data-src="<?php echo $thumbshot_url; ?>"
                                                width="120" height="90" border="1"
                                                alt="<?php echo $adv['short_title_en'] . ' Website Screenshot'; ?>"
                                                class="img-responsive"/>
                                            <?php
                                        }
                                        else {
                                            ?>
                                            <img
                                                data-src="<?=$screenshot_url;?>w=160&h=120&url=<?php echo urlencode($website); ?>"

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

                                                alt="<?php echo $adv['short_title_en'] . ' Website Screenshot'; ?>"
                                                class="img-responsive"/>
                                            <?php
                                        }
                                    }
                                }
                                else {
                                    if($is_hotel) {
                                        $img_container_attr ['class'] = 'click-link flimg';
                                        $img_container_attr ['data-extra_class'] = 'flimg';
                                        $img_container_attr ['data-rel'] = $click_link_id;
                                        $img_container_attr ['data-nofollow'] = "$nofollow_value";
                                        $img_container_attr ['rel'] = "noopener $nofollow_value";
                                        if($has_thumbshot){
                                            ?>
                                            <img
                                                data-src="<?php echo $thumbshot_url; ?>"
                                                width="120" height="90" border="1"
                                                alt="<?php echo $adv['short_title_en'] . ' Website Screenshot'; ?>"
                                                class="img-responsive"/>
                                            <?php
                                        }
                                        else {
                                            ?>
                                            <img
                                                data-src="<?=$screenshot_url;?>w=160&h=120&url=<?php echo urlencode($website); ?>"
                                                sizes="(max-width: 639px) 640px,
                                                                            160px"
                                                data-srcset="<?=$screenshot_url;?>w=160&h=120&url=<?php echo urlencode($website); ?> 160w,
                                                                    <?=$screenshot_url;?>w=640&h=480&url=<?php echo urlencode($website); ?> 640w"
                                                alt="<?php echo $adv['short_title_en'] . ' Website Screenshot'; ?>"
                                                class="img-responsive"/>
                                            <?php
                                        }
                                    }
                                    else {
                                        $img_container_attr ['class'] = 'flimg';
                                        if($has_thumbshot){
                                            ?>
                                            <img
                                                data-src="<?php echo $thumbshot_url; ?>"
                                                width="120" height="90" border="1"
                                                alt="<?php echo $adv['short_title_en'] . ' Website Screenshot'; ?>"
                                                class="img-responsive"/>
                                            <?php
                                        }
                                        else {
                                            ?>
                                            <img
                                                data-src="<?=$screenshot_url;?>w=160&h=120&url=<?php echo urlencode($website); ?>"
                                                sizes="(max-width: 639px) 640px,
                                                                            160px"
                                                data-srcset="<?=$screenshot_url;?>w=160&h=120&url=<?php echo urlencode($website); ?> 160w,
                                                                    <?=$screenshot_url;?>w=640&h=480&url=<?php echo urlencode($website); ?> 640w"
                                                alt="<?php echo $adv['short_title_en'] . ' Website Screenshot'; ?>"
                                                class="img-responsive"/>
                                            <?php
                                        }
                                    }
                                }
                            }
                            else {
                                if($is_hotel) {
                                    $img_container_attr ['class'] = 'click-link flimg';
                                    $img_container_attr ['data-extra_class'] = 'flimg';
                                    $img_container_attr ['data-rel'] = $click_link_id;
                                    $img_container_attr ['data-nofollow'] = "$nofollow_value";
                                    $img_container_attr ['rel'] = "noopener $nofollow_value";
                                    ?>
                                    <img
                                        data-src="<?=$screenshot_url;?>w=120&amp;h=90&amp;url=<?php echo urlencode($website); ?>"
                                        sizes="(max-width: 639px) 640px,
                                                                            160px"
                                        data-srcset="<?=$screenshot_url;?>w=160&h=120&url=<?php echo urlencode($website); ?> 160w,
                                                                    <?=$screenshot_url;?>w=640&h=480&url=<?php echo urlencode($website); ?> 640w"
                                        alt="<?php echo stripslashes($adv['short_title_en']); ?>"
                                        class="img-responsive"/>
                                    <?php
                                }
                                else {
                                    $img_container_attr ['class'] = 'flimg';
                                    ?>
                                    <img
                                        data-src="<?=$screenshot_url;?>w=120&amp;h=90&amp;url=<?php echo urlencode($website); ?>"
                                        sizes="(max-width: 639px) 640px, 160px"
                                        data-srcset="<?=$screenshot_url;?>w=160&h=120&url=<?php echo urlencode($website); ?> 160w,
                                                                    <?=$screenshot_url;?>w=640&h=480&url=<?php echo urlencode($website); ?> 640w"
                                        alt="<?php echo stripslashes($adv['short_title_en']); ?>"
                                        class="img-responsive"/>
                                    <?php
                                }
                            }
                        }

                        $img_tag = ob_get_clean();
                        /*****************************************************************/

                        ?>

                        <div class="wb-card">
                            <div class="wb-card-image">
                                <?php
                                $current_tag = $img_container_attr;
                                $tag = $current_tag['tag'];
                                unset($current_tag['tag']);
                                $tag_attr = array();
                                foreach($current_tag as $tt=>$tv) {
                                    $tag_attr [] = $tt . '="' . $tv . '"';
                                }
                                $tag_code = '<' . $tag . ' ' . implode(" ", $tag_attr) . '>' . $img_tag . '<div class="wb-card-image-shadow"></div></'.$tag.'>';
                                echo $tag_code;
                                ?>

                            </div>
                            <div class="wb-card-content">
                                <?php
                                // Title
                                if(!in_array($protocol . $microdata_host . "/$shortNameURL", $jsonld['links'])) {
                                    $jsonld['links'][] = $protocol . $microdata_host . "/$shortNameURL";
                                }
                                if($is_hotel) {
                                    echo '<div 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;?>" rel="noopener" target="_blank" class="h2-result-link"><?php echo stripslashes($adv["short_title_en"]);?></a>
                                        <?php
                                    }
                                    else {
                                        ?>
                                        <a href="<?php echo $protocol . $microdata_host . '/' .  $shortNameURL;?>" rel="noopener" target="_blank" class="h2-result-link"><?php echo stripslashes($adv["short_title_en"]);?></a>
                                        <?php
                                    }
                                    echo '</div>';
                                }
                                else {
                                    ?>
                                    <div class="wb-title">
                                        <a href="<?php echo $protocol . $microdata_host . '/' .  $shortNameURL;?>" rel="noopener" target="_blank"><?php echo trim(stripslashes($adv["short_title_en"]));?></a>
                                    </div>
                                    <?php
                                }


                                ?>
                                <div class="wb-card-details">
                                    <?=$adv_tel;?>
                                    <span class="wb-card-data">
                                            <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" data-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" data-rel="' . str_replace('@', '[ at ]', trim($l_email)) . '">' . str_replace('@', ' [ at ] ', trim($l_email)) . '</a>';
                                            }

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

                                            <?php
                                        }
                                        else {
                                            echo 'n/a';
                                        }
                                        ?>
                                        </span>
                                    <?php
                                    if($show_website == 1) {
                                        ?>
                                        <span class="wb-card-data">
                                                <i class="fa fa-globe fa-lg pull-left"></i>
                                            <?php
                                            if($website != ""){
                                                if($advert_active || (in_array(str_replace('www.', '', $ultra_domainname), $special_domains))) {
                                                    if($paying_advert || (in_array(str_replace('www.', '', $ultra_domainname), $special_domains))) {
                                                        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;?>" data-link="<?php echo $protocol . $microdata_host . '/';?>click_count.php?link=<?php echo '';?>&amp;type=www&amp;id=<?php echo $adv['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>
                                                            */
                                                            $current_tag = $img_container_attr;
                                                            $tag = $current_tag['tag'];
                                                            unset($current_tag['tag']);
                                                            $tag_attr = array();
                                                            foreach($current_tag as $tt=>$tv) {
                                                                $tag_attr [] = $tt . '="' . $tv . '"';
                                                            }
                                                            $tag_code = '<' . $tag . ' ' . implode(" ", $tag_attr) . '>' . (strlen($website) > 43 ? substr($website, 0, 36) . '..' : $website) . '</'.$tag.'>';
                                                            echo $tag_code;
                                                        }
                                                    }
                                                    else {
                                                        if($is_hotel) {
                                                            //echo '<a href="' . 'click_count.php?link=' .$adv["website"] . '&amp;type=www&amp;id=' . $adv['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=' .$adv["website"] . '&amp;type=www&amp;id=' . $adv['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';
                                            }
                                            ?>
                                            </span>
                                        <?php
                                    }
                                    ?>
                                </div>
                            </div>
                        </div>

                        <?php


                    }
                    ?>
                </div>


            <?php
            if (isset($filter) && $filter != null) {
                require_once(dirname(__FILE__) . "/../common/Paginator/Paginator.php");
                $paginator = new Paginator(array(
                    "base_url" => $homepage_url . "cyprus/",
                    "records_per_page" => $range
                ));
                $params = $search;
                /*$search['i_from'] = $start;*/
                $search['i_limit'] = $range;
                $pagination = $paginator->paginate($NumberOfFields, $search);

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

                echo $pagination;
            } else {
                ?>


                <div class="search-results-pagination text-left">
                    <div class="search-results-num">
                        <?php
                        $end = $NumberOfFields;
                        if ($NumberOfFields > $start + $range) $end = $start + $range;
                        echo "Results <b>", ($start + 1), " - ", $end, "</b> out of <b>", $NumberOfFields, "</b>";
                        ?>
                    </div>
                    <nav>
                        <ul class="pagination pagination-sm">

                            <?php
                            $pages = floor($NumberOfFields / $range);
                            if ($NumberOfFields / $range - $pages > 0) $pages = $pages + 1;

                            $cur_page = 1;
                            if ($start > 0) $cur_page = $start / $range + 1;

                            if ($cur_page > $pages) {
                                $send404 = true;
                            }


                            $canonical_link = $homepage_url . 'cyprus' . (($start > 0) ? $start : '');


                            $startmsg = '';
                            $endmsg = '';
                            $pagesrange = 2;


                            if ($cur_page > 1) {
                                $prev = $start - $range;
                                if ($prev <= 0) $prev = '';
                                $startmsg = '<li><a href="' . $homepage_url . 'cyprus' . $prev . '" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>';

                                $rel_prev_page = $homepage_url . 'cyprus' . $prev;

                            }

                            if ($cur_page < $pages) {
                                $next = $start + $range;
                                $endmsg = '<li><a href="' . $homepage_url . 'cyprus' . $next . '" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>';

                                $rel_next_page = $homepage_url . 'cyprus' . $next;
                            }

                            $page_range = ($cur_page - 1) * $range > 0 ? ($cur_page - 1) * $range : '';
                            $canonical_link = $homepage_url . "cyprus" . $page_range;

                            if ($pages <= 8) {
                                echo $startmsg;
                                for ($i = 1; $i <= $pages; $i++) {
                                    if ($cur_page != $i) {
                                        $page_step = ($i - 1) * $range;
                                        if($page_step <= 0 ) $page_step = '';
                                        ?>
                                        <li>
                                            <a href="<?php echo $homepage_url; ?>cyprus<?php echo $page_step; ?>"><?= ($i) ?></a>
                                        </li>
                                        <?php
                                    } else {
                                        echo '<li class="active"><a>' . ($i > 0 ? $i : '') . '</a></li>';
                                    }
                                }
                                echo $endmsg;
                            } else {
                                $startrange = 1;
                                if ($cur_page - 2 * $pagesrange > 0) {
                                    $startrange = $cur_page - 2;
                                    $startmsg .= '<li><a href="' . $homepage_url . 'cyprus">1</a></li><li><a> .. </a></li>';
                                }

                                $endrange = $pages;
                                if ($cur_page + 2 * $pagesrange < $pages) {
                                    $endrange = $cur_page + 2;
                                    $endmsg = '<li><a> .. </a></li><li><a href="' . $homepage_url . 'cyprus' . ($pages - 1) * $range . '">' . $pages . '</a></li>' . $endmsg;
                                }

                                echo $startmsg;
                                for ($i = $startrange; $i <= $endrange; $i++) {
                                    if ($cur_page != $i) {
                                        $page_step = ($i - 1) * $range;
                                        if($page_step <= 0 ) $page_step = '';
                                        ?>
                                        <li>
                                            <a href="<?php echo $homepage_url; ?>cyprus<?php echo $page_step; ?>"><?= ($i) ?></a>
                                        </li>
                                        <?php
                                    } else {
                                        echo '<li class="active"><a>' . ($i) . '</a></li>';
                                    }
                                }
                                echo $endmsg;

                            }

                            ?>
                        </ul>
                    </nav>
                </div>
                <?php
            }


            if (isset($meta_keywords) && count($meta_keywords) > 0) {
                $meta['keywords'] = implode(", ", $meta_keywords) . ", " . $meta['keywords'];
            }

            if ($start > 0) {
                $webpage_title_en .= " | Page " . ($start / 10 + 1);
            }

            ?>
            <script type="text/javascript">
                var click_count_paths = <?php echo json_encode($click_count_paths);?>;
            </script>
            <?php

        }
        else{
            ?>
            <div class="alert alert-danger">
                <p>There are no listings in database with these criteria.</p>
            </div>
            <?php
        }

        ?>
        </div>
    </section>
                        <?php
}
?>
