<?php /*
============================================================================
GMAPS CODE - Custom
============================================================================
*/


function generateGmap($IDs = '', $default_lat, $default_lgn, $default_zoom, $map_name) {	

	echo "<script>";
	echo "jQuery(document).ready(function() {";
	echo "
		map_$map_name = new GMaps({
			div: '#$map_name',
			lat: $default_lat,
			lng: $default_lgn,
			zoom: $default_zoom,
		});
	";

	if(empty($IDs)) {

		echo "
			map_$map_name.addMarker({
				lat: $default_lat,
				lng: $default_lgn,
			});
		";

	} elseif(!is_array($IDs)) {

		$id = $IDs;

		$the_title = get_the_title($id);

		$temp_address =  get_post_meta($id, 'address_street', true); 

		if(empty($temp_address)) {
			continue;
		}

		$temp_address .= ', ';							
		
		$address_town = get_post_meta($id, 'address_town', true );
		if(!empty($address_town)) { $temp_address .= $address_town. ', '; }
		
		$address_state = get_post_meta($id, 'address_state', true );
		if(!empty($address_state)) { $temp_address .= $address_state; }
 		
 		$address_country = get_post_meta($id, 'address_country', true );
 		if(!empty($address_country)) { $temp_address .= ', '.$address_country; } else {
 			$temp_address .= ', Australia';
 		}

		echo "
			GMaps.geocode({
			  address: '$temp_address',
			  callback: function(results, status) {
			    if (status == 'OK') {
			      var latlng = results[0].geometry.location;
			      map_$map_name.setCenter(latlng.lat(), latlng.lng());
			     
			      map_$map_name.addMarker({
			        lat: latlng.lat(),
			        lng: latlng.lng(),
			        infoWindow: {
					  content: \"<div class='gmapsinfobox'><h3>$the_title</h3></div>\",
					}, 
			      });
			    }
			  }
			});
		";

	} else {

		foreach ($IDs as $key => $id) { 

			$the_title = get_the_title($id);;

			$temp_address =  get_post_meta($id, 'address_street', true); 

			if(empty($temp_address)) {
				continue;
			}

			$temp_address .= ', ';							
			
			$address_town = get_post_meta($id, 'address_town', true );
			if(!empty($address_town)) { $temp_address .= $address_town. ', '; }
			
			$address_state = get_post_meta($id, 'address_state', true );
			if(!empty($address_state)) { $temp_address .= $address_state; }
	 		
	 		$address_country = get_post_meta($id, 'address_country', true );
	 		if(!empty($address_country)) { $temp_address .= ', '.$address_country; } else {
	 			$temp_address .= ', Australia';
	 		}

			echo "
				GMaps.geocode({
				  address: '$temp_address',
				  callback: function(results, status) {
				    if (status == 'OK') {
				      var latlng = results[0].geometry.location;
				     
				      map_$map_name.addMarker({
				        lat: latlng.lat(),
				        lng: latlng.lng(),
				        infoWindow: {
						  content: \"<div class='gmapsinfobox'><h3>$the_title</h3></div>\",
						}, 
				      });
				    }
				  }
				});
			";
		}
	}

	echo "});";
	echo "</script>";
		
}
add_action( 'generateLocationMap', 'generateGmap', 10, 6); ?>