<?php
$in_catdom_id = $domain_id;
if(isset($catdom_id) && intval($catdom_id) > 0) {
	$in_catdom_id = $catdom_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));

$show_bl_business = true;
$display_bl_business = true;
$q = "select c.* from left_content c, module m where c.catdom_id='$in_catdom_id' and c.module_id=m.module_id
	and m.url='bl_middle' and c.`type`='module' and c.location='middle' and c.module_active='yes' and c.module_params !=''
";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
	$row = mysql_fetch_assoc($r);
	$bl_mp = unserialize(base64_decode($row['module_params']));
	$bl_categ_uids = array();
	if(isset($bl_mp['module_bl_categs']) && is_array($bl_mp['module_bl_categs'])) {
		$bl_categ_uids = array_keys($bl_mp['module_bl_categs']);
		$bl_categ_uids_str = "";
		if(count($bl_categ_uids) > 0) {
			$bl_temp = array();
			foreach ($bl_categ_uids as $i=>$t_uid) {
				$bl_temp [] = "'" . $t_uid . "'";
			}
			$bl_categ_uids_str = implode(",", $bl_temp);
		}
		else {
			$send404 = true;
			$show_bl_business = false;
		}
	}
	else {
		$send404 = true;
		$show_bl_business = false;
	}
}
else {
	$send404 = true;
	$show_bl_business = false;
}

