<?php
global $post,$wpdb,$tag;
wp_enqueue_style('iv_directories-style-1109', wp_iv_directories_URLPATH . 'admin/files/css/iv-bootstrap.css');
wp_enqueue_style('iv_directories-style-110', wp_iv_directories_URLPATH . 'admin/files/css/listing_style_4.css');
//wp_enqueue_style('iv_directories-style-64', wp_iv_directories_URLPATH . 'assets/cube/css/cubeportfolio.min.css');
wp_enqueue_style('iv_directories-style-111', wp_iv_directories_URLPATH . 'admin/files/css/new-version.css');
wp_enqueue_script('iv_directories-script-12', wp_iv_directories_URLPATH . 'admin/files/js/markerclusterer.js');

wp_enqueue_style('iv_directories-css-queryUI', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/themes/base/jquery-ui.css');
wp_enqueue_script('iv_directories-jqueryUI', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js');

$directory_url=get_option('_iv_directory_url');					
if($directory_url==""){$directory_url='directories';}
$current_post_type=$directory_url;
$form_action='';
if ( is_front_page() ) {
  $form_action='action="'.get_post_type_archive_link($current_post_type).'"';

}

$dir_map_api=get_option('_dir_map_api');	
if($dir_map_api==""){$dir_map_api='AIzaSyCIqlk2NLa535ojmnA7wsDh0AS8qp0-SdE';}

if(isset($_POST['latitude'])){
	$ins_lat=$_POST['latitude'];
}else{
	$ins_lat='37.4419';
}	
if(isset($_POST['longitude'])){
	$ins_lng=$_POST['longitude'];
}else{
	$ins_lng='-122.1419';
}	

$search_show=0;	
$map_show=0;		

$dir_searchbar_show=get_option('_dir_searchbar_show');	
if($dir_searchbar_show=="yes"){$search_show=1;}
$dir_map_show=get_option('_dir_map_show');	
if($dir_map_show=="yes"){$map_show=1;}
	
	$dirs_data =array();
	$tag_arr= array();
	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
	$args = array(
		'post_type' => $directory_url, // enter your custom post type
		'paged' => $paged, 
		'post_status' => 'publish',
		//'fields' => 'all',
		//'orderby' => 'title',
		//'order' => 'ASC',		
		//'posts_per_page'=> '2',  // overrides posts per page in theme settings
	);
	
	$lat='';$long='';$keyword_post='';$address='';$postcats ='';$selected='';
	
	// Add new shortcode only category
	if(isset($atts['category']) and $atts['category']!="" ){
			$postcats = $atts['category'];
			$args[$directory_url.'-category']=$postcats;
	}	
	
	if(get_query_var($directory_url.'-category')!=''){	
			$postcats = get_query_var($directory_url.'-category');
			$args[$directory_url.'-category']=$postcats;
			$selected=$postcats;	
			$search_show=1;	
		
	}
	
	if( isset($_POST[$directory_url.'-category'])){
		if($_POST[$directory_url.'-category']!=''){
			$postcats = $_POST[$directory_url.'-category'];
			$args[$directory_url.'-category']=$postcats;
			$selected=$postcats;	
			$search_show=1;	
			$args['posts_per_page']='9999';
		}		
	}
	
	
	$radius=get_option('_iv_radius');
	if( isset($_POST['range_value'])){
		$radius = $_POST['range_value'];
	}	
	if($radius==''){$radius='50';}
	
	if( isset($_POST['address'])){
		if($_POST['address']!=""){
			$lat =  $_POST['latitude'];
			$long = $_POST['longitude'];
			$address=trim($_POST['address']);
			
			
				if($lat=='' || $long==''){
					$latitude='';$longitude='';
					
					$prepAddr = str_replace(' ','+',$address);
					$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
					$output= json_decode($geocode);
					if(isset( $output->results[0]->geometry->location->lat)){
						$latitude = $output->results[0]->geometry->location->lat;
					}
					if(isset($output->results[0]->geometry->location->lng)){
						$longitude = $output->results[0]->geometry->location->lng;
					}					
					$lat=$latitude;
					$long=$longitude;	
					$args['distance']='50';
				}else{
					$args['distance']=$radius;
				}						
			
			
			$args['lat']=$lat;
			$args['lng']=$long;
			
			$search_show=1;
			$args['posts_per_page']='9999';				
		}		
	}
	if( isset($_POST['keyword'])){
		if($_POST['keyword']!=""){
			$args['s']= $_POST['keyword'];
			$keyword_post=$_POST['keyword'];
			$search_show=1;	
			$args['posts_per_page']='9999';
		}
	}	
	if( isset($tag)){
		if($tag!=""){
			if(!isset($_POST['keyword'])){
				$args['tag']= $tag;						
			}
		}
	}	
	if( isset($_POST['tag_arr'])){  
		if($_POST['tag_arr']!=""){  			
			$tag_arr= $_POST['tag_arr'];	
			//$tag_arr= get_query_var('tag_arr');
			$tags_string= implode("+", $tag_arr);
			$args['tag']= $tags_string;			
		}
	}		
		// Meta Query***********************
		$city_mq ='';
		if(isset($_REQUEST['dir_city']) AND $_REQUEST['dir_city']!=''){							
				$city_mq = array(
				'relation' => 'AND',
					array(
						'key'     => 'city',
						'value'   => $_REQUEST['dir_city'],
						'compare' => 'LIKE'
					),
				);
		}
		$country_mq='';
		if(isset($_REQUEST['dir_country']) AND $_REQUEST['dir_country']!=''){	
			$country_mq = array(
				'relation' => 'AND',
					array(
						'key'     => 'country',
						'value'   => $_REQUEST['dir_country'],
						'compare' => 'LIKE'
					),
				);
		}
		$zip_mq='';
		if(isset($_REQUEST['zipcode']) AND $_REQUEST['zipcode']!=''){	
			$zip_mq = array(
				'relation' => 'AND',
					array(
						'key'     => 'postcode',
						'value'   => $_REQUEST['zipcode'],
						'compare' => 'LIKE'
					),
				);
		}

		$args['meta_query'] = array(
			$city_mq, $country_mq, $zip_mq,
		);
	//  Bidding Search Paid listing****	
	
		
	// End Bidding Search Paid listing****	
	$dir_load_listing_all=get_option('_dir_load_listing_all');	
	if($dir_load_listing_all==""){$dir_load_listing_all='yes';}
	if($dir_load_listing_all=='yes'){$args['posts_per_page']='9999';}
		
	 $the_query = new WP_GeoQuery( $args ); 
	   
	
$main_class = new wp_iv_directories;
		
?>

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<link href="http://fonts.googleapis.com/css?family=Open+Sans:400,400italic,600,700%7CRoboto:400,500,700%7CRoboto+Condensed%7CLato:400,700" rel="stylesheet" type="text/css">

<!-- Map**************-->
<div id="top-map" style="<?php echo ($map_show==1 ? '': 'display: none;'); ?>">
  <div id="map"  style="width:100%; height: 450px; position: relative; background-color: rgb(229, 227, 223); overflow: hidden;"> </div>
</div>

<div class="wrap-pro">
  <div class="bootstrap-wrapper">
    <div id="top-search" class="navbar-default navbar dir-style-search" style="<?php echo ($search_show==1 ? '': 'display: none;'); ?>">
      <div class="text-center dir-style-4">
        <form class="form-inline" method="POST" <?php echo $form_action;?> onkeypress="return event.keyCode != 13;">
          <?php
						$dir_search_keyword=get_option('_dir_search_keyword');	
						if($dir_search_keyword==""){$dir_search_keyword='yes';}	
						if($dir_search_keyword=='yes'){
						?>
          <div class="form-group">
            <input type="text" class="form-control " id="keyword" name="keyword"  placeholder="<?php _e( 'Keyword', 'ivdirectories' ); ?>" value="<?php echo $keyword_post; ?>">
            <?php $pos = $main_class->get_unique_keyword_values('keyword',$current_post_type);
									
								?>
            <script>									
									jQuery(function() {
									var availableTags = [ "<?php echo  implode('","',$pos); ?>" ];
									jQuery( "#keyword" ).autocomplete({source: availableTags});
								  });
								  
								</script> 
          </div>
          <?php
					}
					 ?>
          <?php
						
					
					   $dir_search_city=get_option('_dir_search_city');	
						if($dir_search_city==""){$dir_search_city='yes';}	
						if($dir_search_city=='yes'){
						// City
						$args_citys = array(
							'post_type'  => $current_post_type,
							'posts_per_page' => -1,
							'meta_query' => array(
								array(
									'key'     => 'city',	
									'orderby' => 'meta_value', 
									'order' => 'ASC',		
								),
								
							),
						);
						$citys = new WP_Query( $args_citys );	
						$citys_all = $citys->posts;
						$get_cityies =array();
						foreach ( $citys_all as $term ) {
							$new_city="";
							$new_city=get_post_meta($term->ID,'city',true);
							if (!in_array($new_city, $get_cityies)) {
								$get_cityies[]=$new_city;
							
							}	
						}	

					// City
					
						
						?>
          <div class="form-group" >
            <select name="dir_city"  id="dir_city" class="form-control" >
              <option   value="">
              <?php esc_html_e('Choose a City   ','ivdirectories'); ?>
              </option>
              <?php	
										$selected_city= (isset($_REQUEST['dir_city'])?$_REQUEST['dir_city']:'' );					
										if(count($get_cityies)) {									  
										  foreach($get_cityies as $row1) {
											  if($row1!=''){													  
											  ?>
              <option   value="<?php echo $row1; ?>" <?php echo ($selected_city==$row1?'selected':''); ?>><?php echo $row1; ?></option>
              <?php
											}
												
											}
										  
										} 
											
										?>
            </select>
          </div>
          <?php
							}
						?>
          <?php
						$dir_search_country=get_option('_dir_search_country');	
						if($dir_search_country==""){$dir_search_country='yes';}	
						if($dir_search_country=='yes'){
						// Country
							$args_country = array(
								'post_type'  => $current_post_type,
								'posts_per_page' => -1,
								'meta_query' => array(
									array(
										'key'     => 'country',	
										'orderby' => 'meta_value', 
										'order' => 'ASC',		
									),
									
								),
							);
							$country = new WP_Query( $args_country );	
							$country_all = $country->posts;
							$get_country =array();
							foreach ( $country_all as $term ) {
								$new_country="";
								$new_country=get_post_meta($term->ID,'country',true);
								if (!in_array($new_country, $get_country)) {
									$get_country[]=$new_country;
								
								}	
							}	

							//Country
							?>
          <div class="form-group" >
            <select name="dir_country"  id="dir_country" class="form-control" >
              <option   value="">
              <?php esc_html_e('Country  ','ivdirectories'); ?>
              </option>
              <?php	$selected_country= (isset($_REQUEST['dir_country'])?$_REQUEST['dir_country']:'' );						
										if(count($get_country)) {									  
										  foreach($get_country as $row1) {
											  if($row1!=''){													  
											  ?>
              <option   value="<?php echo $row1; ?>" <?php echo ($selected_country==$row1?'selected':''); ?> ><?php echo $row1; ?></option>
              <?php
											}
												
											}
										  
										} 
											
										?>
            </select>
          </div>
          <?php
							}
						?>
          <?php
						 $_dir_search_zipcode=get_option('_dir_search_zipcode');	
						if($_dir_search_zipcode==""){$dir_search_location='yes';}	
						if($_dir_search_zipcode=='yes'){
							$zipcode=(isset($_REQUEST['zipcode'])?$_REQUEST['zipcode']:'' )
						?>
          <div class="form-group" >
            <input type="text"  id="zipcode" name="zipcode" class="form-control" placeholder="<?php _e( 'Zipcode', 'ivdirectories' ); ?>"
								value="<?php echo trim($zipcode); ?>">
            <?php $pos = $main_class->get_unique_post_meta_values('postcode',$current_post_type);
									//print_r($pos);
								?>
            <script>
								  jQuery(function() {
									var availableTags = [ "<?php echo  implode('","',$pos); ?>" ];
									jQuery( "#zipcode" ).autocomplete({source: availableTags});
								  });
								</script> 
          </div>
          <?php
						}
						?>
          <div class="form-group" >
            <?php
								echo '<select name="'.$directory_url.'-category" class="form-control">';
								echo'	<option selected="'.$selected.'" value="">'.__('Any Category   ','ivdirectories').'</option>';
								
										
										if( isset($_POST['submit'])){
											$selected = $_POST[$directory_url.'-category'];
										}
											//directories
											$taxonomy = $directory_url.'-category';
											$args = array(
												'orderby'           => 'name', 
												'order'             => 'ASC',
												'hide_empty'        => true, 
												'exclude'           => array(), 
												'exclude_tree'      => array(), 
												'include'           => array(),
												'number'            => '', 
												'fields'            => 'all', 
												'slug'              => '',
												'parent'            => '0',
												'hierarchical'      => true, 
												'child_of'          => 0,
												'childless'         => false,
												'get'               => '', 
												
											);
								$terms = get_terms($taxonomy,$args); // Get all terms of a taxonomy
								if ( $terms && !is_wp_error( $terms ) ) :
									$i=0;
									foreach ( $terms as $term_parent ) {  ?>
            <?php  
											
											echo '<option  value="'.$term_parent->slug.'" '.($selected==$term_parent->slug?'selected':'' ).'><strong>'.$term_parent->name.'<strong></option>';
											?>
            <?php
												
												$args2 = array(
													'type'                     => $directory_url,						
													'parent'                   => $term_parent->term_id,
													'orderby'                  => 'name',
													'order'                    => 'ASC',
													'hide_empty'               => 1,
													'hierarchical'             => 1,
													'exclude'                  => '',
													'include'                  => '',
													'number'                   => '',
													'taxonomy'                 => $directory_url.'-category',
													'pad_counts'               => false 

												); 											
												$categories = get_categories( $args2 );	
												if ( $categories && !is_wp_error( $categories ) ) :
														
														
													foreach ( $categories as $term ) { 
														echo '<option  value="'.$term->slug.'" '.($selected==$term->slug?'selected':'' ).'>-'.$term->name.'</option>';
													} 	
																				
												endif;										
												?>
            <?php
										$i++;
									} 								
								endif;	
									echo '</select>';	
								?>
          </div>
          <?php
						$dir_search_location=get_option('_dir_search_location');	
						if($dir_search_location==""){$dir_search_location='yes';}	
						if($dir_search_location=='yes'){
						?>
          <div class="form-group" >
            <input type="text" class="form-control " id="address" name="address"  placeholder="<?php _e( 'Location', 'ivdirectories' ); ?>" 
								value="<?php echo trim($address); ?>">
            <input type="hidden" id="latitude" name="latitude" placeholder="Latitude" value="<?php echo $lat; ?>" >
            <input type="hidden" id="longitude" name="longitude" placeholder="Longitude"  value="<?php echo $long; ?>">
          </div>
          <?php
						}
						$dir_f_search_radius=get_option('_dir_search_location');	
							if($dir_f_search_radius==""){$dir_f_search_radius='yes';}	
							if($dir_f_search_radius=='yes'){
					  ?>
          <?php
							$dir_search_redius=get_option('_dir_search_redius');	
							if($dir_search_redius==""){$dir_search_redius='Km';}	
							?>
          <div class="form-group range-txt">
            <?php _e( 'Radius', 'ivdirectories' ); ?>
            : <span id="rvalue"><?php echo $radius;?></span><?php echo ' '.$dir_search_redius; ?> </div>
          <div class="form-group range-filter">
            <div class="range range-success">
              <input type="range" name="range" id="range" min="1" max="1000" value="<?php echo $radius;?>" onchange="range.value=value">
              <input type="hidden" name="range_value" id="range_value" value="<?php echo $radius; ?>" >
            </div>
          </div>
          <?php
						}
						?>
          <button type="submit" id="submit" name="submit"  class="btn">
          <?php _e('Search','ivdirectories'); ?>
          </button>
        </form>
      </div>
    </div>
  </div>
  <div class="clearfix" style="margin-top:20px">   
    <div id="js-filters-meet-the-team" class="cbp-l-filters-button cbp-l-filters-left" >
    <?php
				if($postcats==''){	?>
      <div data-filter="*" class="cbp-filter-item">
        <?php _e('Show All', 'ivdirectories' ); ?>
      </div>
      <?php	
													
					$argscat = array(
						'type'                     => $directory_url,
						'orderby'                  => 'name',
						'order'                    => 'ASC',
						'hide_empty'               => true,
						'hierarchical'             => 1,
						'exclude'                  => '',
						'include'                  => '',
						'number'                   => '',
						'taxonomy'                 => $directory_url.'-category',
						'pad_counts'               => false 

					); 											
					$categories = get_categories( $argscat );
					
					if ( $categories && !is_wp_error( $categories ) ) :	
						foreach ( $categories as $term ) { 
							if($dir_load_listing_all=='yes'){
								echo '<div data-filter=".'.$term->slug.'" class="cbp-filter-item"> '.$term->name.' <div class="cbp-filter-counter"></div></div>';
							}else{ ?>
      <div data-filter="" class="cbp-filter-item"><a style="text-decoration:none;" href="<?php echo get_post_type_archive_link( $directory_url).'?&'.$directory_url.'-category='.$term->slug ; ?>"> <?php echo $term->name; ?> </a> </div>
      <?php							
							}							
							?>
      <?php	
						} 							
					endif;
			}
			if($postcats!=''){ ?>
      <div data-filter="" class="cbp-filter-item"><a href="<?php echo get_post_type_archive_link( $directory_url) ; ?>">
        <?php _e('Show All', 'ivdirectories' ); ?>
        </a> </div>
      <?php
				 $term = get_term_by('slug', $postcats, $directory_url.'-category'); 
				 $name = (isset($term->name)? $term->name: $postcats);
				echo '<div data-filter=".'.$postcats.'"  class="cbp-filter-item-active cbp-filter-item"> '.$name.' <div class="cbp-filter-counter"></div></div>';
				
			
			}				
			?>
    </div>
  </div>
  
  <!-- Item Filter Section -->
  <div class="direc-item">
    <div id="js-grid-meet-the-team" class="cbp cbp-l-grid-team" >   
					
      <?php	
	$i=1;
	$dir_popup=get_option('_dir_popup');	
	if($dir_popup==""){$dir_popup='yes';}	
	 if ( $the_query->have_posts() ) : 
	
	while ( $the_query->have_posts() ) : $the_query->the_post();
				$id = get_the_ID();
				
				$gallery_ids=get_post_meta($id ,'image_gallery_ids',true);
				$gallery_ids_array = array_filter(explode(",", $gallery_ids));
				
				$dir_data['link']=get_post_permalink();
				$dir_data['title']=$post->post_title; 				
				$dir_data['lat']=get_post_meta($id,'latitude',true);
				$dir_data['lng']=get_post_meta($id,'longitude',true);
				if(get_post_meta($id,'latitude',true)!=''){$ins_lat=get_post_meta($id,'latitude',true);}
				if(get_post_meta($id,'longitude',true)!=''){$ins_lng=get_post_meta($id,'longitude',true);}
				$dir_data['address']=get_post_meta($id,'address',true); 
				$dir_data['image']= '';
				$feature_image = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'thumbnail' ); 
				if($feature_image[0]!=""){ 
					//$dir_data['image']= '<img class=" img-responsive" src="'. $feature_image[0].'">';
					$dir_data['image']=  $feature_image[0];
				}
				$dir_data['marker_icon']=wp_iv_directories_URLPATH."/assets/images/map-marker/map-marker.png";				
				$currentCategoryId='';
				$terms =get_the_terms($id, $directory_url."-category");				
				if($terms!=""){
					foreach ($terms as $termid) {  
						if(isset($termid->term_id)){
							 $currentCategoryId= $termid->term_id; 
						}					  
					} 
				}
				$marker = get_option('_cat_map_marker_'.$currentCategoryId,true);
				if($marker!=''){
					$image_attributes = wp_get_attachment_image_src( $marker ); // returns an array
					if( $image_attributes ) {
					
						$dir_data['marker_icon']= $image_attributes[0];
					}							
				}
				if($dir_data['lat']!='' AND $dir_data['lng']!='' ){				
					array_push( $dirs_data, $dir_data );
				}
				
				
							
					$feature_img='';
					if(has_post_thumbnail()){ 
						$feature_image = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'medium' ); 
						if($feature_image[0]!=""){ 							
							$feature_img =$feature_image[0];
						}					
					}else{
						$feature_img= wp_iv_directories_URLPATH."/assets/images/default-directory.jpg";					
					
					}
					
					$currentCategory=wp_get_object_terms( $id, $directory_url.'-category');
					$cat_link='';$cat_name='';$cat_slug='';
					if(isset($currentCategory[0]->slug)){										
						$cat_slug = $currentCategory[0]->slug;
						$cat_name = $currentCategory[0]->name;
						$cc=0;
						foreach($currentCategory as $c){		
								if($cc==0){
									$cat_name =$c->name;
									$cat_slug =$c->slug;
								}else{
									$cat_name = $cat_name .', '.$c->name;
									$cat_slug = $cat_slug .' '.$c->slug;
								}															
							$cc++;
						}						
						$cat_link= get_term_link($currentCategory[0], $directory_url.'-category');						
					}
					?>
				  <div class="item-inside cbp-item <?php echo esc_attr($cat_slug); ?> ">
					<article>
					  <div class="img-sec"> 
						  <img src="<?php echo esc_attr($feature_img);?>" alt="" /> 
						  <a href="<?php echo get_the_permalink($id); ?>" class="over-hover"><?php esc_html_e('VIEW DETAIL', 'ivdirectories' ); ?></a> 
					</div>
						<div class="des-info"> <a  class="tittle" href="<?php echo get_the_permalink($id); ?>"> <?php echo esc_attr(  $post->post_title);?></a>
						<p> <?php echo $cat_name;?></p>
						<ul>
								<?php
								if(get_post_meta($id,'city',true)!=""){
								?>
								<li><i class="fa fa-map-marker"></i> <?php echo get_post_meta($id,'city',true);?></li>
							  <?php
								}
							  ?>
							  
							  <?php
								if(get_post_meta($id,'phone',true)!=""){
								?>
								 <li><i class="fa fa-phone"></i> <?php echo get_post_meta($id,'phone',true);?></li>
							  <?php
								}
							  ?>
							   <?php
								if(get_post_meta($id,'contact-email',true)!=""){
								?>
								 <li><i class="fa fa-envelope"></i> <?php echo get_post_meta($id,'contact-email',true);?></li>
							  <?php
								}
							  ?>
						 
						</ul>
					  </div>
					</article>
				  </div>
      
      
      <?php
		$i++;
	endwhile; 
				$dirs_json ='';
				if(!empty($dirs_data)){
					$dirs_json =json_encode($dirs_data);
				}
				?>
      <?php else :
			$dirs_json='';		 ?>
      <?php _e( 'Sorry, no posts matched your criteria.','ivdirectories' ); ?>
      <?php endif; ?>
    </div>
  </div>
  <?php
    if ( !$the_query->have_posts() ){
	_e( 'Sorry, no posts matched your criteria.','ivdirectories' );
	
	} ?>
  <!--
					paging plugin
					https://wordpress.org/plugins/wp-pagenavi/screenshots/
					-->
  <?php 
						if($dir_load_listing_all!='yes'){	
							if (function_exists('wp_pagenavi')) : ?>
  <div style="text-align:center; margin-top:30px">
    <?php wp_pagenavi( array( 'query' => $the_query ) ); ?>
  </div>
  <?php else: ?>
  <br/>
  <br/>
  <div class="cbp-l-filters-left nav-next"><span class="cbp-l-grid-team-name;">
    <?php previous_posts_link( ''.__( ' Newer Entries', 'ivdirectories' ).'' ); ?>
    </span></div>
  <div class="cbp-l-filters-right nav-previous" ><span class="cbp-l-grid-team-name">
    <?php next_posts_link( ''.__( ' Older Entries ', 'ivdirectories' ).'' ); ?>
    </span></div>
  <br/>
  <br/>
  <?php endif;
					}
					   ?>
  
  <!--END .navigation-links--> 
