<?php
ob_start();
$optres['js'][] = $path . "amodules/wb_search_results/js/markerclusterer.js";
//$optres['js'][] = $path . "amodules/wb_search_results/js/infobubble.js";
$optres['css'][] = $path . "assets/plugins/snazzy/snazzy-info-window.css";
$optres['css'][] = $path . "assets/plugins/snazzy/snazzy-custom.css";
$optres['css'][] = $path . "assets/css/bootstrap-dialog.min.css";
$optres['js'][] = $path . "assets/js/bootstrap-dialog.min.js";
//$optres['js'][] = $path . "assets/plugins/snazzy/snazzy-info-window.min.js";
$wb_search_results_show = 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;
}

function show_adverts() {
	global $browser_type, $optres, $domain_id, $catdom_id, $general_path, $ultra_domainname, $ultra_addonname, $path, $domainname, $microdata_domains, $recomented_value_en;
	require_once(dirname(__FILE__) . "/../adverts/adverts.php");
}


$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));

$wb_domainname = str_replace("www.", "", $ultra_domainname);

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

if(isset($module_params)) {

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

	if(isset($_REQUEST['debugwb'])) {
		echo '<br/>$fm_uid = ', $fm_uid;
		echo '<br/>$fm_selected = ', $fm_selected;
		echo '<pre>', var_export($module_params, true), '</pre>';
	}
	//
	
	if(isset($_POST)) {
		//echo 'POST: <pre>', var_export($_POST, true), '</pre>';
	}
	
	
	if(isset($module_params['module_hotel_filter']) && intval($module_params['module_hotel_filter']) > 0) {
		$q = "select * from custom_hotels where catdom_id='$domain_id' and id='{$module_params['module_hotel_filter']}';";
		$r = @mysql_query($q);
		if($r && mysql_num_rows($r) == 1) {
			$row = mysql_fetch_assoc($r);
			$filter_module_params = unserialize(base64_decode($row['params']));
			
			//if(isset($_POST)) {
			//	echo 'FILTER: <pre>', var_export($filter_module_params, true), '</pre>';
			//}
			if(!isset($_POST['wb_search'])) {
				$_POST['wb_search'] = "wb_search";
				if(isset($filter_module_params['fm_uid'])) {
					$_POST['ch_fm_uid'] = $filter_module_params['fm_uid'];
				}
				$_POST = array_replace($_POST, $filter_module_params);
				
				$module_params = array_replace($module_params, $filter_module_params);
				
				//echo '<pre>', var_export($module_params, true), '</pre>';
			}
			else {
				$module_params = array_replace($module_params, $filter_module_params);
				
				$over_dcode = false;
				$over_dcode_value = '';
				$over_lcode = false;
				$over_lcode_value = '';
				
				if(isset($_POST['clear_search'])) {
					$over_dcode = true;
					$over_dcode_value = $_POST['dcode'];
					
					$over_lcode = true;
					$over_lcode_value = $_POST['lcode'];
					
					unset($_POST['clear_search']);
				}
				
				$_POST = array_replace($_POST, $filter_module_params);
				
				if($over_dcode) $_POST['dcode'] = $over_dcode_value;
				if($over_lcode) $_POST['lcode'] = $over_lcode_value;
			}
		}
	}

	$wb_hide_from_filter = isset($module_params['hide']) ? $module_params['hide'] : array();
	$wb_srch_dcode = isset($module_params['dcode']) ? trim($module_params['dcode']) : '';
	$wb_srch_lcode = isset($module_params['lcode']) ? trim($module_params['lcode']) : '';
	$wb_srch_fm_uid = isset($module_params['fm_uid']) ? trim($module_params['fm_uid']) : '';
	$wb_srch_selected_fm_uid = isset($module_params['module_selected_fm_uid']) ? trim($module_params['module_selected_fm_uid']) : '';
	$wb_srch_rpp = isset($module_params['module_num_results']) ? intval($module_params['module_num_results']) :20;
	$wb_srch_page_title = isset($module_params['module_page_title']) ? stripslashes(trim($module_params['module_page_title'])) : '';
	$wb_srch_page_title_tag = isset($module_params['module_title_tag']) ? stripslashes(trim($module_params['module_title_tag'])) : 'h1';
	$module_show_filter = isset($module_params['module_show_filter']) ? intval($module_params['module_show_filter']) : 0;
	
	$module_map_search = isset($module_params['module_map_search']) ? intval($module_params['module_map_search']) : 0;
	$module_map_search_image = isset($module_params['module_map_search_image']) ? trim($module_params['module_map_search_image']) : '';
	$module_show_adverts = isset($module_params['module_show_adverts']) ? trim($module_params['module_show_adverts']) : 'no';

    //echo '<br>$wb_srch_fm_uid = ' . $wb_srch_fm_uid;
    //echo '<br>$wb_srch_selected_fm_uid = ' . $wb_srch_selected_fm_uid;

	
	
	$block_districts = 0;
	if($wb_srch_dcode != "") $block_districts = 1;
	
	
	if(intval($module_show_filter) == 1) {
		$lc = array (
			  'module_id' => $req_modules['wb_search']['module_id'],
			  'module_params' => '',
			  'module_active' => 'yes',
			  'type' => 'module',
			  'location' => 'left',
			  'home_page' => '1',
			  'sec_page' => '1',
			);
			
		$left_pool [] = $lc;
	}
	
	
	if($wb_srch_fm_uid != "" && !isset($_REQUEST['fm_uid']) && !isset($_REQUEST['ch_fm_uid'])) {
		$fm_uid = $wb_srch_fm_uid;
	}
	
	if( (!isset($fm_uid) || $fm_uid == "" ) && $wb_srch_selected_fm_uid != '') {
		$fm_uid = $wb_srch_selected_fm_uid;
	}


    //echo '<br>$fm_uid = ' . $fm_uid;
    //echo '<br>$wb_srch_fm_uid = ' . $wb_srch_fm_uid;
    //echo '<br>$wb_srch_selected_fm_uid = ' . $wb_srch_selected_fm_uid;

	if(isset($_REQUEST['debugwb'])) {
		echo '<br/>$fm_uid = ', $fm_uid;
	}
}

