<?php
ob_start();
mb_internal_encoding("UTF-8");
$qiw = "select * from advertisment where adv_code='$adv_code';";
$result = mysql_query($qiw);
$line = mysql_fetch_assoc($result);
$use_town_dir = false;
$current_advertisment_id = intval($line['advertisment_id']);
$adv_code = $line['adv_code'];
$has_thumbshot = false;

// Get Affiliates rates
$min_price = 0;
$aff = array();
$qaff = "select r.*, a.* from hotel_affiliates_adverts a, hotel_affiliates r where a.adv_code='$adv_code' and a.aff_hotel_status='1' and a.aff_code=r.aff_code and r.aff_status='1' order by a.low_rate asc;";
$raff = @mysql_query($qaff);
if($raff && mysql_num_rows($raff) > 0) {
	
	$hotel_aff = true;
	
	while ($rowaff = mysql_fetch_assoc($raff)) {
		$aff_name = trim($rowaff['aff_name']);
		$aff_code = trim($rowaff['aff_code']);
		
		if(floatval($rowaff['low_rate']) > 1) {
			if($min_price == 0) $min_price = floatval($rowaff['low_rate']);
			elseif ($min_price > floatval($rowaff['low_rate'])) $min_price = floatval($rowaff['low_rate']);
		}
	}
}


// Get Advert Sub-Category
$q = "SELECT m.fm_uid, m.title FROM wb_filters_mains m, `wb_filters_adverts_categs` a where m.wb_uid='$wb_uid' and m.fm_uid=a.fm_uid and m.wb_uid=a.wb_uid and a.adv_code='$adv_code'";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) == 1) {
	$row = mysql_fetch_assoc($r) ;
	$res_fm_uid = $row['fm_uid'];
	$res_fm_title = stripslashes($row['title']);
}

// Get ratings and category
$star_rating = '';
$q = "SELECT a.f_value, e.e_label_display, e.e_db  FROM wb_filters_adverts a, wb_filters_elems e where a.adv_code='$adv_code' and a.wb_uid='$wb_uid' and e.fm_uid='$res_fm_uid' and a.fe_uid=e.fe_uid and e.e_name='star_rating'";
$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
if($r && mysql_num_rows($r) >0) {
	$row = mysql_fetch_assoc($r);
	$f_value = $row['f_value'];
	$e_label_display = stripslashes($row['e_label_display']);
	$e_db = unserialize(base64_decode($row['e_db']));
	//echo '<pre>', var_export($row, true), '</pre>';
	
	$star_rating = 0;
	$star_rating_text = "";
	$star_cnt = 5;
	foreach ($e_db as $star_value=>$star_text) {
		if($f_value == $star_value) {
			$star_rating = $star_cnt;
			$star_rating_text = stripslashes($star_text);
		}
		$star_cnt--;
	}
	//echo '<pre>', var_export($e_db, true), '</pre>';
	
}

// Get Class
$class_vars = array(
	"'apt_class'", "'class'"
);
$apt_class = '';
$q = "SELECT a.f_value, e.e_label_display, e.e_db  FROM wb_filters_adverts a, wb_filters_elems e where a.adv_code='$adv_code' and e.wb_uid='$wb_uid' and e.fm_uid='$res_fm_uid' and a.fe_uid=e.fe_uid and e.e_name in (".implode(",", $class_vars).")";
//echo "<br/>$q";
$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
if($r && mysql_num_rows($r) >0) {
	$row = mysql_fetch_assoc($r);
	$f_value = $row['f_value'];
	$e_label_display = stripslashes($row['e_label_display']);
	$e_db = unserialize(base64_decode($row['e_db']));
	//echo '<pre>', var_export($row, true), '</pre>';
	
	foreach ($e_db as $class_value=>$class_text) {
		if($f_value == $class_value) {
			$apt_class = stripslashes($class_text);
		}
	}
	//echo '<pre>', var_export($e_db, true), '</pre>';
	
}


// Check if it is a paying advert
$paying_advert = 0;
$qp = "select * from domain_advertisment where advertisment_id='$current_advertisment_id' and catdom_id IN (".implode(",", $catdoms).") and (category_top='yes' or active='yes');";
$rp = @mysql_query($qp);
if($rp && mysql_num_rows($rp) > 0) {
	$paying_advert = 1;
}

// Get Gallery image
$th_filename = "";
$th_file_path = "";
$th_path = "";
$qts = "SELECT `filename`,`iwt`,`iht` FROM `advertisment_gallery` WHERE adv_code='$adv_code' order by pos asc limit 0,1";
$rts = @mysql_query($qts);
if($rts && mysql_num_rows($rts) == 1) {
	$rowts = mysql_fetch_assoc($rts);
	
	$th_filename = $rowts['filename'];
	$th_w = $rowts['iwt'];
	$th_h = $rowts['iht'];
	$th_file_path = dirname(__FILE__) . "/../../../images/advertisments/gallery/$current_advertisment_id/thumbs/$th_filename";
	if(file_exists($th_file_path)) {
		$th_path = $path . "images/advertisments/gallery/$current_advertisment_id/thumbs/$th_filename";
		$th_path = $path . "images/advertisments/gallery/$current_advertisment_id/$th_filename";
	}
	//echo "<br/>$th_path";
}

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

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

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



