<?php
$optres['css'][] = $module_css_path . $modules_dir_name .  '/main_article/css/type_poi.css';

$query = "SELECT * FROM  main_article WHERE article_id = '$middle_article_id' AND active='yes'";
$result = mysql_query ($query) or die("<b>$query</b>".mysql_error());
if($result && mysql_num_rows($result)>0) {
	$line = mysql_fetch_assoc($result);

	$has_map = false;
	
	if(trim($line["url"]) != ""){
		$ahref = return_link_article('article_', $line['_blank'], $line['url'], $line['friendly_url'], $line["article_id"]);
		?>
		<div class="article_list_title_link">
			<p class="article_list_title_link">
				<a class="article_class" href="<?php  echo $ahref['link'];?>" target="<?php  echo $ahref['target'];?>"><span><?php  echo $line["title_en"];?></span></a>
			</p>
		</div>
		<?php 
	}
	else {
		$show_title = ($line["active_title_en"] == "yes") ? true : false;
		$show_abstract = true;
		$article_title = stripslashes(trim($line["title_en"]));
		$article_friendly_url = trim($line['friendly_url']);
		$page_title = stripslashes($line['page_title_en']);
		if($article_title == "") $show_title = false;
		$article_description = stripslashes(trim($line["description_en"]));
		$article_meta_description = stripslashes(trim($line["meta_description_en"]));
		$article_meta_keywords = stripslashes(trim($line["meta_keywords_en"]));
		
		$lat = floatval($line['lat']);
		$lng = floatval($line['lng']);
		$zoom = intval($line['zoom']);
		
		
		if($lat != "" && $lng != "") {
			$has_map = true;
			$map_data = array(
				"lat"	=> $lat,
				"lng"	=> $lng,
				"zoom"	=> intval($zoom) > 0 ? $zoom : 13
			);
		
		}
		
		// Get host data
		$_host = $_SERVER['HTTP_HOST'];
		if(str_replace('www.', '', $ultra_domainname) != str_replace('www.', '', $_host)) {
			$_host .= "/$ultra_addonname";
		}
		
		$is_h1 = true;
		
		$article_url = $protocol . $_host . "/article/$article_friendly_url.html";
		$page_url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		$page_url_parts = parse_url($page_url);
		$page_url = $page_url_parts['scheme'] . '://' . $page_url_parts['host'] . $page_url_parts['path'];
		
		
		if($article_url != $page_url) {
			$is_h1 = false;
		}
		
		
		/*
		if(isset($module_params['main_article_link_title']) && trim($module_params['main_article_link_title']) != "") {
			$article_title = trim(stripslashes($module_params['main_article_link_title']));
			
			$show_title = true;
		}
		*/
		
		if(isset($module_params['main_article_show_abstract']) && intval($module_params['main_article_show_abstract']) == 0) {
			$show_abstract = false;
		}

				
		if($show_title) {
			/* Social Media plugin **********/
			/*$sm_class_overload = "sm-categ";
			include(dirname(__FILE__) . "/../sm/sm.php");*/
			/***************************/
			if($is_h1) {
				?>
                <header>
				    <h1 itemprop="name"><?php  echo $article_title; ?></h1>
                </header>
				<?php
			}
			else {
				?>
				<h2  itemprop="name"><?php  echo $article_title; ?></h2>
				<?php
			}
		}
		
		// Get photos
		$main_photo = array();
		$art_photos = array();
		$q = "select * from main_article_images where article_id='$middle_article_id' order by pos asc;";
		$r = @mysql_query($q);
		if($r && mysql_num_rows($r) > 0) {
			
			//$optres['js'][] = $_SERVER['DOCUMENT_ROOT'] . "/js/galleria-1.2.9.min.js";
            $optres['js'][] = $path . "assets/plugins/galleria/galleria.min.js";
			while ($row = mysql_fetch_assoc($r)) {
				if(count($main_photo) == 0) {
					$main_photo = $row;
				}
				
				$art_photos [] = array_map("stripslashes", $row);
			}
		}
		$www_root = $_SERVER['HTTP_HOST'];
		if(str_replace('www.', '', $www_root) != str_replace('www.', '', $ultra_domainname) ) {
			$www_root .= "/$ultra_addonname";
		}
		
		echo '<meta itemprop="url" content="'. $article_url .'"/>';
		

		
		?>
        <div class="article-body cyprus <?php echo ($article_description == "") ? "no-margin": "";?>">
            <?php
            if($has_map || $line['lcode'] != "") {
                ?>
                <div class="poi-address" itemprop="address" itemscope="itemscope" itemtype="http://www.schema.org/PostalAddress">
                    <?php
                    if($line['streetAddress'] != "") {
                        echo '<span itemprop="streetAddress">' . $line['streetAddress'] . '</span>, ';
                    }
                    elseif ($line['poboxnumber'] != "") {
                        echo 'P.O. Box <span itemprop="postOfficeBoxNumber">' . $line['poboxnumber'] . '</span>, ';
                    }

                    if($line['lcode'] != "") {
                        echo '<span itemprop="addressLocality">' . $line['addressLocality'] . '</span>, ';
                        if ($line['postalCode'] != "") {
                            echo '<span itemprop="postalCode">' . $line['postalCode'] . '</span> ';
                        }
                        echo '<span itemprop="addressRegion">' . $line['addressRegion'] . '</span>, ';
                        echo '<span itemprop="addressCountry">' . $line['addressCountry'] . '</span>';
                    }
                    ?>

                </div>
                <?php
            }
            ?>
			<div class="article_description" itemprop="description">
                <div class="row no-margin">
                    <?php

                    $col_photos = 'hidden';
                    $col_map = 'hidden';
                    if(isset($art_photos) && count($art_photos) > 0) {
                        $col_photos = 'col-sm-10';

                        if($lat != "" && $lng != "") {
                            $col_photos = 'col-sm-5';
                            $col_map = 'col-sm-5';
                        }
                    }
                    elseif($lat != "" && $lng != "") {
                        $col_map = 'col-sm-10';
                    }

                    if(isset($art_photos) && count($art_photos) > 0) {
                        ?>
                        <div class="<?=$col_photos;?> no-padding poi-gallery-container">
                            <div id="galleria" class="poi-gallery" itemtype="http://schema.org/ImageObject"
                                 itemscope="itemscope" itemprop="photo">
                                <?php
                                foreach ($art_photos as $i => $img) {
                                    $thumb_file = dirname(__FILE__) . "/../../images/articles/$domain_id/$middle_article_id/gallery/thumbs/" . $img['filename'];
                                    $thumb_url = $protocol . $_SERVER['HTTP_HOST'] . "/images/articles/$domain_id/$middle_article_id/gallery/thumbs/" . $img['filename'];
                                    $image_file = dirname(__FILE__) . "/../../images/articles/$domain_id/$middle_article_id/gallery/" . $img['filename'];
                                    $image_url = $protocol . $_SERVER['HTTP_HOST'] . "/images/articles/$domain_id/$middle_article_id/gallery/" . $img['filename'];

                                    if (intval($img['iwt'] <= 16)) $img['iwt'] = 90;
                                    if (intval($img['iht'] <= 16)) $img['iht'] = 90;

                                    $data_title = $img['title'];
                                    $data_description = $img['description'];

                                    if (file_exists($thumb_file) && file_exists($image_file)) {
                                        echo '<a href="', $image_url, '" itemprop="contentURL"><img src="', $thumb_url, '" data-big="', $image_url, '" data-title="', $data_title, '" data-description="', $data_description, '"  alt="', $data_title, '" width="', $img['iwt'], '" height="', $img['iht'], '" itemprop="thumbnailUrl"/></a>';
                                    } else {
                                        // echo "<br/>$thumb_file does not exists";
                                    }
                                }
                                ?>
                            </div>
                        </div>
                        <?php
                    }
                    if($lat != "" && $lng != "") {
                        $poi_class = 'poi_map';
                        if (isset($art_photos) && count($art_photos) > 0) {
                            $poi_class .= ' poi_map_right';
                        } else {
                            $poi_class .= ' poi_map_left';
                        }
                        ?>
                        <div class="<?=$col_map;?> no-padding poi-map-container">
                            <div class="poi-map" itemprop="geo" itemscope="itemscope"
                                 itemtype="http://schema.org/GeoCoordinates">
                                <?php

                                $static_map_url = 'https://maps.google.com/maps/api/staticmap?key=AIzaSyDxpjfVxlLazmPt1gkiw_wIy55ofWAUJvY&' . ('center=' . $lat . ',' . $lng . '&amp;zoom=' . $zoom . '&amp;size=720x360&amp;markers=%7C' . $lat . ',' . $lng . '&amp;maptype=hybrid');
                                echo '<img src="', $static_map_url, '" border="0" width="720" height="360" class="map_enlarge_link" alt="', stripslashes($article_title), ' | Map"/>';
                                ?>

                                <meta itemprop="latitude" content="<?php echo $lat; ?>"/>
                                <meta itemprop="longitude" content="<?php echo $lng; ?>"/>

                            </div>
							<meta itemprop="hasMap" content="<?php echo $static_map_url; ?>"/>
                        </div>
                        <?php
                    }
                    ?>
                </div>
                <?php
                $article_description = str_replace("../images/", $path ."images/", $article_description);
                $article_description = str_replace(array('<p></p>', '<p> </p>', '<p>&nbsp;</p>'), '', $article_description);
                if($show_title) {
                    $article_description = str_ireplace(array('<h1', '</h1'), array('<h2', '</h2'), $article_description);
                }
                echo $article_description;
                ?>
			</div>
		</div>
		<?php
		
		if(str_replace("/" . $line['friendly_url'].'.html', '', $_SERVER['REQUEST_URI']) != $_SERVER['REQUEST_URI']) {
			$webpage_title_en = $page_title;
			if($article_meta_description != "") {
				$meta['description'] = $article_meta_description;
			}
			else {
				if(trim(strip_tags($line['short_descr_en'])) != "" && $line['use_meta'] == 1) {
					$meta['description'] = mb_substr(trim(fix_stripped_words(stripslashes($line['short_descr_en']))), 0, 150, 'UTF-8');
				}
				else {
					$meta['description'] = mb_substr(trim(fix_stripped_words($article_description)), 0, 150, 'UTF-8');
				}
			}
			
			if($article_meta_keywords != "") {
				$meta['keywords'] = $article_meta_keywords;
			}
			else {
				$meta['keywords'] = $article_title . ', ' . $meta['keywords'];
			}
		}
		
		if($has_map && isset($wb_uid) && $wb_uid != '') {
			$nearby_hotels_query = " SELECT 
				dest.adv_code,
				dest.lat,
				dest.lng,
				(3956 * 2 * ASIN(SQRT(POWER(SIN(($lat - abs( dest.lat)) * pi()/180 / 2),2) + COS($lat * pi()/180 ) * COS( abs(dest.lat) *  pi()/180) * POWER(SIN(($lng - dest.lng) *  pi()/180 / 2), 2) ))) * 1.60934 as distance
			FROM 
				advertisment_locations dest,
				wb_categories_adverts w
			WHERE
				dest.main_location='1'
				AND dest.adv_code=w.wb_advert_uid
				AND w.wb_uid='$wb_uid'
			HAVING distance < 30
			ORDER BY distance limit 0,12;";
			
			//echo $nearby_hotels_query;
			
			require_once(dirname(__FILE__) . "/../nearby_hotels/nearby_hotels.php");
			
		}
		
		
	}
}
?>
<script type="text/javascript">
<?php
$js_map_data = '';
if($has_map) {
	$js_map_data = base64_encode(serialize($map_data));
}

echo 'var map_data = "' . $js_map_data . '";';
?>
    var big_map_title = 'Large Map';

</script>