if($show_bl_business) {
	
	
	
	if(isset($bl_alias_lang)) {
		$q = "select b.* from bl_businesses_tr b, bl_businesses_categories c where b.friendly_name='$bl_business' and b.uid=c.business_uid and c.category_uid in ($bl_categ_uids_str) and b.lang='$bl_alias_lang';";		
		$r = @mysql_query($q);
		if($r && mysql_num_rows($r) == 1) {
			$row = mysql_fetch_assoc($r);
			$parent_uid = $row['uid'];
			
			$q = "select b.* from bl_businesses b where b.uid='$parent_uid'";
			
		}
		else {
			$show_bl_business = false;
			$send404 = true;
		}
	}
	else {
		$q = "select b.* from bl_businesses b, bl_businesses_categories c where b.friendly_name='$bl_business' and b.uid=c.business_uid and c.category_uid in ($bl_categ_uids_str);";
	}
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		$row = mysql_fetch_assoc($r);
		
		$uid = $row['uid'];
		$name = stripslashes($row['name']);
		//$name = mb_convert_case($name, MB_CASE_TITLE, 'UTF-8');
		$description = nl2br(stripslashes(trim($row['description'])));
		$website = fix_url(trim($row['website']));
        $website_error = (array_key_exists("website_error", $row) && !is_null($row['website_error'])) ? true : false;

		
		if(isset($bl_alias_lang)) {
			$bl_lang = $bl_alias_lang;
			
			$qt = "select * from bl_businesses_tr where uid='$uid' and lang='$bl_lang';";
			if(isset($_REQUEST['debug'])) {
				echo "<br/>$qt";
			}
			
			
			$rt = @mysql_query($qt);
			if($rt && mysql_num_rows($rt) == 1) {
				$rowt = mysql_fetch_assoc($rt);
				
				$name = stripslashes($rowt['name']);
				//$name = mb_convert_case($name, MB_CASE_TITLE, 'UTF-8');
				$description = nl2br(stripslashes(trim($rowt['description'])));
			}
			
		}
		
		$q = "select * from domaincategories where catdom_id='$domain_id'";
		$r = @mysql_query($q);
		if($r && mysql_num_rows($r) > 0) {
			$row = mysql_fetch_assoc($r);
			
			$domain_meta_title = stripslashes($row['c_title_en']);
		}
		
		$webpage_title_en = $name . " - $domain_meta_title";
		
		if(isset($bl_alias_lang)) {
			$q = "select c.* from bl_categories_links_tr c, bl_businesses_categories b where b.business_uid='$uid' and c.uid=b.category_uid and c.lang='$bl_alias_lang' and c.district='';";
			$r = @mysql_query($q);
			if($r && mysql_num_rows($r) >0) {
				$row = mysql_fetch_assoc($r);
				$webpage_title_en = $name . " - " . stripslashes($row['title']);
			}
		}
		else {
			$q = "select c.* from bl_categories c, bl_businesses_categories b where b.business_uid='$uid' and c.uid=b.category_uid;";
			$r = @mysql_query($q);
			if($r && mysql_num_rows($r) >0) {
				$row = mysql_fetch_assoc($r);
				$webpage_title_en = $name . " - " . stripslashes($row['cy_admin_title_en']);
			}
		}
		//echo $q;
		
		
		
		
		
		ob_start();
		
		if(!isset($_REQUEST['page'])) {
			include_once(dirname(__FILE__) . "/../adverts.php");
			echo '<br/>';
		}
		
		?>
		<div class="bl_business" style="text-align: left;" <?php echo ($have_microdata) ? 'itemscope itemtype="' . $microdata_types[$microdata_domain]['adverts']['type'] . '"' : '';?>>
			<h1 <?php echo ($have_microdata) ? 'itemprop="name"': '';?>><?php echo $name;?></h1>
			<?php
			
		
			if(!isset($bl_mp['module_bl_links']) || intval($bl_mp['module_bl_links']) == 1) {
				?>
				<p id="ypb_website">
					<?php 
					if($website != ""){
						?>
						<a href="javascript:;" rel="<?php echo $website;?>"  class="yp_js_link" target="_blank">visit website</a>
						<?php 
					} 
					?>
				</p>
				<?php
			}
			if($website != "") {
				if(!isset($bl_mp['module_bl_links']) || intval($bl_mp['module_bl_links']) == 1) {
					?>
					<div id="ypb_img"><a href="javascript:;" rel="<?php echo $website; ?>" target="_blank" class="yp_js_link">
                            <img src="<?=$screenshot_url;?>w=200&h=150&url=<?php echo urlencode($website); ?>" width="200" height="150" border="0" alt="<?php  echo $name; ?>" />
                        </a>
                    </div>
					<?php
				}
				else {
					?>
					<div id="ypb_img">
                        <img src="<?=$screenshot_url;?>w=200&h=150&url=<?php echo urlencode($website); ?>" width="200" height="150" border="0" alt="<?php  echo $name; ?>" />
                    </div>
					<?php
				}
			}
			else {
				echo '<div id="ypb_img">&nbsp;</div>';
			}
			?>
			<div id="ypb_description">
				<p class="dtitle">Description:</p>
				<p class="yp_desc" <?php echo ($have_microdata) ? 'itemprop="description"': '';?>>
					<?php 
					echo $description; 
					?>
				</p>
			</div>
			
			<br/>
			<div style="margin: 5px;">
			<?php
			
				$locs = array();
				$q = "select * from bl_businesses_locations where business_uid='$uid' order by district asc;";
				//echo $q;
				$r = @mysql_query($q);
				if($r && mysql_num_rows($r) > 0) {
					while ($row = mysql_fetch_assoc($r)) {
						
						$l = array(
							"address"	=> "&nbsp;",
							"tel"		=> "&nbsp;",
							"fax"		=> "&nbsp;",
							"email"		=> "&nbsp;",
							"district"	=> "&nbsp;"
						);
						
						$d = array_map('stripslashes', $row);
						$location_id = $d['id'];
						$b_trade_name = $d['trade_name']; 
						$b_trade_name_label = $d['trade_name_label']; 
						$b_description = nl2br(stripslashes($d['description']));
						$b_address = $d['address']; 
						$b_pobox = $d['pobox'];
						$b_pobox_postal_code = $d['pobox_postal_code'];
						$b_postal_code = $d['postal_code'];
						$b_location = $d['location'];
						$b_district = $d['district'];
						$b_district_id = $d['district_id'];
						$b_tel1 = $d['tel1']; 
						$b_tel2 = $d['tel2']; 
						$b_fax1 = $d['fax1']; 
						$b_fax2 = $d['fax2']; 
						$b_mobile1 = $d['mobile1']; 
						$b_mobile2 = $d['mobile2']; 
						$b_email1 = $d['emal1'];  
						$b_email2 = $d['email2'];  
						$b_website = $d['website'];
						
						if(isset($bl_alias_lang)) {
							$qt = "select * from bl_businesses_locations_tr where business_uid='$uid' and location_id='$location_id' and lang='$bl_lang' order by district asc;";
							$rt = @mysql_query($qt);
							if($rt && mysql_num_rows($rt) == 1) {
								$rowt = mysql_fetch_assoc($rt);
								
								$b_trade_name = stripslashes($rowt['trade_name']); 
								$b_trade_name_label = stripslashes($rowt['trade_name_label']); 
								$b_address = stripslashes($rowt['address']); 
								$b_location = stripslashes($rowt['location']);
								$b_district = stripslashes($rowt['district']);
							}
						}
						
						
						if($b_district != "") {
							$l['district'] = $b_district;
						}
						
						if($b_location != "") {
							//
						}
						
						
						$address = array();
						if($b_address != "") {
							if($have_microdata) {
								$address[] = '<span itemprop="streetAddress">' . $b_address . '</span>';
							}
							else {
								$address[] = $b_address;
							}
						}
						
						if($b_pobox != "") {
							$address[] = "<strong>P.O. Box:</strong> " .  $b_pobox;
						}
						
						if($b_pobox_postal_code != "") {
							$address[] = "<strong>P.O. Box Postal Code:</strong> " . $b_pobox_postal_code;
						}
						
						
						$dist = "";
						
						if($b_postal_code != "") {
							if($have_microdata) {
								$dist .= ' <span itemprop="PostalCode">' . $b_postal_code. '</span> ';
							}
							else {
								$dist .= ' ' . $b_postal_code . ', ';
							}
						}
						
						if($b_location != "") {
							if($have_microdata) {
								$dist .= ' <span itemprop="addressLocality">' . $b_location. '</span>, ';
							}
							else {
								$dist .= ' ' . $b_location . ', ';
							}
						}
						
						if($b_district != "") {
							if($have_microdata) {
								$dist .= ' <span itemprop="addressRegion">' . $b_district. '</span><br/>';
							}
							else {
								$dist .= " <strong>$b_district</strong><br/>";
							}
						}
						
						if($have_microdata) {
							$dist .= '<meta itemprop="addressCountry" content="CY">';
						}
						
						
						
						if($dist != "") {
							$address[] = $dist;
						}
						
						if(count($address) > 0) {
							$l['address'] = implode("<br/>", $address);
						}
						
						$tels = array();
						if($b_tel1 != "") $tels [] = $b_tel1;
						if($b_tel2 != "") $tels [] = $b_tel2;
						
						$faxes = array();
						if($b_fax1 != "") $faxes [] = $b_fax1;
						if($b_fax2 != "") $faxes [] = $b_fax2;
						
						if(count($tels) > 0) {
							$l['tel']  = implode(", ", $tels);
						}
						
						if(count($faxes) > 0) {
							$l['fax'] = implode(", ", $faxes);
						}
						
						$emails = array();
						if($b_email1 != "") $emails [] = $b_email1;
						if($b_email2 != "") $emails [] = $b_email2;
						
						if(count($emails) > 0) {
							$l['email'] = implode(", ", $emails);
						}
						
						$have_loc_data = false;
						foreach ($l as $type=>$val) {
							if($val != "&nbsp;") {
								$have_loc_data = true;
								break;
							}
						}
						
						if($have_loc_data) {
							$locs[] = $l;
						}
						
					}
				}
				
				if(count($locs) > 0) {
					
					foreach ($locs as $i=>$loc) {
						?>
						<table cellpadding="1" cellspacing="1" border="0" width="100%" class="ypb_table" <?php echo ($have_microdata) ? 'itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"': '';?>>
							<tr>
								<th colspan="2" align="left">
									<?php echo ucfirst($loc['district']) . ' Address Details'; ?>
								</th>
							</tr>
							<?php
							if($loc['address'] != "&nbsp;") {
								?>
								<tr>
									<td class="yph" align="left" valign="top">Address:</td>
									<td class="yp_lb" align="left" width="90%">
										<?php echo $loc['address'];?>
									</td>
								</tr>
								<?php
							}
							?>
							<?php
							if($loc['tel'] != "&nbsp;") {
								?>
								<tr>
									<td class="yph" align="left" valign="top">Telephone:</td>
									<td class="yp_lb" align="left" width="90%">
										<span <?php echo ($have_microdata) ? 'itemprop="telephone"': '';?>>	
										<?php echo $loc['tel'];?>
										</span>
									</td>
								</tr>
								<?php
							}
							?>
							<?php
							if($loc['fax'] != "&nbsp;") {
								?>
								<tr>
									<td class="yph" align="left">Fax:</td>
									<td class="yp_lb" align="left" width="90%">
										<span <?php echo ($have_microdata) ? 'itemprop="faxNumber"': '';?>>
										<?php echo $loc['fax'];?>
										</span>
									</td>
								</tr>
								<?php
							}
							?>
							<?php
							if($loc['email'] != "&nbsp;") {
									?>
								<tr>
									<td class="yph" align="left">Email:</td>
									<td class="yp_lb" align="left" width="90%">
										<span <?php echo ($have_microdata) ? 'itemprop="email"': '';?>>
										<?php echo $loc['email'];?>
										</span>
									</td>
								</tr>
								<?php
							}
							?>
							
						</table>
						<?php
					}
				}
				
				?>
						
			</div>
			<?php
			
			?>
		</div>
		<script type="text/javascript">
			$(document).ready(function(){
				$('a.yp_js_link').click(function(){
					var url = $(this).attr('rel');
					if(url != "") {
						$(this).attr('href', url);
					}
				});
			});
		</script>
		<?php
		
		$bl_content = ob_get_contents();
		ob_end_clean();
	}
	else {
		$display_bl_business = false;
		$send404 = true;
	}
}

if($show_bl_business && $display_bl_business) {
	echo $bl_content;
}
else {
	require_once(dirname(__FILE__) . "/../404/404.php");
}

?>