<?php
mt_srand();
$index = mt_rand(1, 9999);
?>
<div id="pjWrapper">
	<div class="container-fluid pjYpContainer">
		<?php include_once ROOT_PATH . PJ_TEMPLATE_PATH . 'default/elements/header.php';?>
		
		<div class="pjYpBody">
			
			<div class="panel panel-default pjYpResultsMaped">
				<div class="panel-body">
					<div class="row">
						<?php
						$action_url =  $_SERVER['PHP_SELF'];
						if ($tpl['option_arr']['o_seo_url'] == 'Yes')
						{
							$action_url = $tpl['option_arr']['o_listing_page'];
						} 
						?>
						<form id="pjYpMapSearch_<?php echo $index;?>" action="<?php echo $action_url; ?>" method="get" class="pjYpSearch" role="search">
							<input type="hidden" name="controller" value="pjListings" />
							<input type="hidden" name="action" value="pjActionMap" />
							<input type="hidden" name="listing_map" value="1" />
							
							<div class="col-lg-7 col-md-7 col-sm-6 col-xs-12 pull-right">
								<div class="form-group">
									<input type="text" name="keyword" value="<?php echo isset($_GET['keyword']) ? $_GET['keyword'] : null;?>" placeholder="<?php __('label_search');?>" class="form-control" />
	
									<button type="submit" class="btn btn-default pjYpBtn pjYpBtnSearch">
										<span class="glyphicon glyphicon-search"></span>
									</button>
								</div><!-- /.form-group -->
								
	
								<div id="pjYpMapCanvas" class="pjYpResultsMap">
									
								</div><!-- /.pjYpResultsMap -->
							</div><!-- /.col-lg-7 col-md-7 col-sm-6 col-xs-12 pull-right -->
						
							<div class="col-lg-5 col-md-5 col-sm-6 col-xs-12">
								<div class="pjYpFilters">
									<select id="pjYpCategoryMap_<?php echo $index;?>" name="category_id" class="form-control">
										<option value=""><?php __('label_filter_by_category');?></option>
										<?php
										foreach($tpl['category_arr'] as $v)
										{
											?><option value="<?php echo $v['id']?>"<?php echo isset($_GET['category_id']) ? ($_GET['category_id'] == $v['id'] ? 'selected="selected"' : null ) : null;?>><?php echo $v['name'];?> (<?php echo $v['number_of_listings'];?>)</option><?php
										} 
										?>
									</select>
								</div><!-- /.pjYpFilters -->
								
								<?php
								$ypAddressArr = array();
								if(!empty($tpl['arr']))
								{
									?>
									<div class="pre-scrollable">
										<div class="list-group pjYpMapResults">
											<?php
											foreach($tpl['arr'] as $k => $v)
											{ 
												$listing_title = pjSanitize::html(stripslashes($v['title']));
												if ($tpl['option_arr']['o_seo_url'] == 'No')
												{
													$url = $_SERVER['SCRIPT_NAME'] . '?controller=pjListings&amp;action=pjActionView&amp;id=' . $v['id'] .(isset($_GET['iframe']) ? '&amp;iframe' : NULL);
												} else {
													$path = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']));
													$path = $path == '/' ? '' : $path;
													$url = $path .'/'. $controller->friendlyURL($v['title']) . "-". $v['id'] . ".html";
												}
												
												$ypAddress = array();
												$ypHtml = array();
												
												if (!empty($v['country_title']))
												{
													$ypAddress[] = stripslashes($v['country_title']);
												}
												if (!empty($v['address_state']))
												{
													$ypAddress[] = stripslashes($v['address_state']);
												}
												if (!empty($v['address_city']))
												{
													$ypAddress[] = stripslashes($v['address_city']);
												}
												if (!empty($v['address_postcode']))
												{
													$ypAddress[] = stripslashes($v['address_postcode']);
												}
												if (!empty($v['address_content']))
												{
													$_address_arr = explode("\n", $v['address_content']);
													$address_arr = array();
													foreach($_address_arr as $val)
													{
														$address_arr[] = stripslashes(trim($val));
													}
													$ypAddress[] = join(", ", $address_arr);
												}
												
												$ypHtml[] = '<div class="yp-map-info-window">';
												$ypHtml[] = '<label class="title"><a href="'.$url.'">' . $listing_title . '</a></label>';
												$ypHtml[] = '<span class="address">' . strtolower(__('label_address', true)) . ': <label>' . join(", ", $ypAddress) . '</label></span>';
												if($v['owner_show'] == 'T')
												{
													if(!empty($v['phone']))
													{
														$ypHtml[] = '<span class="phone">'. strtolower(__('label_phone', true)) .': <label>'.$v['phone'].'</label></span>';
													}
													$email = !preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i', $v['email']) ? $v['email'] : '<a href="mailto:'.$v['email'].'">'.$v['email'].'</a>';
													$ypHtml[] = '<span class="email">'. strtolower(__('label_email', true)) .': <label>'.$email.'</label></span>';
													if(!empty($v['url']))
													{
														$ypHtml[] = '<span class="url">'. strtolower(__('label_url', true)) .': <label><a href="'.$v['url'].'" target="_blank">'.$v['url'].'</a></label></span>';
													}
												}
												$ypHtml[] = '<\/div>';
												
												$ypAddress = preg_replace('/\r\n|\n|\t/', '', $ypAddress);
												$ypHtml = preg_replace('/\r\n|\n|\t/', '', $ypHtml);
												
												$ypAddress = str_replace('"', "\\\"", $ypAddress);
												$ypHtml = str_replace("'", "\'", $ypHtml);
												
												$ypAddressArr[] = '{"lat": "' . floatval($v['lat']) . '", "lng": "' . floatval($v['lng']) . '", "html": \'' . join("", $ypHtml) . '\'}';
												?>
												<div class="list-group-item pjYpMapResult" data-index="<?php echo $k;?>">
													<p class="pjYpMapResultTitle"><?php echo $listing_title; ?></p><!-- /.pjYpMapResultTitle -->
											
													<dl class="dl-horizontal pjYpMapResultAddress">
														<dt><?php __('label_address');?>: </dt>
														<dd><?php echo join(", ", $ypAddress);?></dd>
													</dl><!-- /.dl-horizontal pjYpMapResultAddress -->
											
													<div class="pjYpMapResultActions">
														<a href="<?php echo $url;?>" class="btn btn-default pjYpBtn pjYpBtnPrimary pjYpMapViewMore">
															<span class="glyphicon glyphicon-plus"></span>
															<?php __('label_view_more');?>
														</a>
													</div><!-- /.pjYpMapResultActions -->
												</div>
												<?php
											} 
											?>
										</div><!-- /.list-group pjYpMapResults -->
									</div><!-- /.pre-scrollable -->
									<?php
								} else {
									?>
									<p class="text-muted"><?php __('label_not_found', false, true);?></p><!-- /.list-group-item pjYpNoResultsText -->
									<?php
								}
								?>
							</div><!-- /.col-lg-5 col-md-5 col-sm-6 col-xs-12 -->
						</form><!-- /.pjYpSearch -->
					</div><!-- /.row -->
				</div><!-- /.panel-body -->
			</div><!-- /.panel panel-default pjYpResultsMaped -->
			<?php
			$api_key_str = isset($tpl['option_arr']['o_google_map_api']) && !empty($tpl['option_arr']['o_google_map_api']) ? '?key=' . $tpl['option_arr']['o_google_map_api'] : '';
			?>
			<script type="text/javascript" src="//maps.google.com/maps/api/js<?php echo $api_key_str;?>"></script>
			<script type="text/javascript">
				var ypMap, ypMarkersArray = [], previous_infoWindow = false;
				function ypGoogleMapsInit() {
					var ypOptions = {
						zoom: 9,
						mapTypeId: google.maps.MapTypeId.ROADMAP
					};
					ypMap = new google.maps.Map(document.getElementById("pjYpMapCanvas"), ypOptions);
				}
				function ypCodeAddress(lat, lng, contentString) {
					var myLatlng = new google.maps.LatLng(lat, lng);
					ypMap.setCenter(myLatlng);
					var marker = new google.maps.Marker({
						map: ypMap,
						position: myLatlng
					});
					if (contentString.length > 0 && contentString != "") {
						var infoWindow = new google.maps.InfoWindow({
							content: contentString
						});
						google.maps.event.addListener(marker, "click", function() {
							if(previous_infoWindow)
							{
								previous_infoWindow.close();
							}
							previous_infoWindow = infoWindow;
							infoWindow.open(ypMap, marker);
						});
					}
					ypMarkersArray.push(marker);
				}
				<?php
				if(!empty($ypAddressArr))
				{ 
					?>
					ypGoogleMapsInit();
					var ypPoints = [];
					ypPoints.push(<?php echo join(', ', $ypAddressArr); ?>);
					for(i = 0, len = ypPoints.length; i < len; i++) 
					{
						ypCodeAddress(ypPoints[i].lat, ypPoints[i].lng, ypPoints[i].html);
					}
					<?php
				} 
				?>
			</script>
		</div><!-- /.row pjYpBody -->
	</div><!-- /.container-fluid pjYpContainer -->
</div><!-- /#pjWrapper -->