<?php
$line = mysql_fetch_assoc($result);
$use_town_dir = false;
$current_advertisment_id = intval($line['advertisment_id']);
$adv_code = $line['adv_code'];
$has_thumbshot = false;


// Check if it is a paying advert
$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($q);
if($rp && mysql_num_rows($rp) > 0) {
	$paying_advert = true;
}

// 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";
	$th_path = $path . "images/advertisments/gallery/$current_advertisment_id/thumbs/$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"]);

if(isset($_REQUEST['rel_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=' .$line["website"] . '&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=' .$line["website"] . '&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=' .$line["website"] . '&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=' .$line["website"] . '&amp;type=www&amp;id=' . $line['advertisment_id'] . '&amp;domain=' . $line_domain["domainname"] .'&amp;domain_id=' . $domain_id. '&amp;click_from=s';
}

?>
<div class="<?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');
	}
	
	$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;
	}
	
	
	
	if($website != ""){
		if($advert_active) {
			if($paying_advert) {
				if($is_hotel) {
					?>
					<span class="click-link flimg" rel="<?php echo $click_link_id;?>" extra_class="flimg">
					<img src="<?php echo $advert_image; ?>" width="120" height="90" border="0" alt="<?php echo stripslashes($line['short_title_en']); ?>" />
					</span>
					<?php
				}
				else {
					?>
					<a class="flimg" href="click_count.php?link=<?php echo $line["website"];?>&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">
						<img src="<?php echo $advert_image; ?>" width="120" height="90" border="0" alt="<?php echo stripslashes($line['short_title_en']); ?>" />
					</a>
					<?php 
				}
			}
			else {
				if($is_hotel) {
					?>
					<span class="click-link flimg" rel="<?php echo $click_link_id;?>" extra_class="flimg">
						<img src="<?php echo $advert_image; ?>" width="120" height="90" border="0" alt="<?php echo stripslashes($line['short_title_en']); ?>" />
					</span>
					<?php 
				}
				else {
					?>
					<span class="flimg">
					<img src="<?php echo $advert_image; ?>" width="120" height="90" border="0" alt="<?php echo stripslashes($line['short_title_en']); ?>" />
					</span>
					<?php 
				}
			}
		}
		else {
			if($is_hotel) {
				?>
				<span class="click-link flimg" rel="<?php echo $click_link_id;?>" extra_class="flimg">
				<img src="<?php echo $advert_image; ?>" width="120" height="90" border="0" alt="<?php echo stripslashes($line['short_title_en']); ?>" />
				</span>
				<?php 
			}
			else {
				?>
				<span class="flimg">
				<img src="<?php echo $advert_image; ?>" width="120" height="90" border="0" alt="<?php echo stripslashes($line['short_title_en']); ?>" />
				</span>
				<?php 
			}
		}
	}
	
	if($is_hotel) {
		echo '<h2>';
		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>';
	}
	else {
		?>
		<h2><?php echo stripslashes($line["short_title_en"]);?></h2>
		<?php
	}
	if(isset($_REQUEST['debug1'])) {
		echo "<strong>".$categ_order[$i]."</strong>";
	}
	$rec_locations = array();
	$location = array();
	$q = "select * from advertisment_locations where advertisment_id='{$advertisment_id[$i]}' and active='1' order by main_location asc;";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r)>0) {
		while($row = mysql_fetch_assoc($r)) {
			/*
			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)$use_town_dir = true;
	}
	
	if(isset($_REQUEST['debugx'])) {
		echo '<pre>', var_export($rec_locations, true), '</pre>';
		echo '<pre>', var_export($wb_locations, true), '</pre>';
	}
	
	?>
	<table>
		<tr>
			<th>phone:</th>
			<td>
				<?php
				if(isset($location['tel1']) && trim($location['tel1']) != ""){ 
					echo '(+357) ' . number_format($location['tel1'], null, null, ' '); 
				} 
				else { 
					echo 'n/a'; 
				}
				?>
			</td>
		</tr>
		<tr>
			<th>email:</th>
			<td>
				<?php
				if(isset($location['email']) && trim($location['email']) != ""){ 
					?>
					<a href="javascript:;" class="mailto" rel="<?php echo str_replace('@', '[ at ]', trim($location['email'])); ?>"><?php echo str_replace('@', ' [ at ] ', trim($location['email'])); ?></a>
					<?php 
				} 
				else { 
					echo 'n/a'; 
				}
				?>
			</td>
		</tr>
		<tr>
			<th>website:</th>
			<td>
				<?php 
				if($website != ""){
					if($advert_active) {
						if($paying_advert) {
							if($is_hotel) {
								?>
								<span class="click-link" rel="<?php echo $click_link_id;?>"><?php echo $website;?></span>
								<?php
							}
							else {
								?>
								<a href="<?php echo $protocol . $microdata_host . '/' .  $shortNameURL;?>" link="<?php echo $protocol . $microdata_host . '/';?>click_count.php?link=<?php echo $line["website"];?>&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 $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 $website;?></span>
								<?php
							}
							else {
								echo $website;
							}
						}
					}
					else {
						if($is_hotel) {
							?>
							<span class="click-link" rel="<?php echo $click_link_id;?>"><?php echo $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 $website;
						}
					}
				} 
				else { 
					echo 'n/a'; 
				} 
				?>
			</td>
		</tr>
	</table>
	<p><?php
	if($is_hotel) {
		echo '';
		if(isset($_SESSION['user_id'])) {
			echo '<a href="../admin/advert_manager.php?advert_action=advert_edit_main&advertisment_id=',$line['advertisment_id'],'" target="_blank">Edit</a> | ';
		}
	}
	else {
		if($use_town_dir) {
			$shortNameURL = $town_friendly_url . $shortNameURL;
		}
	
		if(isset($_SESSION['user_id']) && intval($_SESSION['user_id']) > 0) {
			
		}
		
		if($paying_advert && $advert_active) {
			?>
			<a href="<?php echo $protocol . $microdata_host . '/' .  $shortNameURL;?>" target="_blank">More about <?php echo stripslashes($line["short_title_en"]);?></a>
			<?php
		}
		else {
			?>
			<a href="<?php echo $protocol . $microdata_host . '/' .  $shortNameURL;?>" target="_blank" class="small10" >More about <?php echo stripslashes($line["short_title_en"]);?></a>
			<?php
		}
	}
	?></p>
</div>
<?php

?>