// Map Coordinates
$llz = '';
$rec_locations = array();
$location = array();
$q = "select * from advertisment_locations where advertisment_id='$current_advertisment_id' and main_location='1' and active='1';";
$r = @mysql_query($q);
if($r && mysql_num_rows($r)>0) {
	$row = mysql_fetch_assoc($r);
	$lat = $row['lat'];
	$lng = $row['lng'];
	$zoom = $row['zoom'];
	$city = stripslashes($row['city']);
	$location_id = intval($row['location_id']);
	
	$llz = "$lat|$lng|$zoom";
	$llz_id = $location_id;
	
	$map_w = 120;
	$map_h = 90;
	$zoom = 15;
	$google_map_url = 'https://maps.google.com/maps/api/staticmap?maptype=map&amp;' . ('center=' . $lat . ',' . $lng .'&amp;zoom='.$zoom.'&amp;size='.$map_w.'x'.$map_h.'&amp;markers=%7C'.$lat . ',' . $lng.'&amp;sensor=false');
}




$box_class = "wb-info-window-new";

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


$advert_image = $screenshot_url . 'w=120&amp;h=90&amp;url=' . urlencode($website);
if(isset($th_path) && $th_path != "" && file_exists($th_file_path)) {
    $advert_image = $th_path;
}
?>

<div class="custom-info">
    <div class="custom-img" style="background-image: url('<?=$advert_image;?>');"></div>
    <section class="custom-content">
        <h1 class="custom-header">
            <?php
            if($is_hotel) {
                ?>
                <a href="<?php echo $protocol . $microdata_host . '/' .  $shortNameURL;?>" target="_blank" class="h2-result-link"><?php echo stripslashes($line["short_title_en"]);?></a>
                <?php
            }
            else {
                ?>
                <?php echo stripslashes($line["short_title_en"]);?>
                <?php
            }
            ?>
            <small>
                <?php
                if(isset($star_rating) && intval($star_rating) > 0) {
                    echo '<span class="hotel-stars stars-'.$star_rating.' pull-left" title="'.$star_rating_text.'">'.$star_rating_text.'</span>';
                }

                if(isset($apt_class) && trim($apt_class) != "") {
                    echo '<span class="hotel-class" title="'.$apt_class.'">'.stripslashes($apt_class).'</span>';
                }

                if($paying_advert == true) {
                    echo '<span class="featured-result pull-right"><i class="fa fa-bookmark fa-2x"></i></span>';
                }
                ?>
            </small>
        </h1>
        <div class="custom-body">
            <?php
            if($min_price > 0) {
                $show_disclaimer['*'] = 'Prices displayed are based on the rates obtained from our network of booking partners. We do not guarantee any specific rates or prices. These rates are estimates therefore check with the specific booking site for the exact amount, currency and fees.';
                ?>
                <div class="custom-rate">
                    <a href="<?php echo $shortNameURL;?>" class="custom-rate-check" target="_blank">
                        From <span class="aff_rate">&euro; <?php echo number_format($min_price, 0);?> *</span>
                    </a>

                </div>
                <?php
            }
            else {
                ?>
                <div class="custom-rate">
                    <a href="<?php echo $shortNameURL;?>" class="custom-rate-find btn btn-warning btn-sm" target="_blank">
                        <span>Check Availability</span>
                    </a>
                </div>
                <?php
            }
            ?>

        </div>
        <div class="custom-footer container-fluid">
            <div class="row">
                <div class="col-xs-5">
                    <?php
                    if($website != ""){
                        ?>
                        <button type="button" class="click-link btn btn-info btn-sm pull-left" data-href="<?php echo $click_count_paths [$click_link_id];?>" rel="<?php echo $click_count_paths [$click_link_id];?>" extra_class="result-click-link btn btn-info btn-sm">
                            <i class="fa fa-globe"></i> Visit Website
                        </button>
                        <?php
                    }
                    ?>
                </div>
                <div class="col-xs-5">
                    <a href="<?php echo $shortNameURL;?>" target="_blank" class="pull-right btn btn-primary btn-sm"><i class="fa fa-info-circle"></i> Info</a>
                </div>
            </div>
        </div>
    </section>
</div>
<?php


$html = ob_get_clean();
$ret = array(
    'html' => $html,
    'tmp_click_links' => json_encode($click_count_paths)
);

echo json_encode($ret);