?>
<?php
$wb_body_class = 'article-body cyprus';

ob_start();
if(isset($wb_srch_page_title) && $wb_srch_page_title != "") {
	if(trim($webpage_title_en) == "" ) $webpage_title_en = $wb_srch_page_title;
	echo '<' . $wb_srch_page_title_tag . '>'. $wb_srch_page_title . '</'.$wb_srch_page_title_tag.'>';
	
}
else {
	$webpage_title_en = $webpage_title_en;
	if(isset($webpage_header_en) && $webpage_header_en != "") {
		echo '<' . $wb_srch_page_title_tag . '>'. $webpage_header_en . '</' . $wb_srch_page_title_tag . '>';
	}
	else {
		echo '<' . $wb_srch_page_title_tag . '>'. $webpage_title_en . '</' . $wb_srch_page_title_tag . '>';
	}
}
$wb_header = ob_get_clean();
if($wb_header != "") {
    if(in_array($wb_srch_page_title_tag, array('h1', 'h2', 'h3'))) {
        echo '<header>' . $wb_header . '</header>';
    }
    else {
        echo $wb_header;
    }

    if($wb_srch_page_title_tag == "h1") $wb_body_class = 'article-body cyprus';
    else $wb_body_class = 'article-body red';
}
?>
<div class="<?=$wb_body_class?>">
<!--[wb_search_top]--!>
<?php
if(isset($_REQUEST['debugwb'])) {
	echo '<br/>before adverts $fm_uid = ', $fm_uid;
}
if($module_show_adverts == 'top') {
	//require_once(dirname(__FILE__) . "/../adverts/adverts.php");
	show_adverts();
}
if(isset($_REQUEST['debugwb'])) {
	echo '<br/>after adverts $fm_uid = ', $fm_uid;
}

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

if($wb_search_results_show == 1) {
	
	
	
	
	// Get domain catdoms
	$catdoms = array();
	$catdoms [] = $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"]);
		}
	}
	
	if(!isset($wb_uid)) {
	
		$q = "select wb.* from wb_categories_domains wb where wb.wb_domainname='$wb_domainname';";
		$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
		if($r && mysql_num_rows($r) > 0) {
			$row = mysql_fetch_assoc($r);
			$wb_uid = $row['wb_uid'];
		}
			
	}
	
	if(isset($wb_uid) && strlen($wb_uid) == 16) {
		
		
		if(isset($_REQUEST['ch_fm_uid'])) {
			$fm_uid = mysql_real_escape_string(trim($_REQUEST['ch_fm_uid']));
		}
		
		if(!isset($fm_uid)) {
			$fm_uid = '';
		}
		
		require_once(dirname(__FILE__) . "/wb_actions.php");
		if($no_adverts_found) {
			require_once(dirname(__FILE__) . "/wb_no_adverts.php");
		}
		require_once(dirname(__FILE__) . "/wb_display.php");
		
		
	}

    if(!isset($click_count_paths)) {
        $click_count_paths = array();
    }
	?>
	<script type="text/javascript">
        var click_links = <?php echo (isset($click_count_paths)) ? json_encode($click_count_paths) : json_encode(array());?>;
        var module_map_search = <?php echo ($module_map_search && count($click_count_paths) > 0) == 1 ? 1 : 0;?>;
        var wb_search_map_init = '<?=$path;?>amodules/wb_search_results/js/wb_search_results.js';
	</script>
	<?php
    $optres['js'][] = $path . 'amodules/wb_search_results/js/wb_search_results_loader.js';

}
else {
    require_once(dirname(__FILE__) . "/wb_no_adverts.php");
}

$show_wb_search_top = false;
$wb_search_top_form = '';
if($wb_search_results_show == 1) {
    $show_wb_search_top = true;
    ob_start();
    include(dirname(__FILE__) . "/../wb_search_top/wb_search_top.php");
    $wb_search_top_form = ob_get_clean();
}


if($module_show_adverts == 'bottom') {
	//require_once(dirname(__FILE__) . "/../adverts/adverts.php");
	show_adverts();
}

echo '</div>';

$wb_content = ob_get_contents();
ob_end_clean();

if($wb_search_results_show == 1) {
    if($show_wb_search_top) {
        $wb_content = str_replace('<!--[wb_search_top]--!>', $wb_search_top_form, $wb_content);
    }
	echo $wb_content;

    //echo '<pre>', var_export($optres['js'], true), '</pre>';
}
else {
	?>

	<div class="more_wb_links">
		<?php	
		echo '<div class="alert alert-info"><p><i class="fa fa-info-circle fa-2x"></i> Please click the following link to find more <a href="'.$protocol.$microdata_host.'/'.$wb_friendly_url.'/page-1.html" class="" title="'.$module_wb_title.'">'.$module_wb_title.'</a>.</p></div>';
		?>
	</div>
	<?php	
}
//else {
//	unset($wb_content);
//}