</div>

<?php
wp_enqueue_script('iv_directories-ar-script-23', wp_iv_directories_URLPATH . 'assets/cube/js/jquery.cubeportfolio.min.js');
wp_enqueue_script('iv_directories-ar-script-102', wp_iv_directories_URLPATH . 'assets/cube/js/meet-team.js');
?>
<?php
	
$dir_map_zoom=get_option('_dir_map_zoom');	
if($dir_map_zoom==""){$dir_map_zoom='7';}
?>
<script type='text/javascript' src='https://maps.googleapis.com/maps/api/js?libraries=places&key=<?php echo $dir_map_api;?>'></script> 
<script type="text/javascript">	
				function initialize() {
					var center = new google.maps.LatLng('<?php echo $ins_lat; ?>', '<?php echo $ins_lng; ?>');
					//var center = new google.maps.LatLng(49, 2.56);
					var map = new google.maps.Map(document.getElementById('map'), {
						zoom: <?php echo $dir_map_zoom; ?>,
						center: center,
						mapTypeId: google.maps.MapTypeId.ROADMAP
					});
					var markers = [];
					var infowindow = new google.maps.InfoWindow();
					var dirs ='';
					var min = .999999;
					var max = 1.000002;
					<?php echo ($dirs_json!=''? 'var dirs ='.$dirs_json:''); ?>;
					if(dirs!=''){
					 for (i = 0; i < dirs.length; i++) {
						//for(var i=0;i<5;i++){
							//console.log(dirs[i);		
							var new_lat= dirs[i].lat  * (Math.random() * (max - min) + min);
							var new_lng= dirs[i].lng  * (Math.random() * (max - min) + min);
							var latLng = new google.maps.LatLng(new_lat,new_lng);							
							//var latLng = new google.maps.LatLng(dirs[i].lat,dirs[i].lng);
							var marker = new google.maps.Marker({
								position: latLng,
								map: map,
								icon: dirs[i].marker_icon,
							});
							markers.push(marker);
								 google.maps.event.addListener(marker, 'click', (function(marker, i) {
									return function() {
											//infowindow.setContent('<div id="map-marker-info " ><a href="'+dirs[i].link +'">'+dirs[i].image+'<h5>'+ dirs[i].title //+'</h5><span class="address">'+dirs[i].address+'</span></a></div>');											
											infowindow.setContent('<div id="map-marker-info" style="overflow: auto; cursor: default; clear: both; position: relative; border-radius: 4px; padding: 15px; border-color: rgb(255, 255, 255); border-style: solid; background-color: rgb(255, 255, 255); border-width: 1px; width: 275px; height: 130px;"><div style="overflow: hidden;" class="map-marker-info"><a  style="text-decoration: none;" href="'+dirs[i].link +'">	<span style="background-image: url('+dirs[i].image+')" class="list-cover has-image"></span><span class="address"><strong>'+dirs[i].title +'</strong></span> <span class="address" style="margin-top:15px">'+dirs[i].address+'</span></a></div></div>');
										infowindow.open(map, marker);
									}
								})(marker, i));
						}
					}
					var markerCluster = new MarkerClusterer(map, markers);
				
				}	
				function cs_toggle_street_view(btn) {
				  var toggle = panorama.getVisible();
				  if (toggle == false) {
					if(btn == 'streetview'){
					  panorama.setVisible(true);
					}
				  } else {
					if(btn == 'mapview'){
					  panorama.setVisible(false);
					}
				  }
				}
                google.maps.event.addDomListener(window, 'load', initialize);					
				
				//google.maps.event.trigger(map, 'resize');					
					jQuery("#search_toggle_div").on('click', function(e) {
						setTimeout(function(){
								initialize();	
								google.maps.event.trigger(map, 'resize');
						},500)							
					});
	function save_favorite(id) {       
		
		  var isLogged ="<?php echo get_current_user_id();?>";
                               
                if (isLogged=="0") {                   
                     alert("<?php _e('Please login to add favorite','ivdirectories'); ?>");
                } else { 
						
						var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
						var loader_image = '<img src="<?php echo wp_iv_directories_URLPATH. "admin/files/images/loader.gif"; ?>" />';
						//jQuery('#fav_message').html(loader_image); 
						var search_params={
							"action"  : 	"iv_directories_save_favorite",	
							"data": "id=" + id,
						};
						
						jQuery.ajax({					
							url : ajaxurl,					 
							dataType : "json",
							type : "post",
							data : search_params,
							success : function(response){ 								
								jQuery("#fav_dir"+id).html('<a href="javascript:;" onclick="save_unfavorite('+id+')" ><span class="hide-sm"><i class="fa fa-heart fa-lg red-heart"></i>&nbsp;&nbsp; </span></a>');
							}
						});						
				} 
    }
	function save_unfavorite(id) { 
		  var isLogged ="<?php echo get_current_user_id();?>";                               
                if (isLogged=="0") {                   
                     alert("<?php _e('Please login to remove favorite','ivdirectories'); ?>");
                } else { 
						var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
						var loader_image = '<img src="<?php echo wp_iv_directories_URLPATH. "admin/files/images/loader.gif"; ?>" />';
						//jQuery('#fav_message'+id).html(loader_image); 
						var search_params={
							"action"  : 	"iv_directories_save_un_favorite",	
							"data": "id=" + id,
						};						
						jQuery.ajax({					
							url : ajaxurl,					 
							dataType : "json",
							type : "post",
							data : search_params,
							success : function(response){								
								jQuery("#fav_dir"+id).html('<a href="javascript:;" onclick="save_favorite('+id+')" ><span class="hide-sm"><i class="fa fa-heart fa-lg "></i>&nbsp;&nbsp; </span></a>');
							}
						});						
				}  				
    }	
function initialize_address() {
        var input = document.getElementById('address');
        var autocomplete = new google.maps.places.Autocomplete(input);
			google.maps.event.addListener(autocomplete, 'place_changed', function () {
            var place = autocomplete.getPlace();
            //document.getElementById('city2').value = place.name;
            document.getElementById('latitude').value = place.geometry.location.lat();
            document.getElementById('longitude').value = place.geometry.location.lng(); 
        });
    }
google.maps.event.addDomListener(window, 'load', initialize_address);
jQuery('input[name="range"]').on("change", function() { 
		//jQuery(this).next().html(jQuery(this).val() + '%');
		jQuery('#rvalue').html(jQuery(this).val());
		jQuery('#range_value').val(jQuery(this).val());
		//console.log(jQuery(this).val());
});

function toggle_top_map(divId) {

   jQuery("#"+divId).toggle('slow');
  
  setTimeout(
  function() 
  {
	 initialize();	
	google.maps.event.trigger(map, 'resize');
  }, 500);
  
  
  
  
}
function toggle_top_search(divId) {	
  	
  jQuery("#"+divId).toggle('slow');
  initialize();	
  google.maps.event.trigger(map, 'resize');
 
}
</script> 
